##// END OF EJS Templates
Fixes in WidgetContex, added _Container.addChild support
cin -
r38:5c6c7e16919c v1.0.0-rc18 default
parent child
Show More
@@ -19,7 +19,7 interface NodeLoadResult {
19
19
20 class DomInject {
20 class DomInject {
21 injectionPoint?: HTMLElement;
21 injectionPoint?: HTMLElement;
22 injectBefore?: HTMLElement;
22 injectAfter?: HTMLElement;
23
23
24 _map: MapOf<Promise<NodeLoadResult>> = {};
24 _map: MapOf<Promise<NodeLoadResult>> = {};
25
25
@@ -49,7 +49,7 class DomInject {
49 mixin(node, attr);
49 mixin(node, attr);
50
50
51 const _injectionPoint = this.injectionPoint || document.head;
51 const _injectionPoint = this.injectionPoint || document.head;
52 const _injectBefore = this.injectBefore || _injectionPoint.firstChild;
52 const _injectBefore = this.injectAfter ? this.injectAfter.nextSibling : null;
53
53
54 _injectionPoint.insertBefore(node, _injectBefore);
54 _injectionPoint.insertBefore(node, _injectBefore);
55 });
55 });
@@ -2,6 +2,7 import { isNull, mixin } from "@implab/c
2 import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits";
2 import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits";
3
3
4 import dom = require("dojo/dom-construct");
4 import dom = require("dojo/dom-construct");
5 import registry = require("dijit/registry");
5
6
6 export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> {
7 export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> {
7 private _attrs = {};
8 private _attrs = {};
@@ -39,8 +40,6 export abstract class BuildContextBase<T
39 }
40 }
40 }
41 }
41
42
42 abstract _getDomNode(): TNode;
43
44 ensureCreated() {
43 ensureCreated() {
45 if (!this._created) {
44 if (!this._created) {
46 this._create(this._attrs, this._children);
45 this._create(this._attrs, this._children);
@@ -50,19 +49,32 export abstract class BuildContextBase<T
50 }
49 }
51 }
50 }
52
51
53 /** @deprecated use getDomNode() */
52 /** @deprecated will be removed in 1.0.0, use getDomNode() */
54 getDomElement() {
53 getDomElement() {
55 return this.getDomNode();
54 return this.getDomNode();
56 }
55 }
57
56
57 /** Creates DOM node if not created. No additional actions are taken. */
58 getDomNode() {
58 getDomNode() {
59 this.ensureCreated();
59 this.ensureCreated();
60 return this._getDomNode();
60 return this._getDomNode();
61 }
61 }
62
62
63 /** Creates DOM node if not created, places it to the specified position
64 * and calls startup() method for all widgets contained by this node.
65 *
66 * @param {string | Node} refNode The reference node where the created
67 * DOM should be placed.
68 * @param {DojoNodePosition} position Optional parameter, specifies the
69 * position relative to refNode. Default is "last" (i.e. last child).
70 */
63 placeAt(refNode: string | Node, position?: DojoNodePosition) {
71 placeAt(refNode: string | Node, position?: DojoNodePosition) {
64 dom.place(this.getDomNode(), refNode, position);
72 const domNode = this.getDomNode();
73 dom.place(domNode, refNode, position);
74 registry.findWidgets(domNode).forEach(w => w.startup());
65 }
75 }
66
76
67 abstract _create(attrs: object, children: any[]): void;
77 abstract _create(attrs: object, children: any[]): void;
78
79 abstract _getDomNode(): TNode;
68 }
80 }
@@ -5,7 +5,6 import { prototype } from "../declare";
5 /** Special widget used to create a document fragment */
5 /** Special widget used to create a document fragment */
6 export class DjxFragment implements _Widget {
6 export class DjxFragment implements _Widget {
7
7
8 @prototype()
9 domNode: Node;
8 domNode: Node;
10
9
11 containerNode?: Node;
10 containerNode?: Node;
@@ -13,13 +12,7 export class DjxFragment implements _Wid
13 constructor() {
12 constructor() {
14 this.domNode = this.containerNode = document.createDocumentFragment();
13 this.domNode = this.containerNode = document.createDocumentFragment();
15 }
14 }
16
15 buildRendering() {
17 get(attr: string) {
16 // this function marks this class as _Widget
18 return undefined;
19 }
20 set(attr: string, value: any): void;
21 set(attrs: MapOf<any>): void;
22 set() {
23 /* do nothing */
24 }
17 }
25 } No newline at end of file
18 }
@@ -1,24 +1,24
1 import dom = require("dojo/dom-construct");
1 import dom = require("dojo/dom-construct");
2 import { argumentNotNull } from "@implab/core-amd/safe";
2 import { argumentNotNull } from "@implab/core-amd/safe";
3 import { BuildContextBase } from "./BuildContextBase";
3 import { BuildContextBase } from "./BuildContextBase";
4 import { MapOf } from "@implab/core-amd/interfaces";
4 import { DojoNodePosition, isWidget } from "./traits";
5
5
6 // tslint:disable-next-line: class-name
6 // tslint:disable-next-line: class-name
7 export interface _Widget {
7 export interface _Widget {
8 domNode: Node;
8 domNode: Node;
9
9
10 get(attr: string): any;
10 containerNode?: Node;
11
11
12 set(attr: string, value: any): void;
12 placeAt?(refNode: string | Node, position?: DojoNodePosition): void;
13 set(attrs: MapOf<any>): void;
13 startup?(): void;
14
14
15 containerNode?: Node
15 addChild?(widget: any, index?: number): void;
16 }
16 }
17
17
18 export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget;
18 export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget;
19
19
20 export class WidgetContext extends BuildContextBase<Node> {
20 export class WidgetContext extends BuildContextBase<Node> {
21 widgetClass: _WidgetCtor;
21 readonly widgetClass: _WidgetCtor;
22
22
23 _instance: _Widget | undefined;
23 _instance: _Widget | undefined;
24
24
@@ -30,10 +30,23 export class WidgetContext extends Build
30 }
30 }
31
31
32 _addChild(child: any): void {
32 _addChild(child: any): void {
33 if (!this._instance || !this._instance.containerNode)
33 const instance = this._getInstance();
34 throw new Error("Widget doesn't support adding children");
35
34
36 dom.place(this.getChildDom(child), this._instance.containerNode);
35 if (instance.addChild) {
36 if (child instanceof WidgetContext)
37 // layout containers add custom logic to addChild methods
38 instance.addChild(child.getWidgetInstance());
39 else if (isWidget(child))
40 instance.addChild(child);
41 else
42 instance.addChild(this.getChildDom(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.getChildDom(child), instance.containerNode);
49 }
37 }
50 }
38
51
39 _create(attrs: object, children: any[]) {
52 _create(attrs: object, children: any[]) {
@@ -42,10 +55,41 export class WidgetContext extends Build
42 children.forEach(x => this._addChild(x));
55 children.forEach(x => this._addChild(x));
43 }
56 }
44
57
58 private _getInstance() {
59 if (!this._instance)
60 throw new Error("The instance of the widget isn't created");
61 return this._instance;
62 }
63
45 _getDomNode() {
64 _getDomNode() {
46 if (!this._instance)
65 if (!this._instance)
47 throw new Error("The instance of the widget isn't created");
66 throw new Error("The instance of the widget isn't created");
48 return this._instance.domNode;
67 return this._instance.domNode;
49 }
68 }
50
69
70 /** Overrides default placeAt implementation. Calls placeAt of the
71 * widget and then starts it.
72 *
73 * @param refNode A node or id of the node where the widget should be placed.
74 * @param position A position relative to refNode.
75 */
76 placeAt(refNode: string | Node, position?: DojoNodePosition) {
77 this.ensureCreated();
78 const instance = this._getInstance();
79 if (typeof instance.placeAt === "function") {
80 instance.placeAt(refNode, position);
81
82 // do we need to force widget startup?
83 if (typeof instance.startup === "function")
84 instance.startup();
85 } else {
86 super.placeAt(refNode, position);
87 }
88 }
89
90 getWidgetInstance() {
91 this.ensureCreated();
92 return this._getInstance();
93 }
94
51 }
95 }
@@ -59,5 +59,8 export function isPlainObject(v: object)
59 }
59 }
60
60
61 export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor {
61 export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor {
62 return typeof v === "function" && v.prototype && "domNode" in v.prototype;
62 return typeof v === "function" && v.prototype && (
63 "domNode" in v.prototype ||
64 "buildRendering" in v.prototype
65 );
63 }
66 }
General Comments 0
You need to be logged in to leave comments. Login now