initial
[plewww.git] / plekit / table / table.js
1 /* $Id$ */
2
3 /* when a table gets paginated, displays context info */
4 function plekit_table_paginator (opts,table_id) {
5
6   if(!("currentPage" in opts)) { return; }
7     
8   var p = document.createElement('p');
9   var table=$(table_id);
10   var t = $(table_id+'-fdtablePaginaterWrapTop');
11   var b = $(table_id+'-fdtablePaginaterWrapBottom');
12
13   /* when there's no visible entry, the pagination code removes the wrappers */
14   if ( (!t) || (!b) ) return;
15
16   /* get how many entries are matching:
17      opts.visibleRows only holds the contents of the current page
18      so we store the number of matching entries in the table 'matching' attribute
19   */
20   var totalMatches = opts.totalRows;
21   var matching=table['matching'];
22   if (matching) totalMatches = matching;
23
24   var label;
25
26   var matches_text;
27   if (totalMatches != opts.totalRows) {
28     matches_text = totalMatches + "/" + opts.totalRows;
29   } else {
30     matches_text = opts.totalRows;
31   }
32   var first = ((opts.currentPage-1) * opts.rowsPerPage) +1;
33   var last = Math.min((opts.currentPage * opts.rowsPerPage),totalMatches);
34   var items_text = "Items [" + first + " - " + last + "] of " + matches_text;
35   var page_text = "Page " + opts.currentPage + " of " + Math.ceil(totalMatches / opts.rowsPerPage);
36   label = items_text + " -- " + page_text;
37
38   p.className = "paginationText";    
39   p.appendChild(document.createTextNode(label));
40
41   /*  t.insertBefore(p.cloneNode(true), t.firstChild); */
42   b.appendChild(p);
43 }
44
45
46 /* locates a table from its id and alters the classname to reflect new table size */
47 function plekit_pagesize_set (table_id,size_id,def_size) {
48   var table=document.getElementById(table_id);
49   var size_area=document.getElementById(size_id);
50   if ( ! size_area.value ) {
51     size_area.value=def_size;
52   }
53   var size=size_area.value;
54   table.className=table.className.replace(/paginate-\d+/,"paginate-"+size); 
55   tablePaginater.init(table_id);
56 }
57
58 function plekit_pagesize_reset(table_id, size_id, size) {
59   var table=document.getElementById(table_id);
60   var size_area=document.getElementById(size_id);
61   size_area.value=size;
62   table.className=table.className.replace(/paginate-\d+/,"paginate-"+size); 
63   tablePaginater.init(table_id);
64 }
65   
66 /* set or clear the ' invisibleRow' in the tr's classname, according to visible */
67 function plekit_table_row_visible (row,visible) {
68   var cn=row.className;
69   /* clear */
70   cn=cn.replace("invisibleRow","");
71   cn.strip();
72   if (! visible) cn += " invisibleRow";
73   row.className=cn;
74 }
75
76 // Working around MSIE...
77 if ('undefined' == typeof Node)
78     Node = { ELEMENT_NODE: 1, TEXT_NODE: 3 };
79
80 // Extract actual text from a DOM node (remove internal tags and so on)
81 function getInnerText(node) {
82         var result = '';
83         if (Node.TEXT_NODE == node.nodeType)
84                 return node.nodeValue;
85         if (Node.ELEMENT_NODE != node.nodeType)
86                 return '';
87         for (var index = 0; index < node.childNodes.length; ++index)
88                 result += getInnerText(node.childNodes.item(index));
89         return result;
90 } // getInnerText
91
92 // cache in the <tr> node the concatenation of the innerTexts of its cells
93 function plekit_tr_text (tr) {
94   // if cached, use it
95   if (tr['text_to_match']) return tr['text_to_match'];
96   // otherwise compute it
97   var text="";
98   var cells=tr.cells;
99   for (var i=0; i<cells.length; i++) 
100     text += getInnerText(cells[i]) + " ";
101   text = text.strip().toLowerCase();
102   tr['text_to_match'] = text;
103   return text;
104 }
105
106 var plekit_table_filter_timeout = null;
107 /* scan the table, and mark as visible 
108    the rows that match (either AND or OR the patterns) */
109 function plekit_table_filter(table_id,pattern_id,and_id) {
110   clearTimeout(plekit_table_filter_timeout);
111   plekit_table_filter_timeout = setTimeout(function() {
112                                              plekit_lazy_table_filter(table_id,pattern_id,and_id)
113                                            },
114                                            200);
115 }
116
117 function plekit_lazy_table_filter (table_id,pattern_id,and_id) {
118   var table=$(table_id);
119   var css='#'+table_id+'>tbody';
120   var rows = $$(css)[0].rows;
121   var pattern_area = $(pattern_id);
122   var pattern_text = pattern_area.value;
123   var matching_entries=0;
124   var and_button=$(and_id);
125   var and_if_true=and_button.checked;
126
127   // canonicalize white spaces 
128   pattern_text = pattern_text.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g,' ');
129   
130   if (pattern_text.indexOf ("&") != -1) {
131     pattern_text = pattern_text.replace(/&/," ");
132     pattern_area.value=pattern_text;
133     and_button.checked=true;
134     return;
135   } else if (pattern_text.indexOf ("|") != -1 ) {
136     pattern_text = pattern_text.replace(/\|/," ");
137     pattern_area.value=pattern_text;
138     and_button.checked=false;
139     return;
140   }
141     
142   var match_attempts=0;
143   var start=(new Date).getTime();
144
145   // if we're running with the same pattern
146   var previous_pattern=table['previous_pattern'];
147   var previous_mode=table['previous_mode'];
148   if ( (previous_pattern == pattern_text) && (previous_mode == and_if_true) ) {
149     return;
150   }
151
152   var searches=new Array();
153   var patterns=new Array();
154   if (pattern_text.length > 0) {
155       // re compile all patterns 
156       var pattern_texts = pattern_text.strip().split(" ");
157       for (var i=0; i < pattern_texts.length; i++) {
158           // ignore case
159           searches[i]=pattern_texts[i].toLowerCase();
160           patterns[i]=new RegExp(pattern_texts[i],"i");
161       }
162   }
163
164   // scan rows, elaborate 'visible'
165   for (var row_index = 0; row_index < rows.length ; row_index++) {
166     var tr=rows[row_index];
167     var visible=false;
168     
169     /*empty pattern */
170     if (patterns.length == 0) {
171       visible=true;
172     } else if (and_if_true) {
173       /* AND mode: all patterns must match */
174       visible=true;
175       var against=plekit_tr_text (tr);
176       for (var search_index=0; search_index<searches.length; search_index++) {
177         var search=searches[search_index];
178         match_attempts++;
179         if ( against.search(search) < 0) {
180           visible=false;
181           break;          
182         }
183       }
184     } else {
185       /* OR mode: any match is good enough */
186       visible=false;
187       var against = plekit_tr_text(tr);
188       for (var search_index=0; search_index < searches.length; search_index++) {
189         var search=searches[search_index];
190         match_attempts++;
191         if (against.search(search) >= 0) {
192           visible=true;
193           break;
194         }
195       }
196     }
197
198     plekit_table_row_visible(tr,visible);
199     if (visible) matching_entries +=1;
200   }
201   // save for next run
202   table['previous_pattern']=pattern_text;
203   table['previous_mode']=and_if_true;
204   
205   var end=(new Date).getTime();
206   var match_ms=end-start;
207
208   // optimize useless calls to init, by comparing # of matching entries
209   var previous_matching=table['previous_matching'];
210   if (matching_entries == previous_matching) {
211     return;
212   }
213   
214   table['matching']=matching_entries;
215   table['match_attempts']=match_attempts;
216   tablePaginater.init(table_id);
217   var end2=(new Date).getTime();
218   var paginate_ms=end2-end;
219   
220 }
221
222 function plekit_table_filter_reset (table_id, pattern_id,and_id) {
223   /* reset pattern */
224   document.getElementById(pattern_id).value="";
225   plekit_table_filter (table_id, pattern_id,and_id);
226 }