diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-04-02 02:06:31 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-04-02 02:06:31 -0400 |
commit | 6ceae6b02d89bfebbb3bec13ce9525014cbdd2a7 (patch) | |
tree | 3aa1292d70e199a7919c5ea522758e6d8ea0f5b6 /data/stuff/js | |
parent | 23ba337de24e88c6aaf6ceb37507997f5c5f4920 (diff) | |
download | old-guff-6ceae6b02d89bfebbb3bec13ce9525014cbdd2a7.tar.bz2 old-guff-6ceae6b02d89bfebbb3bec13ce9525014cbdd2a7.zip |
add search-field and searching to users panel
Diffstat (limited to 'data/stuff/js')
-rw-r--r-- | data/stuff/js/search-field.js | 79 | ||||
-rw-r--r-- | data/stuff/js/util.js | 15 |
2 files changed, 94 insertions, 0 deletions
diff --git a/data/stuff/js/search-field.js b/data/stuff/js/search-field.js new file mode 100644 index 0000000..42bec10 --- /dev/null +++ b/data/stuff/js/search-field.js @@ -0,0 +1,79 @@ +jQuery(function($) { + "use strict"; + + function clear_delay(el) { + var ival = el.data('search-interval'); + + if (ival) { + // clear interval + clearInterval(ival); + el.data('search-interval', null); + } + } + + function trigger_search(el, q) { + if (q == el.data('q')) + return; + + // cache search + el.data('q', q); + + // trigger search + el.trigger({ + type: "search-update", + query: "", + }); + } + + $('.btn.search-toggle').click(function() { + var btn = $(this), + bar = btn.parents('.panel').find('.panel-heading.search-toggle'), + field = bar.find('input'); + + // toggle button highlight + btn.toggleClass('btn-primary btn-default') + var active = btn.hasClass('btn-primary'); + + // clear field + field.val(''); + + // toggle bar visibility + bar.toggleClass('hidden', !active); + + if (active) { + setTimeout(function() { + // focus text field + field.focus(); + }, 10); + } else { + // trigger search + trigger_search(field, ''); + } + }); + + $('.panel-heading.search-toggle input').keydown(function(ev) { + var me = $(this), + trigger = me.data('search-trigger') || 'delay'; + + if (trigger == 'delay') { + clear_delay(me); + + setTimeout(function() { + clear_delay(me); + trigger_search(me, me.val()); + }, me.data('search-delay') || 100); + } else if (trigger == 'enter') { + if (ev.which == 13) { + setTimeout(function() { + trigger_search(me, me.val()); + }, 10); + + // stop event + return false; + } + } else { + // unknown trigger type + throw new Error('unknown search trigger: ' + trigger); + } + }); +}); diff --git a/data/stuff/js/util.js b/data/stuff/js/util.js index c021499..ed048ec 100644 --- a/data/stuff/js/util.js +++ b/data/stuff/js/util.js @@ -1,3 +1,18 @@ +$.fn.reduce = (function() { + if (Array.prototype.reduce) { + return Array.prototype.reduce; + } else { + return function(a, cb, r) { + a = [].concat(a); + + for (var i = 0, l = a.length; i < l; i++) + r = cb(r, a[i]); + + return r; + }; + } +})(); + jQuery(function($) { "use strict"; |