termcol: fix cell description fallback of index 0
[sheet.git] / searchlocal.js
index 1a5037f1f7def5737a8ce3455f1c7e2ff74de848..512f07b8482cf1bf293e16c09d385a6178fb014a 100644 (file)
@@ -1,4 +1,8 @@
+var filterupdate;
+var filtertoggles = document.body.classList !== undefined;
+
 function filtercell(el, set, action) {
+       if (set === undefined) return;
        switch (action) {
                case 'focus':
                        el.classList[set ? 'add' : 'remove'](action);
@@ -6,32 +10,99 @@ function filtercell(el, set, action) {
                case 'target':
                        if (set) el.classList.toggle(action);
                        break;
+               case 'remove':
+                       if (set) el.style.display = 'none';
+                       break;
+               case 'add':
+                       if (set) el.style.display = '';
+                       break;
+               case 'toggle':
+                       if (set) el.style.display = el.style.display == 'none' ? '' : 'none';
+                       break;
                case 'filter':
                        el.style.display = set ? '' : 'none';
-                       if (!Element.prototype.hasOwnProperty('classList')) return;
-                       // continue
+                       if (filtertoggles) el.classList.remove('focus');
+                       break;
                default: // reset
                        el.classList.remove('focus');
                        el.classList.remove('target');
        }
 }
 
+function filtercols(table, match, action) {
+       var matchloc = [];
+       for (var y = 0; y < table.rows.length; y++) {
+               var loc = 0;
+               for (var x = 0; x < table.rows[y].cells.length; x++) {
+                       var cell = table.rows[y].cells[x];
+                       if (y == 0) {
+                               var res = match(cell);
+                               for (var i = loc; i < loc + cell.colSpan; i++) matchloc[i] = res;
+                               filtercell(table.children.item(x), res, action); // colgroup
+                       }
+                       filtercell(cell, matchloc[loc], action);
+                       loc += cell.colSpan;
+               }
+       }
+}
+
 function filterrows(table, match, action) {
        var rows = table.tBodies[0].rows;
        for (var i = 0; i < rows.length; i++) {
-               filtercell(rows[i], match(rows[i]), action);
+               filtercell(rows[i], match && match(rows[i]), action);
        }
 }
 
 function filtertable(query, action) {
+       filterupdate = undefined;
+       if (query === undefined) query = document.getElementById('search').q.value;
        var table = document.getElementsByTagName('TABLE')[0];
 
+       if (!action) {
+               var match = /^([-+?=]?)(.*)/.exec(query);
+               switch (match[1]) {
+                       case '+': action = 'add';    break;
+                       case '-': action = 'remove'; break;
+                       case '?': action = 'toggle'; break;
+                       case '=': action = 'filter'; break;
+               }
+               query = match[2];
+       }
+
+if (document.querySelector !== undefined) {
+       if (query == '' && action == 'add') {
+               // restore all columns if explicitly adding all ("+")
+               filtercols(table, function(){return true}, 'add');
+               // continue to restore rows
+       }
+       if (/^[a-z_]+$/.test(query) && document.querySelector('.b-a-'+query)) {
+               // column if class b-a-* exists
+               var match = function(th) {
+                       if (!/\bb-a-/.test(th.className)) return;
+                       return new RegExp('-'+query+'\\b').test(th.className);
+               }
+               return filtercols(table, match, action || 'toggle');
+       }
+}
+
        if (/^[A-Z0-9 ]{2,}$/.test(query)) {
                // category title if all uppercase
                var match = function(row) {
                        return row.cells[0].title.match(query, 'i');
                };
        }
+       else if (numquery = /^([<>])(\d+)$/.exec(query)) {
+               // support percentage if numeric comparison
+               var match = function(row) {
+                       var pct = row.cells[row.cells.length - 1].textContent;
+                       pct -= numquery[2]; // compare to query
+                       return numquery[1] == '<' ? pct < 0 : pct >= 0;
+               };
+       }
+       else if (action == 'focus' && query.length <= 1) {
+               // prevent superfluous highlighting
+               var match = false;
+       }
        else {
                // title text (case-insensitive unless caps in input)
                var match = function(row) {
@@ -47,7 +118,7 @@ function newelement(tagname, attrlist, childlist) {
        for (var name in attrlist)
                el.setAttribute(name, attrlist[name]);
        if (childlist) for (var i = 0; i < childlist.length; i++)
-               el.appendChild(childlist[i]);
+               if (childlist[i]) el.appendChild(childlist[i]);
        return el;
 }
 
@@ -56,20 +127,22 @@ function prependsearch(target) {
                'form', {
                        id: 'search',
                        'class': 'aside',
-                       onsubmit: "filtertable(this.q.value); this.q.value = ''; return false",
+                       'className': 'aside', // msie
+                       onsubmit: "filtertable(this.q.value); this.q.value = ''; return false"
                },
                [
                        newelement('input', {
                                type: 'search',
                                name: 'q',
-                               onkeyup: "filtertable(this.value, this.value.length > 1 ? 'focus' : 'reset')",
+                               onkeyup: "if (filtertoggles && !filterupdate) filterupdate = "
+                                       + "window.setTimeout(filtertable, 300, undefined, 'focus')"
                        }),
-                       newelement('input', {
+                       filtertoggles && newelement('input', {
                                type: 'button',
                                value: 'toggle',
-                               onclick: "filtertable(this.form.q.value, 'target')",
+                               onclick: "filtertable(this.form.q.value, 'target')"
                        }),
-                       newelement('input', {type:'submit', value:'filter'}),
+                       newelement('input', {type:'submit', value:'filter'})
                ]
        ), target);
 }