Make key callback registration more concise

Refs #264.
This commit is contained in:
Franz Liedke 2016-05-15 22:58:41 +09:00
parent 5bbe6f1865
commit a2b961e525
2 changed files with 20 additions and 74 deletions

View File

@ -31636,13 +31636,7 @@ System.register('flarum/utils/KeyboardNavigatable', [], function (_export, _cont
function KeyboardNavigatable() { function KeyboardNavigatable() {
babelHelpers.classCallCheck(this, KeyboardNavigatable); babelHelpers.classCallCheck(this, KeyboardNavigatable);
var defaultCallback = function defaultCallback() {/* noop */}; this.callbacks = {};
// 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;
// By default, always handle keyboard navigation. // By default, always handle keyboard navigation.
this.whenCallback = function () { this.whenCallback = function () {
@ -31664,28 +31658,28 @@ System.register('flarum/utils/KeyboardNavigatable', [], function (_export, _cont
babelHelpers.createClass(KeyboardNavigatable, [{ babelHelpers.createClass(KeyboardNavigatable, [{
key: 'onUp', key: 'onUp',
value: function onUp(callback) { value: function onUp(callback) {
this.upCallback = callback; this.callbacks[38] = callback;
return this; return this;
} }
}, { }, {
key: 'onDown', key: 'onDown',
value: function onDown(callback) { value: function onDown(callback) {
this.downCallback = callback; this.callbacks[40] = callback;
return this; return this;
} }
}, { }, {
key: 'onSelect', key: 'onSelect',
value: function onSelect(callback) { value: function onSelect(callback) {
this.selectCallback = callback; this.callbacks[9] = this.callbacks[13] = callback;
return this; return this;
} }
}, { }, {
key: 'onCancel', key: 'onCancel',
value: function onCancel(callback) { value: function onCancel(callback) {
this.cancelCallback = callback; this.callbacks[27] = callback;
return this; return this;
} }
@ -31708,34 +31702,11 @@ System.register('flarum/utils/KeyboardNavigatable', [], function (_export, _cont
// This callback determines whether keyboard should be handled or ignored. // This callback determines whether keyboard should be handled or ignored.
if (!this.whenCallback()) return; if (!this.whenCallback()) return;
switch (event.which) { var keyCallback = this.callbacks[event.which];
case 9:case 13: if (keyCallback) {
// Tab / Return keyCallback();
this.selectCallback();
event.preventDefault();
break;
case 27:
// Escape
this.cancelCallback();
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
break;
case 38:
// Up
this.upCallback();
event.preventDefault();
break;
case 40:
// Down
this.downCallback();
event.preventDefault();
break;
default:
// no default
} }
} }
}]); }]);

View File

@ -7,13 +7,7 @@
*/ */
export default class KeyboardNavigatable { export default class KeyboardNavigatable {
constructor() { constructor() {
const defaultCallback = () => { /* noop */ }; this.callbacks = {};
// 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;
// By default, always handle keyboard navigation. // By default, always handle keyboard navigation.
this.whenCallback = () => true; this.whenCallback = () => true;
@ -29,7 +23,7 @@ export default class KeyboardNavigatable {
* @return {KeyboardNavigatable} * @return {KeyboardNavigatable}
*/ */
onUp(callback) { onUp(callback) {
this.upCallback = callback; this.callbacks[38] = callback;
return this; return this;
} }
@ -44,7 +38,7 @@ export default class KeyboardNavigatable {
* @return {KeyboardNavigatable} * @return {KeyboardNavigatable}
*/ */
onDown(callback) { onDown(callback) {
this.downCallback = callback; this.callbacks[40] = callback;
return this; return this;
} }
@ -59,7 +53,7 @@ export default class KeyboardNavigatable {
* @return {KeyboardNavigatable} * @return {KeyboardNavigatable}
*/ */
onSelect(callback) { onSelect(callback) {
this.selectCallback = callback; this.callbacks[9] = this.callbacks[13] = callback;
return this; return this;
} }
@ -74,7 +68,7 @@ export default class KeyboardNavigatable {
* @return {KeyboardNavigatable} * @return {KeyboardNavigatable}
*/ */
onCancel(callback) { onCancel(callback) {
this.cancelCallback = callback; this.callbacks[27] = callback;
return this; return this;
} }
@ -113,30 +107,11 @@ export default class KeyboardNavigatable {
// This callback determines whether keyboard should be handled or ignored. // This callback determines whether keyboard should be handled or ignored.
if (!this.whenCallback()) return; if (!this.whenCallback()) return;
switch (event.which) { const keyCallback = this.callbacks[event.which];
case 9: case 13: // Tab / Return if (keyCallback) {
this.selectCallback(); keyCallback();
event.preventDefault();
break;
case 27: // Escape
this.cancelCallback();
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
break;
case 38: // Up
this.upCallback();
event.preventDefault();
break;
case 40: // Down
this.downCallback();
event.preventDefault();
break;
default:
// no default
} }
} }
} }