-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
viewService.js
601 lines (505 loc) · 18.9 KB
/
viewService.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
/**
* @private
* TODO document
*/
IonicModule
.run([
'$rootScope',
'$state',
'$location',
'$document',
'$animate',
'$ionicPlatform',
'$ionicViewService',
function($rootScope, $state, $location, $document, $animate, $ionicPlatform, $ionicViewService) {
// init the variables that keep track of the view history
$rootScope.$viewHistory = {
histories: { root: { historyId: 'root', parentHistoryId: null, stack: [], cursor: -1 } },
views: {},
backView: null,
forwardView: null,
currentView: null,
disabledRegistrableTagNames: []
};
// set that these directives should not animate when transitioning
// to it. Instead, the children <tab> directives would animate
if ($ionicViewService.disableRegisterByTagName) {
$ionicViewService.disableRegisterByTagName('ion-tabs');
$ionicViewService.disableRegisterByTagName('ion-side-menus');
}
// always reset the keyboard state when change stage
$rootScope.$on('$stateChangeStart', function(){
ionic.keyboard.hide();
});
$rootScope.$on('viewState.changeHistory', function(e, data) {
if(!data) return;
var hist = (data.historyId ? $rootScope.$viewHistory.histories[ data.historyId ] : null );
if(hist && hist.cursor > -1 && hist.cursor < hist.stack.length) {
// the history they're going to already exists
// go to it's last view in its stack
var view = hist.stack[ hist.cursor ];
return view.go(data);
}
// this history does not have a URL, but it does have a uiSref
// figure out its URL from the uiSref
if(!data.url && data.uiSref) {
data.url = $state.href(data.uiSref);
}
if(data.url) {
// don't let it start with a #, messes with $location.url()
if(data.url.indexOf('#') === 0) {
data.url = data.url.replace('#', '');
}
if(data.url !== $location.url()) {
// we've got a good URL, ready GO!
$location.url(data.url);
}
}
});
// Set the document title when a new view is shown
$rootScope.$on('viewState.viewEnter', function(e, data) {
if(data && data.title) {
$document[0].title = data.title;
}
});
// Triggered when devices with a hardware back button (Android) is clicked by the user
// This is a Cordova/Phonegap platform specifc method
function onHardwareBackButton(e) {
if($rootScope.$viewHistory.backView) {
// there is a back view, go to it
$rootScope.$viewHistory.backView.go();
} else {
// there is no back view, so close the app instead
ionic.Platform.exitApp();
}
e.preventDefault();
return false;
}
$ionicPlatform.registerBackButtonAction(
onHardwareBackButton,
PLATFORM_BACK_BUTTON_PRIORITY_VIEW
);
}])
.factory('$ionicViewService', [
'$rootScope',
'$state',
'$location',
'$window',
'$injector',
'$animate',
'$ionicNavViewConfig',
'$ionicClickBlock',
function($rootScope, $state, $location, $window, $injector, $animate, $ionicNavViewConfig, $ionicClickBlock) {
var View = function(){};
View.prototype.initialize = function(data) {
if(data) {
for(var name in data) this[name] = data[name];
return this;
}
return null;
};
View.prototype.go = function() {
if(this.stateName) {
return $state.go(this.stateName, this.stateParams);
}
if(this.url && this.url !== $location.url()) {
if($rootScope.$viewHistory.backView === this) {
return $window.history.go(-1);
} else if($rootScope.$viewHistory.forwardView === this) {
return $window.history.go(1);
}
$location.url(this.url);
return;
}
return null;
};
View.prototype.destroy = function() {
if(this.scope) {
this.scope.$destroy && this.scope.$destroy();
this.scope = null;
}
};
function createViewId(stateId) {
return ionic.Utils.nextUid();
}
return {
register: function(containerScope, element) {
var viewHistory = $rootScope.$viewHistory,
currentStateId = this.getCurrentStateId(),
hist = this._getHistory(containerScope),
currentView = viewHistory.currentView,
backView = viewHistory.backView,
forwardView = viewHistory.forwardView,
nextViewOptions = this.nextViewOptions(),
rsp = {
viewId: null,
navAction: null,
navDirection: null,
historyId: hist.historyId
};
if(element && !this.isTagNameRegistrable(element)) {
// first check to see if this element can even be registered as a view.
// Certain tags are only containers for views, but are not views themselves.
// For example, the <ion-tabs> directive contains a <ion-tab> and the <ion-tab> is the
// view, but the <ion-tabs> directive itself should not be registered as a view.
rsp.navAction = 'disabledByTagName';
return rsp;
}
if(currentView &&
currentView.stateId === currentStateId &&
currentView.historyId === hist.historyId) {
// do nothing if its the same stateId in the same history
rsp.navAction = 'noChange';
return rsp;
}
if(viewHistory.forcedNav) {
// we've previously set exactly what to do
ionic.Utils.extend(rsp, viewHistory.forcedNav);
$rootScope.$viewHistory.forcedNav = null;
} else if(backView && backView.stateId === currentStateId) {
// they went back one, set the old current view as a forward view
rsp.viewId = backView.viewId;
rsp.navAction = 'moveBack';
rsp.viewId = backView.viewId;
if(backView.historyId === currentView.historyId) {
// went back in the same history
rsp.navDirection = 'back';
}
} else if(forwardView && forwardView.stateId === currentStateId) {
// they went to the forward one, set the forward view to no longer a forward view
rsp.viewId = forwardView.viewId;
rsp.navAction = 'moveForward';
if(forwardView.historyId === currentView.historyId) {
rsp.navDirection = 'forward';
}
var parentHistory = this._getParentHistoryObj(containerScope);
if(forwardView.historyId && parentHistory.scope) {
// if a history has already been created by the forward view then make sure it stays the same
parentHistory.scope.$historyId = forwardView.historyId;
rsp.historyId = forwardView.historyId;
}
} else if(currentView && currentView.historyId !== hist.historyId &&
hist.cursor > -1 && hist.stack.length > 0 && hist.cursor < hist.stack.length &&
hist.stack[hist.cursor].stateId === currentStateId) {
// they just changed to a different history and the history already has views in it
var switchToView = hist.stack[hist.cursor];
rsp.viewId = switchToView.viewId;
rsp.navAction = 'moveBack';
// if switching to a different history, and the history of the view we're switching
// to has an existing back view from a different history than itself, then
// it's back view would be better represented using the current view as its back view
var switchToViewBackView = this._getViewById(switchToView.backViewId);
if(switchToViewBackView && switchToView.historyId !== switchToViewBackView.historyId) {
hist.stack[hist.cursor].backViewId = currentView.viewId;
}
} else {
// set a new unique viewId
rsp.viewId = createViewId(currentStateId);
if(currentView) {
// set the forward view if there is a current view (ie: if its not the first view)
currentView.forwardViewId = rsp.viewId;
// its only moving forward if its in the same history
if(hist.historyId === currentView.historyId) {
rsp.navDirection = 'forward';
}
rsp.navAction = 'newView';
// check if there is a new forward view within the same history
if(forwardView && currentView.stateId !== forwardView.stateId &&
currentView.historyId === forwardView.historyId) {
// they navigated to a new view but the stack already has a forward view
// since its a new view remove any forwards that existed
var forwardsHistory = this._getHistoryById(forwardView.historyId);
if(forwardsHistory) {
// the forward has a history
for(var x=forwardsHistory.stack.length - 1; x >= forwardView.index; x--) {
// starting from the end destroy all forwards in this history from this point
forwardsHistory.stack[x].destroy();
forwardsHistory.stack.splice(x);
}
}
}
} else {
// there's no current view, so this must be the initial view
rsp.navAction = 'initialView';
}
// add the new view
viewHistory.views[rsp.viewId] = this.createView({
viewId: rsp.viewId,
index: hist.stack.length,
historyId: hist.historyId,
backViewId: (currentView && currentView.viewId ? currentView.viewId : null),
forwardViewId: null,
stateId: currentStateId,
stateName: this.getCurrentStateName(),
stateParams: this.getCurrentStateParams(),
url: $location.url(),
});
if (rsp.navAction == 'moveBack') {
//moveBack(from, to);
$rootScope.$emit('$viewHistory.viewBack', currentView.viewId, rsp.viewId);
}
// add the new view to this history's stack
hist.stack.push(viewHistory.views[rsp.viewId]);
}
if(nextViewOptions) {
if(nextViewOptions.disableAnimate) rsp.navDirection = null;
if(nextViewOptions.disableBack) viewHistory.views[rsp.viewId].backViewId = null;
this.nextViewOptions(null);
}
this.setNavViews(rsp.viewId);
hist.cursor = viewHistory.currentView.index;
return rsp;
},
setNavViews: function(viewId) {
var viewHistory = $rootScope.$viewHistory;
viewHistory.currentView = this._getViewById(viewId);
viewHistory.backView = this._getBackView(viewHistory.currentView);
viewHistory.forwardView = this._getForwardView(viewHistory.currentView);
$rootScope.$broadcast('$viewHistory.historyChange', {
showBack: (viewHistory.backView && viewHistory.backView.historyId === viewHistory.currentView.historyId)
});
},
registerHistory: function(scope) {
scope.$historyId = ionic.Utils.nextUid();
},
createView: function(data) {
var newView = new View();
return newView.initialize(data);
},
getCurrentView: function() {
return $rootScope.$viewHistory.currentView;
},
getBackView: function() {
return $rootScope.$viewHistory.backView;
},
getForwardView: function() {
return $rootScope.$viewHistory.forwardView;
},
getNavDirection: function() {
return $rootScope.$viewHistory.navDirection;
},
getCurrentStateName: function() {
return ($state && $state.current ? $state.current.name : null);
},
isCurrentStateNavView: function(navView) {
return ($state &&
$state.current &&
$state.current.views &&
$state.current.views[navView] ? true : false);
},
getCurrentStateParams: function() {
var rtn;
if ($state && $state.params) {
for(var key in $state.params) {
if($state.params.hasOwnProperty(key)) {
rtn = rtn || {};
rtn[key] = $state.params[key];
}
}
}
return rtn;
},
getCurrentStateId: function() {
var id;
if($state && $state.current && $state.current.name) {
id = $state.current.name;
if($state.params) {
for(var key in $state.params) {
if($state.params.hasOwnProperty(key) && $state.params[key]) {
id += "_" + key + "=" + $state.params[key];
}
}
}
return id;
}
// if something goes wrong make sure its got a unique stateId
return ionic.Utils.nextUid();
},
goToHistoryRoot: function(historyId) {
if(historyId) {
var hist = $rootScope.$viewHistory.histories[ historyId ];
if(hist && hist.stack.length) {
if($rootScope.$viewHistory.currentView && $rootScope.$viewHistory.currentView.viewId === hist.stack[0].viewId) {
return;
}
$rootScope.$viewHistory.forcedNav = {
viewId: hist.stack[0].viewId,
navAction: 'moveBack',
navDirection: 'back'
};
hist.stack[0].go();
}
}
},
_getViewById: function(viewId) {
return (viewId ? $rootScope.$viewHistory.views[ viewId ] : null );
},
_getBackView: function(view) {
return (view ? this._getViewById(view.backViewId) : null );
},
_getForwardView: function(view) {
return (view ? this._getViewById(view.forwardViewId) : null );
},
_getHistoryById: function(historyId) {
return (historyId ? $rootScope.$viewHistory.histories[ historyId ] : null );
},
_getHistory: function(scope) {
var histObj = this._getParentHistoryObj(scope);
if( !$rootScope.$viewHistory.histories[ histObj.historyId ] ) {
// this history object exists in parent scope, but doesn't
// exist in the history data yet
$rootScope.$viewHistory.histories[ histObj.historyId ] = {
historyId: histObj.historyId,
parentHistoryId: this._getParentHistoryObj(histObj.scope.$parent).historyId,
stack: [],
cursor: -1
};
}
return $rootScope.$viewHistory.histories[ histObj.historyId ];
},
_getParentHistoryObj: function(scope) {
var parentScope = scope;
while(parentScope) {
if(parentScope.hasOwnProperty('$historyId')) {
// this parent scope has a historyId
return { historyId: parentScope.$historyId, scope: parentScope };
}
// nothing found keep climbing up
parentScope = parentScope.$parent;
}
// no history for for the parent, use the root
return { historyId: 'root', scope: $rootScope };
},
nextViewOptions: function(opts) {
if(arguments.length) {
this._nextOpts = opts;
} else {
return this._nextOpts;
}
},
getRenderer: function(navViewElement, navViewAttrs, navViewScope) {
var service = this;
var registerData;
var doAnimation;
// climb up the DOM and see which animation classname to use, if any
var animationClass = getParentAnimationClass(navViewElement[0]);
function getParentAnimationClass(el) {
var className = '';
while(!className && el) {
className = el.getAttribute('animation');
el = el.parentElement;
}
// If they don't have an animation set explicitly, use the value in the config
if(!className) {
return $ionicNavViewConfig.transition;
}
return className;
}
function setAnimationClass() {
// add the animation CSS class we're gonna use to transition between views
if (animationClass) {
navViewElement[0].classList.add(animationClass);
}
if(registerData.navDirection === 'back') {
// animate like we're moving backward
navViewElement[0].classList.add('reverse');
} else {
// defaults to animate forward
// make sure the reverse class isn't already added
navViewElement[0].classList.remove('reverse');
}
}
return function(shouldAnimate) {
return {
enter: function(element) {
if(doAnimation && shouldAnimate) {
// enter with an animation
setAnimationClass();
element.addClass('ng-enter');
$ionicClickBlock.show();
$animate.enter(element, navViewElement, null, function() {
$ionicClickBlock.hide();
if (animationClass) {
navViewElement[0].classList.remove(animationClass);
}
});
return;
} else if(!doAnimation) {
$ionicClickBlock.hide();
}
// no animation
navViewElement.append(element);
},
leave: function() {
var element = navViewElement.contents();
if(doAnimation && shouldAnimate) {
// leave with an animation
setAnimationClass();
$animate.leave(element, function() {
element.remove();
});
return;
}
// no animation
element.remove();
},
register: function(element) {
// register a new view
registerData = service.register(navViewScope, element);
doAnimation = (animationClass !== null && registerData.navDirection !== null);
return registerData;
}
};
};
},
disableRegisterByTagName: function(tagName) {
// not every element should animate betwee transitions
// For example, the <ion-tabs> directive should not animate when it enters,
// but instead the <ion-tabs> directve would just show, and its children
// <ion-tab> directives would do the animating, but <ion-tabs> itself is not a view
$rootScope.$viewHistory.disabledRegistrableTagNames.push(tagName.toUpperCase());
},
isTagNameRegistrable: function(element) {
// check if this element has a tagName (at its root, not recursively)
// that shouldn't be animated, like <ion-tabs> or <ion-side-menu>
var x, y, disabledTags = $rootScope.$viewHistory.disabledRegistrableTagNames;
for(x=0; x<element.length; x++) {
if(element[x].nodeType !== 1) continue;
for(y=0; y<disabledTags.length; y++) {
if(element[x].tagName === disabledTags[y]) {
return false;
}
}
}
return true;
},
clearHistory: function() {
var
histories = $rootScope.$viewHistory.histories,
currentView = $rootScope.$viewHistory.currentView;
if(histories) {
for(var historyId in histories) {
if(histories[historyId].stack) {
histories[historyId].stack = [];
histories[historyId].cursor = -1;
}
if(currentView && currentView.historyId === historyId) {
currentView.backViewId = null;
currentView.forwardViewId = null;
histories[historyId].stack.push(currentView);
} else if(histories[historyId].destroy) {
histories[historyId].destroy();
}
}
}
for(var viewId in $rootScope.$viewHistory.views) {
if(viewId !== currentView.viewId) {
delete $rootScope.$viewHistory.views[viewId];
}
}
if(currentView) {
this.setNavViews(currentView.viewId);
}
}
};
}]);