DEV: adds support for input/change events on widgets (#8309)

This commit is contained in:
Joffrey JAFFEUX 2019-11-07 10:15:05 +01:00 committed by GitHub
parent 5d42f052bc
commit 7d25d65ccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -7,6 +7,8 @@ const MOUSE_DOWN_OUTSIDE_ATTRIBUTE_NAME =
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 INPUT_ATTRIBUTE_NAME = "_discourse_input_widget";
const CHANGE_ATTRIBUTE_NAME = "_discourse_change_widget";
function buildHook(attributeName, setAttr) {
return class {
@ -42,6 +44,8 @@ export const WidgetMouseDownOutsideHook = buildHook(
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 WidgetInputHook = buildHook(INPUT_ATTRIBUTE_NAME);
export const WidgetChangeHook = buildHook(CHANGE_ATTRIBUTE_NAME);
function nodeCallback(node, attrName, cb) {
const widget = findWidget(node, attrName);
@ -168,5 +172,13 @@ WidgetClickHook.setupDocumentCallback = function() {
nodeCallback(e.target, KEY_DOWN_ATTRIBUTE_NAME, w => w.keyDown(e));
});
$(document).on("input.discourse-widget", e => {
nodeCallback(e.target, INPUT_ATTRIBUTE_NAME, w => w.input(e));
});
$(document).on("change.discourse-widget", e => {
nodeCallback(e.target, CHANGE_ATTRIBUTE_NAME, w => w.change(e));
});
_watchingDocument = true;
};

View File

@ -4,7 +4,9 @@ import {
WidgetKeyUpHook,
WidgetKeyDownHook,
WidgetMouseDownOutsideHook,
WidgetDragHook
WidgetDragHook,
WidgetInputHook,
WidgetChangeHook
} from "discourse/widgets/hooks";
import { h } from "virtual-dom";
import DecoratorHelper from "discourse/widgets/decorator-helper";
@ -370,6 +372,14 @@ export default class Widget {
properties["widget-drag"] = new WidgetDragHook(this);
}
if (this.input) {
properties["widget-input"] = new WidgetInputHook(this);
}
if (this.change) {
properties["widget-change"] = new WidgetChangeHook(this);
}
const attributes = properties["attributes"] || {};
properties.attributes = attributes;