Add keydown hook for widgets (#4308)

This commit is contained in:
James Kiesel 2016-07-05 11:25:08 -04:00 committed by Robin Ward
parent 3cb8bbb851
commit 5c1e18ecc8
2 changed files with 11 additions and 1 deletions

View File

@ -3,6 +3,7 @@
const CLICK_ATTRIBUTE_NAME = '_discourse_click_widget'; const CLICK_ATTRIBUTE_NAME = '_discourse_click_widget';
const CLICK_OUTSIDE_ATTRIBUTE_NAME = '_discourse_click_outside_widget'; const CLICK_OUTSIDE_ATTRIBUTE_NAME = '_discourse_click_outside_widget';
const KEY_UP_ATTRIBUTE_NAME = '_discourse_key_up_widget'; const KEY_UP_ATTRIBUTE_NAME = '_discourse_key_up_widget';
const KEY_DOWN_ATTRIBUTE_NAME = '_discourse_key_down_widget';
const DRAG_ATTRIBUTE_NAME = '_discourse_drag_widget'; const DRAG_ATTRIBUTE_NAME = '_discourse_drag_widget';
function buildHook(attributeName, setAttr) { function buildHook(attributeName, setAttr) {
@ -30,6 +31,7 @@ function buildHook(attributeName, setAttr) {
export const WidgetClickHook = buildHook(CLICK_ATTRIBUTE_NAME); export const WidgetClickHook = buildHook(CLICK_ATTRIBUTE_NAME);
export const WidgetClickOutsideHook = buildHook(CLICK_OUTSIDE_ATTRIBUTE_NAME, 'data-click-outside'); export const WidgetClickOutsideHook = buildHook(CLICK_OUTSIDE_ATTRIBUTE_NAME, 'data-click-outside');
export const WidgetKeyUpHook = buildHook(KEY_UP_ATTRIBUTE_NAME); export const WidgetKeyUpHook = buildHook(KEY_UP_ATTRIBUTE_NAME);
export const WidgetKeyDownHook = buildHook(KEY_DOWN_ATTRIBUTE_NAME);
export const WidgetDragHook = buildHook(DRAG_ATTRIBUTE_NAME); export const WidgetDragHook = buildHook(DRAG_ATTRIBUTE_NAME);
@ -105,5 +107,9 @@ WidgetClickHook.setupDocumentCallback = function() {
nodeCallback(e.target, KEY_UP_ATTRIBUTE_NAME, w => w.keyUp(e)); nodeCallback(e.target, KEY_UP_ATTRIBUTE_NAME, w => w.keyUp(e));
}); });
$(document).on('keydown.discourse-widget', e => {
nodeCallback(e.target, KEY_DOWN_ATTRIBUTE_NAME, w => w.keyDown(e));
});
_watchingDocument = true; _watchingDocument = true;
}; };

View File

@ -1,4 +1,4 @@
import { WidgetClickHook, WidgetClickOutsideHook, WidgetKeyUpHook, WidgetDragHook } from 'discourse/widgets/hooks'; import { WidgetClickHook, WidgetClickOutsideHook, WidgetKeyUpHook, WidgetKeyDownHook, WidgetDragHook } from 'discourse/widgets/hooks';
import { h } from 'virtual-dom'; import { h } from 'virtual-dom';
import DecoratorHelper from 'discourse/widgets/decorator-helper'; import DecoratorHelper from 'discourse/widgets/decorator-helper';
@ -71,6 +71,10 @@ function drawWidget(builder, attrs, state) {
properties['widget-key-up'] = new WidgetKeyUpHook(this); properties['widget-key-up'] = new WidgetKeyUpHook(this);
} }
if (this.keyDown) {
properties['widget-key-down'] = new WidgetKeyDownHook(this);
}
if (this.clickOutside) { if (this.clickOutside) {
properties['widget-click-outside'] = new WidgetClickOutsideHook(this); properties['widget-click-outside'] = new WidgetClickOutsideHook(this);
} }