Ignore:
Timestamp:
2015-09-02T13:36:28+12:00 (9 years ago)
Author:
kjdon
Message:

changing the filtering algorithm. Can choose between pagenum and title. pagenum is numeric by default, title is string matching.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/document_scripts.js

    r29978 r30169  
    11var _imageZoomEnabled = false;
     2// Choose which types of filtering you want: sectionnum, or sectiontitle.
     3var _filter_on_types = ["sectiontitle", "sectionnum"];
     4// the text strings for the two radio buttons
     5var _filter_on_num_text = "pagenum";
     6var _filter_on_title_text = "title";
     7// are titles numeric match?
     8var _filter_title_numeric = false;
     9
    210var _linkCellMap = new Array();
    311var _onCells = new Array();
     
    674682}
    675683
     684
    676685function SliderWidget(_links)
    677686{
     
    711720    //PRIVATE FUNCTIONS
    712721    //*****************
    713    
    714     var setUpFilterBox = function()
    715     {
    716         var filter = $("#filterText");
    717         filter.keyup(function()
    718         {
    719             var fullValue = filter.val();
    720             var values = fullValue.split(",");
     722
     723    //  _filter_on_types can be "sectionnum", "sectiontitle"
     724 var setUpFilterButtons = function() {
     725
     726  var button_div = $("#filterOnButtons");
     727  button_div.onclick = doFiltering;
     728  button_div.html("radio");
     729  if (_filter_on_types.length == 0) {
     730    _filter_on_types = ["sectionnum", "sectiontitle"];
     731  }
     732  else if (_filter_on_types.length == 1) {
     733    if (_filter_on_types[0] == "sectionnum") {
     734      button_div.html("(<input type='radio' name='filterOn' value='num' checked>"+_filter_on_num_text+"</input>)");
     735    } else {
     736      button_div.html("(<input type='radio' name='filterOn' value='title' checked>"+_filter_on_title_text+"</input>)");
     737    }
     738  } else {
     739    // should be both options
     740    button_div.html("<input type='radio' name='filterOn' value='num' checked>"+_filter_on_num_text+"</input><input type='radio' name='filterOn' value='title'>"+_filter_on_title_text+"</input>");
     741  }
     742}
     743
     744    var doFiltering = function () {
     745  if (typeof _titles == "undefined") {
     746    return;
     747  }
     748
     749  var filter_string = $("#filterText").val();
     750  var filter_type = $('input[name="filterOn"]:checked').val();
     751 
     752  var index = 2; // section num
     753  var numeric_match = true;
     754  if (filter_type == "title") {
     755    index = 3;
     756    if (_filter_title_numeric != true) {
     757      numeric_match = false;
     758    }
     759     
     760  }
     761  var values = filter_string.split(",");
    721762           
    722             var matchingTitles = new Array();
    723            
    724             for (var l = 0; l < values.length; l++)
    725             {
    726                 var currentValue = values[l].replace(/^ +/g, "").replace(/ +$/g, "");
    727                 var isRange = (currentValue.search(/\d+-\d+/) != -1)
    728                
    729                 var found = false;
    730                 for(var i = 0; i < _titles.length; i++)
    731                 {
    732                     if(_titles[i][0] == currentValue)
    733                     {
    734                         found = true;
    735                     }
    736                 }
    737                
    738                 if(!found && isRange)
    739                 {
    740                     var firstNumber = currentValue.replace(/(\d+)-\d+/, "$1");
    741                     var secondNumber = currentValue.replace(/\d+-(\d+)/, "$1");
    742                    
    743                     if(firstNumber <= secondNumber)
    744                     {
    745                         for(var i = firstNumber; i <= secondNumber; i++)
    746                         {
    747                             var numString = i + "";
    748                             for(var j = 0; j < _titles.length; j++)
    749                             {
    750                                 var currentTitle = _titles[j];
    751                                 if(currentTitle[0].search(numString) != -1)
    752                                 {
    753                                     matchingTitles.push(currentTitle);
    754                                 }
    755                             }
    756                         }
    757                     }
    758                 }
    759                 else
    760                 {
    761                     for(var i = 0; i < _titles.length; i++)
    762                     {
    763                         var currentTitle = _titles[i];
    764                         if(currentTitle[0].search(currentValue.replace(/\./g, "\\.")) != -1)
    765                         {
    766                             matchingTitles.push(currentTitle);
    767                         }
    768                     }
    769                 }
    770             }
    771            
    772             for(var i = 0; i < _titles.length; i++)
    773             {
    774                 $(_titles[i][1].cell).css("display", "none");
    775             }
    776            
    777             for(var i = 0; i < matchingTitles.length; i++)
    778             {
    779                 $(matchingTitles[i][1].cell).css("display", "table-cell");
    780             }
    781         });
     763  var matchingTitles = new Array();
     764 
     765  for (var l = 0; l < values.length; l++)
     766    {
     767      var currentValue = values[l].replace(/^ +/g, "").replace(/ +$/g, "");
     768      if (numeric_match) {
     769    var isRange = (currentValue.search(/\d+-\d+/) != -1);
     770    if (isRange) {
     771      var firstNumber = currentValue.replace(/(\d+)-\d+/, "$1");
     772      var secondNumber = currentValue.replace(/\d+-(\d+)/, "$1");
     773     
     774      if(firstNumber <= secondNumber)
     775        {
     776          for(var i = firstNumber; i <= secondNumber; i++)
     777        {
     778          var numString = i + "";
     779          for(var j = 0; j < _titles.length; j++) {
     780           
     781            var currentTitle = _titles[j];
     782            if(currentTitle[index] == numString) {
     783              matchingTitles.push(currentTitle);
     784            }
     785          }
     786        }
     787        }
     788    } // if isRange
     789    else {
     790      for(var j = 0; j < _titles.length; j++) {
     791        if (_titles[j][index]==currentValue) {
     792          matchingTitles.push(_titles[j]);
     793        }
     794      }
     795
     796    }
     797   
     798      } else { // not numeric match.
     799    // need to do a search
     800      for(var i = 0; i < _titles.length; i++)
     801        {
     802          var currentTitle = _titles[i];
     803          if(currentTitle[index].toLowerCase().search(currentValue.toLowerCase().replace(/\./g, "\\.")) != -1)
     804        {
     805          matchingTitles.push(currentTitle);
     806        }
     807        }
     808      }
     809    } // for each value from filter string
     810 
     811  // set all to hide...
     812  for(var i = 0; i < _titles.length; i++)
     813    {
     814      $(_titles[i][1].cell).css("display", "none");
     815    }
     816 
     817  // .. then display the matching ones
     818  for(var i = 0; i < matchingTitles.length; i++)
     819    {
     820      $(matchingTitles[i][1].cell).css("display", "table-cell");
     821    }
     822}
     823
     824    var setUpFilterBox = function()
     825    {
     826      var filter = $("#filterText");
     827     
     828     filter.keyup(function()
     829    {
     830      doFiltering();
     831    });
    782832    }
    783833   
     
    946996
    947997        var title = $(_links[i]).html();
    948         if(title.search(/^[^ ]+ [^ ]+$/) != -1)
    949         {
    950             var section = title.replace(/^([^ ]+) [^ ]+$/, "$1");
    951             var page = title.replace(/^[^ ]+ ([^ ]+)$/, "$1");
    952             if(page.search(/^[0-9]+$/) != -1)
    953             {
    954                 title = page;
    955             }
    956         }
    957         _titles.push([title, _links[i]]);
     998        var t_section = "";
     999        var t_title = "";
     1000        if (title.search(/tocSectionNumber/) != -1)
     1001          {
     1002            var matching_regex = /<span class=\"tocSectionNumber\">([0-9]+)<\/span>\s*(.+)$/;
     1003            var matches_array = matching_regex.exec(title);
     1004            if (matches_array.length == 3) {
     1005              t_section = matches_array[1];
     1006              t_title = matches_array[2];
     1007              }
     1008          }
     1009       
     1010        _titles.push([title, _links[i], t_section, t_title]);
    9581011
    9591012        col.append($("<br>"));
     
    9621015
    9631016    setUpFilterBox();
     1017    setUpFilterButtons();
    9641018    startCheckFunction();
    965 }
     1019    }
    9661020
    9671021/***********************
Note: See TracChangeset for help on using the changeset viewer.