[483] | 1 | define(["dojo/_base/declare", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/window", |
---|
| 2 | "dojo/query", "dojo/dom-geometry", "dojo/dom-attr", "dojo/dom-style", "dijit/registry", |
---|
| 3 | "./LayoutBase", "../utils/layout", "../utils/constraints"], |
---|
| 4 | function(declare, lang, array, win, query, domGeom, domAttr, domStyle, registry, LayoutBase, layout, constraints){ |
---|
| 5 | // module: |
---|
| 6 | // dojox/app/controllers/Layout |
---|
| 7 | // summary: |
---|
| 8 | // Extends LayoutBase which binds "app-initLayout", "app-layoutView" and "app-resize" events on application instance. |
---|
| 9 | |
---|
| 10 | return declare("dojox.app.controllers.Layout", LayoutBase, { |
---|
| 11 | |
---|
| 12 | constructor: function(app, events){ |
---|
| 13 | // summary: |
---|
| 14 | // bind "app-initLayout" and "app-layoutView" events on application instance. |
---|
| 15 | // |
---|
| 16 | // app: |
---|
| 17 | // dojox/app application instance. |
---|
| 18 | // events: |
---|
| 19 | // {event : handler} |
---|
| 20 | }, |
---|
| 21 | |
---|
| 22 | onResize: function(){ |
---|
| 23 | this._doResize(this.app); |
---|
| 24 | // this is needed to resize the children on an orientation change or a resize of the browser. |
---|
| 25 | // it was being done in _doResize, but was not needed for every call to _doResize. |
---|
| 26 | this.resizeSelectedChildren(this.app); |
---|
| 27 | }, |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | resizeSelectedChildren: function(w){ |
---|
| 31 | for(var hash in w.selectedChildren){ // need this to handle all selectedChildren |
---|
| 32 | if(w.selectedChildren[hash] && w.selectedChildren[hash].domNode){ |
---|
| 33 | this.app.log("in Layout resizeSelectedChildren calling resizeSelectedChildren calling _doResize for w.selectedChildren[hash].id="+w.selectedChildren[hash].id); |
---|
| 34 | this._doResize(w.selectedChildren[hash]); |
---|
| 35 | // Call resize on child widgets, needed to get the scrollableView to resize correctly initially |
---|
| 36 | array.forEach(w.selectedChildren[hash].domNode.children, function(child){ |
---|
| 37 | if(registry.byId(child.id) && registry.byId(child.id).resize){ |
---|
| 38 | registry.byId(child.id).resize(); |
---|
| 39 | } |
---|
| 40 | }); |
---|
| 41 | |
---|
| 42 | this.resizeSelectedChildren(w.selectedChildren[hash]); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | }, |
---|
| 46 | |
---|
| 47 | initLayout: function(event){ |
---|
| 48 | // summary: |
---|
| 49 | // Response to dojox/app "app-initLayout" event. |
---|
| 50 | // |
---|
| 51 | // example: |
---|
| 52 | // Use emit to trigger "app-initLayout" event, and this function will respond to the event. For example: |
---|
| 53 | // | this.app.emit("app-initLayout", view); |
---|
| 54 | // |
---|
| 55 | // event: Object |
---|
| 56 | // | {"view": view, "callback": function(){}}; |
---|
| 57 | this.app.log("in app/controllers/Layout.initLayout event=",event); |
---|
| 58 | this.app.log("in app/controllers/Layout.initLayout event.view.parent.name=[",event.view.parent.name,"]"); |
---|
| 59 | |
---|
| 60 | if (!event.view.domNode.parentNode) { |
---|
| 61 | event.view.parent.domNode.appendChild(event.view.domNode); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | domAttr.set(event.view.domNode, "data-app-constraint", event.view.constraint); |
---|
| 65 | |
---|
| 66 | this.inherited(arguments); |
---|
| 67 | }, |
---|
| 68 | |
---|
| 69 | _doResize: function(view){ |
---|
| 70 | // summary: |
---|
| 71 | // resize view. |
---|
| 72 | // |
---|
| 73 | // view: Object |
---|
| 74 | // view instance needs to do layout. |
---|
| 75 | var node = view.domNode; |
---|
| 76 | if(!node){ |
---|
| 77 | this.app.log("Warning - View has not been loaded, in Layout _doResize view.domNode is not set for view.id="+view.id+" view=",view); |
---|
| 78 | return; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // If either height or width wasn't specified by the user, then query node for it. |
---|
| 82 | // But note that setting the margin box and then immediately querying dimensions may return |
---|
| 83 | // inaccurate results, so try not to depend on it. |
---|
| 84 | var mb = {}; |
---|
| 85 | if( !("h" in mb) || !("w" in mb) ){ |
---|
| 86 | mb = lang.mixin(domGeom.getMarginBox(node), mb); // just use dojo/_base/html.marginBox() to fill in missing values |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | // Compute and save the size of my border box and content box |
---|
| 90 | // (w/out calling dojo/_base/html.contentBox() since that may fail if size was recently set) |
---|
| 91 | if(view !== this.app){ |
---|
| 92 | var cs = domStyle.getComputedStyle(node); |
---|
| 93 | var me = domGeom.getMarginExtents(node, cs); |
---|
| 94 | var be = domGeom.getBorderExtents(node, cs); |
---|
| 95 | var bb = (view._borderBox = { |
---|
| 96 | w: mb.w - (me.w + be.w), |
---|
| 97 | h: mb.h - (me.h + be.h) |
---|
| 98 | }); |
---|
| 99 | var pe = domGeom.getPadExtents(node, cs); |
---|
| 100 | view._contentBox = { |
---|
| 101 | l: domStyle.toPixelValue(node, cs.paddingLeft), |
---|
| 102 | t: domStyle.toPixelValue(node, cs.paddingTop), |
---|
| 103 | w: bb.w - pe.w, |
---|
| 104 | h: bb.h - pe.h |
---|
| 105 | }; |
---|
| 106 | }else{ |
---|
| 107 | // if we are layouting the top level app the above code does not work when hiding address bar |
---|
| 108 | // so let's use similar code to dojo mobile. |
---|
| 109 | view._contentBox = { |
---|
| 110 | l: 0, |
---|
| 111 | t: 0, |
---|
| 112 | h: win.global.innerHeight || win.doc.documentElement.clientHeight, |
---|
| 113 | w: win.global.innerWidth || win.doc.documentElement.clientWidth |
---|
| 114 | }; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | this.inherited(arguments); |
---|
| 118 | }, |
---|
| 119 | |
---|
| 120 | layoutView: function(event){ |
---|
| 121 | // summary: |
---|
| 122 | // Response to dojox/app "app-layoutView" event. |
---|
| 123 | // |
---|
| 124 | // example: |
---|
| 125 | // Use emit to trigger "app-layoutView" event, and this function will response the event. For example: |
---|
| 126 | // | this.app.emit("app-layoutView", view); |
---|
| 127 | // |
---|
| 128 | // event: Object |
---|
| 129 | // | {"parent":parent, "view":view, "removeView": boolean} |
---|
| 130 | if(event.view){ |
---|
| 131 | this.inherited(arguments); |
---|
| 132 | // normally when called from transition doResize will be false, and the resize will only be done when the app-resize event is fired |
---|
| 133 | if(event.doResize){ |
---|
| 134 | this._doResize(event.parent || this.app); |
---|
| 135 | this._doResize(event.view); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | }, |
---|
| 139 | |
---|
| 140 | _doLayout: function(view){ |
---|
| 141 | // summary: |
---|
| 142 | // do view layout. |
---|
| 143 | // |
---|
| 144 | // view: Object |
---|
| 145 | // view instance needs to do layout. |
---|
| 146 | |
---|
| 147 | if(!view){ |
---|
| 148 | console.warn("layout empty view."); |
---|
| 149 | return; |
---|
| 150 | } |
---|
| 151 | this.app.log("in Layout _doLayout called for view.id="+view.id+" view=",view); |
---|
| 152 | |
---|
| 153 | var children; |
---|
| 154 | // TODO: probably need to handle selectedChildren here, not just selected child... |
---|
| 155 | // TODO: why are we passing view here? not parent? This call does not seem logical? |
---|
| 156 | var selectedChild = constraints.getSelectedChild(view, view.constraint); |
---|
| 157 | if(selectedChild && selectedChild.isFullScreen){ |
---|
| 158 | console.warn("fullscreen sceen layout"); |
---|
| 159 | /* |
---|
| 160 | fullScreenScene=true; |
---|
| 161 | children=[{domNode: selectedChild.domNode,constraint: "center"}]; |
---|
| 162 | query("> [constraint]",this.domNode).forEach(function(c){ |
---|
| 163 | if(selectedChild.domNode!==c.domNode){ |
---|
| 164 | dstyle(c.domNode,"display","none"); |
---|
| 165 | } |
---|
| 166 | }) |
---|
| 167 | */ |
---|
| 168 | }else{ |
---|
| 169 | children = query("> [data-app-constraint]", view.domNode).map(function(node){ |
---|
| 170 | var w = registry.getEnclosingWidget(node); |
---|
| 171 | if(w){ |
---|
| 172 | w._constraint = domAttr.get(node, "data-app-constraint"); |
---|
| 173 | return w; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return { |
---|
| 177 | domNode: node, |
---|
| 178 | _constraint: domAttr.get(node, "data-app-constraint") |
---|
| 179 | }; |
---|
| 180 | }); |
---|
| 181 | |
---|
| 182 | if(selectedChild){ |
---|
| 183 | children = array.filter(children, function(c){ |
---|
| 184 | // do not need to set display none here it is set in select. |
---|
| 185 | return c.domNode && c._constraint; |
---|
| 186 | }, view); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | // We don't need to layout children if this._contentBox is null for the operation will do nothing. |
---|
| 190 | if(view._contentBox){ |
---|
| 191 | layout.layoutChildren(view.domNode, view._contentBox, children); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | }); |
---|
| 195 | }); |
---|