_Container.js 3.5 KB

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