3 /* when a table gets paginated, displays context info */
4 function plc_table_paginator (opts,tablename) {
6 if(!("currentPage" in opts)) { return; }
8 var p = document.createElement('p');
9 var t = document.getElementById(tablename+'-fdtablePaginaterWrapTop');
10 var b = document.getElementById(tablename+'-fdtablePaginaterWrapBottom');
12 /* when there's no visible entry, the pagination code removes the wrappers */
13 if ( (!t) || (!b) ) return;
15 /* get how many entries are matching:
16 opts.visibleRows only holds the contents of the current page
17 so we store the number of matching entries in the tbody's 'matching' attribute
19 var totalMatches = opts.totalRows;
20 var tbody=document.getElementById(tablename).getElementsByTagName("tbody")[0];
21 var matching=tbody['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 plc_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 plc_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 plc_table_row_visible (row,visible) {
70 cn=cn.replace(" invisibleRow","");
71 if (! visible) cn += " invisibleRow";
75 // from a cell, extract visible text by removing <> and cache in 'plc_text' attribute
76 var re_brackets = new RegExp ('<[^>]*>','g');
78 function plc_table_cell_text (cell) {
79 if (cell['plc_text']) return cell['plc_text'];
80 var text = cell.innerHTML;
81 // remove what's between <>
82 text = text.replace(re_brackets,'');
83 cell['plc_text'] = text;
87 /* scan the table, and mark as visible
88 the rows that match (either AND or OR the patterns) */
89 function plc_table_filter (table_id,pattern_id,and_id) {
90 var tbody = document.getElementById(table_id).getElementsByTagName("tbody")[0];
92 var pattern_area = document.getElementById(pattern_id);
93 var pattern_text = pattern_area.value;
94 var row_index, row, cells, cell_index, cell, visible;
95 var matching_entries=0;
96 var and_button=document.getElementById(and_id);
97 var and_if_true=and_button.checked;
99 // remove whitespaces at the beginning and end
100 pattern_text = pattern_text.replace(/[ \t]+$/,"");
101 pattern_text = pattern_text.replace(/^[ \t]+/,"");
103 if (pattern_text.indexOf ("&") != -1) {
104 pattern_text = pattern_text.replace(/&/," ");
105 pattern_area.value=pattern_text;
106 and_button.checked=true;
108 } else if (pattern_text.indexOf ("|") != -1 ) {
109 pattern_text = pattern_text.replace(/\|/," ");
110 pattern_area.value=pattern_text;
111 and_button.checked=false;
115 var match_attempts=0;
116 var start=(new Date).getTime();
118 // re compile all patterns - ignore case
119 var pattern_texts = pattern_text.split(" ");
120 var patterns=new Array();
122 for (i in pattern_texts)
123 patterns[i]=new RegExp(pattern_texts[i],"i");
126 for (row_index = 0; row=rows[row_index]; row_index++) {
130 if (patterns.length == 0) {
132 } else if (and_if_true) {
133 /* AND mode: all patterns must match */
135 for (i in patterns) {
137 var pattern=patterns[i];
138 for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
139 var against=plc_table_cell_text (cell);
141 if ( against.match(pattern)) {
146 if ( ! matched ) visible=false;
149 /* OR mode: any match is good enough */
151 for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
152 var against = cell.plc_table_cell_text(cell);
153 for (i in patterns) {
156 if (against.match(pattern)) {
158 // alert ('OR matched! p='+pattern+' c='+cell.innerHTML);
164 plc_table_row_visible(row,visible);
165 if (visible) matching_entries +=1;
167 var end=(new Date).getTime();
169 window.console.log ("plc_table_filter: " +
170 match_attempts + " matches - " +
171 matching_entries + " lines - " + ms + " ms");
172 tbody['matching']=matching_entries;
173 tbody['match_attempts']=match_attempts;
174 tablePaginater.init(table_id);
177 function plc_table_filter_reset (table_id, pattern_id,and_id) {
179 document.getElementById(pattern_id).value="";
180 plc_table_filter (table_id, pattern_id,and_id);