browser: write out search javascript code
[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 'filter':
10                         el.style.display = set ? '' : 'none';
11                         if (!Element.prototype.hasOwnProperty('classList')) return;
12                         // continue
13                 default: // reset
14                         el.classList.remove('focus');
15                         el.classList.remove('target');
16         }
17 }
18
19 function filterrows(table, match, action) {
20         var rows = table.tBodies[0].rows;
21         for (var i = 0; i < rows.length; i++) {
22                 filtercell(rows[i], match(rows[i]), action);
23         }
24 }
25
26 function filtertable(query, action) {
27         var table = document.getElementsByTagName('TABLE')[0];
28
29         if (/^[A-Z0-9 ]{2,}$/.test(query)) {
30                 // category title if all uppercase
31                 var match = function(row) {
32                         return row.cells[0].title.match(query, 'i');
33                 };
34         }
35         else {
36                 // title text (case-insensitive unless caps in input)
37                 var match = function(row) {
38                         return row.cells[1].textContent.match(query, /[A-Z]/.test(query) ? '' : 'i');
39                 };
40         }
41         filterrows(table, match, action || 'filter');
42 }
43
44 function newelement(tagname, attrlist, childlist) {
45         if (!attrlist) return document.createTextNode(tagname);
46         var el = document.createElement(tagname);
47         for (var name in attrlist)
48                 el.setAttribute(name, attrlist[name]);
49         if (childlist) for (var i = 0; i < childlist.length; i++)
50                 el.appendChild(childlist[i]);
51         return el;
52 }
53
54 function prependsearch(target) {
55         target.parentNode.insertBefore(newelement(
56                 'form', {
57                         id: 'search',
58                         'class': 'aside',
59                         onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
60                 },
61                 [
62                         newelement('input', {
63                                 type: 'search',
64                                 name: 'q',
65                                 onkeyup: "filtertable(this.value, this.value.length > 1 ? 'focus' : 'reset')",
66                         }),
67                         newelement('input', {
68                                 type: 'button',
69                                 value: 'toggle',
70                                 onclick: "filtertable(this.form.q.value, 'target')",
71                         }),
72                         newelement('input', {type:'submit', value:'filter'}),
73                 ]
74         ), target);
75 }
76