browser: display 'js api' category as 'js'
[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 (query == '' && action == 'add') {
71                 // restore all columns if explicitly adding all ("+")
72                 filtercols(table, function(){return true}, 'add');
73                 // continue to restore rows
74         }
75         if (/^[a-z_]+$/.test(query) && document.querySelector('.b-a-'+query)) {
76                 // column if class b-a-* exists
77                 var match = function(th) {
78                         return new RegExp('-'+query+'\\b').test(th.className);
79                 }
80                 return filtercols(table, match, action || 'toggle');
81         }
82
83         if (/^[A-Z0-9 ]{2,}$/.test(query)) {
84                 // category title if all uppercase
85                 var match = function(row) {
86                         return row.cells[0].title.match(query, 'i');
87                 };
88         }
89         else if (numquery = /^([<>])(\d+)$/.exec(query)) {
90                 // support percentage if numeric comparison
91                 var match = function(row) {
92                         var pct = row.cells[row.cells.length - 1].textContent;
93                         pct -= numquery[2]; // compare to query
94                         return numquery[1] == '<' ? pct < 0 : pct >= 0;
95                 };
96         }
97         else if (action == 'focus' && query.length <= 1) {
98                 // prevent superfluous highlighting
99                 var match = false;
100         }
101         else {
102                 // title text (case-insensitive unless caps in input)
103                 var match = function(row) {
104                         return row.cells[1].textContent.match(query, /[A-Z]/.test(query) ? '' : 'i');
105                 };
106         }
107         filterrows(table, match, action || 'filter');
108 }
109
110 function newelement(tagname, attrlist, childlist) {
111         if (!attrlist) return document.createTextNode(tagname);
112         var el = document.createElement(tagname);
113         for (var name in attrlist)
114                 el.setAttribute(name, attrlist[name]);
115         if (childlist) for (var i = 0; i < childlist.length; i++)
116                 el.appendChild(childlist[i]);
117         return el;
118 }
119
120 function prependsearch(target) {
121         target.parentNode.insertBefore(newelement(
122                 'form', {
123                         id: 'search',
124                         'class': 'aside',
125                         onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
126                 },
127                 [
128                         newelement('input', {
129                                 type: 'search',
130                                 name: 'q',
131                                 onkeyup: "filtertable(this.value, 'focus')",
132                         }),
133                         newelement('input', {
134                                 type: 'button',
135                                 value: 'toggle',
136                                 onclick: "filtertable(this.form.q.value, 'target')",
137                         }),
138                         newelement('input', {type:'submit', value:'filter'}),
139                 ]
140         ), target);
141 }
142