@@ -0,0 +1,29 | |||
|
1 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
|
2 | import { RenditionBase } from "./RenditionBase"; | |
|
3 | ||
|
4 | export class FunctionRendition extends RenditionBase<Node> { | |
|
5 | private _component: (props: any) => any; | |
|
6 | ||
|
7 | private _node: Node | undefined; | |
|
8 | ||
|
9 | constructor(component: (props: any) => any) { | |
|
10 | super(); | |
|
11 | argumentNotNull(component, "component"); | |
|
12 | ||
|
13 | this._component = component; | |
|
14 | } | |
|
15 | ||
|
16 | _create(attrs: object, children: any[]) { | |
|
17 | const _attrs: any = attrs || {}; | |
|
18 | _attrs.children = children.map(x => this.getItemDom(x)); | |
|
19 | ||
|
20 | this._node = this.getItemDom(this._component.call(null, _attrs)); | |
|
21 | } | |
|
22 | ||
|
23 | _getDomNode() { | |
|
24 | if (!this._node) | |
|
25 | throw new Error("The instance of the widget isn't created"); | |
|
26 | return this._node; | |
|
27 | } | |
|
28 | ||
|
29 | } |
@@ -0,0 +1,37 | |||
|
1 | import dom = require("dojo/dom-construct"); | |
|
2 | import { argumentNotEmptyString } from "@implab/core-amd/safe"; | |
|
3 | import { RenditionBase } from "./RenditionBase"; | |
|
4 | ||
|
5 | export class HtmlRendition extends RenditionBase<HTMLElement> { | |
|
6 | elementType: string; | |
|
7 | ||
|
8 | _element: HTMLElement | undefined; | |
|
9 | ||
|
10 | constructor(elementType: string) { | |
|
11 | argumentNotEmptyString(elementType, "elementType"); | |
|
12 | super(); | |
|
13 | ||
|
14 | this.elementType = elementType; | |
|
15 | } | |
|
16 | ||
|
17 | _addChild(child: any): void { | |
|
18 | if (!this._element) | |
|
19 | throw new Error("The HTML element isn't created"); | |
|
20 | dom.place(this.getItemDom(child), this._element); | |
|
21 | } | |
|
22 | ||
|
23 | _create(attrs: object, children: any[]) { | |
|
24 | this._element = dom.create(this.elementType, attrs); | |
|
25 | ||
|
26 | if (children) | |
|
27 | children.forEach(v => this._addChild(v)); | |
|
28 | } | |
|
29 | ||
|
30 | _getDomNode() { | |
|
31 | if (!this._element) | |
|
32 | throw new Error("The HTML element isn't created"); | |
|
33 | ||
|
34 | return this._element; | |
|
35 | } | |
|
36 | ||
|
37 | } |
@@ -0,0 +1,116 | |||
|
1 | import { isNull, mixin } from "@implab/core-amd/safe"; | |
|
2 | import { isPlainObject, isNode, isRendition, DojoNodePosition, Rendition, isInPage, isWidget, isDocumentFragmentNode, startupWidgets } from "./traits"; | |
|
3 | ||
|
4 | import dom = require("dojo/dom-construct"); | |
|
5 | import registry = require("dijit/registry"); | |
|
6 | ||
|
7 | ||
|
8 | export abstract class RenditionBase<TNode extends Node> implements Rendition<TNode> { | |
|
9 | private _attrs = {}; | |
|
10 | ||
|
11 | private _children = new Array(); | |
|
12 | ||
|
13 | private _created: boolean = false; | |
|
14 | ||
|
15 | visitNext(v: any) { | |
|
16 | if (this._created) | |
|
17 | throw new Error("The Element is already created"); | |
|
18 | ||
|
19 | if (isNull(v) || typeof v === "boolean") | |
|
20 | // skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | |
|
21 | return; | |
|
22 | ||
|
23 | if (isPlainObject(v)) { | |
|
24 | mixin(this._attrs, v); | |
|
25 | } else if (v instanceof Array) { | |
|
26 | v.forEach(x => this.visitNext(x)); | |
|
27 | } else { | |
|
28 | this._children.push(v); | |
|
29 | } | |
|
30 | } | |
|
31 | ||
|
32 | protected getItemDom(v: any) { | |
|
33 | const tv = typeof v; | |
|
34 | ||
|
35 | if (tv === "string" || tv === "number" || v instanceof RegExp || v instanceof Date) { | |
|
36 | // primitive types converted to the text nodes | |
|
37 | return document.createTextNode(v.toString()); | |
|
38 | } else if (isNode(v)) { | |
|
39 | // nodes are kept as is | |
|
40 | return v; | |
|
41 | } else if (isRendition(v)) { | |
|
42 | // renditions as instantinated | |
|
43 | return v.getDomNode(); | |
|
44 | } else if (isWidget(v)) { | |
|
45 | // widgets are converted to it's markup | |
|
46 | return v.domNode; | |
|
47 | } else if (tv === "boolean" || v === null || v === undefined) { | |
|
48 | // null | undefined | boolean are removed, converted to comments | |
|
49 | return document.createComment(`[${tv} ${String(v)}]`); | |
|
50 | } else { | |
|
51 | // bug: explicit error otherwise | |
|
52 | throw new Error("Invalid parameter: " + v); | |
|
53 | } | |
|
54 | } | |
|
55 | ||
|
56 | ensureCreated() { | |
|
57 | if (!this._created) { | |
|
58 | this._create(this._attrs, this._children); | |
|
59 | this._children = []; | |
|
60 | this._attrs = {}; | |
|
61 | this._created = true; | |
|
62 | } | |
|
63 | } | |
|
64 | ||
|
65 | /** @deprecated will be removed in 1.0.0, use getDomNode() */ | |
|
66 | getDomElement() { | |
|
67 | return this.getDomNode(); | |
|
68 | } | |
|
69 | ||
|
70 | /** Creates DOM node if not created. No additional actions are taken. */ | |
|
71 | getDomNode() { | |
|
72 | this.ensureCreated(); | |
|
73 | return this._getDomNode(); | |
|
74 | } | |
|
75 | ||
|
76 | /** Creates DOM node if not created, places it to the specified position | |
|
77 | * and calls startup() method for all widgets contained by this node. | |
|
78 | * | |
|
79 | * @param {string | Node} refNode The reference node where the created | |
|
80 | * DOM should be placed. | |
|
81 | * @param {DojoNodePosition} position Optional parameter, specifies the | |
|
82 | * position relative to refNode. Default is "last" (i.e. last child). | |
|
83 | */ | |
|
84 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
|
85 | const domNode = this.getDomNode(); | |
|
86 | ||
|
87 | const collect = (collection: HTMLCollection) => { | |
|
88 | const items = []; | |
|
89 | for (let i = 0, n = items.length; i < length; i++) { | |
|
90 | items.push(collection[i]); | |
|
91 | } | |
|
92 | return items; | |
|
93 | } | |
|
94 | ||
|
95 | const startup = (node: Node) => { | |
|
96 | if (node.parentNode) { | |
|
97 | const parentWidget = registry.getEnclosingWidget(node.parentNode); | |
|
98 | if (parentWidget && parentWidget._started) | |
|
99 | return startupWidgets(node); | |
|
100 | } | |
|
101 | if (isInPage(node)) | |
|
102 | startupWidgets(node); | |
|
103 | } | |
|
104 | ||
|
105 | const startupPending = isDocumentFragmentNode(domNode) ? collect(domNode.children) : [domNode] | |
|
106 | ||
|
107 | dom.place(domNode, refNode, position); | |
|
108 | ||
|
109 | startupPending.forEach(startup); | |
|
110 | ||
|
111 | } | |
|
112 | ||
|
113 | protected abstract _create(attrs: object, children: any[]): void; | |
|
114 | ||
|
115 | protected abstract _getDomNode(): TNode; | |
|
116 | } |
@@ -0,0 +1,121 | |||
|
1 | import dom = require("dojo/dom-construct"); | |
|
2 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
|
3 | import { RenditionBase } from "./RenditionBase"; | |
|
4 | import { DojoNodePosition, isInPage, isWidget } from "./traits"; | |
|
5 | import registry = require("dijit/registry"); | |
|
6 | import ContentPane = require("dijit/layout/ContentPane"); | |
|
7 | ||
|
8 | // tslint:disable-next-line: class-name | |
|
9 | export interface _Widget { | |
|
10 | domNode: Node; | |
|
11 | ||
|
12 | containerNode?: Node; | |
|
13 | ||
|
14 | placeAt?(refNode: string | Node, position?: DojoNodePosition): void; | |
|
15 | startup?(): void; | |
|
16 | ||
|
17 | addChild?(widget: any, index?: number): void; | |
|
18 | } | |
|
19 | ||
|
20 | export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget; | |
|
21 | ||
|
22 | export class WidgetRendition extends RenditionBase<Node> { | |
|
23 | readonly widgetClass: _WidgetCtor; | |
|
24 | ||
|
25 | _instance: _Widget | undefined; | |
|
26 | ||
|
27 | constructor(widgetClass: _WidgetCtor) { | |
|
28 | super(); | |
|
29 | argumentNotNull(widgetClass, "widgetClass"); | |
|
30 | ||
|
31 | this.widgetClass = widgetClass; | |
|
32 | } | |
|
33 | ||
|
34 | _addChild(child: any): void { | |
|
35 | const instance = this._getInstance(); | |
|
36 | ||
|
37 | if (instance.addChild) { | |
|
38 | if (child instanceof WidgetRendition) { | |
|
39 | // layout containers add custom logic to addChild methods | |
|
40 | instance.addChild(child.getWidgetInstance()); | |
|
41 | } else if (isWidget(child)) { | |
|
42 | instance.addChild(child); | |
|
43 | } else { | |
|
44 | if (!instance.containerNode) | |
|
45 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | |
|
46 | ||
|
47 | // the current widget isn't started, it's children shouldn't start too | |
|
48 | dom.place(this.getItemDom(child), instance.containerNode); | |
|
49 | } | |
|
50 | } else { | |
|
51 | if (!instance.containerNode) | |
|
52 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | |
|
53 | ||
|
54 | // the current widget isn't started, it's children shouldn't start too | |
|
55 | dom.place(this.getItemDom(child), instance.containerNode); | |
|
56 | } | |
|
57 | } | |
|
58 | ||
|
59 | protected _create(attrs: any, children: any[]) { | |
|
60 | if (this.widgetClass.prototype instanceof ContentPane) { | |
|
61 | // a special case for the ContentPane this is for | |
|
62 | // the compatibility with this heavy widget, all | |
|
63 | // regular containers could be easily manipulated | |
|
64 | // through `containerNode` property or `addChild` method. | |
|
65 | ||
|
66 | // render children to the DocumentFragment | |
|
67 | const content = document.createDocumentFragment(); | |
|
68 | children.forEach(child => content.appendChild(this.getItemDom(child))); | |
|
69 | ||
|
70 | // set the content property to the parameters of the widget | |
|
71 | const _attrs = { ...attrs, content }; | |
|
72 | this._instance = new this.widgetClass(_attrs); | |
|
73 | } else { | |
|
74 | this._instance = new this.widgetClass(attrs); | |
|
75 | children.forEach(x => this._addChild(x)); | |
|
76 | } | |
|
77 | ||
|
78 | } | |
|
79 | ||
|
80 | private _getInstance() { | |
|
81 | if (!this._instance) | |
|
82 | throw new Error("The instance of the widget isn't created"); | |
|
83 | return this._instance; | |
|
84 | } | |
|
85 | ||
|
86 | protected _getDomNode() { | |
|
87 | if (!this._instance) | |
|
88 | throw new Error("The instance of the widget isn't created"); | |
|
89 | return this._instance.domNode; | |
|
90 | } | |
|
91 | ||
|
92 | /** Overrides default placeAt implementation. Calls placeAt of the | |
|
93 | * widget and then starts it. | |
|
94 | * | |
|
95 | * @param refNode A node or id of the node where the widget should be placed. | |
|
96 | * @param position A position relative to refNode. | |
|
97 | */ | |
|
98 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
|
99 | this.ensureCreated(); | |
|
100 | const instance = this._getInstance(); | |
|
101 | if (typeof instance.placeAt === "function") { | |
|
102 | instance.placeAt(refNode, position); | |
|
103 | ||
|
104 | // fix the dojo startup behavior when the widget is placed | |
|
105 | // directly to the document and doesn't have any enclosing widgets | |
|
106 | const parentWidget = instance.domNode.parentNode ? | |
|
107 | registry.getEnclosingWidget(instance.domNode.parentNode) : null | |
|
108 | if (!parentWidget && isInPage(instance.domNode) && typeof instance.startup === "function") | |
|
109 | instance.startup(); | |
|
110 | } else { | |
|
111 | // the widget doesn't have a placeAt method, strange but whatever | |
|
112 | super.placeAt(refNode, position); | |
|
113 | } | |
|
114 | } | |
|
115 | ||
|
116 | getWidgetInstance() { | |
|
117 | this.ensureCreated(); | |
|
118 | return this._getInstance(); | |
|
119 | } | |
|
120 | ||
|
121 | } |
@@ -24,7 +24,7 | |||
|
24 | 24 | }, |
|
25 | 25 | "peerDependencies": { |
|
26 | 26 | "@implab/core-amd": "^1.4.0", |
|
27 |
"dojo": " |
|
|
27 | "dojo": "^1.10.0" | |
|
28 | 28 | } |
|
29 | 29 | }, |
|
30 | 30 | "node_modules/@babel/code-frame": { |
@@ -1,24 +1,24 | |||
|
1 | 1 | import { Constructor } from "@implab/core-amd/interfaces"; |
|
2 |
import { Html |
|
|
3 |
import { Widget |
|
|
4 |
import { isWidgetConstructor, |
|
|
5 |
import { Function |
|
|
2 | import { HtmlRendition } from "./tsx/HtmlRendition"; | |
|
3 | import { WidgetRendition } from "./tsx/WidgetRendition"; | |
|
4 | import { isWidgetConstructor, Rendition } from "./tsx/traits"; | |
|
5 | import { FunctionRendition } from "./tsx/FunctionRendition"; | |
|
6 | 6 | |
|
7 |
export function createElement<T extends Constructor | string | ((props: any) => Element)>(elementType: T, ...args: any[]): |
|
|
7 | export function createElement<T extends Constructor | string | ((props: any) => Element)>(elementType: T, ...args: any[]): Rendition { | |
|
8 | 8 | if (typeof elementType === "string") { |
|
9 |
const ctx = new Html |
|
|
9 | const ctx = new HtmlRendition(elementType); | |
|
10 | 10 | if (args) |
|
11 | 11 | args.forEach(x => ctx.visitNext(x)); |
|
12 | 12 | |
|
13 | 13 | return ctx; |
|
14 | 14 | } else if (isWidgetConstructor(elementType)) { |
|
15 |
const ctx = new Widget |
|
|
15 | const ctx = new WidgetRendition(elementType); | |
|
16 | 16 | if (args) |
|
17 | 17 | args.forEach(x => ctx.visitNext(x)); |
|
18 | 18 | |
|
19 | 19 | return ctx; |
|
20 | 20 | } else if (typeof elementType === "function") { |
|
21 |
const ctx = new Function |
|
|
21 | const ctx = new FunctionRendition(elementType as (props: any) => Element); | |
|
22 | 22 | if (args) |
|
23 | 23 | args.forEach(x => ctx.visitNext(x)); |
|
24 | 24 |
@@ -1,93 +1,11 | |||
|
1 | import { isNull, mixin } from "@implab/core-amd/safe"; | |
|
2 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext, isInPage, isWidget } from "./traits"; | |
|
3 | ||
|
4 | import dom = require("dojo/dom-construct"); | |
|
5 | import registry = require("dijit/registry"); | |
|
6 | ||
|
7 | ||
|
8 | export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> { | |
|
9 | private _attrs = {}; | |
|
10 | ||
|
11 | private _children = new Array(); | |
|
12 | ||
|
13 | private _created: boolean = false; | |
|
14 | ||
|
15 | visitNext(v: any) { | |
|
16 | if (this._created) | |
|
17 | throw new Error("The Element is already created"); | |
|
18 | ||
|
19 | if (isNull(v) || typeof v === "boolean") | |
|
20 | // skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | |
|
21 | return; | |
|
22 | ||
|
23 | if (isPlainObject(v)) { | |
|
24 | mixin(this._attrs, v); | |
|
25 | } else if (v instanceof Array) { | |
|
26 | v.forEach(x => this.visitNext(x)); | |
|
27 | } else { | |
|
28 | this._children.push(v); | |
|
29 | } | |
|
30 | } | |
|
1 | import { RenditionBase } from "./RenditionBase"; | |
|
31 | 2 | |
|
32 | protected getItemDom(v: any) { | |
|
33 | const tv = typeof v; | |
|
34 | if (tv === "string" || tv === "number" || v instanceof RegExp || v instanceof Date) { | |
|
35 | return document.createTextNode(v.toString()); | |
|
36 | } else if (isNode(v)) { | |
|
37 | return v; | |
|
38 | } else if (isBuildContext(v)) { | |
|
39 | return v.getDomNode(); | |
|
40 | } else if(isWidget(v)) { | |
|
41 | return v.domNode; | |
|
42 | } else if(tv === "boolean" || v === null || v === undefined) { | |
|
43 | return document.createComment(`[${tv} ${String(v)}]`); | |
|
44 | } else { | |
|
45 | throw new Error("Invalid parameter"); | |
|
46 | } | |
|
47 | } | |
|
48 | ||
|
49 | ensureCreated() { | |
|
50 | if (!this._created) { | |
|
51 | this._create(this._attrs, this._children); | |
|
52 | this._children = []; | |
|
53 | this._attrs = {}; | |
|
54 | this._created = true; | |
|
55 | } | |
|
56 | } | |
|
57 | ||
|
58 | /** @deprecated will be removed in 1.0.0, use getDomNode() */ | |
|
59 | getDomElement() { | |
|
60 | return this.getDomNode(); | |
|
61 | } | |
|
3 | /** | |
|
4 | * @deprecated use RenditionBase instead | |
|
5 | */ | |
|
6 | export type BuildContextBase<TNode extends Node> = RenditionBase<TNode>; | |
|
62 | 7 | |
|
63 | /** Creates DOM node if not created. No additional actions are taken. */ | |
|
64 | getDomNode() { | |
|
65 | this.ensureCreated(); | |
|
66 | return this._getDomNode(); | |
|
67 | } | |
|
68 | ||
|
69 | /** Creates DOM node if not created, places it to the specified position | |
|
70 | * and calls startup() method for all widgets contained by this node. | |
|
71 | * | |
|
72 | * @param {string | Node} refNode The reference node where the created | |
|
73 | * DOM should be placed. | |
|
74 | * @param {DojoNodePosition} position Optional parameter, specifies the | |
|
75 | * position relative to refNode. Default is "last" (i.e. last child). | |
|
76 | */ | |
|
77 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
|
78 | const domNode = this.getDomNode(); | |
|
79 | dom.place(domNode, refNode, position); | |
|
80 | const parentWidget = domNode.parentNode ? registry.getEnclosingWidget(domNode.parentNode) : null; | |
|
81 | ||
|
82 | if ((parentWidget && parentWidget._started) || isInPage(domNode)) | |
|
83 | this._startup(); | |
|
84 | } | |
|
85 | ||
|
86 | _startup () { | |
|
87 | registry.findWidgets(this._getDomNode()).forEach(w => w.startup()); | |
|
88 | } | |
|
89 | ||
|
90 | abstract _create(attrs: object, children: any[]): void; | |
|
91 | ||
|
92 | abstract _getDomNode(): TNode; | |
|
93 | } | |
|
8 | /** | |
|
9 | * @deprecated use RenditionBase instead | |
|
10 | */ | |
|
11 | export const BuildContextBase = RenditionBase; |
@@ -1,6 +1,7 | |||
|
1 | 1 | /** Special functional component used to create a document fragment */ |
|
2 | export function DjxFragment({children}: {children: Node[]}){ | |
|
2 | export function DjxFragment({children}: {children?: Node | Node[]}){ | |
|
3 | 3 | const fragment = document.createDocumentFragment(); |
|
4 | 4 | if (children) |
|
5 | children.forEach(child => fragment.appendChild(child)); | |
|
5 | (children instanceof Array ? children : [children]).forEach(child => fragment.appendChild(child)); | |
|
6 | return fragment; | |
|
6 | 7 | } No newline at end of file |
@@ -1,7 +1,7 | |||
|
1 | 1 | import { djbase, djclass } from "../declare"; |
|
2 | 2 | import _WidgetBase = require("dijit/_WidgetBase"); |
|
3 | 3 | import _AttachMixin = require("dijit/_AttachMixin"); |
|
4 |
import { |
|
|
4 | import { Rendition, isNode, startupWidgets } from "./traits"; | |
|
5 | 5 | import registry = require("dijit/registry"); |
|
6 | 6 | |
|
7 | 7 | // type Handle = dojo.Handle; |
@@ -42,7 +42,7 export abstract class DjxWidgetBase<Attr | |||
|
42 | 42 | } |
|
43 | 43 | } |
|
44 | 44 | |
|
45 |
abstract render(): |
|
|
45 | abstract render(): Rendition<HTMLElement>; | |
|
46 | 46 | |
|
47 | 47 | _processTemplateNode<T extends (Element | Node | _WidgetBase)>( |
|
48 | 48 | baseNode: T, |
@@ -82,7 +82,7 export abstract class DjxWidgetBase<Attr | |||
|
82 | 82 | */ |
|
83 | 83 | startup() { |
|
84 | 84 | // startup supporting widgets |
|
85 |
|
|
|
85 | registry.findWidgets(this.domNode, this.containerNode).forEach(w => w.startup()); | |
|
86 | 86 | super.startup(); |
|
87 | 87 | } |
|
88 | 88 | } |
@@ -1,33 +1,11 | |||
|
1 | import dom = require("dojo/dom-construct"); | |
|
2 | import attr = require("dojo/dom-attr"); | |
|
3 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
|
4 | import { BuildContextBase } from "./BuildContextBase"; | |
|
5 | import registry = require("dijit/registry"); | |
|
6 | ||
|
7 | ||
|
8 | export class FunctionComponentContext extends BuildContextBase<Node> { | |
|
9 | private _component: (props: any) => any; | |
|
10 | ||
|
11 | private _node: Node | undefined; | |
|
12 | ||
|
13 | constructor(component: (props: any) => any) { | |
|
14 | super(); | |
|
15 | argumentNotNull(component, "component"); | |
|
1 | import { FunctionRendition } from "./FunctionRendition"; | |
|
16 | 2 | |
|
17 | this._component = component; | |
|
18 | } | |
|
19 | ||
|
20 | _create(attrs: object, children: any[]) { | |
|
21 | const _attrs: any = attrs || {}; | |
|
22 | _attrs.children = children.map(x => this.getItemDom(x)); | |
|
3 | /** | |
|
4 | * @deprecated use FunctionRendition | |
|
5 | */ | |
|
6 | export type FunctionComponentContext = FunctionRendition; | |
|
23 | 7 | |
|
24 | this._node = this.getItemDom(this._component.call(null, _attrs)); | |
|
25 | } | |
|
26 | ||
|
27 | _getDomNode() { | |
|
28 | if (!this._node) | |
|
29 | throw new Error("The instance of the widget isn't created"); | |
|
30 | return this._node; | |
|
31 | } | |
|
32 | ||
|
33 | } | |
|
8 | /** | |
|
9 | * @deprecated use FunctionRendition | |
|
10 | */ | |
|
11 | export const FunctionComponentContext = FunctionRendition; |
@@ -1,37 +1,11 | |||
|
1 | import dom = require("dojo/dom-construct"); | |
|
2 | import { argumentNotEmptyString } from "@implab/core-amd/safe"; | |
|
3 | import { BuildContextBase } from "./BuildContextBase"; | |
|
4 | ||
|
5 | export class HtmlElementContext extends BuildContextBase<HTMLElement> { | |
|
6 | elementType: string; | |
|
7 | ||
|
8 | _element: HTMLElement | undefined; | |
|
9 | ||
|
10 | constructor(elementType: string) { | |
|
11 | argumentNotEmptyString(elementType, "elementType"); | |
|
12 | super(); | |
|
13 | ||
|
14 | this.elementType = elementType; | |
|
15 | } | |
|
1 | import { HtmlRendition } from "./HtmlRendition"; | |
|
16 | 2 | |
|
17 | _addChild(child: any): void { | |
|
18 | if (!this._element) | |
|
19 | throw new Error("The HTML element isn't created"); | |
|
20 | dom.place(this.getItemDom(child), this._element); | |
|
21 | } | |
|
22 | ||
|
23 | _create(attrs: object, children: any[]) { | |
|
24 | this._element = dom.create(this.elementType, attrs); | |
|
3 | /** | |
|
4 | * @deprecated use HtmlRendition | |
|
5 | */ | |
|
6 | export type HtmlElementContext = HtmlRendition; | |
|
25 | 7 | |
|
26 | if (children) | |
|
27 | children.forEach(v => this._addChild(v)); | |
|
28 | } | |
|
29 | ||
|
30 | _getDomNode() { | |
|
31 | if (!this._element) | |
|
32 | throw new Error("The HTML element isn't created"); | |
|
33 | ||
|
34 | return this._element; | |
|
35 | } | |
|
36 | ||
|
37 | } | |
|
8 | /** | |
|
9 | * @deprecated use HtmlRendition | |
|
10 | */ | |
|
11 | export const HtmlElementContext = HtmlRendition; |
@@ -1,128 +1,11 | |||
|
1 | import dom = require("dojo/dom-construct"); | |
|
2 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
|
3 | import { BuildContextBase } from "./BuildContextBase"; | |
|
4 | import { DojoNodePosition, isInPage, isWidget } from "./traits"; | |
|
5 | import registry = require("dijit/registry"); | |
|
6 | import ContentPane = require("dijit/layout/ContentPane"); | |
|
7 | ||
|
8 | // tslint:disable-next-line: class-name | |
|
9 | export interface _Widget { | |
|
10 | domNode: Node; | |
|
11 | ||
|
12 | containerNode?: Node; | |
|
13 | ||
|
14 | placeAt?(refNode: string | Node, position?: DojoNodePosition): void; | |
|
15 | startup?(): void; | |
|
16 | ||
|
17 | addChild?(widget: any, index?: number): void; | |
|
18 | } | |
|
19 | ||
|
20 | export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget; | |
|
21 | ||
|
22 | export class WidgetContext extends BuildContextBase<Node> { | |
|
23 | readonly widgetClass: _WidgetCtor; | |
|
24 | ||
|
25 | _instance: _Widget | undefined; | |
|
26 | ||
|
27 | constructor(widgetClass: _WidgetCtor) { | |
|
28 | super(); | |
|
29 | argumentNotNull(widgetClass, "widgetClass"); | |
|
30 | ||
|
31 | this.widgetClass = widgetClass; | |
|
32 | } | |
|
33 | ||
|
34 | _addChild(child: any): void { | |
|
35 | const instance = this._getInstance(); | |
|
36 | ||
|
37 | if (instance.addChild) { | |
|
38 | if (child instanceof WidgetContext) { | |
|
39 | // layout containers add custom logic to addChild methods | |
|
40 | instance.addChild(child.getWidgetInstance()); | |
|
41 | } else if (isWidget(child)) { | |
|
42 | instance.addChild(child); | |
|
43 | } else { | |
|
44 | if (!instance.containerNode) | |
|
45 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | |
|
46 | ||
|
47 | // the current widget isn't started, it's children shouldn't start too | |
|
48 | dom.place(this.getItemDom(child), instance.containerNode); | |
|
49 | } | |
|
50 | } else { | |
|
51 | if (!instance.containerNode) | |
|
52 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | |
|
53 | ||
|
54 | // the current widget isn't started, it's children shouldn't start too | |
|
55 | dom.place(this.getItemDom(child), instance.containerNode); | |
|
56 | } | |
|
57 | } | |
|
1 | import { WidgetRendition } from "./WidgetRendition"; | |
|
58 | 2 | |
|
59 | _create(attrs: any, children: any[]) { | |
|
60 | if (this.widgetClass.prototype instanceof ContentPane) { | |
|
61 | // a special case for the ContentPane this is for | |
|
62 | // the compatibility with this heavy widget, all | |
|
63 | // regular containers could be easily manipulated | |
|
64 | // through `containerNode` property or `addChild` method. | |
|
65 | ||
|
66 | // render children to the DocumentFragment | |
|
67 | const content = document.createDocumentFragment(); | |
|
68 | children.forEach(child => content.appendChild(this.getItemDom(child))); | |
|
69 | ||
|
70 | // set the content property to the parameters of the widget | |
|
71 | const _attrs = { ...attrs, content }; | |
|
72 | this._instance = new this.widgetClass(_attrs); | |
|
73 | } else { | |
|
74 | this._instance = new this.widgetClass(attrs); | |
|
75 | children.forEach(x => this._addChild(x)); | |
|
76 | } | |
|
77 | ||
|
78 | } | |
|
79 | ||
|
80 | private _getInstance() { | |
|
81 | if (!this._instance) | |
|
82 | throw new Error("The instance of the widget isn't created"); | |
|
83 | return this._instance; | |
|
84 | } | |
|
85 | ||
|
86 | _getDomNode() { | |
|
87 | if (!this._instance) | |
|
88 | throw new Error("The instance of the widget isn't created"); | |
|
89 | return this._instance.domNode; | |
|
90 | } | |
|
3 | /** | |
|
4 | * @deprecated use WidgetRendition | |
|
5 | */ | |
|
6 | export type WidgetContext = WidgetRendition; | |
|
91 | 7 | |
|
92 | /** Overrides default placeAt implementation. Calls placeAt of the | |
|
93 | * widget and then starts it. | |
|
94 | * | |
|
95 | * @param refNode A node or id of the node where the widget should be placed. | |
|
96 | * @param position A position relative to refNode. | |
|
97 | */ | |
|
98 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
|
99 | this.ensureCreated(); | |
|
100 | const instance = this._getInstance(); | |
|
101 | if (typeof instance.placeAt === "function") { | |
|
102 | instance.placeAt(refNode, position); | |
|
103 | ||
|
104 | // fix the dojo startup behavior when the widget is placed | |
|
105 | // directly to the document and doesn't have any enclosing widgets | |
|
106 | const parentWidget = instance.domNode.parentNode ? | |
|
107 | registry.getEnclosingWidget(instance.domNode.parentNode) : null | |
|
108 | if (!parentWidget && isInPage(instance.domNode)) | |
|
109 | this._startup(); | |
|
110 | } else { | |
|
111 | // the widget doesn't have a placeAt method, strange but whatever | |
|
112 | super.placeAt(refNode, position); | |
|
113 | } | |
|
114 | } | |
|
115 | ||
|
116 | _startup() { | |
|
117 | const instance = this._getInstance(); | |
|
118 | ||
|
119 | if (typeof instance.startup === "function") | |
|
120 | instance.startup(); | |
|
121 | } | |
|
122 | ||
|
123 | getWidgetInstance() { | |
|
124 | this.ensureCreated(); | |
|
125 | return this._getInstance(); | |
|
126 | } | |
|
127 | ||
|
128 | } | |
|
8 | /** | |
|
9 | * @deprecated use WidgetRendition | |
|
10 | */ | |
|
11 | export const WidgetContext = WidgetRendition; |
@@ -8,12 +8,17 type _WidgetBaseConstructor = typeof _Wi | |||
|
8 | 8 | |
|
9 | 9 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; |
|
10 | 10 | |
|
11 |
export interface |
|
|
11 | export interface Rendition<TNode extends Node = Node> { | |
|
12 | 12 | getDomNode(): TNode; |
|
13 | 13 | |
|
14 | 14 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | /** | |
|
18 | * @deprecated use Rendition | |
|
19 | */ | |
|
20 | export type BuildContext<TNode extends Node = Node> = Rendition<TNode>; | |
|
21 | ||
|
17 | 22 | export interface IRecursivelyDestroyable { |
|
18 | 23 | destroyRecursive(): void; |
|
19 | 24 | } |
@@ -54,10 +59,15 export function isWidget(v: any): v is _ | |||
|
54 | 59 | return v && "domNode" in v; |
|
55 | 60 | } |
|
56 | 61 | |
|
57 |
export function is |
|
|
62 | export function isRendition(v: any): v is Rendition { | |
|
58 | 63 | return typeof v === "object" && typeof v.getDomElement === "function"; |
|
59 | 64 | } |
|
60 | 65 | |
|
66 | /** | |
|
67 | * @deprecated use isRendition | |
|
68 | */ | |
|
69 | export const isBuildContext = isRendition; | |
|
70 | ||
|
61 | 71 | export function isPlainObject(v: object) { |
|
62 | 72 | if (typeof v !== "object") |
|
63 | 73 | return false; |
@@ -116,15 +126,21 export function emptyNode(target: Node) | |||
|
116 | 126 | dom.empty(target); |
|
117 | 127 | } |
|
118 | 128 | |
|
119 |
/** This function starts all widgets inside the DOM node if the target is a node |
|
|
120 | * or starts widget itself if the target is the widget. | |
|
129 | /** This function starts all widgets inside the DOM node if the target is a node | |
|
130 | * or starts widget itself if the target is the widget. If the specified node | |
|
131 | * associated with the widget that widget will be started. | |
|
121 | 132 | * |
|
122 | 133 | * @param target DOM node to find and start widgets or the widget itself. |
|
123 | 134 | */ |
|
124 | 135 | export function startupWidgets(target: Node | _WidgetBase, skipNode?: Node) { |
|
125 | 136 | if (isNode(target)) { |
|
126 | registry.findWidgets(target, skipNode).forEach(w => w.startup()); | |
|
137 | const w = registry.byNode(target); | |
|
138 | if (w) { | |
|
139 | w.startup && w.startup(); | |
|
140 | } else { | |
|
141 | registry.findWidgets(target, skipNode).forEach(w => w.startup()); | |
|
142 | } | |
|
127 | 143 | } else { |
|
128 | target.startup(); | |
|
144 | target.startup && target.startup(); | |
|
129 | 145 | } |
|
130 | 146 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now