Debian Codegraph @

dojox/app/controllers/Layout.js

raw symbols SCIP
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", "dojo/sniff"],
4 function(declare, lang, array, win, query, domGeom, domAttr, domStyle, registry, LayoutBase, layout, constraints, has){
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 || (has("ie") == 8 && !event.view.domNode.parentElement)) {
61 if(this.app.useConfigOrder){
62 event.view.parent.domNode.appendChild(event.view.domNode);
63 }else{
64 this.addViewToParentDomByConstraint(event);
65 }
66 }
67 domAttr.set(event.view.domNode, "data-app-constraint", event.view.constraint);
68 this.inherited(arguments);
69 },
70
71 addViewToParentDomByConstraint: function(event){
72 // summary:
73 // Insert the view domNode into the parent domNode based upon the constraints.
74 // It should layout the children in this order: top, left, center, right, bottom
75 // Unless it is rtl then it should layout the children in this order: top, right, center, left, bottom
76 //
77 // event: Object
78 // | {"parent":parent, "view":view, "removeView": boolean}
79 var newViewConstraint = event.view.constraint;
80 if(newViewConstraint === "bottom"){ // if new is bottom always place last
81 event.view.parent.domNode.appendChild(event.view.domNode);
82 }else if(newViewConstraint === "top"){ // if new is top always place first
83 event.view.parent.domNode.insertBefore(event.view.domNode, event.view.parent.domNode.firstChild);
84 }else{ // need to compare new constraint to the previous ones
85 if(event.view.parent.domNode.children.length > 0){ // parent node has children, check constraints
86 // in this loop if previous is top or left skip it and look for next child, otherwise process it
87 for(var childIndex in event.view.parent.domNode.children){
88 var child = event.view.parent.domNode.children[childIndex];
89 var dir = domStyle.get(event.view.parent.domNode,"direction");
90 var isltr = (dir === "ltr");
91 var LEADING_VIEW = isltr ? "left" : "right";
92 var TRAILING_VIEW = isltr ? "right" : "left";
93 if(child.getAttribute && child.getAttribute("data-app-constraint")) {
94 var previousViewConstraint = child.getAttribute("data-app-constraint");
95 // if previous is bottom or previous is Trailing
96 // or previous is not top and newView is Leading we need to insert before this child
97 if(previousViewConstraint === "bottom" ||
98 (previousViewConstraint === TRAILING_VIEW) ||
99 (previousViewConstraint !== "top" &&
100 (newViewConstraint === LEADING_VIEW))){
101 event.view.parent.domNode.insertBefore(event.view.domNode, child);
102 break;
103 }
104 }
105 }
106 }
107 }
108 // if the domNode was not added to the parent yet add it to the end now
109 if (!event.view.domNode.parentNode || (has("ie") == 8 && !event.view.domNode.parentElement)) {
110 event.view.parent.domNode.appendChild(event.view.domNode);
111 }
112 },
113
114 _doResize: function(view){
115 // summary:
116 // resize view.
117 //
118 // view: Object
119 // view instance needs to do layout.
120 var node = view.domNode;
121 if(!node){
122 this.app.log("Warning - View has not been loaded, in Layout _doResize view.domNode is not set for view.id="+view.id+" view=",view);
123 return;
124 }
125
126 // If either height or width wasn't specified by the user, then query node for it.
127 // But note that setting the margin box and then immediately querying dimensions may return
128 // inaccurate results, so try not to depend on it.
129 var mb = {};
130 if( !("h" in mb) || !("w" in mb) ){
131 mb = lang.mixin(domGeom.getMarginBox(node), mb); // just use dojo/_base/html.marginBox() to fill in missing values
132 }
133
134 // Compute and save the size of my border box and content box
135 // (w/out calling dojo/_base/html.contentBox() since that may fail if size was recently set)
136 if(view !== this.app){
137 var cs = domStyle.getComputedStyle(node);
138 var me = domGeom.getMarginExtents(node, cs);
139 var be = domGeom.getBorderExtents(node, cs);
140 var bb = (view._borderBox = {
141 w: mb.w - (me.w + be.w),
142 h: mb.h - (me.h + be.h)
143 });
144 var pe = domGeom.getPadExtents(node, cs);
145 view._contentBox = {
146 l: domStyle.toPixelValue(node, cs.paddingLeft),
147 t: domStyle.toPixelValue(node, cs.paddingTop),
148 w: bb.w - pe.w,
149 h: bb.h - pe.h
150 };
151 }else{
152 // if we are layouting the top level app the above code does not work when hiding address bar
153 // so let's use similar code to dojo mobile.
154 view._contentBox = {
155 l: 0,
156 t: 0,
157 h: win.global.innerHeight || win.doc.documentElement.clientHeight,
158 w: win.global.innerWidth || win.doc.documentElement.clientWidth
159 };
160 }
161
162 this.inherited(arguments);
163 },
164
165 layoutView: function(event){
166 // summary:
167 // Response to dojox/app "app-layoutView" event.
168 //
169 // example:
170 // Use emit to trigger "app-layoutView" event, and this function will response the event. For example:
171 // | this.app.emit("app-layoutView", view);
172 //
173 // event: Object
174 // | {"parent":parent, "view":view, "removeView": boolean}
175 if(event.view){
176 this.inherited(arguments);
177 // normally when called from transition doResize will be false, and the resize will only be done when the app-resize event is fired
178 if(event.doResize){
179 this._doResize(event.parent || this.app);
180 this._doResize(event.view);
181 }
182 }
183 },
184
185 _doLayout: function(view){
186 // summary:
187 // do view layout.
188 //
189 // view: Object
190 // view instance needs to do layout.
191
192 if(!view){
193 console.warn("layout empty view.");
194 return;
195 }
196 this.app.log("in Layout _doLayout called for view.id="+view.id+" view=",view);
197
198 var children;
199 // TODO: probably need to handle selectedChildren here, not just selected child...
200 // TODO: why are we passing view here? not parent? This call does not seem logical?
201 var selectedChild = constraints.getSelectedChild(view, view.constraint);
202 if(selectedChild && selectedChild.isFullScreen){
203 console.warn("fullscreen sceen layout");
204 /*
205 fullScreenScene=true;
206 children=[{domNode: selectedChild.domNode,constraint: "center"}];
207 query("> [constraint]",this.domNode).forEach(function(c){
208 if(selectedChild.domNode!==c.domNode){
209 dstyle(c.domNode,"display","none");
210 }
211 })
212 */
213 }else{
214 children = query("> [data-app-constraint]", view.domNode).map(function(node){
215 var w = registry.getEnclosingWidget(node);
216 if(w){
217 w._constraint = domAttr.get(node, "data-app-constraint");
218 return w;
219 }
220
221 return {
222 domNode: node,
223 _constraint: domAttr.get(node, "data-app-constraint")
224 };
225 });
226
227 if(selectedChild){
228 children = array.filter(children, function(c){
229 // do not need to set display none here it is set in select.
230 return c.domNode && c._constraint;
231 }, view);
232 }
233 }
234 // We don't need to layout children if this._contentBox is null for the operation will do nothing.
235 if(view._contentBox){
236 layout.layoutChildren(view.domNode, view._contentBox, children);
237 }
238 }
239 });
240 });