From a2b961e5250e37d643c8138a5b5d58ecdbc7bc35 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 15 May 2016 22:58:41 +0900 Subject: [PATCH] Make key callback registration more concise Refs #264. --- framework/core/js/forum/dist/app.js | 49 ++++--------------- .../js/forum/src/utils/KeyboardNavigatable.js | 45 ++++------------- 2 files changed, 20 insertions(+), 74 deletions(-) diff --git a/framework/core/js/forum/dist/app.js b/framework/core/js/forum/dist/app.js index 756cea76a..7c570405b 100644 --- a/framework/core/js/forum/dist/app.js +++ b/framework/core/js/forum/dist/app.js @@ -31636,13 +31636,7 @@ System.register('flarum/utils/KeyboardNavigatable', [], function (_export, _cont function KeyboardNavigatable() { babelHelpers.classCallCheck(this, KeyboardNavigatable); - var defaultCallback = function defaultCallback() {/* noop */}; - - // Set all callbacks to a noop function so that not all of them have to be set. - this.upCallback = defaultCallback; - this.downCallback = defaultCallback; - this.selectCallback = defaultCallback; - this.cancelCallback = defaultCallback; + this.callbacks = {}; // By default, always handle keyboard navigation. this.whenCallback = function () { @@ -31664,28 +31658,28 @@ System.register('flarum/utils/KeyboardNavigatable', [], function (_export, _cont babelHelpers.createClass(KeyboardNavigatable, [{ key: 'onUp', value: function onUp(callback) { - this.upCallback = callback; + this.callbacks[38] = callback; return this; } }, { key: 'onDown', value: function onDown(callback) { - this.downCallback = callback; + this.callbacks[40] = callback; return this; } }, { key: 'onSelect', value: function onSelect(callback) { - this.selectCallback = callback; + this.callbacks[9] = this.callbacks[13] = callback; return this; } }, { key: 'onCancel', value: function onCancel(callback) { - this.cancelCallback = callback; + this.callbacks[27] = callback; return this; } @@ -31708,34 +31702,11 @@ System.register('flarum/utils/KeyboardNavigatable', [], function (_export, _cont // This callback determines whether keyboard should be handled or ignored. if (!this.whenCallback()) return; - switch (event.which) { - case 9:case 13: - // Tab / Return - this.selectCallback(); - event.preventDefault(); - break; - - case 27: - // Escape - this.cancelCallback(); - event.stopPropagation(); - event.preventDefault(); - break; - - case 38: - // Up - this.upCallback(); - event.preventDefault(); - break; - - case 40: - // Down - this.downCallback(); - event.preventDefault(); - break; - - default: - // no default + var keyCallback = this.callbacks[event.which]; + if (keyCallback) { + keyCallback(); + event.stopPropagation(); + event.preventDefault(); } } }]); diff --git a/framework/core/js/forum/src/utils/KeyboardNavigatable.js b/framework/core/js/forum/src/utils/KeyboardNavigatable.js index cdf2f3101..f2e39c09e 100644 --- a/framework/core/js/forum/src/utils/KeyboardNavigatable.js +++ b/framework/core/js/forum/src/utils/KeyboardNavigatable.js @@ -7,13 +7,7 @@ */ export default class KeyboardNavigatable { constructor() { - const defaultCallback = () => { /* noop */ }; - - // Set all callbacks to a noop function so that not all of them have to be set. - this.upCallback = defaultCallback; - this.downCallback = defaultCallback; - this.selectCallback = defaultCallback; - this.cancelCallback = defaultCallback; + this.callbacks = {}; // By default, always handle keyboard navigation. this.whenCallback = () => true; @@ -29,7 +23,7 @@ export default class KeyboardNavigatable { * @return {KeyboardNavigatable} */ onUp(callback) { - this.upCallback = callback; + this.callbacks[38] = callback; return this; } @@ -44,7 +38,7 @@ export default class KeyboardNavigatable { * @return {KeyboardNavigatable} */ onDown(callback) { - this.downCallback = callback; + this.callbacks[40] = callback; return this; } @@ -59,7 +53,7 @@ export default class KeyboardNavigatable { * @return {KeyboardNavigatable} */ onSelect(callback) { - this.selectCallback = callback; + this.callbacks[9] = this.callbacks[13] = callback; return this; } @@ -74,7 +68,7 @@ export default class KeyboardNavigatable { * @return {KeyboardNavigatable} */ onCancel(callback) { - this.cancelCallback = callback; + this.callbacks[27] = callback; return this; } @@ -113,30 +107,11 @@ export default class KeyboardNavigatable { // This callback determines whether keyboard should be handled or ignored. if (!this.whenCallback()) return; - switch (event.which) { - case 9: case 13: // Tab / Return - this.selectCallback(); - event.preventDefault(); - break; - - case 27: // Escape - this.cancelCallback(); - event.stopPropagation(); - event.preventDefault(); - break; - - case 38: // Up - this.upCallback(); - event.preventDefault(); - break; - - case 40: // Down - this.downCallback(); - event.preventDefault(); - break; - - default: - // no default + const keyCallback = this.callbacks[event.which]; + if (keyCallback) { + keyCallback(); + event.stopPropagation(); + event.preventDefault(); } } }