| 1 | define([ |
| 2 | "dojo/_base/array", |
| 3 | "dojo/_base/declare", // declare |
| 4 | "dojo/dom-class", |
| 5 | "dojo/dom-style", |
| 6 | "dojo/_base/lang", |
| 7 | "../_WidgetBase", |
| 8 | "./_LayoutWidget", |
| 9 | "./utils" // layoutUtils.layoutChildren |
| 10 | ], function(array, declare, domClass, domStyle, lang, _WidgetBase, _LayoutWidget, layoutUtils){ |
| 11 | |
| 12 | // module: |
| 13 | // dijit/layout/LayoutContainer |
| 14 | |
| 15 | var LayoutContainer = declare("dijit.layout.LayoutContainer", _LayoutWidget, { |
| 16 | // summary: |
| 17 | // A LayoutContainer is a box with a specified size, such as style="width: 500px; height: 500px;", |
| 18 | // that contains a child widget marked region="center" and optionally children widgets marked |
| 19 | // region equal to "top", "bottom", "leading", "trailing", "left" or "right". |
| 20 | // Children along the edges will be laid out according to width or height dimensions. The remaining |
| 21 | // space is designated for the center region. |
| 22 | // |
| 23 | // The outer size must be specified on the LayoutContainer node. Width must be specified for the sides |
| 24 | // and height for the top and bottom, respectively. No dimensions should be specified on the center; |
| 25 | // it will fill the remaining space. Regions named "leading" and "trailing" may be used just like |
| 26 | // "left" and "right" except that they will be reversed in right-to-left environments. |
| 27 | // |
| 28 | // For complex layouts, multiple children can be specified for a single region. In this case, the |
| 29 | // layoutPriority flag on the children determines which child is closer to the edge (low layoutPriority) |
| 30 | // and which child is closer to the center (high layoutPriority). layoutPriority can also be used |
| 31 | // instead of the design attribute to control layout precedence of horizontal vs. vertical panes. |
| 32 | // |
| 33 | // See `LayoutContainer.ChildWidgetProperties` for details on the properties that can be set on |
| 34 | // children of a `LayoutContainer`. |
| 35 | // |
| 36 | // If layoutPriority is not set, lays out each child in the natural order the children occur in. |
| 37 | // Basically each child is laid out into the "remaining space", where "remaining space" is initially |
| 38 | // the content area of this widget, but is reduced to a smaller rectangle each time a child is added. |
| 39 | |
| 40 | // design: String |
| 41 | // Which design is used for the layout: |
| 42 | // |
| 43 | // - "headline" (default) where the top and bottom extend the full width of the container |
| 44 | // - "sidebar" where the left and right sides extend from top to bottom. |
| 45 | // |
| 46 | // However, a `layoutPriority` setting on child panes overrides the `design` attribute on the parent. |
| 47 | // In other words, if the top and bottom sections have a lower `layoutPriority` than the left and right |
| 48 | // panes, the top and bottom panes will extend the entire width of the box. |
| 49 | design: "headline", |
| 50 | |
| 51 | baseClass: "dijitLayoutContainer", |
| 52 | |
| 53 | startup: function(){ |
| 54 | if(this._started){ |
| 55 | return; |
| 56 | } |
| 57 | array.forEach(this.getChildren(), this._setupChild, this); |
| 58 | this.inherited(arguments); |
| 59 | }, |
| 60 | |
| 61 | _setupChild: function(/*dijit/_WidgetBase*/ child){ |
| 62 | // Override _LayoutWidget._setupChild(). |
| 63 | |
| 64 | this.inherited(arguments); |
| 65 | |
| 66 | var region = child.region; |
| 67 | if(region){ |
| 68 | domClass.add(child.domNode, this.baseClass + "Pane"); |
| 69 | } |
| 70 | }, |
| 71 | |
| 72 | _getOrderedChildren: function(){ |
| 73 | // summary: |
| 74 | // Return list of my children in the order that I want layoutChildren() |
| 75 | // to process them (i.e. from the outside to the inside) |
| 76 | |
| 77 | var wrappers = array.map(this.getChildren(), function(child, idx){ |
| 78 | return { |
| 79 | pane: child, |
| 80 | weight: [ |
| 81 | child.region == "center" ? Infinity : 0, |
| 82 | child.layoutPriority, |
| 83 | (this.design == "sidebar" ? 1 : -1) * (/top|bottom/.test(child.region) ? 1 : -1), |
| 84 | idx |
| 85 | ] |
| 86 | }; |
| 87 | }, this); |
| 88 | wrappers.sort(function(a, b){ |
| 89 | var aw = a.weight, bw = b.weight; |
| 90 | for(var i = 0; i < aw.length; i++){ |
| 91 | if(aw[i] != bw[i]){ |
| 92 | return aw[i] - bw[i]; |
| 93 | } |
| 94 | } |
| 95 | return 0; |
| 96 | }); |
| 97 | |
| 98 | return array.map(wrappers, function(w){ return w.pane; }); |
| 99 | }, |
| 100 | |
| 101 | layout: function(){ |
| 102 | layoutUtils.layoutChildren(this.domNode, this._contentBox, this._getOrderedChildren()); |
| 103 | }, |
| 104 | |
| 105 | addChild: function(/*dijit/_WidgetBase*/ child, /*Integer?*/ insertIndex){ |
| 106 | this.inherited(arguments); |
| 107 | if(this._started){ |
| 108 | this.layout(); |
| 109 | } |
| 110 | }, |
| 111 | |
| 112 | removeChild: function(/*dijit/_WidgetBase*/ child){ |
| 113 | this.inherited(arguments); |
| 114 | if(this._started){ |
| 115 | this.layout(); |
| 116 | } |
| 117 | |
| 118 | // Clean up whatever style changes we made to the child pane. |
| 119 | // Unclear how height and width should be handled. |
| 120 | domClass.remove(child.domNode, this.baseClass + "Pane"); |
| 121 | domStyle.set(child.domNode, { |
| 122 | top: "auto", |
| 123 | bottom: "auto", |
| 124 | left: "auto", |
| 125 | right: "auto", |
| 126 | position: "static" |
| 127 | }); |
| 128 | domStyle.set(child.domNode, /top|bottom/.test(child.region) ? "width" : "height", "auto"); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | LayoutContainer.ChildWidgetProperties = { |
| 133 | // summary: |
| 134 | // These properties can be specified for the children of a LayoutContainer. |
| 135 | |
| 136 | // region: [const] String |
| 137 | // Values: "top", "bottom", "leading", "trailing", "left", "right", "center". |
| 138 | // See the `dijit/layout/LayoutContainer` description for details. |
| 139 | region: '', |
| 140 | |
| 141 | // layoutAlign: [const deprecated] String |
| 142 | // Synonym for region, except using "client" instead of "center". Deprecated; use region instead. |
| 143 | layoutAlign: '', |
| 144 | |
| 145 | // layoutPriority: [const] Number |
| 146 | // Children with a higher layoutPriority will be placed closer to the LayoutContainer center, |
| 147 | // between children with a lower layoutPriority. |
| 148 | layoutPriority: 0 |
| 149 | }; |
| 150 | |
| 151 | // Since any widget can be specified as a LayoutContainer child, mix it |
| 152 | // into the base widget class. (This is a hack, but it's effective.) |
| 153 | // This is for the benefit of the parser. Remove for 2.0. Also, hide from doc viewer. |
| 154 | lang.extend(_WidgetBase, /*===== {} || =====*/ LayoutContainer.ChildWidgetProperties); |
| 155 | |
| 156 | return LayoutContainer; |
| 157 | }); |