| 1 | define([ |
| 2 | "dojo/_base/array", // array.forEach array.indexOf |
| 3 | "dojo/_base/declare", // declare |
| 4 | "dojo/dom-construct", // domConstruct.place |
| 5 | "dojo/_base/kernel" // kernel.deprecated |
| 6 | ], function(array, declare, domConstruct, kernel){ |
| 7 | |
| 8 | // module: |
| 9 | // dijit/_Container |
| 10 | |
| 11 | return declare("dijit._Container", null, { |
| 12 | // summary: |
| 13 | // Mixin for widgets that contain HTML and/or a set of widget children. |
| 14 | |
| 15 | buildRendering: function(){ |
| 16 | this.inherited(arguments); |
| 17 | if(!this.containerNode){ |
| 18 | // All widgets with descendants must set containerNode. |
| 19 | // NB: this code doesn't quite work right because for TabContainer it runs before |
| 20 | // _TemplatedMixin::buildRendering(), and thus |
| 21 | // sets this.containerNode to this.domNode, later to be overridden by the assignment in the template. |
| 22 | this.containerNode = this.domNode; |
| 23 | } |
| 24 | }, |
| 25 | |
| 26 | addChild: function(/*dijit/_WidgetBase*/ widget, /*int?*/ insertIndex){ |
| 27 | // summary: |
| 28 | // Makes the given widget a child of this widget. |
| 29 | // description: |
| 30 | // Inserts specified child widget's dom node as a child of this widget's |
| 31 | // container node, and possibly does other processing (such as layout). |
| 32 | |
| 33 | // I want to just call domConstruct.place(widget.domNode, this.containerNode, insertIndex), but the counting |
| 34 | // is thrown off by text nodes and comment nodes that show up when constructed by markup. |
| 35 | // In the future consider stripping those nodes on construction, either in the parser or this widget code. |
| 36 | var refNode = this.containerNode; |
| 37 | if(insertIndex > 0){ |
| 38 | // Old-school way to get nth child; dojo.query would be easier but _Container was weened from dojo.query |
| 39 | // in #10087 to minimize download size. Not sure if that's still and issue with new smaller dojo/query. |
| 40 | refNode = refNode.firstChild; |
| 41 | while(insertIndex > 0){ |
| 42 | if(refNode.nodeType == 1){ insertIndex--; } |
| 43 | refNode = refNode.nextSibling; |
| 44 | } |
| 45 | if(refNode){ |
| 46 | insertIndex = "before"; |
| 47 | }else{ |
| 48 | // to support addChild(child, n-1) where there are n children (should add child at end) |
| 49 | refNode = this.containerNode; |
| 50 | insertIndex = "last"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | domConstruct.place(widget.domNode, refNode, insertIndex); |
| 55 | |
| 56 | // If I've been started but the child widget hasn't been started, |
| 57 | // start it now. Make sure to do this after widget has been |
| 58 | // inserted into the DOM tree, so it can see that it's being controlled by me, |
| 59 | // so it doesn't try to size itself. |
| 60 | if(this._started && !widget._started){ |
| 61 | widget.startup(); |
| 62 | } |
| 63 | }, |
| 64 | |
| 65 | removeChild: function(/*Widget|int*/ widget){ |
| 66 | // summary: |
| 67 | // Removes the passed widget instance from this widget but does |
| 68 | // not destroy it. You can also pass in an integer indicating |
| 69 | // the index within the container to remove (ie, removeChild(5) removes the sixth widget). |
| 70 | |
| 71 | if(typeof widget == "number"){ |
| 72 | widget = this.getChildren()[widget]; |
| 73 | } |
| 74 | |
| 75 | if(widget){ |
| 76 | var node = widget.domNode; |
| 77 | if(node && node.parentNode){ |
| 78 | node.parentNode.removeChild(node); // detach but don't destroy |
| 79 | } |
| 80 | } |
| 81 | }, |
| 82 | |
| 83 | hasChildren: function(){ |
| 84 | // summary: |
| 85 | // Returns true if widget has child widgets, i.e. if this.containerNode contains widgets. |
| 86 | return this.getChildren().length > 0; // Boolean |
| 87 | }, |
| 88 | |
| 89 | _getSiblingOfChild: function(/*dijit/_WidgetBase*/ child, /*int*/ dir){ |
| 90 | // summary: |
| 91 | // Get the next or previous widget sibling of child |
| 92 | // dir: |
| 93 | // if 1, get the next sibling |
| 94 | // if -1, get the previous sibling |
| 95 | // tags: |
| 96 | // private |
| 97 | var children = this.getChildren(), |
| 98 | idx = array.indexOf(children, child); // int |
| 99 | return children[idx + dir]; |
| 100 | }, |
| 101 | |
| 102 | getIndexOfChild: function(/*dijit/_WidgetBase*/ child){ |
| 103 | // summary: |
| 104 | // Gets the index of the child in this container or -1 if not found |
| 105 | return array.indexOf(this.getChildren(), child); // int |
| 106 | } |
| 107 | }); |
| 108 | }); |