forked from ibm-js/delite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.js
73 lines (66 loc) · 2.33 KB
/
Container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/** @module delite/Container */
define([
"dcl/dcl",
"./Widget"
], function (dcl, Widget) {
/**
* Widget that contains a set of Element children (either widgets or plain DOM nodes).
* @mixin module:delite/Container
* @augments module:delite/Widget
*/
return dcl(Widget, /** @lends module:delite/Container# */{
buildRendering: dcl.after(function () {
if (!this.containerNode) {
// All widgets with descendants must set containerNode.
this.containerNode = this;
}
}),
/**
* Inserts specified Element as a child of this widget's
* container node, and possibly does other processing (such as layout).
* @param {Element} node - Element to add as a child.
* @param {number} [insertIndex] - Position the child as at the specified position relative to other children.
*/
addChild: function (node, insertIndex) {
// Note: insertBefore(node, null) equivalent to appendChild(). Null arg is needed (only) on IE.
var cn = this.containerNode, nextSibling = cn.children[insertIndex];
cn.insertBefore(node, nextSibling || null);
// If I've been started but the child widget hasn't been started,
// start it now. Make sure to do this after widget has been
// inserted into the DOM tree, so it can see that it's being controlled by me,
// so it doesn't try to size itself.
if (this._started && !node._started && dcl.isInstanceOf(node, Widget)) {
node.startup();
}
},
/**
* Removes the passed node instance from this widget but does
* not destroy it. You can also pass in an integer indicating
* the index within the container to remove (ie, removeChild(5) removes the sixth node).
* @param {Element|number} node
*/
removeChild: function (node) {
if (typeof node === "number") {
node = this.getChildren()[node];
}
if (node && node.parentNode) {
HTMLElement.prototype.removeChild.call(node.parentNode, node); // detach but don't destroy
}
},
/**
* Returns true if widget has child widgets, i.e. if this.containerNode contains widgets.
* @returns {boolean}
*/
hasChildren: function () {
return this.getChildren().length > 0;
},
/**
* Returns the index of the child in this container or -1 if not found.
* @param {Element} child
* @returns {number}
*/
getIndexOfChild: function (child) {
return this.getChildren().indexOf(child);
}
});
});