| @@ -1,146 +1,148 | |||||
| 1 | import { IDestroyable } from "@implab/core-amd/interfaces"; |
|
1 | import { IDestroyable } from "@implab/core-amd/interfaces"; | |
| 2 | import { isDestroyable } from "@implab/core-amd/safe"; |
|
2 | import { isDestroyable } from "@implab/core-amd/safe"; | |
| 3 | import _WidgetBase = require("dijit/_WidgetBase"); |
|
3 | import _WidgetBase = require("dijit/_WidgetBase"); | |
| 4 | import registry = require("dijit/registry"); |
|
4 | import registry = require("dijit/registry"); | |
| 5 | import dom = require("dojo/dom-construct"); |
|
5 | import dom = require("dojo/dom-construct"); | |
| 6 |
|
6 | |||
| 7 | type _WidgetBaseConstructor = typeof _WidgetBase; |
|
7 | type _WidgetBaseConstructor = typeof _WidgetBase; | |
| 8 |
|
8 | |||
| 9 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; |
|
9 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; | |
| 10 |
|
10 | |||
| 11 | export interface Rendition<TNode extends Node = Node> { |
|
11 | export interface Rendition<TNode extends Node = Node> { | |
| 12 | getDomNode(): TNode; |
|
12 | getDomNode(): TNode; | |
| 13 |
|
13 | |||
| 14 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; |
|
14 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; | |
| 15 | } |
|
15 | } | |
| 16 |
|
16 | |||
| 17 | /** |
|
17 | /** | |
| 18 | * @deprecated use Rendition |
|
18 | * @deprecated use Rendition | |
| 19 | */ |
|
19 | */ | |
| 20 | export type BuildContext<TNode extends Node = Node> = Rendition<TNode>; |
|
20 | export type BuildContext<TNode extends Node = Node> = Rendition<TNode>; | |
| 21 |
|
21 | |||
| 22 | export interface IRecursivelyDestroyable { |
|
22 | export interface IRecursivelyDestroyable { | |
| 23 | destroyRecursive(): void; |
|
23 | destroyRecursive(): void; | |
| 24 | } |
|
24 | } | |
| 25 |
|
25 | |||
| 26 | export function isNode(el: any): el is Node { |
|
26 | export function isNode(el: any): el is Node { | |
| 27 | return el && el.nodeName && el.nodeType; |
|
27 | return el && el.nodeName && el.nodeType; | |
| 28 | } |
|
28 | } | |
| 29 |
|
29 | |||
| 30 | export function isElementNode(el: any): el is Element { |
|
30 | export function isElementNode(el: any): el is Element { | |
| 31 | return isNode(el) && el.nodeType === 1; |
|
31 | return isNode(el) && el.nodeType === 1; | |
| 32 | } |
|
32 | } | |
| 33 |
|
33 | |||
| 34 | export function isTextNode(el: any): el is Text { |
|
34 | export function isTextNode(el: any): el is Text { | |
| 35 | return isNode(el) && el.nodeType === 3; |
|
35 | return isNode(el) && el.nodeType === 3; | |
| 36 | } |
|
36 | } | |
| 37 |
|
37 | |||
| 38 | export function isProcessingInstructionNode(el: any): el is ProcessingInstruction { |
|
38 | export function isProcessingInstructionNode(el: any): el is ProcessingInstruction { | |
| 39 | return isNode(el) && el.nodeType === 7; |
|
39 | return isNode(el) && el.nodeType === 7; | |
| 40 | } |
|
40 | } | |
| 41 |
|
41 | |||
| 42 | export function isCommentNode(el: any): el is Comment { |
|
42 | export function isCommentNode(el: any): el is Comment { | |
| 43 | return isNode(el) && el.nodeType === 8; |
|
43 | return isNode(el) && el.nodeType === 8; | |
| 44 | } |
|
44 | } | |
| 45 |
|
45 | |||
| 46 | export function isDocumentNode(el: any): el is Document { |
|
46 | export function isDocumentNode(el: any): el is Document { | |
| 47 | return isNode(el) && el.nodeType === 9; |
|
47 | return isNode(el) && el.nodeType === 9; | |
| 48 | } |
|
48 | } | |
| 49 |
|
49 | |||
| 50 | export function isDocumentTypeNode(el: any): el is DocumentType { |
|
50 | export function isDocumentTypeNode(el: any): el is DocumentType { | |
| 51 | return isNode(el) && el.nodeType === 10; |
|
51 | return isNode(el) && el.nodeType === 10; | |
| 52 | } |
|
52 | } | |
| 53 |
|
53 | |||
| 54 | export function isDocumentFragmentNode(el: any): el is DocumentFragment { |
|
54 | export function isDocumentFragmentNode(el: any): el is DocumentFragment { | |
| 55 | return isNode(el) && el.nodeType === 11; |
|
55 | return isNode(el) && el.nodeType === 11; | |
| 56 | } |
|
56 | } | |
| 57 |
|
57 | |||
| 58 | export function isWidget(v: any): v is _WidgetBase { |
|
58 | export function isWidget(v: any): v is _WidgetBase { | |
| 59 | return v && "domNode" in v; |
|
59 | return v && "domNode" in v; | |
| 60 | } |
|
60 | } | |
| 61 |
|
61 | |||
| 62 | export function isRendition(v: any): v is Rendition { |
|
62 | export function isRendition(v: any): v is Rendition { | |
| 63 | return typeof v === "object" && typeof v.getDomElement === "function"; |
|
63 | return typeof v === "object" && typeof v.getDomElement === "function"; | |
| 64 | } |
|
64 | } | |
| 65 |
|
65 | |||
| 66 | /** |
|
66 | /** | |
| 67 | * @deprecated use isRendition |
|
67 | * @deprecated use isRendition | |
| 68 | */ |
|
68 | */ | |
| 69 | export const isBuildContext = isRendition; |
|
69 | export const isBuildContext = isRendition; | |
| 70 |
|
70 | |||
| 71 | export function isPlainObject(v: object) { |
|
71 | export function isPlainObject(v: object) { | |
| 72 | if (typeof v !== "object") |
|
72 | if (typeof v !== "object") | |
| 73 | return false; |
|
73 | return false; | |
| 74 |
|
74 | |||
| 75 | const vp = Object.getPrototypeOf(v); |
|
75 | const vp = Object.getPrototypeOf(v); | |
| 76 | return !vp || vp === Object.prototype; |
|
76 | return !vp || vp === Object.prototype; | |
| 77 | } |
|
77 | } | |
| 78 |
|
78 | |||
| 79 | export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor { |
|
79 | export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor { | |
| 80 | return typeof v === "function" && v.prototype && ( |
|
80 | return typeof v === "function" && v.prototype && ( | |
| 81 | "domNode" in v.prototype || |
|
81 | "domNode" in v.prototype || | |
| 82 | "buildRendering" in v.prototype |
|
82 | "buildRendering" in v.prototype | |
| 83 | ); |
|
83 | ); | |
| 84 | } |
|
84 | } | |
| 85 |
|
85 | |||
| 86 | /** Tests whether the specified node is placed in visible dom. |
|
86 | /** Tests whether the specified node is placed in visible dom. | |
| 87 | * @param {Node} node The node to test |
|
87 | * @param {Node} node The node to test | |
| 88 | */ |
|
88 | */ | |
| 89 | export function isInPage(node: Node) { |
|
89 | export function isInPage(node: Node) { | |
| 90 | return (node === document.body) ? false : document.body.contains(node); |
|
90 | return (node === document.body) ? false : document.body.contains(node); | |
| 91 | } |
|
91 | } | |
| 92 |
|
92 | |||
| 93 | export function isRecursivelyDestroyable(target: any): target is IRecursivelyDestroyable { |
|
93 | export function isRecursivelyDestroyable(target: any): target is IRecursivelyDestroyable { | |
| 94 | return target && typeof target.destroyRecursive === "function"; |
|
94 | return target && typeof target.destroyRecursive === "function"; | |
| 95 | } |
|
95 | } | |
| 96 |
|
96 | |||
| 97 |
|
97 | |||
| 98 | /** Destroys DOM Node with all contained widgets. |
|
98 | /** Destroys DOM Node with all contained widgets. | |
| 99 | * If the specified node is the root node of a widget, then the |
|
99 | * If the specified node is the root node of a widget, then the | |
| 100 | * widget will be destroyed. |
|
100 | * widget will be destroyed. | |
| 101 | * |
|
101 | * | |
| 102 | * @param target DOM Node or widget to destroy |
|
102 | * @param target DOM Node or widget to destroy | |
| 103 | */ |
|
103 | */ | |
| 104 | export function destroy(target: Node | IDestroyable | IRecursivelyDestroyable) { |
|
104 | export function destroy(target: Node | IDestroyable | IRecursivelyDestroyable) { | |
| 105 | if (isRecursivelyDestroyable(target)) { |
|
105 | if (isRecursivelyDestroyable(target)) { | |
| 106 | target.destroyRecursive(); |
|
106 | target.destroyRecursive(); | |
| 107 | } else if (isDestroyable(target)) { |
|
107 | } else if (isDestroyable(target)) { | |
| 108 | target.destroy(); |
|
108 | target.destroy(); | |
| 109 | } else if (isNode(target)) { |
|
109 | } else if (isNode(target)) { | |
| 110 | const self = registry.byNode(target); |
|
110 | const self = registry.byNode(target); | |
| 111 | if (self) { |
|
111 | if (self) { | |
| 112 | self.destroyRecursive(); |
|
112 | self.destroyRecursive(); | |
| 113 | } else { |
|
113 | } else { | |
| 114 | registry.findWidgets(target).forEach(destroy); |
|
114 | registry.findWidgets(target).forEach(destroy); | |
| 115 | dom.destroy(target); |
|
115 | dom.destroy(target); | |
| 116 | } |
|
116 | } | |
| 117 | } |
|
117 | } | |
| 118 | } |
|
118 | } | |
| 119 |
|
119 | |||
| 120 | /** Empties a content of the specified node and destroys all contained widgets. |
|
120 | /** Empties a content of the specified node and destroys all contained widgets. | |
| 121 | * |
|
121 | * | |
| 122 | * @param target DOM node to . |
|
122 | * @param target DOM node to . | |
| 123 | */ |
|
123 | */ | |
| 124 | export function emptyNode(target: Node) { |
|
124 | export function emptyNode(target: Node) { | |
| 125 | registry.findWidgets(target).forEach(destroy); |
|
125 | registry.findWidgets(target).forEach(destroy); | |
| 126 | dom.empty(target); |
|
126 | dom.empty(target); | |
| 127 | } |
|
127 | } | |
| 128 |
|
128 | |||
| 129 | /** This function starts all widgets inside the DOM node if the target is a node |
|
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 |
|
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. |
|
131 | * associated with the widget that widget will be started. | |
| 132 | * |
|
132 | * | |
| 133 | * @param target DOM node to find and start widgets or the widget itself. |
|
133 | * @param target DOM node to find and start widgets or the widget itself. | |
| 134 | */ |
|
134 | */ | |
| 135 | export function startupWidgets(target: Node | _WidgetBase, skipNode?: Node) { |
|
135 | export function startupWidgets(target: Node | _WidgetBase, skipNode?: Node) { | |
| 136 | if (isNode(target)) { |
|
136 | if (isNode(target)) { | |
| 137 | const w = registry.byNode(target); |
|
137 | const w = isElementNode(target) ? registry.byNode(target) : undefined; | |
| 138 | if (w) { |
|
138 | if (w) { | |
| 139 |
w.startup |
|
139 | if (w.startup) | |
|
|
140 | w.startup(); | |||
| 140 | } else { |
|
141 | } else { | |
| 141 |
registry.findWidgets(target, skipNode).forEach( |
|
142 | registry.findWidgets(target, skipNode).forEach(x => x.startup()); | |
| 142 | } |
|
143 | } | |
| 143 | } else { |
|
144 | } else { | |
| 144 |
target.startup |
|
145 | if(target.startup) | |
|
|
146 | target.startup(); | |||
| 145 | } |
|
147 | } | |
| 146 | } No newline at end of file |
|
148 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
