WidgetContext.ts
95 lines
| 2.9 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r7 | import dom = require("dojo/dom-construct"); | ||
import { argumentNotNull } from "@implab/core-amd/safe"; | ||||
import { BuildContextBase } from "./BuildContextBase"; | ||||
cin
|
r38 | import { DojoNodePosition, isWidget } from "./traits"; | ||
cin
|
r7 | |||
cin
|
r19 | // tslint:disable-next-line: class-name | ||
export interface _Widget { | ||||
domNode: Node; | ||||
cin
|
r38 | containerNode?: Node; | ||
cin
|
r19 | |||
cin
|
r38 | placeAt?(refNode: string | Node, position?: DojoNodePosition): void; | ||
startup?(): void; | ||||
cin
|
r19 | |||
cin
|
r38 | addChild?(widget: any, index?: number): void; | ||
cin
|
r19 | } | ||
export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget; | ||||
cin
|
r7 | |||
cin
|
r22 | export class WidgetContext extends BuildContextBase<Node> { | ||
cin
|
r38 | readonly widgetClass: _WidgetCtor; | ||
cin
|
r7 | |||
cin
|
r19 | _instance: _Widget | undefined; | ||
cin
|
r7 | |||
cin
|
r19 | constructor(widgetClass: _WidgetCtor) { | ||
cin
|
r7 | super(); | ||
argumentNotNull(widgetClass, "widgetClass"); | ||||
this.widgetClass = widgetClass; | ||||
} | ||||
_addChild(child: any): void { | ||||
cin
|
r38 | const instance = this._getInstance(); | ||
cin
|
r7 | |||
cin
|
r38 | if (instance.addChild) { | ||
if (child instanceof WidgetContext) | ||||
// layout containers add custom logic to addChild methods | ||||
instance.addChild(child.getWidgetInstance()); | ||||
else if (isWidget(child)) | ||||
instance.addChild(child); | ||||
else | ||||
instance.addChild(this.getChildDom(child)); | ||||
} else { | ||||
if (!instance.containerNode) | ||||
throw new Error("The widget doesn't have neither addChild nor containerNode"); | ||||
// the current widget isn't started, it's children shouldn't start too | ||||
dom.place(this.getChildDom(child), instance.containerNode); | ||||
} | ||||
cin
|
r7 | } | ||
_create(attrs: object, children: any[]) { | ||||
cin
|
r34 | this._instance = new this.widgetClass(attrs); | ||
cin
|
r7 | if (children) | ||
children.forEach(x => this._addChild(x)); | ||||
} | ||||
cin
|
r38 | private _getInstance() { | ||
if (!this._instance) | ||||
throw new Error("The instance of the widget isn't created"); | ||||
return this._instance; | ||||
} | ||||
cin
|
r19 | _getDomNode() { | ||
cin
|
r7 | if (!this._instance) | ||
throw new Error("The instance of the widget isn't created"); | ||||
return this._instance.domNode; | ||||
} | ||||
cin
|
r38 | /** Overrides default placeAt implementation. Calls placeAt of the | ||
* widget and then starts it. | ||||
* | ||||
* @param refNode A node or id of the node where the widget should be placed. | ||||
* @param position A position relative to refNode. | ||||
*/ | ||||
placeAt(refNode: string | Node, position?: DojoNodePosition) { | ||||
this.ensureCreated(); | ||||
const instance = this._getInstance(); | ||||
if (typeof instance.placeAt === "function") { | ||||
instance.placeAt(refNode, position); | ||||
// do we need to force widget startup? | ||||
if (typeof instance.startup === "function") | ||||
instance.startup(); | ||||
} else { | ||||
super.placeAt(refNode, position); | ||||
} | ||||
} | ||||
getWidgetInstance() { | ||||
this.ensureCreated(); | ||||
return this._getInstance(); | ||||
} | ||||
cin
|
r7 | } | ||