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