| 1 | define([ |
| 2 | "dojo/_base/array", // array.filter array.forEach |
| 3 | "dojo/_base/declare", // declare |
| 4 | "dojo/dom-class", // domClass.contains domClass.toggle |
| 5 | "dojo/dom-geometry", // domGeometry.contentBox domGeometry.marginBox |
| 6 | "dojo/dom-style", |
| 7 | "dojo/_base/lang", // lang.mixin |
| 8 | "dojo/query", // query |
| 9 | "../registry", // registry.byId |
| 10 | "../Viewport", |
| 11 | "./utils" // marginBox2contextBox |
| 12 | ], function(array, declare, domClass, domGeometry, domStyle, lang, query, |
| 13 | registry, Viewport, layoutUtils){ |
| 14 | |
| 15 | // module: |
| 16 | // dijit/layout/_ContentPaneResizeMixin |
| 17 | |
| 18 | return declare("dijit.layout._ContentPaneResizeMixin", null, { |
| 19 | // summary: |
| 20 | // Resize() functionality of ContentPane. If there's a single layout widget |
| 21 | // child then it will call resize() with the same dimensions as the ContentPane. |
| 22 | // Otherwise just calls resize on each child. |
| 23 | // |
| 24 | // Also implements basic startup() functionality, where starting the parent |
| 25 | // will start the children |
| 26 | |
| 27 | // doLayout: Boolean |
| 28 | // - false - don't adjust size of children |
| 29 | // - true - if there is a single visible child widget, set it's size to however big the ContentPane is |
| 30 | doLayout: true, |
| 31 | |
| 32 | // isLayoutContainer: [protected] Boolean |
| 33 | // Indicates that this widget will call resize() on it's child widgets |
| 34 | // when they become visible. |
| 35 | isLayoutContainer: true, |
| 36 | |
| 37 | startup: function(){ |
| 38 | // summary: |
| 39 | // See `dijit/layout/_LayoutWidget.startup()` for description. |
| 40 | // Although ContentPane doesn't extend _LayoutWidget, it does implement |
| 41 | // the same API. |
| 42 | |
| 43 | if(this._started){ |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | var parent = this.getParent(); |
| 48 | this._childOfLayoutWidget = parent && parent.isLayoutContainer; |
| 49 | |
| 50 | // I need to call resize() on my child/children (when I become visible), unless |
| 51 | // I'm the child of a layout widget in which case my parent will call resize() on me and I'll do it then. |
| 52 | this._needLayout = !this._childOfLayoutWidget; |
| 53 | |
| 54 | this.inherited(arguments); |
| 55 | |
| 56 | if(this._isShown()){ |
| 57 | this._onShow(); |
| 58 | } |
| 59 | |
| 60 | if(!this._childOfLayoutWidget){ |
| 61 | // Since my parent isn't a layout container, and my style *may be* width=height=100% |
| 62 | // or something similar (either set directly or via a CSS class), |
| 63 | // monitor when viewport size changes so that I can re-layout. |
| 64 | // This is more for subclasses of ContentPane than ContentPane itself, although it |
| 65 | // could be useful for a ContentPane if it has a single child widget inheriting ContentPane's size. |
| 66 | this.own(Viewport.on("resize", lang.hitch(this, "resize"))); |
| 67 | } |
| 68 | }, |
| 69 | |
| 70 | _checkIfSingleChild: function(){ |
| 71 | // summary: |
| 72 | // Test if we have exactly one visible widget as a child, |
| 73 | // and if so assume that we are a container for that widget, |
| 74 | // and should propagate startup() and resize() calls to it. |
| 75 | // Skips over things like data stores since they aren't visible. |
| 76 | |
| 77 | if(!this.doLayout){ return; } |
| 78 | |
| 79 | var candidateWidgets = [], |
| 80 | otherVisibleNodes = false; |
| 81 | |
| 82 | query("> *", this.containerNode).some(function(node){ |
| 83 | var widget = registry.byNode(node); |
| 84 | if(widget && widget.resize){ |
| 85 | candidateWidgets.push(widget); |
| 86 | }else if(!/script|link|style/i.test(node.nodeName) && node.offsetHeight){ |
| 87 | otherVisibleNodes = true; |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | this._singleChild = candidateWidgets.length == 1 && !otherVisibleNodes ? |
| 92 | candidateWidgets[0] : null; |
| 93 | |
| 94 | // So we can set overflow: hidden to avoid a safari bug w/scrollbars showing up (#9449) |
| 95 | domClass.toggle(this.containerNode, this.baseClass + "SingleChild", !!this._singleChild); |
| 96 | }, |
| 97 | |
| 98 | resize: function(changeSize, resultSize){ |
| 99 | // summary: |
| 100 | // See `dijit/layout/_LayoutWidget.resize()` for description. |
| 101 | // Although ContentPane doesn't extend _LayoutWidget, it does implement |
| 102 | // the same API. |
| 103 | |
| 104 | this._resizeCalled = true; |
| 105 | |
| 106 | this._scheduleLayout(changeSize, resultSize); |
| 107 | }, |
| 108 | |
| 109 | _scheduleLayout: function(changeSize, resultSize){ |
| 110 | // summary: |
| 111 | // Resize myself, and call resize() on each of my child layout widgets, either now |
| 112 | // (if I'm currently visible) or when I become visible |
| 113 | if(this._isShown()){ |
| 114 | this._layout(changeSize, resultSize); |
| 115 | }else{ |
| 116 | this._needLayout = true; |
| 117 | this._changeSize = changeSize; |
| 118 | this._resultSize = resultSize; |
| 119 | } |
| 120 | }, |
| 121 | |
| 122 | _layout: function(changeSize, resultSize){ |
| 123 | // summary: |
| 124 | // Resize myself according to optional changeSize/resultSize parameters, like a layout widget. |
| 125 | // Also, since I am an isLayoutContainer widget, each of my children expects me to |
| 126 | // call resize() or layout() on it. |
| 127 | // |
| 128 | // Should be called on initialization and also whenever we get new content |
| 129 | // (from an href, or from set('content', ...))... but deferred until |
| 130 | // the ContentPane is visible |
| 131 | |
| 132 | delete this._needLayout; |
| 133 | |
| 134 | // For the TabContainer --> BorderContainer --> ContentPane case, _onShow() is |
| 135 | // never called directly, so resize() is our trigger to do the initial href download (see [20099]). |
| 136 | // However, don't load href for closed TitlePanes. |
| 137 | if(!this._wasShown && this.open !== false){ |
| 138 | this._onShow(); |
| 139 | } |
| 140 | |
| 141 | // Set margin box size, unless it wasn't specified, in which case use current size. |
| 142 | if(changeSize){ |
| 143 | domGeometry.setMarginBox(this.domNode, changeSize); |
| 144 | } |
| 145 | |
| 146 | // Compute content box size of containerNode in case we [later] need to size our single child. |
| 147 | var cn = this.containerNode; |
| 148 | if(cn === this.domNode){ |
| 149 | // If changeSize or resultSize was passed to this method and this.containerNode == |
| 150 | // this.domNode then we can compute the content-box size without querying the node, |
| 151 | // which is more reliable (similar to LayoutWidget.resize) (see for example #9449). |
| 152 | var mb = resultSize || {}; |
| 153 | lang.mixin(mb, changeSize || {}); // changeSize overrides resultSize |
| 154 | if(!("h" in mb) || !("w" in mb)){ |
| 155 | mb = lang.mixin(domGeometry.getMarginBox(cn), mb); // just use domGeometry.setMarginBox() to fill in missing values |
| 156 | } |
| 157 | this._contentBox = layoutUtils.marginBox2contentBox(cn, mb); |
| 158 | }else{ |
| 159 | this._contentBox = domGeometry.getContentBox(cn); |
| 160 | } |
| 161 | |
| 162 | this._layoutChildren(); |
| 163 | }, |
| 164 | |
| 165 | _layoutChildren: function(){ |
| 166 | // Call _checkIfSingleChild() again in case app has manually mucked w/the content |
| 167 | // of the ContentPane (rather than changing it through the set("content", ...) API. |
| 168 | this._checkIfSingleChild(); |
| 169 | |
| 170 | if(this._singleChild && this._singleChild.resize){ |
| 171 | var cb = this._contentBox || domGeometry.getContentBox(this.containerNode); |
| 172 | |
| 173 | // note: if widget has padding this._contentBox will have l and t set, |
| 174 | // but don't pass them to resize() or it will doubly-offset the child |
| 175 | this._singleChild.resize({w: cb.w, h: cb.h}); |
| 176 | }else{ |
| 177 | // All my child widgets are independently sized (rather than matching my size), |
| 178 | // but I still need to call resize() on each child to make it layout. |
| 179 | var children = this.getChildren(), |
| 180 | widget, |
| 181 | i = 0; |
| 182 | while(widget = children[i++]){ |
| 183 | if(widget.resize){ |
| 184 | widget.resize(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | }, |
| 189 | |
| 190 | _isShown: function(){ |
| 191 | // summary: |
| 192 | // Returns true if the content is currently shown. |
| 193 | // description: |
| 194 | // If I am a child of a layout widget then it actually returns true if I've ever been visible, |
| 195 | // not whether I'm currently visible, since that's much faster than tracing up the DOM/widget |
| 196 | // tree every call, and at least solves the performance problem on page load by deferring loading |
| 197 | // hidden ContentPanes until they are first shown |
| 198 | |
| 199 | if(this._childOfLayoutWidget){ |
| 200 | // If we are TitlePane, etc - we return that only *IF* we've been resized |
| 201 | if(this._resizeCalled && "open" in this){ |
| 202 | return this.open; |
| 203 | } |
| 204 | return this._resizeCalled; |
| 205 | }else if("open" in this){ |
| 206 | return this.open; // for TitlePane, etc. |
| 207 | }else{ |
| 208 | var node = this.domNode, parent = this.domNode.parentNode; |
| 209 | return (node.style.display != 'none') && (node.style.visibility != 'hidden') && !domClass.contains(node, "dijitHidden") && |
| 210 | parent && parent.style && (parent.style.display != 'none'); |
| 211 | } |
| 212 | }, |
| 213 | |
| 214 | _onShow: function(){ |
| 215 | // summary: |
| 216 | // Called when the ContentPane is made visible |
| 217 | // description: |
| 218 | // For a plain ContentPane, this is called on initialization, from startup(). |
| 219 | // If the ContentPane is a hidden pane of a TabContainer etc., then it's |
| 220 | // called whenever the pane is made visible. |
| 221 | // |
| 222 | // Does layout/resize of child widget(s) |
| 223 | |
| 224 | // Need to keep track of whether ContentPane has been shown (which is different than |
| 225 | // whether or not it's currently visible). |
| 226 | this._wasShown = true; |
| 227 | |
| 228 | if(this._needLayout){ |
| 229 | // If a layout has been scheduled for when we become visible, do it now |
| 230 | this._layout(this._changeSize, this._resultSize); |
| 231 | } |
| 232 | |
| 233 | this.inherited(arguments); |
| 234 | } |
| 235 | }); |
| 236 | }); |