| 1 | define([ |
| 2 | "dojo/_base/lang", // lang.mixin |
| 3 | "../_Widget", |
| 4 | "../_Container", |
| 5 | "../_Contained", |
| 6 | "../Viewport", |
| 7 | "dojo/_base/declare", // declare |
| 8 | "dojo/dom-class", // domClass.add domClass.remove |
| 9 | "dojo/dom-geometry", // domGeometry.marginBox |
| 10 | "dojo/dom-style" // domStyle.getComputedStyle |
| 11 | ], function(lang, _Widget, _Container, _Contained, Viewport, |
| 12 | declare, domClass, domGeometry, domStyle){ |
| 13 | |
| 14 | // module: |
| 15 | // dijit/layout/_LayoutWidget |
| 16 | |
| 17 | |
| 18 | return declare("dijit.layout._LayoutWidget", [_Widget, _Container, _Contained], { |
| 19 | // summary: |
| 20 | // Base class for a _Container widget which is responsible for laying out its children. |
| 21 | // Widgets which mixin this code must define layout() to manage placement and sizing of the children. |
| 22 | |
| 23 | // baseClass: [protected extension] String |
| 24 | // This class name is applied to the widget's domNode |
| 25 | // and also may be used to generate names for sub nodes, |
| 26 | // for example dijitTabContainer-content. |
| 27 | baseClass: "dijitLayoutContainer", |
| 28 | |
| 29 | // isLayoutContainer: [protected] Boolean |
| 30 | // Indicates that this widget is going to call resize() on its |
| 31 | // children widgets, setting their size, when they become visible. |
| 32 | isLayoutContainer: true, |
| 33 | |
| 34 | // Cancel _WidgetBase's _setTitleAttr because we don't want the title property (used to specify |
| 35 | // tab labels) to be set as an attribute on this.domNode... otherwise a tooltip shows up over the |
| 36 | // entire widget. |
| 37 | _setTitleAttr: null, |
| 38 | |
| 39 | buildRendering: function(){ |
| 40 | this.inherited(arguments); |
| 41 | domClass.add(this.domNode, "dijitContainer"); |
| 42 | }, |
| 43 | |
| 44 | startup: function(){ |
| 45 | // summary: |
| 46 | // Called after all the widgets have been instantiated and their |
| 47 | // dom nodes have been inserted somewhere under <body>. |
| 48 | // |
| 49 | // Widgets should override this method to do any initialization |
| 50 | // dependent on other widgets existing, and then call |
| 51 | // this superclass method to finish things off. |
| 52 | // |
| 53 | // startup() in subclasses shouldn't do anything |
| 54 | // size related because the size of the widget hasn't been set yet. |
| 55 | |
| 56 | if(this._started){ return; } |
| 57 | |
| 58 | // Need to call inherited first - so that child widgets get started |
| 59 | // up correctly |
| 60 | this.inherited(arguments); |
| 61 | |
| 62 | // If I am a not being controlled by a parent layout widget... |
| 63 | var parent = this.getParent && this.getParent(); |
| 64 | if(!(parent && parent.isLayoutContainer)){ |
| 65 | // Do recursive sizing and layout of all my descendants |
| 66 | // (passing in no argument to resize means that it has to glean the size itself) |
| 67 | this.resize(); |
| 68 | |
| 69 | // Since my parent isn't a layout container, and my style *may be* width=height=100% |
| 70 | // or something similar (either set directly or via a CSS class), |
| 71 | // monitor when viewport size changes so that I can re-layout. |
| 72 | this.own(Viewport.on("resize", lang.hitch(this, "resize"))); |
| 73 | } |
| 74 | }, |
| 75 | |
| 76 | resize: function(changeSize, resultSize){ |
| 77 | // summary: |
| 78 | // Call this to resize a widget, or after its size has changed. |
| 79 | // description: |
| 80 | // ####Change size mode: |
| 81 | // |
| 82 | // When changeSize is specified, changes the marginBox of this widget |
| 83 | // and forces it to re-layout its contents accordingly. |
| 84 | // changeSize may specify height, width, or both. |
| 85 | // |
| 86 | // If resultSize is specified it indicates the size the widget will |
| 87 | // become after changeSize has been applied. |
| 88 | // |
| 89 | // ####Notification mode: |
| 90 | // |
| 91 | // When changeSize is null, indicates that the caller has already changed |
| 92 | // the size of the widget, or perhaps it changed because the browser |
| 93 | // window was resized. Tells widget to re-layout its contents accordingly. |
| 94 | // |
| 95 | // If resultSize is also specified it indicates the size the widget has |
| 96 | // become. |
| 97 | // |
| 98 | // In either mode, this method also: |
| 99 | // |
| 100 | // 1. Sets this._borderBox and this._contentBox to the new size of |
| 101 | // the widget. Queries the current domNode size if necessary. |
| 102 | // 2. Calls layout() to resize contents (and maybe adjust child widgets). |
| 103 | // changeSize: Object? |
| 104 | // Sets the widget to this margin-box size and position. |
| 105 | // May include any/all of the following properties: |
| 106 | // | {w: int, h: int, l: int, t: int} |
| 107 | // resultSize: Object? |
| 108 | // The margin-box size of this widget after applying changeSize (if |
| 109 | // changeSize is specified). If caller knows this size and |
| 110 | // passes it in, we don't need to query the browser to get the size. |
| 111 | // | {w: int, h: int} |
| 112 | |
| 113 | var node = this.domNode; |
| 114 | |
| 115 | // set margin box size, unless it wasn't specified, in which case use current size |
| 116 | if(changeSize){ |
| 117 | domGeometry.setMarginBox(node, changeSize); |
| 118 | } |
| 119 | |
| 120 | // If either height or width wasn't specified by the user, then query node for it. |
| 121 | // But note that setting the margin box and then immediately querying dimensions may return |
| 122 | // inaccurate results, so try not to depend on it. |
| 123 | var mb = resultSize || {}; |
| 124 | lang.mixin(mb, changeSize || {}); // changeSize overrides resultSize |
| 125 | if( !("h" in mb) || !("w" in mb) ){ |
| 126 | mb = lang.mixin(domGeometry.getMarginBox(node), mb); // just use domGeometry.marginBox() to fill in missing values |
| 127 | } |
| 128 | |
| 129 | // Compute and save the size of my border box and content box |
| 130 | // (w/out calling domGeometry.getContentBox() since that may fail if size was recently set) |
| 131 | var cs = domStyle.getComputedStyle(node); |
| 132 | var me = domGeometry.getMarginExtents(node, cs); |
| 133 | var be = domGeometry.getBorderExtents(node, cs); |
| 134 | var bb = (this._borderBox = { |
| 135 | w: mb.w - (me.w + be.w), |
| 136 | h: mb.h - (me.h + be.h) |
| 137 | }); |
| 138 | var pe = domGeometry.getPadExtents(node, cs); |
| 139 | this._contentBox = { |
| 140 | l: domStyle.toPixelValue(node, cs.paddingLeft), |
| 141 | t: domStyle.toPixelValue(node, cs.paddingTop), |
| 142 | w: bb.w - pe.w, |
| 143 | h: bb.h - pe.h |
| 144 | }; |
| 145 | |
| 146 | // Callback for widget to adjust size of its children |
| 147 | this.layout(); |
| 148 | }, |
| 149 | |
| 150 | layout: function(){ |
| 151 | // summary: |
| 152 | // Widgets override this method to size and position their contents/children. |
| 153 | // When this is called this._contentBox is guaranteed to be set (see resize()). |
| 154 | // |
| 155 | // This is called after startup(), and also when the widget's size has been |
| 156 | // changed. |
| 157 | // tags: |
| 158 | // protected extension |
| 159 | }, |
| 160 | |
| 161 | _setupChild: function(/*dijit/_WidgetBase*/child){ |
| 162 | // summary: |
| 163 | // Common setup for initial children and children which are added after startup |
| 164 | // tags: |
| 165 | // protected extension |
| 166 | |
| 167 | var cls = this.baseClass + "-child " |
| 168 | + (child.baseClass ? this.baseClass + "-" + child.baseClass : ""); |
| 169 | domClass.add(child.domNode, cls); |
| 170 | }, |
| 171 | |
| 172 | addChild: function(/*dijit/_WidgetBase*/ child, /*Integer?*/ insertIndex){ |
| 173 | // Overrides _Container.addChild() to call _setupChild() |
| 174 | this.inherited(arguments); |
| 175 | if(this._started){ |
| 176 | this._setupChild(child); |
| 177 | } |
| 178 | }, |
| 179 | |
| 180 | removeChild: function(/*dijit/_WidgetBase*/ child){ |
| 181 | // Overrides _Container.removeChild() to remove class added by _setupChild() |
| 182 | var cls = this.baseClass + "-child" |
| 183 | + (child.baseClass ? |
| 184 | " " + this.baseClass + "-" + child.baseClass : ""); |
| 185 | domClass.remove(child.domNode, cls); |
| 186 | |
| 187 | this.inherited(arguments); |
| 188 | } |
| 189 | }); |
| 190 | }); |