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 (query == '' && action == 'add') {
71 // restore all columns if explicitly adding all ("+")
72 filtercols(table, function(){return true}, 'add');
73 // continue to restore rows
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);
80 return filtercols(table, match, action || 'toggle');
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');
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;
97 else if (action == 'focus' && query.length <= 1) {
98 // prevent superfluous highlighting
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');
107 filterrows(table, match, action || 'filter');
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]);
120 function prependsearch(target) {
121 target.parentNode.insertBefore(newelement(
125 onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
128 newelement('input', {
131 onkeyup: "filtertable(this.value, 'focus')",
133 newelement('input', {
136 onclick: "filtertable(this.form.q.value, 'target')",
138 newelement('input', {type:'submit', value:'filter'}),