From ffb392617895095b824a35150e620a68920f9260 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 11 Dec 2016 15:46:54 -0500 Subject: Replace Chosen jQuery plugin by custom UI component --- assets/js/core/base.js | 25 +++++++++++++++++++++++++ assets/js/core/dom.js | 6 ++++-- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'assets/js/core') diff --git a/assets/js/core/base.js b/assets/js/core/base.js index 1da72357..d304d8bd 100644 --- a/assets/js/core/base.js +++ b/assets/js/core/base.js @@ -6,6 +6,7 @@ var KB = { listeners: { clicks: {}, changes: {}, + keys: {}, internals: {} } }; @@ -36,6 +37,10 @@ KB.onChange = function (selector, callback) { this.listeners.changes[selector] = callback; }; +KB.onKey = function (key, callback) { + this.listeners.keys[key] = callback; +}; + KB.listen = function () { var self = this; @@ -56,8 +61,28 @@ KB.listen = function () { } } + function onKeypress(e) { + var key = (typeof e.which === 'number') ? e.which : e.keyCode; + var element = e.target; + + if (element.tagName === 'INPUT' || + element.tagName === 'SELECT' || + element.tagName === 'TEXTAREA' || + element.isContentEditable) { + return; + } + + for (var keyMap in self.listeners.keys) { + if (self.listeners.keys.hasOwnProperty(keyMap) && key === parseInt(keyMap)) { + e.preventDefault(); + self.listeners.keys[key](e); + } + } + } + document.addEventListener('click', onClick, false); document.addEventListener('change', onChange, false); + document.addEventListener('keypress', onKeypress, false); }; KB.component = function (name, object) { diff --git a/assets/js/core/dom.js b/assets/js/core/dom.js index 20510d44..2c44de58 100644 --- a/assets/js/core/dom.js +++ b/assets/js/core/dom.js @@ -43,9 +43,11 @@ KB.dom = function (tag) { return this; }; - this.on = function (eventName, callback) { + this.on = function (eventName, callback, ignorePrevent) { element.addEventListener(eventName, function (e) { - e.preventDefault(); + if (! ignorePrevent) { + e.preventDefault(); + } callback(e.target); }); -- cgit v1.2.3