DjxWidgetBase.ts
135 lines
| 4.9 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r83 | import { djbase, djclass } from "../declare"; | ||
|
|
r65 | import _WidgetBase = require("dijit/_WidgetBase"); | ||
| import _AttachMixin = require("dijit/_AttachMixin"); | ||||
|
|
r112 | import { isNode, isElementNode } from "./traits"; | ||
|
|
r65 | import registry = require("dijit/registry"); | ||
|
|
r85 | import on = require("dojo/on"); | ||
|
|
r102 | import { Scope } from "./Scope"; | ||
| import { render } from "./render"; | ||||
|
|
r112 | import { isNull } from "@implab/core-amd/safe"; | ||
|
|
r65 | |||
| // type Handle = dojo.Handle; | ||||
| export interface EventArgs { | ||||
| bubbles?: boolean; | ||||
| cancelable?: boolean; | ||||
| composed?: boolean; | ||||
| } | ||||
|
|
r109 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| export interface DjxWidgetBase<Attrs = object, Events extends { [name in keyof Events]: Event } = object> extends | ||||
|
|
r71 | _WidgetBase<Events> { | ||
|
|
r72 | |||
|
|
r73 | /** This property is declared only for type inference to work, it is never assigned | ||
| * and should not be used. | ||||
| */ | ||||
|
|
r72 | readonly _eventMap: Events & GlobalEventHandlersEventMap; | ||
|
|
r81 | |||
| /** The list of pairs of event and method names. When the widget is created all methods from | ||||
| * this list will be connected to corresponding events. | ||||
| * | ||||
| * This property is maintained in the prototype | ||||
| */ | ||||
| _eventHandlers: Array<{ | ||||
| eventName: string, | ||||
|
|
r109 | handlerMethod: string; | ||
|
|
r81 | }>; | ||
|
|
r71 | } | ||
|
|
r65 | |||
|
|
r71 | type _super = { | ||
| startup(): void; | ||||
|
|
r102 | |||
| destroy(preserveDom?: boolean): void; | ||||
|
|
r79 | }; | ||
|
|
r65 | |||
| @djclass | ||||
|
|
r109 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| export abstract class DjxWidgetBase<Attrs = object, Events = object> extends djbase<_super, _AttachMixin>(_WidgetBase, _AttachMixin) { | ||||
|
|
r102 | private readonly _scope = new Scope(); | ||
|
|
r65 | |||
| buildRendering() { | ||||
|
|
r102 | const node = render(this.render(), this._scope); | ||
| if (!isElementNode(node)) | ||||
| throw new Error("The render method must return a single DOM element"); | ||||
| this.domNode = node as HTMLElement; | ||||
|
|
r65 | super.buildRendering(); | ||
| // now we should get assigned data-dojo-attach-points | ||||
| // place the contents of the original srcNode to the containerNode | ||||
| const src = this.srcNodeRef; | ||||
| const dest = this.containerNode; | ||||
|
|
r73 | // the donNode is constructed now we need to connect event handlers | ||
| this._connectEventHandlers(); | ||||
|
|
r65 | if (src && dest) { | ||
| while (src.firstChild) | ||||
| dest.appendChild(src.firstChild); | ||||
| } | ||||
| } | ||||
|
|
r112 | abstract render(): JSX.Element; | ||
|
|
r65 | |||
|
|
r72 | private _connectEventHandlers() { | ||
|
|
r83 | if (this._eventHandlers) | ||
| this._eventHandlers.forEach(({ eventName, handlerMethod }) => { | ||||
| const handler = this[handlerMethod as keyof this]; | ||||
| if (typeof handler === "function") | ||||
|
|
r109 | on(this.domNode, eventName, handler.bind(this) as (...args: unknown[]) => unknown); | ||
|
|
r83 | }); | ||
|
|
r72 | } | ||
|
|
r65 | _processTemplateNode<T extends (Element | Node | _WidgetBase)>( | ||
| baseNode: T, | ||||
|
|
r112 | getAttrFunc: (baseNode: T, attr: string) => string | undefined, | ||
|
|
r65 | // tslint:disable-next-line: ban-types | ||
|
|
r109 | attachFunc: (node: T, type: string, func?: (...args: unknown[]) => unknown) => dojo.Handle | ||
|
|
r65 | ): boolean { | ||
| if (isNode(baseNode)) { | ||||
| const w = registry.byNode(baseNode); | ||||
| if (w) { | ||||
| // from dijit/_WidgetsInTemplateMixin | ||||
| this._processTemplateNode(w, | ||||
|
|
r112 | (n, p) => { | ||
| const v = n.get(p as keyof typeof n); | ||||
| return isNull(v) ? undefined : String(v); | ||||
| }, // callback to get a property of a widget | ||||
|
|
r65 | (widget, type, callback) => { | ||
| if (!callback) | ||||
| throw new Error("The callback must be specified"); | ||||
| // callback to do data-dojo-attach-event to a widget | ||||
| if (type in widget) { | ||||
| // back-compat, remove for 2.0 | ||||
| return widget.connect(widget, type, callback as EventListener); | ||||
| } else { | ||||
| // 1.x may never hit this branch, but it's the default for 2.0 | ||||
|
|
r109 | return widget.on(type as keyof GlobalEventHandlersEventMap, callback); | ||
|
|
r65 | } | ||
| }); | ||||
| // don't process widgets internals | ||||
| return false; | ||||
| } | ||||
| } | ||||
|
|
r109 | // eslint-disable-next-line @typescript-eslint/ban-types | ||
|
|
r112 | return super._processTemplateNode(baseNode, getAttrFunc as (baseNode: T, attr: string) => string, attachFunc as (node: T, type: string, func?: Function) => dojo.Handle); | ||
|
|
r65 | } | ||
| /** Starts current widget and all its supporting widgets (placed outside | ||||
| * `containerNode`) and child widgets (placed inside `containerNode`) | ||||
| */ | ||||
| startup() { | ||||
| // startup supporting widgets | ||||
| registry.findWidgets(this.domNode, this.containerNode).forEach(w => w.startup()); | ||||
| super.startup(); | ||||
| } | ||||
|
|
r102 | |||
| destroy(preserveDom?: boolean) { | ||||
| this._scope.destroy(); | ||||
| super.destroy(preserveDom); | ||||
| } | ||||
|
|
r65 | } | ||
