From 236dad51fad9db96f46b45a7764583f48a97ad37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Wed, 27 Feb 2019 09:21:04 +0100 Subject: [PATCH] Allow search unregister MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- core/search/js/search.js | 106 ++++++++++++++++++++++++++++----------- 1 file changed, 78 insertions(+), 28 deletions(-) diff --git a/core/search/js/search.js b/core/search/js/search.js index 5b44027af3..0822eee036 100644 --- a/core/search/js/search.js +++ b/core/search/js/search.js @@ -1,4 +1,4 @@ -/* +/** * @copyright Copyright (c) 2018 John Molakvoæ * * @author John Molakvoæ @@ -69,7 +69,7 @@ /** * Search */ - this.search = function(event) { + this._search = function(event) { event.preventDefault(); var query = document.getElementById('searchbox').value; self.searchCallback(query); @@ -78,34 +78,25 @@ /** * Reset form */ - this.reset = function(event) { + this._reset = function(event) { event.preventDefault(); document.getElementById('searchbox').value = ''; self.resetCallback(); }; - // Show search - document.getElementById('searchbox').style.display = 'block'; + /** + * Submit event handler + */ + this._submitHandler = function(event) { + // Avoid form submit + event.preventDefault(); + _.debounce(self.search, 500); + } - // Register input event - document - .getElementById('searchbox') - .addEventListener('input', _.debounce(self.search, 500), true); - document - .querySelector('form.searchbox') - .addEventListener('submit', function(event) { - // Avoid form submit - event.preventDefault(); - _.debounce(self.search, 500); - }, true); - - // Register reset - document - .querySelector('form.searchbox') - .addEventListener('reset', _.debounce(self.reset, 500), true); - - // Register esc key shortcut reset if focused - document.addEventListener('keyup', function(event) { + /** + * keyUp event handler for the escape key + */ + this._keyUpHandler = function(event) { if (event.defaultPrevented) { return; } @@ -116,13 +107,20 @@ document.getElementById('searchbox').value === '' ) { if (key === 'Escape' || key === 'Esc' || key === 27) { + document.activeElement.blur(); // remove focus on searchbox _.debounce(self.reset, 500); } } - }); + } - // Register ctrl+F key shortcut to focus - document.addEventListener('keydown', function(event) { + this._searchDebounced = _.debounce(self._search, 500) + + this._resetDebounced = _.debounce(self._reset, 500) + + /** + * keyDown event handler for the ctrl+F shortcut + */ + this._keyDownHandler = function(event) { if (event.defaultPrevented) { return; } @@ -139,7 +137,59 @@ document.getElementById('searchbox').select(); } } - }); + } + + // Show search + document.getElementById('searchbox').style.display = 'block'; + + // Register input event + document + .getElementById('searchbox') + .addEventListener('input', this._searchDebounced, true); + document + .querySelector('form.searchbox') + .addEventListener('submit', this._submitHandler, true); + + // Register reset + document + .querySelector('form.searchbox') + .addEventListener('reset', this._resetDebounced, true); + + // Register esc key shortcut reset if focused + document.addEventListener('keyup', this._keyUpHandler); + + // Register ctrl+F key shortcut to focus + document.addEventListener('keydown', this._keyDownHandler); + }, + + unregister: function() { + // Hide search + document.getElementById('searchbox').style.display = 'none'; + + // Reset search + document.getElementById('searchbox').value = ''; + this.resetCallback(); + + // Unregister input event + document + .getElementById('searchbox') + .removeEventListener('input', this._searchDebounced, true); + document + .querySelector('form.searchbox') + .removeEventListener('submit', this._submitHandler, true); + + // Unregister reset + document + .querySelector('form.searchbox') + .removeEventListener('reset', this._resetDebounced, true); + + // Unregister esc key shortcut reset if focused + document.removeEventListener('keyup', this._keyUpHandler); + + // Unregister ctrl+F key shortcut to focus + document.removeEventListener('keydown', this._keyDownHandler); + + console.debug('Search handler unregistered'); } };