3 function filtercell(el, set, action) {
6 el.classList[set ? 'add' : 'remove'](action);
9 if (set) el.classList.toggle(action);
12 if (set) el.style.display = 'none';
15 if (set) el.style.display = '';
18 if (set) el.style.display = el.style.display == 'none' ? '' : 'none';
21 el.style.display = set ? '' : 'none';
22 if (!Element.prototype.hasOwnProperty('classList')) return;
23 el.classList.remove('focus');
26 el.classList.remove('focus');
27 el.classList.remove('target');
31 function filtercols(table, match, action) {
33 for (var y = 0; y < table.rows.length; y++) {
35 for (var x = 0; x < table.rows[y].cells.length; x++) {
36 var cell = table.rows[y].cells[x];
39 if (!matchloc) matchloc = [loc];
40 matchloc[1] = loc + cell.colSpan;
41 filtercell(table.children.item(x+2), true, action); // colgroup
44 var keep = matchloc && loc >= matchloc[0] && loc < matchloc[1];
45 filtercell(cell, keep, action);
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);
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];
64 var match = /^([-+?=]?)(.*)/.exec(query);
66 case '+': action = 'add'; break;
67 case '-': action = 'remove'; break;
68 case '?': action = 'toggle'; break;
69 case '=': action = 'filter'; break;
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
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);
84 return filtercols(table, match, action || 'toggle');
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');
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;
101 else if (action == 'focus' && query.length <= 1) {
102 // prevent superfluous highlighting
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');
111 filterrows(table, match, action || 'filter');
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]);
124 function prependsearch(target) {
125 target.parentNode.insertBefore(newelement(
129 onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
132 newelement('input', {
135 onkeyup: "if (!filterupdate) filterupdate = "
136 + "window.setTimeout(filtertable, 300, undefined, 'focus')",
138 newelement('input', {
141 onclick: "filtertable(this.form.q.value, 'target')",
143 newelement('input', {type:'submit', value:'filter'}),