##// END OF EJS Templates
bind event handler methods marked with `@on` before subscribing the event
cin -
r77:bc7556143fe5 v1.2.2 default
parent child
Show More
@@ -1,110 +1,110
1 1 import { AbstractConstructor, djbase, djclass } from "../declare";
2 2 import _WidgetBase = require("dijit/_WidgetBase");
3 3 import _AttachMixin = require("dijit/_AttachMixin");
4 4 import { Rendition, isNode, startupWidgets } from "./traits";
5 5 import registry = require("dijit/registry");
6 6
7 7 // type Handle = dojo.Handle;
8 8
9 9 export interface EventArgs {
10 10 bubbles?: boolean;
11 11
12 12 cancelable?: boolean;
13 13
14 14 composed?: boolean;
15 15 }
16 16
17 17 export interface DjxWidgetBase<Attrs = {}, Events extends { [name in keyof Events]: Event } = {}> extends
18 18 _WidgetBase<Events> {
19 19
20 20 /** This property is declared only for type inference to work, it is never assigned
21 21 * and should not be used.
22 22 */
23 23 readonly _eventMap: Events & GlobalEventHandlersEventMap;
24 24 }
25 25
26 26 type _super = {
27 27 startup(): void;
28 28 }
29 29
30 30 @djclass
31 31 export abstract class DjxWidgetBase<Attrs = {}, Events = {}> extends djbase<_super, _AttachMixin>(_WidgetBase, _AttachMixin) {
32 32
33 33 /** The list of pairs of event and method names. When the widget is created all methods from
34 34 * this list will be connected to corresponding events.
35 35 */
36 36 _eventHandlers: Array<{
37 37 eventName: string,
38 38 handlerMethod: keyof any;
39 39 }> = [];
40 40
41 41 buildRendering() {
42 42 this.domNode = this.render().getDomNode();
43 43 super.buildRendering();
44 44
45 45 // now we should get assigned data-dojo-attach-points
46 46 // place the contents of the original srcNode to the containerNode
47 47 const src = this.srcNodeRef;
48 48 const dest = this.containerNode;
49 49
50 50 // the donNode is constructed now we need to connect event handlers
51 51 this._connectEventHandlers();
52 52
53 53 if (src && dest) {
54 54 while (src.firstChild)
55 55 dest.appendChild(src.firstChild);
56 56 }
57 57 }
58 58
59 59 abstract render(): Rendition<HTMLElement>;
60 60
61 61 private _connectEventHandlers() {
62 62 this._eventHandlers.forEach(({eventName, handlerMethod}) => {
63 63 const handler = this[handlerMethod as keyof this];
64 64 if (typeof handler === "function")
65 this.on(eventName, handler);
65 this.on(eventName, handler.bind(this));
66 66 });
67 67 }
68 68
69 69 _processTemplateNode<T extends (Element | Node | _WidgetBase)>(
70 70 baseNode: T,
71 71 getAttrFunc: (baseNode: T, attr: string) => any,
72 72 // tslint:disable-next-line: ban-types
73 73 attachFunc: (node: T, type: string, func?: Function) => dojo.Handle
74 74 ): boolean {
75 75 if (isNode(baseNode)) {
76 76 const w = registry.byNode(baseNode);
77 77 if (w) {
78 78 // from dijit/_WidgetsInTemplateMixin
79 79 this._processTemplateNode(w,
80 80 (n, p) => n.get(p as any), // callback to get a property of a widget
81 81 (widget, type, callback) => {
82 82 if (!callback)
83 83 throw new Error("The callback must be specified");
84 84
85 85 // callback to do data-dojo-attach-event to a widget
86 86 if (type in widget) {
87 87 // back-compat, remove for 2.0
88 88 return widget.connect(widget, type, callback as EventListener);
89 89 } else {
90 90 // 1.x may never hit this branch, but it's the default for 2.0
91 91 return widget.on(type, callback);
92 92 }
93 93
94 94 });
95 95 // don't process widgets internals
96 96 return false;
97 97 }
98 98 }
99 99 return super._processTemplateNode(baseNode, getAttrFunc, attachFunc);
100 100 }
101 101
102 102 /** Starts current widget and all its supporting widgets (placed outside
103 103 * `containerNode`) and child widgets (placed inside `containerNode`)
104 104 */
105 105 startup() {
106 106 // startup supporting widgets
107 107 registry.findWidgets(this.domNode, this.containerNode).forEach(w => w.startup());
108 108 super.startup();
109 109 }
110 110 }
General Comments 0
You need to be logged in to leave comments. Login now