From ae667b4222a718f99091ec8bb1fb130970b051e7 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 15 May 2016 17:07:16 -0400 Subject: initial commit --- data/assets/js/search-field.js | 82 ++++++++++++++++++++++++++++++++++++++++++ data/assets/js/util.js | 31 ++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 data/assets/js/search-field.js create mode 100644 data/assets/js/util.js (limited to 'data/assets/js') diff --git a/data/assets/js/search-field.js b/data/assets/js/search-field.js new file mode 100644 index 0000000..76ff69f --- /dev/null +++ b/data/assets/js/search-field.js @@ -0,0 +1,82 @@ +jQuery(function($) { + "use strict"; + + function clear_delay(el) { + var ival = el.data('search-interval'); + + if (ival) { + // clear timeout + clearTimeout(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, ''); + } + + // stop event + return false; + }); + + $('.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/assets/js/util.js b/data/assets/js/util.js new file mode 100644 index 0000000..ed048ec --- /dev/null +++ b/data/assets/js/util.js @@ -0,0 +1,31 @@ +$.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"; + + window.send = function(fn, args) { + return $.ajax({ + url: "/api/" + fn, + method: 'POST', + dataType: 'json', + data: args, + }); + }; + + LuigiTemplate.FILTERS.json = function(val) { + return JSON.stringify(val); + }; +}); -- cgit v1.2.3