1 function filtercell(el, set, action) {
4 el.classList[set ? 'add' : 'remove'](action);
7 if (set) el.classList.toggle(action);
10 if (set) el.style.display = 'none';
13 if (set) el.style.display = '';
16 if (set) el.style.display = el.style.display == 'none' ? '' : 'none';
19 el.style.display = set ? '' : 'none';
20 if (!Element.prototype.hasOwnProperty('classList')) return;
21 el.classList.remove('focus');
24 el.classList.remove('focus');
25 el.classList.remove('target');
29 function filtercols(table, match, action) {
31 for (var y = 0; y < table.rows.length; y++) {
33 for (var x = 0; x < table.rows[y].cells.length; x++) {
34 var cell = table.rows[y].cells[x];
37 if (!matchloc) matchloc = [loc];
38 matchloc[1] = loc + cell.colSpan;
39 filtercell(table.children.item(x+2), true, action); // colgroup
42 var keep = matchloc && loc >= matchloc[0] && loc < matchloc[1];
43 filtercell(cell, keep, action);
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);
56 function filtertable(query, action) {
57 var table = document.getElementsByTagName('TABLE')[0];
60 var match = /^([-+?=]?)(.*)/.exec(query);
62 case '+': action = 'add'; break;
63 case '-': action = 'remove'; break;
64 case '?': action = 'toggle'; break;
65 case '=': action = 'filter'; break;
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);
75 return filtercols(table, match, action || 'toggle');
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');
84 else if (action == 'focus' && query.length <= 1) {
85 // prevent superfluous highlighting
89 // title text (case-insensitive unless caps in input)
90 var match = function(row) {
91 return row.cells[1].textContent.match(query, /[A-Z]/.test(query) ? '' : 'i');
94 filterrows(table, match, action || 'filter');
97 function newelement(tagname, attrlist, childlist) {
98 if (!attrlist) return document.createTextNode(tagname);
99 var el = document.createElement(tagname);
100 for (var name in attrlist)
101 el.setAttribute(name, attrlist[name]);
102 if (childlist) for (var i = 0; i < childlist.length; i++)
103 el.appendChild(childlist[i]);
107 function prependsearch(target) {
108 target.parentNode.insertBefore(newelement(
112 onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
115 newelement('input', {
118 onkeyup: "filtertable(this.value, 'focus')",
120 newelement('input', {
123 onclick: "filtertable(this.form.q.value, 'target')",
125 newelement('input', {type:'submit', value:'filter'}),