1 | define([ |
---|
2 | "dojo/_base/array", // array.forEach array.indexOf |
---|
3 | "dojo/_base/declare", // declare |
---|
4 | "dojo/dom-construct", // domConstruct.place |
---|
5 | "./registry" // registry.byNode() |
---|
6 | ], function(array, declare, domConstruct, registry){ |
---|
7 | |
---|
8 | // module: |
---|
9 | // dijit/_Container |
---|
10 | // summary: |
---|
11 | // Mixin for widgets that contain a set of widget children. |
---|
12 | |
---|
13 | return declare("dijit._Container", null, { |
---|
14 | // summary: |
---|
15 | // Mixin for widgets that contain a set of widget children. |
---|
16 | // description: |
---|
17 | // Use this mixin for widgets that needs to know about and |
---|
18 | // keep track of their widget children. Suitable for widgets like BorderContainer |
---|
19 | // and TabContainer which contain (only) a set of child widgets. |
---|
20 | // |
---|
21 | // It's not suitable for widgets like ContentPane |
---|
22 | // which contains mixed HTML (plain DOM nodes in addition to widgets), |
---|
23 | // and where contained widgets are not necessarily directly below |
---|
24 | // this.containerNode. In that case calls like addChild(node, position) |
---|
25 | // wouldn't make sense. |
---|
26 | |
---|
27 | buildRendering: function(){ |
---|
28 | this.inherited(arguments); |
---|
29 | if(!this.containerNode){ |
---|
30 | // all widgets with descendants must set containerNode |
---|
31 | this.containerNode = this.domNode; |
---|
32 | } |
---|
33 | }, |
---|
34 | |
---|
35 | addChild: function(/*dijit._Widget*/ widget, /*int?*/ insertIndex){ |
---|
36 | // summary: |
---|
37 | // Makes the given widget a child of this widget. |
---|
38 | // description: |
---|
39 | // Inserts specified child widget's dom node as a child of this widget's |
---|
40 | // container node, and possibly does other processing (such as layout). |
---|
41 | |
---|
42 | var refNode = this.containerNode; |
---|
43 | if(insertIndex && typeof insertIndex == "number"){ |
---|
44 | var children = this.getChildren(); |
---|
45 | if(children && children.length >= insertIndex){ |
---|
46 | refNode = children[insertIndex-1].domNode; |
---|
47 | insertIndex = "after"; |
---|
48 | } |
---|
49 | } |
---|
50 | domConstruct.place(widget.domNode, refNode, insertIndex); |
---|
51 | |
---|
52 | // If I've been started but the child widget hasn't been started, |
---|
53 | // start it now. Make sure to do this after widget has been |
---|
54 | // inserted into the DOM tree, so it can see that it's being controlled by me, |
---|
55 | // so it doesn't try to size itself. |
---|
56 | if(this._started && !widget._started){ |
---|
57 | widget.startup(); |
---|
58 | } |
---|
59 | }, |
---|
60 | |
---|
61 | removeChild: function(/*Widget|int*/ widget){ |
---|
62 | // summary: |
---|
63 | // Removes the passed widget instance from this widget but does |
---|
64 | // not destroy it. You can also pass in an integer indicating |
---|
65 | // the index within the container to remove |
---|
66 | |
---|
67 | if(typeof widget == "number"){ |
---|
68 | widget = this.getChildren()[widget]; |
---|
69 | } |
---|
70 | |
---|
71 | if(widget){ |
---|
72 | var node = widget.domNode; |
---|
73 | if(node && node.parentNode){ |
---|
74 | node.parentNode.removeChild(node); // detach but don't destroy |
---|
75 | } |
---|
76 | } |
---|
77 | }, |
---|
78 | |
---|
79 | hasChildren: function(){ |
---|
80 | // summary: |
---|
81 | // Returns true if widget has children, i.e. if this.containerNode contains something. |
---|
82 | return this.getChildren().length > 0; // Boolean |
---|
83 | }, |
---|
84 | |
---|
85 | _getSiblingOfChild: function(/*dijit._Widget*/ child, /*int*/ dir){ |
---|
86 | // summary: |
---|
87 | // Get the next or previous widget sibling of child |
---|
88 | // dir: |
---|
89 | // if 1, get the next sibling |
---|
90 | // if -1, get the previous sibling |
---|
91 | // tags: |
---|
92 | // private |
---|
93 | var node = child.domNode, |
---|
94 | which = (dir>0 ? "nextSibling" : "previousSibling"); |
---|
95 | do{ |
---|
96 | node = node[which]; |
---|
97 | }while(node && (node.nodeType != 1 || !registry.byNode(node))); |
---|
98 | return node && registry.byNode(node); // dijit._Widget |
---|
99 | }, |
---|
100 | |
---|
101 | getIndexOfChild: function(/*dijit._Widget*/ child){ |
---|
102 | // summary: |
---|
103 | // Gets the index of the child in this container or -1 if not found |
---|
104 | return array.indexOf(this.getChildren(), child); // int |
---|
105 | } |
---|
106 | }); |
---|
107 | }); |
---|