##// END OF EJS Templates
Added 'Attrs', 'Events' type parameters to DjxWidgetBase, typed 'on' and 'emit' methods
Added 'Attrs', 'Events' type parameters to DjxWidgetBase, typed 'on' and 'emit' methods

File last commit:

r22:a1ab2b5975ad v1.0.0-rc10 default
r30:a46488b209e8 v1.0.0-rc14 default
Show More
WidgetContext.ts
55 lines | 1.5 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / tsx / WidgetContext.ts
import dom = require("dojo/dom-construct");
import { argumentNotNull } from "@implab/core-amd/safe";
import { BuildContextBase } from "./BuildContextBase";
import { MapOf } from "@implab/core-amd/interfaces";
// tslint:disable-next-line: class-name
export interface _Widget {
domNode: Node;
get(attr: string): any;
set(attr: string, value: any): void;
set(attrs: MapOf<any>): void;
containerNode?: Node
}
export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget;
export class WidgetContext extends BuildContextBase<Node> {
widgetClass: _WidgetCtor;
_instance: _Widget | undefined;
constructor(widgetClass: _WidgetCtor) {
super();
argumentNotNull(widgetClass, "widgetClass");
this.widgetClass = widgetClass;
}
_addChild(child: any): void {
if (!this._instance || !this._instance.containerNode)
throw new Error("Widget doesn't support adding children");
dom.place(this.getChildDom(child), this._instance.containerNode);
}
_setAttrs(attrs: object): void {
this._instance?.set(attrs);
}
_create(attrs: object, children: any[]) {
this._instance = new this.widgetClass(this._attrs);
if (children)
children.forEach(x => this._addChild(x));
}
_getDomNode() {
if (!this._instance)
throw new Error("The instance of the widget isn't created");
return this._instance.domNode;
}
}