/[bfilter]/trunk/bfilter.js
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bfilter.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 29 by dpavlin, Fri Sep 24 15:44:27 2004 UTC revision 30 by dpavlin, Fri Sep 24 16:58:08 2004 UTC
# Line 4  Line 4 
4     Matko Andjelinic, matko.andjelinic@gmail.com 2004-09-09 (contributed OO implementation)     Matko Andjelinic, matko.andjelinic@gmail.com 2004-09-09 (contributed OO implementation)
5  */  */
6    
7    top.__bfilter_obj = new Array();
8    
9    
10  function BFilter(arr) {  function BFilter(arr) {
11          this.id_cache = Array();          this.id_cache = Array();
12          // total number of hits          // total number of hits
13          this.hits = 0;          this.hits = 0;
14    
15            // store reference to this object for later (YAK)
16            this.obj_count = top.__bfilter_obj.length;
17            top.__bfilter_obj[this.obj_count] = this;
18    
19            // clear results html
20          this.results_html = '';          this.results_html = '';
21    
22            // show results after 0.2s
23            this.timeout = 200;
24            this.timeout_handle = null;
25    
26          // this function is called for each result          // this function is called for each result
27          this.result = function (arr) {          this.result = function (arr) {
28                  this.results_html += '<li><a href="'+arr[1]+'">'+                  this.results_html += '<li><a href="'+arr[1]+'">'+
# Line 57  BFilter.prototype.element_id = function Line 68  BFilter.prototype.element_id = function
68          if (this.id_cache[id]) {          if (this.id_cache[id]) {
69                  return this.id_cache[id];                  return this.id_cache[id];
70          } else {          } else {
71                  var el = document.getElementById(id);                  var el = self.document.getElementById(id);
72                  if (el) {                  if (el) {
73                          this.id_cache[id] = el;                          this.id_cache[id] = el;
74                          return el;                          return el;
# Line 123  BFilter.prototype.debug = function (html Line 134  BFilter.prototype.debug = function (html
134    
135    
136  // modified binary search to find first element with substring  // modified binary search to find first element with substring
137  BFilter.prototype.binarySearch = function (arr, find) {  BFilter.prototype.binarySearch = function (arr, user_filter) {
138          if (!arr || typeof (find) == "undefined" || !arr.length) {          if (!arr || typeof (user_filter) == "undefined" || !arr.length) {
139                  return null;                  return null;
140          }          }
141          var low = 0;          var low = 0;
# Line 135  BFilter.prototype.binarySearch = functio Line 146  BFilter.prototype.binarySearch = functio
146                  var mid = (low + high) / 2;                  var mid = (low + high) / 2;
147                  var aTry = (mid < 1) ? 0 : parseInt(mid);                  var aTry = (mid < 1) ? 0 : parseInt(mid);
148                    
149                  var curr = arr[aTry][0].substr(0,find.length).toLowerCase();                  var curr = arr[aTry][0].substr(0,user_filter.length).toLowerCase();
150                  this.debug("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr);                  this.debug("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr);
151                  if (curr < find) {                  if (curr < user_filter) {
152                          low = aTry + 1;                          low = aTry + 1;
153                          continue;                          continue;
154                  }                  }
155                  if (curr > find) {                  if (curr > user_filter) {
156                          high = aTry - 1;                          high = aTry - 1;
157                          continue;                          continue;
158                  }                  }
159                  if (curr == find) {                  if (curr == user_filter) {
160                          high = aTry - 1;                          high = aTry - 1;
161                          lastTry = aTry;                          lastTry = aTry;
162                          continue;                          continue;
# Line 161  BFilter.prototype.binarySearch = functio Line 172  BFilter.prototype.binarySearch = functio
172          }          }
173  }  }
174    
175  BFilter.prototype.filter = function (document, find) {  BFilter.prototype.filter = function (user_filter) {
176            this.debug("set timeout for "+this.obj_count+" to "+this.timeout);
177            if (this.timeout_handle) {
178                    clearTimeout(this.timeout_handle);
179                    this.timeout_handle = null;
180            }
181            this.timeout_handle=setTimeout("top.__bfilter_obj["+this.obj_count+"].show_filter('"+user_filter.replace(/'/,"\\'")+"');", this.timeout);
182            return true;
183    }
184    
185    BFilter.prototype.show_filter = function (user_filter) {
186    
187            if (this.timeout_handle) {
188                    clearTimeout(this.timeout_handle);
189                    this.timeout_handle = null;
190                    this.debug("timeout cleared");
191            }
192    
193          this.results('',1);          this.results('',1);
194          this.hits = 0;          this.hits = 0;
195    
196          if (find.length < this.min_len) {          if (user_filter.length < this.min_len) {
197                  this.show_status();                  this.show_status();
198                  return;                  return;
199          }          }
200    
201          this.debug("filter: '"+find+"'");          this.debug("filter: '"+user_filter+"'");
202    
203          var find_lc = find.toLowerCase();          var user_filter_lc = user_filter.toLowerCase();
204    
205          var part = find_lc.substr(0,this.min_len);          var part = user_filter_lc.substr(0,this.min_len);
206    
207          // no part found          // no part found
208          if (! this.arr[part]) {          if (! this.arr[part]) {
209                  this.show_status(" for <em>"+find+"</em><br>");                  this.show_status(" for <em>"+user_filter+"</em><br>");
210                  this.debug("no part "+part);                  this.debug("no part "+part);
211                  return;                  return;
212          }          }
# Line 190  BFilter.prototype.filter = function (doc Line 217  BFilter.prototype.filter = function (doc
217          var i;          var i;
218    
219          // full part? (optimization)          // full part? (optimization)
220          if (find.length == this.min_len) {          if (user_filter.length == this.min_len) {
221                  for (i = 0; i < this.arr[part].length; i++) {                  for (i = 0; i < this.arr[part].length; i++) {
222                          this.result(this.arr[part][i]);                          this.result(this.arr[part][i]);
223                          this.hits++;                          this.hits++;
# Line 198  BFilter.prototype.filter = function (doc Line 225  BFilter.prototype.filter = function (doc
225                  this.results();                  this.results();
226          } else {          } else {
227    
228                  var from = this.binarySearch(this.arr[part], find_lc);                  var from = this.binarySearch(this.arr[part], user_filter_lc);
229    
230                  if (from != null) {                  if (from != null) {
231                                    
232                          this.debug("loop "+from+" ... "+this.arr[part].length);                          this.debug("loop "+from+" ... "+this.arr[part].length);
233    
234                          for(i = from ; i < this.arr[part].length ; i++) {                          for(i = from ; i < this.arr[part].length ; i++) {
235                                  if (this.arr[part][i][0].substring(0,find.length).toLowerCase() != find_lc) {                                  if (this.arr[part][i][0].substring(0,user_filter.length).toLowerCase() != user_filter_lc) {
236                                          this.debug("loop exit at "+i);                                          this.debug("loop exit at "+i);
237                                          break;                                          break;
238                                  }                                  }
# Line 218  BFilter.prototype.filter = function (doc Line 245  BFilter.prototype.filter = function (doc
245    
246          }          }
247    
248          this.show_status(" for <em>"+find+"</em>");          this.show_status(" for <em>"+user_filter+"</em>");
249    
250  }  }
251    

Legend:
Removed from v.29  
changed lines
  Added in v.30

  ViewVC Help
Powered by ViewVC 1.1.26