summaryrefslogtreecommitdiffstats
path: root/assets/js/filtertable.js
blob: ecee7ab18eb7dd239eaaba432df0487c9d3d612b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 LightTableFilter by Chris Coyier
 http://codepen.io/chriscoyier/pen/tIuBL
 */
(function(document) {
    'use strict';

    var LightTableFilter = (function(Arr) {

	var _input;

	function _onInputEvent(e) {
	    _input = e.target;
	    var tables = document.getElementsByClassName(_input.getAttribute('data-table'));
	    Arr.forEach.call(tables, function(table) {
		Arr.forEach.call(table.tBodies, function(tbody) {
		    Arr.forEach.call(tbody.rows, _filter);
		});
	    });
	}

	function _filterExpr(val, text) {
	    return !val.split(' ').every(function(word) {
		if (word.charAt(0) === '-') {
		    return text.indexOf(word.substring(1)) === -1;
		} else {
		    return text.indexOf(word) !== -1;
		}
	    });
	}

	function _filter(row) {
	    var text = row.textContent.toLowerCase(), val = _input.value.toLowerCase();
	    row.style.display = _filterExpr(val, text) ? 'none' : 'table-row';
	}

	return {
	    init: function() {
		var inputs = document.getElementsByClassName('light-table-filter');
		Arr.forEach.call(inputs, function(input) {
		    input.oninput = _onInputEvent;
		});
	    }
	};
    })(Array.prototype);

    document.addEventListener('readystatechange', function() {
	if (document.readyState === 'complete') {
	    LightTableFilter.init();
	}
    });

})(document);