browser: avoid exceptional colgroup filter offset
[sheet.git] / searchlocal.js
1 var filterupdate;
2
3 function filtercell(el, set, action) {
4         switch (action) {
5                 case 'focus':
6                         el.classList[set ? 'add' : 'remove'](action);
7                         break;
8                 case 'target':
9                         if (set) el.classList.toggle(action);
10                         break;
11                 case 'remove':
12                         if (set) el.style.display = 'none';
13                         break;
14                 case 'add':
15                         if (set) el.style.display = '';
16                         break;
17                 case 'toggle':
18                         if (set) el.style.display = el.style.display == 'none' ? '' : 'none';
19                         break;
20                 case 'filter':
21                         el.style.display = set ? '' : 'none';
22                         if (!Element.prototype.hasOwnProperty('classList')) return;
23                         el.classList.remove('focus');
24                         break;
25                 default: // reset
26                         el.classList.remove('focus');
27                         el.classList.remove('target');
28         }
29 }
30
31 function filtercols(table, match, action) {
32         var matchloc;
33         for (var y = 0; y < table.rows.length; y++) {
34                 var loc = 0;
35                 for (var x = 0; x < table.rows[y].cells.length; x++) {
36                         var cell = table.rows[y].cells[x];
37                         if (y == 0) {
38                                 if (match(cell)) {
39                                         if (!matchloc) matchloc = [loc];
40                                         matchloc[1] = loc + cell.colSpan;
41                                         filtercell(table.children.item(x), true, action); // colgroup
42                                 }
43                         }
44                         var keep = matchloc && loc >= matchloc[0] && loc < matchloc[1];
45                         filtercell(cell, keep, action);
46                         loc += cell.colSpan;
47                 }
48         }
49 }
50
51 function filterrows(table, match, action) {
52         var rows = table.tBodies[0].rows;
53         for (var i = 0; i < rows.length; i++) {
54                 filtercell(rows[i], match && match(rows[i]), action);
55         }
56 }
57
58 function filtertable(query, action) {
59         filterupdate = undefined;
60         if (query === undefined) query = document.getElementById('search').q.value;
61         var table = document.getElementsByTagName('TABLE')[0];
62
63         if (!action) {
64                 var match = /^([-+?=]?)(.*)/.exec(query);
65                 switch (match[1]) {
66                         case '+': action = 'add';    break;
67                         case '-': action = 'remove'; break;
68                         case '?': action = 'toggle'; break;
69                         case '=': action = 'filter'; break;
70                 }
71                 query = match[2];
72         }
73
74         if (query == '' && action == 'add') {
75                 // restore all columns if explicitly adding all ("+")
76                 filtercols(table, function(){return true}, 'add');
77                 // continue to restore rows
78         }
79         if (/^[a-z_]+$/.test(query) && document.querySelector('.b-a-'+query)) {
80                 // column if class b-a-* exists
81                 var match = function(th) {
82                         return new RegExp('-'+query+'\\b').test(th.className);
83                 }
84                 return filtercols(table, match, action || 'toggle');
85         }
86
87         if (/^[A-Z0-9 ]{2,}$/.test(query)) {
88                 // category title if all uppercase
89                 var match = function(row) {
90                         return row.cells[0].title.match(query, 'i');
91                 };
92         }
93         else if (numquery = /^([<>])(\d+)$/.exec(query)) {
94                 // support percentage if numeric comparison
95                 var match = function(row) {
96                         var pct = row.cells[row.cells.length - 1].textContent;
97                         pct -= numquery[2]; // compare to query
98                         return numquery[1] == '<' ? pct < 0 : pct >= 0;
99                 };
100         }
101         else if (action == 'focus' && query.length <= 1) {
102                 // prevent superfluous highlighting
103                 var match = false;
104         }
105         else {
106                 // title text (case-insensitive unless caps in input)
107                 var match = function(row) {
108                         return row.cells[1].textContent.match(query, /[A-Z]/.test(query) ? '' : 'i');
109                 };
110         }
111         filterrows(table, match, action || 'filter');
112 }
113
114 function newelement(tagname, attrlist, childlist) {
115         if (!attrlist) return document.createTextNode(tagname);
116         var el = document.createElement(tagname);
117         for (var name in attrlist)
118                 el.setAttribute(name, attrlist[name]);
119         if (childlist) for (var i = 0; i < childlist.length; i++)
120                 el.appendChild(childlist[i]);
121         return el;
122 }
123
124 function prependsearch(target) {
125         target.parentNode.insertBefore(newelement(
126                 'form', {
127                         id: 'search',
128                         'class': 'aside',
129                         onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
130                 },
131                 [
132                         newelement('input', {
133                                 type: 'search',
134                                 name: 'q',
135                                 onkeyup: "if (!filterupdate) filterupdate = "
136                                         + "window.setTimeout(filtertable, 300, undefined, 'focus')",
137                         }),
138                         newelement('input', {
139                                 type: 'button',
140                                 value: 'toggle',
141                                 onclick: "filtertable(this.form.q.value, 'target')",
142                         }),
143                         newelement('input', {type:'submit', value:'filter'}),
144                 ]
145         ), target);
146 }
147