Debian Codegraph @

dojox/app/controllers/Transition.js

raw symbols SCIP
1 define(["require", "dojo/_base/lang", "dojo/_base/declare", "dojo/has", "dojo/on", "dojo/Deferred", "dojo/when",
2 "dojo/dom-style", "../Controller", "../utils/constraints"],
3 function(require, lang, declare, has, on, Deferred, when, domStyle, Controller, constraints){
4
5 var transit;
6 var MODULE = "app/controllers/Transition";
7 var LOGKEY = "logTransitions:";
8
9 // module:
10 // dojox/app/controllers/Transition
11 // Bind "app-transition" event on dojox/app application instance.
12 // Do transition from one view to another view.
13 return declare("dojox.app.controllers.Transition", Controller, {
14
15 proceeding: false,
16
17 waitingQueue:[],
18
19 constructor: function(app, events){
20 // summary:
21 // bind "app-transition" event on application instance.
22 //
23 // app:
24 // dojox/app application instance.
25 // events:
26 // {event : handler}
27 this.events = {
28 "app-transition": this.transition,
29 "app-domNode": this.onDomNodeChange
30 };
31 require([this.app.transit || "dojox/css3/transit"], function(t){
32 transit = t;
33 });
34 if(this.app.domNode){
35 this.onDomNodeChange({oldNode: null, newNode: this.app.domNode});
36 }
37 },
38
39 transition: function(event){
40 // summary:
41 // Response to dojox/app "app-transition" event.
42 //
43 // example:
44 // Use emit to trigger "app-transition" event, and this function will response to the event. For example:
45 // | this.app.emit("app-transition", {"viewId": viewId, "opts": opts});
46 //
47 // event: Object
48 // "app-transition" event parameter. It should be like: {"viewId": viewId, "opts": opts}
49 var F = MODULE+":transition";
50 this.app.log(LOGKEY,F," ");
51 this.app.log(LOGKEY,F,"New Transition event.viewId=["+event.viewId+"]");
52 this.app.log(F,"event.viewId=["+event.viewId+"]","event.opts=",event.opts);
53
54 var viewsId = event.viewId || "";
55 this.proceedingSaved = this.proceeding;
56 var parts = viewsId.split('+');
57 var removePartsTest = viewsId.split('-');
58 var viewId, newEvent;
59 if(parts.length > 0 || removePartsTest.length > 0){
60 while(parts.length > 1){
61 viewId = parts.shift();
62 newEvent = lang.clone(event);
63 if(viewId.indexOf("-") >= 0){ // there is a remove
64 var removeParts = viewId.split('-');
65 if(removeParts.length > 0){
66 viewId = removeParts.shift();
67 if(viewId){
68 newEvent._removeView = false;
69 newEvent.viewId = viewId;
70 this.proceeding = true;
71 this.proceedTransition(newEvent);
72 newEvent = lang.clone(event);
73 }
74 viewId = removeParts.shift();
75 if(viewId){
76 newEvent._removeView = true;
77 newEvent.viewId = viewId;
78 this.proceeding = true;
79 this.proceedTransition(newEvent);
80 }
81 }
82 }else{
83 newEvent._removeView = false;
84 newEvent.viewId = viewId;
85 this.proceeding = true;
86 this.proceedTransition(newEvent);
87 }
88 }
89 viewId = parts.shift();
90 var removeParts = viewId.split('-');
91 if(removeParts.length > 0){
92 viewId = removeParts.shift();
93 }
94 if(viewId.length > 0){ // check viewId.length > 0 to skip this section for a transition with only -viewId
95 this.proceeding = this.proceedingSaved;
96 event.viewId = viewId;
97 event._doResize = true; // at the end of the last transition call resize
98 event._removeView = false;
99 this.proceedTransition(event);
100 }
101 if(removeParts.length > 0){
102 while(removeParts.length > 0){
103 var remViewId = removeParts.shift();
104 newEvent = lang.clone(event);
105 newEvent.viewId = remViewId;
106 newEvent._removeView = true;
107 newEvent._doResize = true; // at the end of the last transition call resize
108 this.proceedTransition(newEvent);
109 }
110 }
111 }else{
112 event._doResize = true; // at the end of the last transition call resize
113 event._removeView = false;
114 this.proceedTransition(event);
115 }
116 },
117
118 onDomNodeChange: function(evt){
119 if(evt.oldNode != null){
120 this.unbind(evt.oldNode, "startTransition");
121 }
122 this.bind(evt.newNode, "startTransition", lang.hitch(this, this.onStartTransition));
123 },
124
125 onStartTransition: function(evt){
126 // summary:
127 // Response to dojox/app "startTransition" event.
128 //
129 // example:
130 // Use "dojox/mobile/TransitionEvent" to trigger "startTransition" event, and this function will response the event. For example:
131 // | var transOpts = {
132 // | title:"List",
133 // | target:"items,list",
134 // | url: "#items,list",
135 // | data: {}
136 // | };
137 // | new TransitionEvent(domNode, transOpts, e).dispatch();
138 //
139 // evt: Object
140 // transition options parameter
141
142 // prevent event from bubbling to window and being
143 // processed by dojox/mobile/ViewController
144 if(evt.preventDefault){
145 evt.preventDefault();
146 }
147 evt.cancelBubble = true;
148 if(evt.stopPropagation){
149 evt.stopPropagation();
150 }
151
152 var target = evt.detail.target;
153 var regex = /#(.+)/;
154 if(!target && regex.test(evt.detail.href)){
155 target = evt.detail.href.match(regex)[1];
156 }
157
158 // transition to the target view
159 this.transition({ "viewId":target, opts: lang.mixin({}, evt.detail), data: evt.detail.data });
160 },
161
162 _addTransitionEventToWaitingQueue: function(transitionEvt){
163 if(transitionEvt.defaultView && this.waitingQueue.length > 0){ // need to test for defaultView to position this view correctly
164 var addedEvt = false;
165 for(var i = 0; i < this.waitingQueue.length; i++){
166 var evt = this.waitingQueue[i];
167 if(!evt.defaultView){
168 this.waitingQueue.splice(i,0,transitionEvt); // insert before first non defaultView
169 addedEvt = true;
170 break;
171 }
172 }
173 if(!addedEvt){
174 this.waitingQueue.push(transitionEvt);
175 }
176 }else{
177 this.waitingQueue.push(transitionEvt);
178 }
179 },
180 proceedTransition: function(transitionEvt){
181 // summary:
182 // Proceed transition queue by FIFO by default.
183 // If transition is in proceeding, add the next transition to waiting queue.
184 //
185 // transitionEvt: Object
186 // "app-transition" event parameter. It should be like: {"viewId":viewId, "opts":opts}
187 var F = MODULE+":proceedTransition";
188
189 if(this.proceeding){
190 this._addTransitionEventToWaitingQueue(transitionEvt);
191 this.app.log(F+" added this event to waitingQueue", transitionEvt);
192 this.processingQueue = false;
193 return;
194 }
195 // If there are events waiting, needed to have the last in be the last processed, so add it to waitingQueue
196 // process the events in order.
197 this.app.log(F+" this.waitingQueue.length ="+ this.waitingQueue.length+ " this.processingQueue="+this.processingQueue);
198 if(this.waitingQueue.length > 0 && !this.processingQueue){
199 this.processingQueue = true;
200 this._addTransitionEventToWaitingQueue(transitionEvt);
201 this.app.log(F+" added this event to waitingQueue passed proceeding", transitionEvt);
202 transitionEvt = this.waitingQueue.shift();
203 this.app.log(F+" shifted waitingQueue to process", transitionEvt);
204 }
205
206 this.proceeding = true;
207
208 this.app.log(F+" calling trigger load", transitionEvt);
209 if(!transitionEvt.opts){
210 transitionEvt.opts = {};
211 }
212 var params = transitionEvt.params || transitionEvt.opts.params;
213 this.app.emit("app-load", {
214 "viewId": transitionEvt.viewId,
215 "params": params,
216 "forceTransitionNone": transitionEvt.forceTransitionNone,
217 "callback": lang.hitch(this, function(needToHandleDefaultView, defaultHasPlus){
218 if(needToHandleDefaultView){ // do not process this view if needToHandleDefaultView true
219 this.proceeding = false;
220 this.processingQueue = true;
221 // use pop instead of shift here to get the last event for the defaultView when the default does not have a +
222 // use shift when it has a + or the defaults will be out of order but it can move the default to be after other views if we are processing views with a +
223 var nextEvt = (defaultHasPlus) ? this.waitingQueue.shift() : this.waitingQueue.pop();
224 if(nextEvt){
225 this.proceedTransition(nextEvt);
226 }
227 }else{
228 var transitionDef = this._doTransition(transitionEvt.viewId, transitionEvt.opts, params, transitionEvt.opts.data, this.app, transitionEvt._removeView, transitionEvt._doResize, transitionEvt.forceTransitionNone);
229 when(transitionDef, lang.hitch(this, function(){
230 this.proceeding = false;
231 this.processingQueue = true;
232 var nextEvt = this.waitingQueue.shift();
233 if(nextEvt){
234 this.proceedTransition(nextEvt);
235 }
236 }));
237 }
238 })
239 });
240 },
241
242 _getTransition: function(nextView, parent, transitionTo, opts, forceTransitionNone){
243 // summary:
244 // Get view's transition type from the config for the view or from the parent view recursively.
245 // If not available use the transition option otherwise get view default transition type in the
246 // config from parent view.
247 //
248 // parent: Object
249 // view's parent
250 // transitionTo: Object
251 // view to transition to
252 // opts: Object
253 // transition options
254 // forceTransitionNone: boolean
255 // true if the transition type should be forced to none, used for the initial defaultView
256 //
257 // returns:
258 // transition type like "slide", "fade", "flip" or "none".
259 if(forceTransitionNone){
260 return "none";
261 }
262 var parentView = parent;
263 var transition = null;
264 if(nextView){
265 transition = nextView.transition;
266 }
267 if(!transition && parentView.views[transitionTo]){
268 transition = parentView.views[transitionTo].transition;
269 }
270 if(!transition){
271 transition = parentView.transition;
272 }
273 var defaultTransition = (nextView && nextView.defaultTransition) ? nextView.defaultTransition : parentView.defaultTransition;
274 while(!transition && parentView.parent){
275 parentView = parentView.parent;
276 transition = parentView.transition;
277 if(!defaultTransition){
278 defaultTransition = parentView.defaultTransition;
279 }
280 }
281 return transition || opts.transition || defaultTransition || "none";
282 },
283
284
285 _getParamsForView: function(view, params){
286 // summary:
287 // Get view's params only include view specific params if they are for this view.
288 //
289 // view: String
290 // the view's name
291 // params: Object
292 // the params
293 //
294 // returns:
295 // params Object for this view
296 var viewParams = {};
297 for(var item in params){
298 var value = params[item];
299 if(lang.isObject(value)){ // view specific params
300 if(item == view){ // it is for this view
301 // need to add these params for the view
302 viewParams = lang.mixin(viewParams, value);
303 }
304 }else{ // these params are for all views, so add them
305 if(item && value != null){
306 viewParams[item] = params[item];
307 }
308 }
309 }
310 return viewParams;
311 },
312
313 _doTransition: function(transitionTo, opts, params, data, parent, removeView, doResize, forceTransitionNone, nested){
314 // summary:
315 // Transitions from the currently visible view to the defined view.
316 // It should determine what would be the best transition unless
317 // an override in opts tells it to use a specific transitioning methodology
318 // the transitionTo is a string in the form of [view1,view2].
319 //
320 // transitionTo: Object
321 // transition to view id. It looks like #tabView,tab1
322 // opts: Object
323 // transition options
324 // params: Object
325 // params
326 // data: Object
327 // data object that will be passed on activate & de-activate methods of the view
328 // parent: Object
329 // view's parent
330 // removeView: Boolean
331 // remove the view instead of transition to it
332 // doResize: Boolean
333 // emit a resize event
334 // forceTransitionNone: Boolean
335 // force the transition type to be none, used for the initial default view
336 // nested: Boolean
337 // whether the method is called from the transitioning of a parent view
338 //
339 // returns:
340 // transit dojo/promise/all object.
341 var F = MODULE+":_doTransition";
342
343 if(!parent){
344 throw Error("view parent not found in transition.");
345 }
346
347 this.app.log(F+" transitionTo=[",transitionTo,"], removeView=[",removeView,"] parent.name=[",parent.name,"], opts=",opts);
348
349 var parts, toId, subIds, next;
350 if(transitionTo){
351 parts = transitionTo.split(",");
352 }else{
353 // If parent.defaultView is like "main,main", we also need to split it and set the value to toId and subIds.
354 // Or cannot get the next view by "parent.children[parent.id + '_' + toId]"
355 parts = parent.defaultView.split(",");
356 }
357 toId = parts.shift();
358 subIds = parts.join(',');
359
360 // next is loaded and ready for transition
361 next = parent.children[parent.id + '_' + toId];
362 if(!next){
363 if(removeView){
364 this.app.log(F+" called with removeView true, but that view is not available to remove");
365 return; // trying to remove a view which is not showing
366 }
367 throw Error("child view must be loaded before transition.");
368 }
369 // if no subIds and next has default view,
370 // set the subIds to the default view and transition to default view.
371 if(!subIds && next.defaultView){
372 subIds = next.defaultView;
373 }
374
375 var nextSubViewArray = [next || parent];
376 if(subIds){
377 nextSubViewArray = this._getNextSubViewArray(subIds, next, parent);
378 }
379
380 var current = constraints.getSelectedChild(parent, next.constraint);
381 var currentSubViewArray = this._getCurrentSubViewArray(parent, nextSubViewArray, removeView);
382
383 var currentSubNames = this._getNamesFromArray(currentSubViewArray, false);
384 var nextSubNames = this._getNamesFromArray(nextSubViewArray, true);
385
386 // set params on next view.
387 next.params = this._getParamsForView(next.name, params);
388
389 if(removeView){
390 if(next !== current){ // nothing to remove
391 this.app.log(F+" called with removeView true, but that view is not available to remove");
392 return; // trying to remove a view which is not showing
393 }
394 this.app.log(LOGKEY,F,"Transition Remove current From=["+currentSubNames+"]");
395 // if next == current we will set next to null and remove the view with out a replacement
396 next = null;
397 }
398
399 if(nextSubNames == currentSubNames && next == current){ // new test to see if current matches next
400 this.app.log(LOGKEY,F,"Transition current and next DO MATCH From=["+currentSubNames+"] TO=["+nextSubNames+"]");
401 this._handleMatchingViews(nextSubViewArray, next, current, parent, data, removeView, doResize, subIds, currentSubNames, toId, forceTransitionNone, opts);
402
403 }else{
404 this.app.log(LOGKEY,F,"Transition current and next DO NOT MATCH From=["+currentSubNames+"] TO=["+nextSubNames+"]");
405 //When clicking fast, history module will cache the transition request que
406 //and prevent the transition conflicts.
407 //Originally when we conduct transition, selectedChild will not be the
408 //view we want to start transition. For example, during transition 1 -> 2
409 //if user click button to transition to 3 and then transition to 1. After
410 //1->2 completes, it will perform transition 2 -> 3 and 2 -> 1 because
411 //selectedChild is always point to 2 during 1 -> 2 transition and transition
412 //will record 2->3 and 2->1 right after the button is clicked.
413
414 //assume next is already loaded so that this.set(...) will not return
415 //a promise object. this.set(...) will handles the this.selectedChild,
416 //activate or deactivate views and refresh layout.
417
418 //necessary, to avoid a flash when the layout sets display before resize
419 if(!removeView && next){
420 var nextLastSubChild = this.nextLastSubChildMatch || next;
421 var startHiding = false; // only hide views which will transition in
422 for(var i = nextSubViewArray.length-1; i >= 0; i--){
423 var v = nextSubViewArray[i];
424 if(startHiding || v.id == nextLastSubChild.id){
425 startHiding = true;
426 if(!v._needsResize && v.domNode){
427 this.app.log(LOGKEY,F," setting domStyle visibility hidden for v.id=["+v.id+"], display=["+v.domNode.style.display+"], visibility=["+v.domNode.style.visibility+"]");
428 this._setViewVisible(v, false);
429 }
430 }
431 }
432 }
433
434 if(current && current._active){
435 this._handleBeforeDeactivateCalls(currentSubViewArray, this.nextLastSubChildMatch || next, current, data, subIds);
436 }
437 if(next){
438 this.app.log(F+" calling _handleBeforeActivateCalls next name=[",next.name,"], parent.name=[",next.parent.name,"]");
439 this._handleBeforeActivateCalls(nextSubViewArray, this.currentLastSubChildMatch || current, data, subIds);
440 }
441 if(!removeView){
442 var nextLastSubChild = this.nextLastSubChildMatch || next;
443 var trans = this._getTransition(nextLastSubChild, parent, toId, opts, forceTransitionNone)
444 this.app.log(F+" calling _handleLayoutAndResizeCalls trans="+trans);
445 this._handleLayoutAndResizeCalls(nextSubViewArray, removeView, doResize, subIds, forceTransitionNone, trans);
446 }else{
447 // for removeView need to set visible before transition do it here
448 for(var i = 0; i < nextSubViewArray.length; i++){
449 var v = nextSubViewArray[i];
450 this.app.log(LOGKEY,F,"setting visibility visible for v.id=["+v.id+"]");
451 if(v.domNode){
452 this.app.log(LOGKEY,F," setting domStyle for removeView visibility visible for v.id=["+v.id+"], display=["+v.domNode.style.display+"]");
453 this._setViewVisible(v, true);
454 }
455 }
456 }
457 var result = true;
458
459 // this.currentLastSubChildMatch holds the view to transition from
460 if(transit && (!nested || this.currentLastSubChildMatch != null) && this.currentLastSubChildMatch !== next){
461 // css3 transit has the check for IE so it will not try to do it on ie, so we do not need to check it here.
462 // We skip in we are transitioning to a nested view from a parent view and that nested view
463 // did not have any current
464 result = this._handleTransit(next, parent, this.currentLastSubChildMatch, opts, toId, removeView, forceTransitionNone, doResize);
465 }
466 when(result, lang.hitch(this, function(){
467 if(next){
468 this.app.log(F+" back from transit for next ="+next.name);
469 }
470 if(removeView){
471 var nextLastSubChild = this.nextLastSubChildMatch || next;
472 var trans = this._getTransition(nextLastSubChild, parent, toId, opts, forceTransitionNone)
473 this._handleLayoutAndResizeCalls(nextSubViewArray, removeView, doResize, subIds, forceTransitionNone, trans);
474 }
475
476 // Add call to handleAfterDeactivate and handleAfterActivate here!
477 this._handleAfterDeactivateCalls(currentSubViewArray, this.nextLastSubChildMatch || next, current, data, subIds);
478 this._handleAfterActivateCalls(nextSubViewArray, removeView, this.currentLastSubChildMatch || current, data, subIds);
479 }));
480 return result; // dojo/promise/all
481 }
482 },
483
484 _handleMatchingViews: function(subs, next, current, parent, data, removeView, doResize, subIds, currentSubNames, toId, forceTransitionNone, opts){
485 // summary:
486 // Called when the current views and the next views match
487 var F = MODULE+":_handleMatchingViews";
488
489 this._handleBeforeDeactivateCalls(subs, this.nextLastSubChildMatch || next, current, data, subIds);
490 // this is the order that things were being done before on a reload of the same views, so I left it
491 // calling _handleAfterDeactivateCalls here instead of after _handleLayoutAndResizeCalls
492 this._handleAfterDeactivateCalls(subs, this.nextLastSubChildMatch || next, current, data, subIds);
493 this._handleBeforeActivateCalls(subs, this.currentLastSubChildMatch || current, data, subIds);
494 var nextLastSubChild = this.nextLastSubChildMatch || next;
495 var trans = this._getTransition(nextLastSubChild, parent, toId, opts, forceTransitionNone)
496 this._handleLayoutAndResizeCalls(subs, removeView, doResize, subIds, trans);
497 this._handleAfterActivateCalls(subs, removeView, this.currentLastSubChildMatch || current, data, subIds);
498 },
499
500 _handleBeforeDeactivateCalls: function(subs, next, current, /*parent,*/ data, /*removeView, doResize,*/ subIds/*, currentSubNames*/){
501 // summary:
502 // Call beforeDeactivate for each of the current views which are about to be deactivated
503 var F = MODULE+":_handleBeforeDeactivateCalls";
504 if(current._active){
505 //now we need to loop backwards thru subs calling beforeDeactivate
506 for(var i = subs.length-1; i >= 0; i--){
507 var v = subs[i];
508 if(v && v.beforeDeactivate && v._active){
509 this.app.log(LOGKEY,F,"beforeDeactivate for v.id="+v.id);
510 v.beforeDeactivate(next, data);
511 }
512 }
513 }
514 },
515
516 _handleAfterDeactivateCalls: function(subs, next, current, data, subIds){
517 // summary:
518 // Call afterDeactivate for each of the current views which have been deactivated
519 var F = MODULE+":_handleAfterDeactivateCalls";
520 if(current && current._active){
521 //now we need to loop forwards thru subs calling afterDeactivate
522 for(var i = 0; i < subs.length; i++){
523 var v = subs[i];
524 if(v && v.beforeDeactivate && v._active){
525 this.app.log(LOGKEY,F,"afterDeactivate for v.id="+v.id);
526 v.afterDeactivate(next, data);
527 v._active = false;
528 }
529 }
530
531 }
532 },
533
534 _handleBeforeActivateCalls: function(subs, current, data, subIds){
535 // summary:
536 // Call beforeActivate for each of the next views about to be activated
537 var F = MODULE+":_handleBeforeActivateCalls";
538 //now we need to loop backwards thru subs calling beforeActivate (ok since next matches current)
539 for(var i = subs.length-1; i >= 0; i--){
540 var v = subs[i];
541 this.app.log(LOGKEY,F,"beforeActivate for v.id="+v.id);
542 v.beforeActivate(current, data);
543 }
544 },
545
546 _handleLayoutAndResizeCalls: function(subs, removeView, doResize, subIds, forceTransitionNone, transition){
547 // summary:
548 // fire app-layoutView for each of the next views about to be activated, and fire app-resize if doResize is true
549 var F = MODULE+":_handleLayoutAndResizeCalls";
550 var remove = removeView;
551 for(var i = 0; i < subs.length; i++){
552 var v = subs[i];
553 this.app.log(LOGKEY,F,"emit layoutView v.id=["+v.id+"] removeView=["+remove+"]");
554 // it seems like we should be able to minimize calls to resize by passing doResize: false and only doing resize on the app-resize emit
555 this.app.emit("app-layoutView", {"parent": v.parent, "view": v, "removeView": remove, "doResize": false, "transition": transition, "currentLastSubChildMatch": this.currentLastSubChildMatch});
556 remove = false;
557 }
558 if(doResize){
559 this.app.log(LOGKEY,F,"emit doResize called");
560 this.app.emit("app-resize"); // after last layoutView fire app-resize
561 if(transition == "none"){
562 this._showSelectedChildren(this.app); // Need to set visible too before transition do it now.
563 }
564 }
565
566 },
567
568 _showSelectedChildren: function(w){
569 var F = MODULE+":_showSelectedChildren";
570 this.app.log(LOGKEY,F," setting domStyle visibility visible for w.id=["+w.id+"], display=["+w.domNode.style.display+"], visibility=["+w.domNode.style.visibility+"]");
571 this._setViewVisible(w, true);
572 w._needsResize = false;
573 for(var hash in w.selectedChildren){ // need this to handle all selectedChildren
574 if(w.selectedChildren[hash] && w.selectedChildren[hash].domNode){
575 this.app.log(LOGKEY,F," calling _showSelectedChildren for w.selectedChildren[hash].id="+w.selectedChildren[hash].id);
576 this._showSelectedChildren(w.selectedChildren[hash]);
577 }
578 }
579 },
580
581 _setViewVisible: function(v, visible){
582 if(visible){
583 domStyle.set(v.domNode, "visibility", "visible");
584 }else{
585 domStyle.set(v.domNode, "visibility", "hidden");
586 }
587 },
588
589
590 _handleAfterActivateCalls: function(subs, removeView, current, data, subIds){
591 // summary:
592 // Call afterActivate for each of the next views which have been activated
593 var F = MODULE+":_handleAfterActivateCalls";
594 //now we need to loop backwards thru subs calling beforeActivate (ok since next matches current)
595 var startInt = 0;
596 if(removeView && subs.length > 1){
597 startInt = 1;
598 }
599 for(var i = startInt; i < subs.length; i++){
600 var v = subs[i];
601 if(v.afterActivate){
602 this.app.log(LOGKEY,F,"afterActivate for v.id="+v.id);
603 v.afterActivate(current, data);
604 v._active = true;
605 }
606 }
607 },
608
609 _getNextSubViewArray: function(subIds, next, parent){
610 // summary:
611 // Get next sub view array, this array will hold the views which are about to be transitioned to
612 //
613 // subIds: String
614 // the subids, the views are separated with a comma
615 // next: Object
616 // the next view to be transitioned to.
617 // parent: Object
618 // the parent view used in place of next if next is not set.
619 //
620 // returns:
621 // Array of views which will be transitioned to during this transition
622 var F = MODULE+":_getNextSubViewArray";
623 var parts = [];
624 var p = next || parent;
625 if(subIds){
626 parts = subIds.split(",");
627 }
628 var nextSubViewArray = [p];
629 //now we need to loop forwards thru subIds calling beforeActivate
630 for(var i = 0; i < parts.length; i++){
631 toId = parts[i];
632 var v = p.children[p.id + '_' + toId];
633 if(v){
634 nextSubViewArray.push(v);
635 p = v;
636 }
637 }
638 nextSubViewArray.reverse();
639 return nextSubViewArray;
640 },
641
642 _getCurrentSubViewArray: function(parent, nextSubViewArray, removeView){
643 // summary:
644 // Get current sub view array which will be replaced by the views in the nextSubViewArray
645 //
646 // parent: String
647 // the parent view whose selected children will be replaced
648 // nextSubViewArray: Array
649 // the array of views which are to be transitioned to.
650 //
651 // returns:
652 // Array of views which will be deactivated during this transition
653 var F = MODULE+":_getCurrentSubViewArray";
654 var currentSubViewArray = [];
655 var constraint, type, hash;
656 var p = parent;
657 this.currentLastSubChildMatch = null;
658 this.nextLastSubChildMatch = null;
659
660 for(var i = nextSubViewArray.length-1; i >= 0; i--){
661 constraint = nextSubViewArray[i].constraint;
662 type = typeof(constraint);
663 hash = (type == "string" || type == "number") ? constraint : constraint.__hash;
664 // if there is a selected child for this constraint, and the child matches this view, push it.
665 if(p && p.selectedChildren && p.selectedChildren[hash]){
666 if(p.selectedChildren[hash] == nextSubViewArray[i]){
667 this.currentLastSubChildMatch = p.selectedChildren[hash];
668 this.nextLastSubChildMatch = nextSubViewArray[i];
669 currentSubViewArray.push(this.currentLastSubChildMatch);
670 p = this.currentLastSubChildMatch;
671 }else{
672 this.currentLastSubChildMatch = p.selectedChildren[hash];
673 currentSubViewArray.push(this.currentLastSubChildMatch);
674 this.nextLastSubChildMatch = nextSubViewArray[i]; // setting this means the transition will be done to the child instead of the parent
675 // since the constraint was set, but it did not match, need to deactivate all selected children of this.currentLastSubChildMatch
676 if(!removeView){
677 var selChildren = constraints.getAllSelectedChildren(this.currentLastSubChildMatch);
678 currentSubViewArray = currentSubViewArray.concat(selChildren);
679 }
680 break;
681 }
682 }else{ // the else is for the constraint not matching which means no more to deactivate.
683 this.currentLastSubChildMatch = null; // there was no view selected for this constraint
684 this.nextLastSubChildMatch = nextSubViewArray[i]; // set this to the next view for transition to an empty constraint
685 break;
686 }
687
688 }
689 // Here since they had the constraint but it was not the same I need to deactivate all children of p
690 if(removeView){
691 var selChildren = constraints.getAllSelectedChildren(p);
692 currentSubViewArray = currentSubViewArray.concat(selChildren);
693 }
694
695 return currentSubViewArray;
696 },
697
698 _getNamesFromArray: function(subViewArray, backward){
699 // summary:
700 // Get names from the sub view names array
701 //
702 // subViewArray: Array
703 // the array of views to get the names from.
704 // backward: boolean
705 // the direction to loop thru the array to get the names.
706 //
707 // returns:
708 // String of view names separated by a comma
709 //
710 var F = MODULE+":_getNamesFromArray";
711 var subViewNames = "";
712 if(backward){
713 for (var i = subViewArray.length - 1; i >= 0; i--) {
714 subViewNames = subViewNames ? subViewNames+","+subViewArray[i].name : subViewArray[i].name;
715 }
716 }else{
717 for(var i = 0; i < subViewArray.length; i++){
718 subViewNames = subViewNames ? subViewNames+","+subViewArray[i].name : subViewArray[i].name;
719 }
720 }
721 return subViewNames;
722 },
723
724 _handleTransit: function(next, parent, currentLastSubChild, opts, toId, removeView, forceTransitionNone, resizeDone){
725 // summary:
726 // Setup the options and call transit to do the transition
727 //
728 // next: Object
729 // the next view, the view which will be transitioned to
730 // parent: Object
731 // the parent view which is used to get the transition type to be used
732 // currentLastSubChild: Object
733 // the current view which is being transitioned away from
734 // opts: Object
735 // the options used for the transition
736 // toId: String
737 // the id of the view being transitioned to
738 // removeView: boolean
739 // true if the view is being removed
740 // forceTransitionNone: boolean
741 // true if the transition type should be forced to none, used for the initial defaultView
742 // resizeDone: boolean
743 // true if resize was called before this transition
744 //
745 // returns:
746 // the promise returned by the call to transit
747 var F = MODULE+":_handleTransit";
748
749 var nextLastSubChild = this.nextLastSubChildMatch || next;
750
751 var mergedOpts = lang.mixin({}, opts); // handle reverse from mergedOpts or transitionDir
752 mergedOpts = lang.mixin({}, mergedOpts, {
753 reverse: (mergedOpts.reverse || mergedOpts.transitionDir === -1)?true:false,
754 // if transition is set for the view (or parent) in the config use it, otherwise use it from the event or defaultTransition from the config
755 transition: this._getTransition(nextLastSubChild, parent, toId, mergedOpts, forceTransitionNone)
756 });
757
758 if(removeView){
759 nextLastSubChild = null;
760 }
761 if(currentLastSubChild){
762 this.app.log(LOGKEY,F,"transit FROM currentLastSubChild.id=["+currentLastSubChild.id+"]");
763 }
764 if(nextLastSubChild){
765 if(mergedOpts.transition !== "none"){
766 if(!resizeDone && nextLastSubChild._needsResize){ // need to resize if not done yet or things will not be positioned correctly
767 this.app.log(LOGKEY,F,"emit doResize called from _handleTransit");
768 this.app.emit("app-resize"); // after last layoutView fire app-resize
769 }
770 this.app.log(LOGKEY,F," calling _showSelectedChildren for w3.id=["+nextLastSubChild.id+"], display=["+nextLastSubChild.domNode.style.display+"], visibility=["+nextLastSubChild.domNode.style.visibility+"]");
771 this._showSelectedChildren(this.app); // Need to set visible too before transition do it now.
772 }
773 this.app.log(LOGKEY,F,"transit TO nextLastSubChild.id=["+nextLastSubChild.id+"] transition=["+mergedOpts.transition+"]");
774 }else{
775 this._showSelectedChildren(this.app); // Need to set visible too before transition do it now.
776 }
777 return transit(currentLastSubChild && currentLastSubChild.domNode, nextLastSubChild && nextLastSubChild.domNode, mergedOpts);
778 }
779
780 });
781 });