-
Notifications
You must be signed in to change notification settings - Fork 26
/
backstack.js
893 lines (731 loc) · 30.8 KB
/
backstack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//////////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2012 Piotr Walczyszyn (http://outof.me | @pwalczyszyn)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//////////////////////////////////////////////////////////////////////////////////////
// BackStak version 0.9.0
(function (root, factory) {
// Set up BackStack appropriately for the environment.
if (typeof exports !== 'undefined') {
// Node/CommonJS, no need for jQuery in that case.
factory(root, exports, require('underscore'), require('jquery'), require('Backbone'));
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'jquery', 'Backbone', 'exports'],
function (_, $, Backbone, exports) {
// Export global even in AMD case in case this script is loaded with
// others that may still expect a global Backbone.
root.BackStack = factory(root, exports, _, $, Backbone);
});
} else {
// Browser globals
root.BackStack = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender), root.Backbone);
}
}(this, function (root, BackStack, _, $, Backbone) {
/**
* almond 0.0.3 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
*/
/*jslint strict: false, plusplus: false */
/*global setTimeout: false */
var requirejs, require, define;
(function (undef) {
var defined = {},
waiting = {},
aps = [].slice,
main, req;
if (typeof define === "function") {
//If a define is already in play via another AMD loader,
//do not overwrite.
return;
}
/**
* Given a relative module name, like ./something, normalize it to
* a real name that can be mapped to a path.
* @param {String} name the relative name
* @param {String} baseName a real name that the name arg is relative
* to.
* @returns {String} normalized name
*/
function normalize(name, baseName) {
//Adjust any relative paths.
if (name && name.charAt(0) === ".") {
//If have a base name, try to normalize against it,
//otherwise, assume it is a top-level require that will
//be relative to baseUrl in the end.
if (baseName) {
//Convert baseName to array, and lop off the last part,
//so that . matches that "directory" and not name of the baseName's
//module. For instance, baseName of "one/two/three", maps to
//"one/two/three.js", but we want the directory, "one/two" for
//this normalization.
baseName = baseName.split("/");
baseName = baseName.slice(0, baseName.length - 1);
name = baseName.concat(name.split("/"));
//start trimDots
var i, part;
for (i = 0; (part = name[i]); i++) {
if (part === ".") {
name.splice(i, 1);
i -= 1;
} else if (part === "..") {
if (i === 1 && (name[2] === '..' || name[0] === '..')) {
//End of the line. Keep at least one non-dot
//path segment at the front so it can be mapped
//correctly to disk. Otherwise, there is likely
//no path mapping for a path starting with '..'.
//This can still fail, but catches the most reasonable
//uses of ..
break;
} else if (i > 0) {
name.splice(i - 1, 2);
i -= 2;
}
}
}
//end trimDots
name = name.join("/");
}
}
return name;
}
function makeRequire(relName, forceSync) {
return function () {
//A version of a require function that passes a moduleName
//value for items that may need to
//look up paths relative to the moduleName
return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
};
}
function makeNormalize(relName) {
return function (name) {
return normalize(name, relName);
};
}
function makeLoad(depName) {
return function (value) {
defined[depName] = value;
};
}
function callDep(name) {
if (waiting.hasOwnProperty(name)) {
var args = waiting[name];
delete waiting[name];
main.apply(undef, args);
}
return defined[name];
}
/**
* Makes a name map, normalizing the name, and using a plugin
* for normalization if necessary. Grabs a ref to plugin
* too, as an optimization.
*/
function makeMap(name, relName) {
var prefix, plugin,
index = name.indexOf('!');
if (index !== -1) {
prefix = normalize(name.slice(0, index), relName);
name = name.slice(index + 1);
plugin = callDep(prefix);
//Normalize according
if (plugin && plugin.normalize) {
name = plugin.normalize(name, makeNormalize(relName));
} else {
name = normalize(name, relName);
}
} else {
name = normalize(name, relName);
}
//Using ridiculous property names for space reasons
return {
f: prefix ? prefix + '!' + name : name, //fullName
n: name,
p: plugin
};
}
main = function (name, deps, callback, relName) {
var args = [],
usingExports,
cjsModule, depName, i, ret, map;
//Use name if no relName
if (!relName) {
relName = name;
}
//Call the callback to define the module, if necessary.
if (typeof callback === 'function') {
//Default to require, exports, module if no deps if
//the factory arg has any arguments specified.
if (!deps.length && callback.length) {
deps = ['require', 'exports', 'module'];
}
//Pull out the defined dependencies and pass the ordered
//values to the callback.
for (i = 0; i < deps.length; i++) {
map = makeMap(deps[i], relName);
depName = map.f;
//Fast path CommonJS standard dependencies.
if (depName === "require") {
args[i] = makeRequire(name);
} else if (depName === "exports") {
//CommonJS module spec 1.1
args[i] = defined[name] = {};
usingExports = true;
} else if (depName === "module") {
//CommonJS module spec 1.1
cjsModule = args[i] = {
id: name,
uri: '',
exports: defined[name]
};
} else if (defined.hasOwnProperty(depName) || waiting.hasOwnProperty(depName)) {
args[i] = callDep(depName);
} else if (map.p) {
map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
args[i] = defined[depName];
} else {
throw name + ' missing ' + depName;
}
}
ret = callback.apply(defined[name], args);
if (name) {
//If setting exports via "module" is in play,
//favor that over return value and exports. After that,
//favor a non-undefined return value over exports use.
if (cjsModule && cjsModule.exports !== undef) {
defined[name] = cjsModule.exports;
} else if (!usingExports) {
//Use the return value from the function.
defined[name] = ret;
}
}
} else if (name) {
//May just be an object definition for the module. Only
//worry about defining if have a module name.
defined[name] = callback;
}
};
requirejs = req = function (deps, callback, relName, forceSync) {
if (typeof deps === "string") {
//Just return the module wanted. In this scenario, the
//deps arg is the module name, and second arg (if passed)
//is just the relName.
//Normalize module name, if it contains . or ..
return callDep(makeMap(deps, callback).f);
} else if (!deps.splice) {
//deps is a config object, not an array.
//Drop the config stuff on the ground.
if (callback.splice) {
//callback is an array, which means it is a dependency list.
//Adjust args if there are dependencies
deps = callback;
callback = arguments[2];
} else {
deps = [];
}
}
//Simulate async callback;
if (forceSync) {
main(undef, deps, callback, relName);
} else {
setTimeout(function () {
main(undef, deps, callback, relName);
}, 15);
}
return req;
};
/**
* Just drops the config on the floor, but returns req in case
* the config return value is used.
*/
req.config = function () {
return req;
};
/**
* Export require as a global, but only if it does not already exist.
*/
if (!require) {
require = req;
}
define = function (name, deps, callback) {
//This module may not have dependencies
if (!deps.splice) {
//deps is not an array, so probably means
//an object literal or factory function for
//the value. Adjust args.
callback = deps;
deps = [];
}
if (define.unordered) {
waiting[name] = [name, deps, callback];
} else {
main(name, deps, callback);
}
};
define.amd = {
jQuery: true
};
}());
define("almond", function(){});
define('StackView',[],function () {
var StackView = Backbone.View.extend({
/**
* Posible options auto or never
*/
destructionPolicy:"auto",
/**
* Reference to parent StackNavigator
*/
stackNavigator:null,
/**
*
*/
rendered:false,
setStackNavigator:function (stackNavigator, navigationOptions) {
this.stackNavigator = stackNavigator;
if (navigationOptions) {
if (navigationOptions.destructionPolicy)
this.destructionPolicy = navigationOptions.destructionPolicy;
}
// Setting default styles
this.$el.css({position:'absolute', overflow:'hidden', width:'100%', height:'100%'});
}
});
return StackView;
});
define('effects/vendorPrefix',[], function () {
/**
* Helper function to detect browser vendor prefix.
* Thanks to Lea Verou: http://lea.verou.me/2009/02/find-the-vendor-prefix-of-the-current-browser/
* I just modified it slightly as I expect it to be used in mobile/WebKit scenarios mostly.
*/
var vendorPrefix,
regex = /^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/,
someScript = document.getElementsByTagName('script')[0];
// Exception for WebKit based browsers
if ('WebkitOpacity' in someScript.style) {
vendorPrefix = 'Webkit';
} else if ('KhtmlOpacity' in someScript.style) {
vendorPrefix = 'Khtml';
} else {
for (var prop in someScript.style) {
if (regex.test(prop)) {
// test is faster than match, so it's better to perform
// that on the lot and match only when necessary
vendorPrefix = prop.match(regex)[0];
break;
}
}
}
return (vendorPrefix || '');
});
define('effects/Effect',['effects/vendorPrefix'], function (vendorPrefix) {
var Effect = function Effect(params) {
if (this.params)
this.params = _.extend(this.params, params);
else
this.params = params;
this.vendorPrefix = vendorPrefix;
if (this.vendorPrefix == 'Moz' || this.vendorPrefix == '')
this.transitionEndEvent = 'transitionend';
else if (this.vendorPrefix == 'ms')
this.transitionEndEvent = 'MSTransitionEnd';
else
this.transitionEndEvent = this.vendorPrefix.toLowerCase() + 'TransitionEnd';
};
// Shared empty constructor function to aid in prototype-chain creation.
var ctor = function () {
};
Effect.extend = function (protoProps, staticProps) {
var child = function () {
Effect.apply(this, arguments);
};
// Inherit class (static) properties from parent.
_.extend(child, Effect);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
ctor.prototype = Effect.prototype;
child.prototype = new ctor();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Add static properties to the constructor function, if supplied.
if (staticProps) _.extend(child, staticProps);
// Correctly set child's `prototype.constructor`.
child.prototype.constructor = child;
// Set a convenience property in case the parent's prototype is needed later.
child.__super__ = Effect.prototype;
return child;
};
return Effect;
});
define('effects/NoEffect',['effects/Effect'], function (Effect) {
var NoEffect = Effect.extend();
NoEffect.prototype.play = function ($fromView, $toView, callback, context) {
if ($toView) {
// Showing the view
$toView.css('display', $toView.data('original-display'));
$toView.removeData('original-display');
}
callback.call(context);
};
return NoEffect;
});
define('effects/SlideEffect',['effects/Effect'], function (Effect) {
var SlideEffect = Effect.extend({
params:{
direction:'left',
fromViewTransitionProps:{duration:0.4, easing:'ease-out', delay:0},
toViewTransitionProps:{duration:0.4, easing:'ease-out', delay:0}
}
});
SlideEffect.prototype.play = function ($fromView, $toView, callback, context) {
var that = this,
activeTransitions = 0,
transformParams,
timeout;
var transitionEndHandler = function (event) {
activeTransitions--;
$(event.target)[0].style[that.vendorPrefix + 'Transition'] = '';
if (activeTransitions == 0 && callback) {
callback.call(context);
}
};
if ($fromView) {
activeTransitions++;
$fromView.one(that.transitionEndEvent, transitionEndHandler);
$fromView.css('left', 0);
$fromView[0].style[that.vendorPrefix + 'Transition'] = ['all ',
that.params.fromViewTransitionProps.duration, 's ',
that.params.fromViewTransitionProps.easing, ' ',
that.params.fromViewTransitionProps.delay, 's'].join('');
}
if ($toView) {
activeTransitions++;
$toView.one(that.transitionEndEvent, transitionEndHandler);
$toView.css('left', that.params.direction == 'left' ? context.$el.width() : -context.$el.width());
$toView[0].style[that.vendorPrefix + 'Transition'] = ['all ',
that.params.toViewTransitionProps.duration, 's ',
that.params.toViewTransitionProps.easing, ' ',
that.params.toViewTransitionProps.delay, 's'].join('');
// Showing the view
$toView.css('display', $toView.data('original-display'));
$toView.removeData('original-display');
}
if ($fromView || $toView) {
// This is a hack to force DOM reflow before transition starts
context.$el.css('width');
transformParams = 'translateX(' + (that.params.direction == 'left' ? -context.$el.width() : context.$el.width()) + 'px)';
}
// This is a fallback for situations when TransitionEnd event doesn't get triggered
var transDuration = Math.max(that.params.fromViewTransitionProps.duration, that.params.toViewTransitionProps.duration) +
Math.max(that.params.fromViewTransitionProps.delay, that.params.toViewTransitionProps.delay);
timeout = setTimeout(function () {
if (activeTransitions > 0) {
activeTransitions = -1;
console.log('Warning ' + that.transitionEndEvent + ' didn\'t trigger in expected time!');
if ($toView) {
$toView.off(that.transitionEndEvent, transitionEndHandler);
$toView[0].style[that.vendorPrefix + 'Transition'] = '';
}
if ($fromView) {
$fromView.off(that.transitionEndEvent, transitionEndHandler);
$fromView[0].style[that.vendorPrefix + 'Transition'] = '';
}
callback.call(context);
}
}, transDuration * 1.5 * 1000);
if ($fromView && $toView)
$fromView[0].style[that.vendorPrefix + 'Transform'] = $toView[0].style[that.vendorPrefix + 'Transform'] = transformParams;
else if ($toView)
$toView[0].style[that.vendorPrefix + 'Transform'] = transformParams;
else if ($fromView)
$fromView[0].style[that.vendorPrefix + 'Transform'] = transformParams;
};
return SlideEffect;
});
define('StackNavigator',['effects/SlideEffect'], function (SlideEffect) {
/**
* Private common push method.
*
* @param fromViewRef - reference to from view
* @param toViewRef - reference to to view
* @param transition - transition to played during push
*/
var push = function (fromViewRef, toViewRef, transition) {
// Hiding view
toViewRef.instance.$el.data('original-display', toViewRef.instance.$el.css('display'));
toViewRef.instance.$el.css('display', 'none');
// Adding view to the DOM
this.$el.append(toViewRef.instance.$el);
// Rendering view if required
if (!toViewRef.instance.rendered) {
toViewRef.instance.render.call(toViewRef.instance);
toViewRef.instance.rendered = true;
}
// Adding view to the stack internal array
this.viewsStack.push(toViewRef);
transition = transition || this.defaultPushTransition || (this.defaultPushTransition = new SlideEffect({direction:'left'}));
transition.play(fromViewRef ? fromViewRef.instance.$el : null, toViewRef.instance.$el,
function () {
this.activeView = toViewRef.instance;
toViewRef.instance.$el.trigger('viewActivate');
if (fromViewRef) {
fromViewRef.instance.$el.trigger('viewDeactivate');
if (fromViewRef.instance.destructionPolicy == 'never') {
fromViewRef.instance.$el.detach();
} else {
fromViewRef.instance.remove();
fromViewRef.instance = null;
}
}
this.trigger('viewChanged');
}, this);
};
/**
* Private common pop method.
*
* @param fromViewRef - reference to from view
* @param toViewRef - reference to to view
* @param transition - transition to played during pop
*/
var pop = function (fromViewRef, toViewRef, transition) {
// Removing top view ref from the stack array
this.viewsStack.pop();
if (toViewRef) {
if (!toViewRef.instance) {
// Getting view class declaration
var viewClass = toViewRef.viewClass;
// Creating view instance
toViewRef.instance = new viewClass(toViewRef.options);
// Setting ref to StackNavigator
toViewRef.instance.setStackNavigator(this, toViewRef.options ? toViewRef.options.navigationOptions : null);
}
// Hiding view
toViewRef.instance.$el.data('original-display', toViewRef.instance.$el.css('display'));
toViewRef.instance.$el.css('display', 'none');
// Adding view to the DOM
this.$el.append(toViewRef.instance.$el);
// Rendering view if required
if (!toViewRef.instance.rendered) {
toViewRef.instance.render.call(toViewRef.instance);
toViewRef.instance.rendered = true;
}
}
transition = transition || this.defaultPopTransition || (this.defaultPopTransition = new SlideEffect({direction:'right'}));
transition.play(fromViewRef.instance.$el, toViewRef ? toViewRef.instance.$el : null,
function () {
if (toViewRef) {
this.activeView = toViewRef.instance;
toViewRef.instance.$el.trigger('viewActivate');
} else {
this.activeView = null;
}
fromViewRef.instance.$el.trigger('viewDeactivate');
fromViewRef.instance.remove();
fromViewRef.instance = null;
this.trigger('viewChanged');
}, this);
};
var StackNavigator = Backbone.View.extend({
viewsStack:null,
activeView:null,
defaultPushTransition:null,
defaultPopTransition:null,
events:{
'viewActivate':'proxyActivationEvents',
'viewDeactivate':'proxyActivationEvents'
},
proxyActivationEvents:function (event) {
this.trigger(event.type, event);
},
initialize:function (options) {
// Setting default styles
this.$el.css({overflow:'hidden'});
// Setting new viewsStack array
this.viewsStack = [];
},
pushView:function (view, viewOptions, transition) {
var toView, toViewRef,
isViewInstance = (typeof view !== 'function'),
fromViewRef = _.last(this.viewsStack);
toView = (!isViewInstance) ? new view(viewOptions) : view;
toView.setStackNavigator(this, (viewOptions) ? viewOptions.navigationOptions : null);
toViewRef = {instance:toView, viewClass:toView.constructor, options:viewOptions};
var event = $.Event('viewChanging',
{
action:'push',
fromViewClass:fromViewRef ? fromViewRef.viewClass : null,
fromView:fromViewRef ? fromViewRef.instance : null,
toViewClass:toViewRef.viewClass,
toView:toViewRef.instance
});
this.trigger(event.type, event);
if (!event.isDefaultPrevented()) {
push.call(this, fromViewRef, toViewRef, transition);
return toView;
}
return null;
},
popView:function (transition) {
var toViewRef, fromViewRef;
if (this.viewsStack.length > 0)
fromViewRef = this.viewsStack[this.viewsStack.length - 1];
if (this.viewsStack.length > 1)
toViewRef = this.viewsStack[this.viewsStack.length - 2];
var event = $.Event('viewChanging',
{
action:'pop',
fromViewClass:fromViewRef ? fromViewRef.viewClass : null,
fromView:fromViewRef ? fromViewRef.instance : null,
toViewClass:toViewRef ? toViewRef.viewClass : null,
toView:toViewRef ? toViewRef.instance : null
});
this.trigger(event.type, event);
if (!event.isDefaultPrevented()) {
var fromView = fromViewRef.instance;
pop.call(this, fromViewRef, toViewRef, transition);
return fromView;
}
return null;
},
popAll:function (transition) {
if (this.viewsStack.length > 0) {
var fromViewRef;
if (this.viewsStack.length > 0)
fromViewRef = this.viewsStack[this.viewsStack.length - 1];
var event = $.Event('viewChanging',
{
action:'popAll',
fromViewClass:fromViewRef ? fromViewRef.viewClass : null,
fromView:fromViewRef ? fromViewRef.instance : null,
toViewClass:null,
toView:null
});
this.trigger(event.type, event);
if (!event.isDefaultPrevented()) {
// Removing views except the top one
this.viewsStack.splice(0, this.viewsStack.length - 1);
pop.call(this, fromViewRef, null, transition);
}
}
return null;
},
replaceView:function (view, viewOptions, transition) {
if (this.viewsStack.length > 0) {
var toView, toViewRef,
isViewInstance = (typeof view !== 'function'),
fromViewRef = this.viewsStack[this.viewsStack.length - 1];
toView = (!isViewInstance) ? new view(viewOptions) : view;
toView.setStackNavigator(this, (viewOptions) ? viewOptions.navigationOptions : null);
toViewRef = {instance:toView, viewClass:toView.constructor, options:viewOptions};
var event = $.Event('viewChanging',
{
action:'replace',
fromViewClass:fromViewRef.viewClass,
fromView:fromViewRef.instance,
toViewClass:toViewRef.viewClass,
toView:toViewRef.instance
});
this.trigger(event.type, event);
if (!event.isDefaultPrevented()) {
this.viewsStack.pop();
push.call(this, fromViewRef, toViewRef, transition);
return toView;
}
}
return null;
}
});
return StackNavigator;
});
define('effects/FadeEffect',['effects/Effect'], function (Effect) {
var FadeEffect = Effect.extend({
params:{
fromViewTransitionProps:{duration:0.4, easing:'linear', delay:0.1},
toViewTransitionProps:{duration:0.4, easing:'linear', delay:0.1}
}
});
FadeEffect.prototype.play = function ($fromView, $toView, callback, context) {
var that = this,
activeTransitions = 0,
timeout;
var transitionEndHandler = function (event) {
activeTransitions--;
$(event.target)[0].style[that.vendorPrefix + 'Transition'] = '';
if (activeTransitions == 0 && callback) {
if (timeout) clearTimeout(timeout);
callback.call(context);
}
};
if ($fromView) {
activeTransitions++;
$fromView.one(that.transitionEndEvent, transitionEndHandler);
$fromView[0].style[that.vendorPrefix + 'Transition'] = ['opacity ',
that.params.fromViewTransitionProps.duration, 's ',
that.params.fromViewTransitionProps.easing, ' ',
that.params.fromViewTransitionProps.delay, 's'].join('');
}
if ($toView) {
activeTransitions++;
// Setting initial opacity
$toView.css('opacity', 0);
$toView.one(that.transitionEndEvent, transitionEndHandler);
$toView[0].style[that.vendorPrefix + 'Transition'] = ['opacity ',
that.params.toViewTransitionProps.duration, 's ',
that.params.toViewTransitionProps.easing, ' ',
that.params.toViewTransitionProps.delay, 's'].join('');
// Showing the view
$toView.css('display', $toView.data('original-display'));
$toView.removeData('original-display');
}
// This is a hack to force DOM reflow before transition starts
context.$el.css('width');
// This is a fallback for situations when TransitionEnd event doesn't get triggered
var transDuration = Math.max(that.params.fromViewTransitionProps.duration, that.params.toViewTransitionProps.duration) +
Math.max(that.params.fromViewTransitionProps.delay, that.params.toViewTransitionProps.delay);
timeout = setTimeout(function () {
if (activeTransitions > 0) {
activeTransitions = -1;
console.log('Warning ' + that.transitionEndEvent + ' didn\'t trigger in expected time!');
if ($toView) {
$toView.off(that.transitionEndEvent, transitionEndHandler);
$toView[0].style[that.vendorPrefix + 'Transition'] = '';
}
if ($fromView) {
$fromView.off(that.transitionEndEvent, transitionEndHandler);
$fromView[0].style[that.vendorPrefix + 'Transition'] = '';
}
callback.call(context);
}
}, transDuration * 1.5 * 1000);
if ($toView)
$toView.css('opacity', 1);
if ($fromView)
$fromView.css('opacity', 0);
};
return FadeEffect;
});
define('BackStack',['StackNavigator', 'StackView', 'effects/NoEffect', 'effects/SlideEffect', 'effects/FadeEffect'],
function (StackNavigator, StackView, NoEffect, SlideEffect, FadeEffect) {
BackStack.StackNavigator = StackNavigator;
BackStack.StackView = StackView;
BackStack.NoEffect = NoEffect;
BackStack.SlideEffect = SlideEffect;
BackStack.FadeEffect = FadeEffect;
return BackStack;
});
return BackStack;
}));