/[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 30 by dpavlin, Fri Sep 24 16:58:08 2004 UTC revision 32 by dpavlin, Sat Sep 25 22:13:13 2004 UTC
# Line 17  function BFilter(arr) { Line 17  function BFilter(arr) {
17          top.__bfilter_obj[this.obj_count] = this;          top.__bfilter_obj[this.obj_count] = this;
18    
19          // clear results html          // clear results html
20          this.results_html = '';          this.results_html = null;
21    
22          // show results after 0.2s          // show results after 0.2s
23          this.timeout = 200;          this.timeout = 200;
24          this.timeout_handle = null;          this.timeout_handle = null;
25    
         // this function is called for each result  
         this.result = function (arr) {  
                 this.results_html += '<li><a href="'+arr[1]+'">'+  
                         (this.hits % 2 == 0 ? '<span style="background: #e0e0e0;">' : '') +  
                         arr[0] +  
                         (this.hits % 2 == 0 ? '</span>' : '') +  
                         '</a></li>';  
                 return true;  
         }  
   
         // this function is called when updating innerHTML with results  
         this.display = function () {  
                 if (this.results_html) {  
                         return '<ul>'+this.results_html+'</ul>';  
                 } else {  
                         return null;  
                 }  
         }  
   
   
26          if (! arr) {          if (! arr) {
27                  this.debug("ERROR: can't search empty array");                  this.debug("ERROR: can't search empty array");
28                  return;                  return;
# Line 52  function BFilter(arr) { Line 32  function BFilter(arr) {
32          this.debug("index has "+this.arr.length+" parts");          this.debug("index has "+this.arr.length+" parts");
33    
34          if (! arr.min_len) {          if (! arr.min_len) {
35                  this.results("ERROR: index structure problem");                  alert("ERROR: index structure problem");
36                  return;                  return;
37          } else {          } else {
38                  this.min_len = arr.min_len;                  this.min_len = arr.min_len;
# Line 62  function BFilter(arr) { Line 42  function BFilter(arr) {
42    
43          this.show_status();          this.show_status();
44    
45            // show alert for non-existent elements?
46            this.show_alert = 1;
47    
48  }  }
49    
50  BFilter.prototype.element_id = function (id,no_alert) {  BFilter.prototype.element_id = function (id,no_alert) {
# Line 73  BFilter.prototype.element_id = function Line 56  BFilter.prototype.element_id = function
56                          this.id_cache[id] = el;                          this.id_cache[id] = el;
57                          return el;                          return el;
58                  } else {                  } else {
59                          if (! no_alert) {                          if (! no_alert && this.show_alert) {
60                                  alert("can't find element id: "+id);                                  this.show_alert = confirm("can't find element id: "+id);
61                          } else {                          } else {
62                                  // don't look it up again                                  // don't look it up again
63                                  this.id_cache[id] = null;                                  this.id_cache[id] = null;
# Line 85  BFilter.prototype.element_id = function Line 68  BFilter.prototype.element_id = function
68  }  }
69    
70    
71  BFilter.prototype.show_status = function (status) {  BFilter.prototype.show_status = function (status,no_hits) {
72          var html;          var html = "";
73          if (this.hits > 0) {          if (! no_hits) {
74                  html = "shown "+this.hits+" entries";                  if (this.hits > 0) {
75          } else {                          html = "shown "+this.hits+" entries";
76                  html = "no results";                  } else {
77          }                          html = "no results";
78          if (! status) {                  }
79                  html = "Enter "+this.min_len+" letter"+(this.min_len == 1 ? '' : 's')+" to filter entries";                  if (! status) {
80                  status = "";                          html = "Enter "+this.min_len+" letter"+(this.min_len == 1 ? '' : 's')+" to filter entries";
81                            status = "";
82                    }
83          }          }
84    
85          var el = this.element_id("status");          var el = this.element_id("status");
86          el.innerHTML = html+status+"\n";          el.innerHTML = html+status+"\n";
87    
88            return true;
89  }  }
90    
91  BFilter.prototype.results = function (html,clean) {  // this function is called to clean old results list
92    BFilter.prototype.clear_results = function () {
93            var results_div = this.element_id("results");
94            results_div.innerHTML = '';
95            this.results_html = '';
96            return true;
97    }
98    
99          if (! html) { html = ""; }  // this function is called for each result
100    BFilter.prototype.result = function (arr) {
101            this.results_html += '<li><a href="'+arr[1]+'">'+
102                    (this.hits % 2 == 0 ? '<span style="background: #e0e0e0;">' : '') +
103                    arr[0] +
104                    (this.hits % 2 == 0 ? '</span>' : '') +
105                    '</a></li>';
106            return true;
107    }
108    
109          // results_div.style.cursor = 'wait'; // 'auto'  // this function is called when updating innerHTML with results
110    BFilter.prototype.show_results = function () {
111          var results_div = this.element_id("results");          var results_div = this.element_id("results");
112          if (clean) {          if (this.results_html) {
113                  results_div.innerHTML = html;                  results_div.innerHTML = '<ul>'+this.results_html+'</ul>';
                 this.results_html = '';  
         } else {  
                 html = this.display();  
                 if (html) results_div.innerHTML += html;  
114          }          }
115            return true;
116  }  }
117    
   
118  BFilter.prototype.debug = function (html) {  BFilter.prototype.debug = function (html) {
119    
120          //return;          //return;
# Line 141  BFilter.prototype.binarySearch = functio Line 139  BFilter.prototype.binarySearch = functio
139          var low = 0;          var low = 0;
140          var high = arr.length - 1;          var high = arr.length - 1;
141          var middlearr = parseInt(arr.length / 2);          var middlearr = parseInt(arr.length / 2);
142            this.debug("binarySearch: "+low+"-("+middlearr+")-"+high+" for "+user_filter);
143          var lastTry;          var lastTry;
144          while (low <= high) {          while (low <= high) {
145                  var mid = (low + high) / 2;                  var mid = (low + high) / 2;
146                  var aTry = (mid < 1) ? 0 : parseInt(mid);                  var aTry = (mid < 1) ? 0 : parseInt(mid);
147                    
148                  var curr = arr[aTry][0].substr(0,user_filter.length).toLowerCase();                  var curr = arr[aTry][0].substr(0,user_filter.length).toLowerCase();
149                  this.debug("low="+low+" high="+high+" lastTry="+lastTry+" "+aTry+": "+curr);                  this.debug(low+"-"+high+", "+aTry+"="+curr+" last="+lastTry);
150                  if (curr < user_filter) {                  if (curr < user_filter) {
151                          low = aTry + 1;                          low = aTry + 1;
152                          continue;                          continue;
# Line 184  BFilter.prototype.filter = function (use Line 183  BFilter.prototype.filter = function (use
183    
184  BFilter.prototype.show_filter = function (user_filter) {  BFilter.prototype.show_filter = function (user_filter) {
185    
186            this.show_status("Showing entries with <em>"+user_filter+"</em>\n");
187    
188          if (this.timeout_handle) {          if (this.timeout_handle) {
189                  clearTimeout(this.timeout_handle);                  clearTimeout(this.timeout_handle);
190                  this.timeout_handle = null;                  this.timeout_handle = null;
191                  this.debug("timeout cleared");                  this.debug("timeout cleared");
192          }          }
193    
194          this.results('',1);          this.clear_results();
195          this.hits = 0;          this.hits = 0;
196    
197          if (user_filter.length < this.min_len) {          if (user_filter.length < this.min_len) {
# Line 198  BFilter.prototype.show_filter = function Line 199  BFilter.prototype.show_filter = function
199                  return;                  return;
200          }          }
201    
         this.debug("filter: '"+user_filter+"'");  
   
202          var user_filter_lc = user_filter.toLowerCase();          var user_filter_lc = user_filter.toLowerCase();
203            
204            this.debug("filter: '"+user_filter_lc+"'");
205    
206          var part = user_filter_lc.substr(0,this.min_len);          var part = user_filter_lc.substr(0,this.min_len);
207    
# Line 212  BFilter.prototype.show_filter = function Line 213  BFilter.prototype.show_filter = function
213          }          }
214    
215          // start anim icon          // start anim icon
216          //results("<img  src=\"pie.gif\">&nbsp;Please wait, filtering...\n",1);          //<img  src=\"pie.gif\">&nbsp;Please wait, filtering...
217    
218          var i;          var i;
219    
# Line 222  BFilter.prototype.show_filter = function Line 223  BFilter.prototype.show_filter = function
223                          this.result(this.arr[part][i]);                          this.result(this.arr[part][i]);
224                          this.hits++;                          this.hits++;
225                  }                  }
226                  this.results();                  this.show_results();
227          } else {          } else {
228    
229                  var from = this.binarySearch(this.arr[part], user_filter_lc);                  var from = this.binarySearch(this.arr[part], user_filter_lc);
# Line 240  BFilter.prototype.show_filter = function Line 241  BFilter.prototype.show_filter = function
241                                  this.hits++;                                  this.hits++;
242                          }                          }
243    
244                          this.results();                          this.show_results();
245                  }                  }
246    
247          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26