3 /* when a table gets paginated, displays context info */
4 function plekit_table_paginator (opts,table_id) {
6 if(!("currentPage" in opts)) { return; }
8 var p = document.createElement('p');
10 var t = $(table_id+'-fdtablePaginaterWrapTop');
11 var b = $(table_id+'-fdtablePaginaterWrapBottom');
13 /* when there's no visible entry, the pagination code removes the wrappers */
14 if ( (!t) || (!b) ) return;
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
20 var totalMatches = opts.totalRows;
21 var matching=table['matching'];
22 if (matching) totalMatches = matching;
27 if (totalMatches != opts.totalRows) {
28 matches_text = totalMatches + "/" + opts.totalRows;
30 matches_text = opts.totalRows;
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;
38 p.className = "paginationText";
39 p.appendChild(document.createTextNode(label));
41 /* t.insertBefore(p.cloneNode(true), t.firstChild); */
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;
53 var size=size_area.value;
54 table.className=table.className.replace(/paginate-\d+/,"paginate-"+size);
55 tablePaginater.init(table_id);
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);
62 table.className=table.className.replace(/paginate-\d+/,"paginate-"+size);
63 tablePaginater.init(table_id);
66 /* set or clear the ' invisibleRow' in the tr's classname, according to visible */
67 function plekit_table_row_visible (row,visible) {
70 cn=cn.replace(" invisibleRow","");
71 if (! visible) cn += " invisibleRow";
75 // Working around MSIE...
76 if ('undefined' == typeof Node)
77 Node = { ELEMENT_NODE: 1, TEXT_NODE: 3 };
79 // Extract actual text from a DOM node (remove internal tags and so on)
80 function getInnerText(node) {
82 if (Node.TEXT_NODE == node.nodeType)
83 return node.nodeValue;
84 if (Node.ELEMENT_NODE != node.nodeType)
86 for (var index = 0; index < node.childNodes.length; ++index)
87 result += getInnerText(node.childNodes.item(index));
91 // cache in the <tr> node the concatenation of the innerTexts of its cells
92 function plekit_tr_text (tr) {
94 if (tr['text_to_match']) return tr['text_to_match'];
95 // otherwise compute it
98 for (var i=0; i<cells.length; i++)
99 text += getInnerText(cells[i]) + " ";
100 text = text.strip().toLowerCase();
101 tr['text_to_match'] = text;
105 /* scan the table, and mark as visible
106 the rows that match (either AND or OR the patterns) */
107 function plekit_table_filter (table_id,pattern_id,and_id) {
108 var table=$(table_id);
109 var css='#'+table_id+'>tbody';
110 var rows = $$(css)[0].rows;
111 var pattern_area = $(pattern_id);
112 var pattern_text = pattern_area.value;
113 var matching_entries=0;
114 var and_button=$(and_id);
115 var and_if_true=and_button.checked;
117 // canonicalize white spaces
118 pattern_text = pattern_text.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/g,' ');
120 if (pattern_text.indexOf ("&") != -1) {
121 pattern_text = pattern_text.replace(/&/," ");
122 pattern_area.value=pattern_text;
123 and_button.checked=true;
125 } else if (pattern_text.indexOf ("|") != -1 ) {
126 pattern_text = pattern_text.replace(/\|/," ");
127 pattern_area.value=pattern_text;
128 and_button.checked=false;
132 var match_attempts=0;
133 var start=(new Date).getTime();
135 // if we're running with the same pattern
136 var previous_pattern=table['previous_pattern'];
137 var previous_mode=table['previous_mode'];
138 if ( (previous_pattern == pattern_text) && (previous_mode == and_if_true) ) {
142 // re compile all patterns
143 var pattern_texts = pattern_text.strip().split(" ");
144 var searches=new Array();
145 var patterns=new Array();
146 for (var i=0; i < pattern_texts.length; i++) {
148 searches[i]=pattern_texts[i].toLowerCase();
149 patterns[i]=new RegExp(pattern_texts[i],"i");
152 // scan rows, elaborate 'visible'
153 for (var row_index = 0; row_index < rows.length ; row_index++) {
154 var tr=rows[row_index];
158 if (patterns.length == 0) {
160 } else if (and_if_true) {
161 /* AND mode: all patterns must match */
163 var against=plekit_tr_text (tr);
164 for (var search_index=0; search_index<searches.length; search_index++) {
165 var search=searches[search_index];
167 if ( against.search(search) < 0) {
173 /* OR mode: any match is good enough */
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];
179 if (against.search(search) >= 0) {
186 plekit_table_row_visible(tr,visible);
187 if (visible) matching_entries +=1;
190 table['previous_pattern']=pattern_text;
191 table['previous_mode']=and_if_true;
193 var end=(new Date).getTime();
194 var match_ms=end-start;
196 // optimize useless calls to init, by comparing # of matching entries
197 var previous_matching=table['previous_matching'];
198 if (matching_entries == previous_matching) {
202 table['matching']=matching_entries;
203 table['match_attempts']=match_attempts;
204 tablePaginater.init(table_id);
205 var end2=(new Date).getTime();
206 var paginate_ms=end2-end;
210 function plekit_table_filter_reset (table_id, pattern_id,and_id) {
212 document.getElementById(pattern_id).value="";
213 plekit_table_filter (table_id, pattern_id,and_id);