forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavController.js
152 lines (122 loc) · 3.68 KB
/
navController.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
(function(ionic) {
'use strict';
/**
* The NavController makes it easy to have a stack
* of views or screens that can be pushed and popped
* for a dynamic navigation flow. This API is modelled
* off of the UINavigationController in iOS.
*
* The NavController can drive a nav bar to show a back button
* if the stack can be poppped to go back to the last view, and
* it will handle updating the title of the nav bar and processing animations.
*/
ionic.controllers.NavController = ionic.controllers.ViewController.inherit({
initialize: function(opts) {
var _this = this;
this.navBar = opts.navBar;
this.content = opts.content;
this.controllers = opts.controllers || [];
this._updateNavBar();
// TODO: Is this the best way?
this.navBar.shouldGoBack = function() {
_this.pop();
};
},
/**
* @return {array} the array of controllers on the stack.
*/
getControllers: function() {
return this.controllers;
},
/**
* @return {object} the controller at the top of the stack.
*/
getTopController: function() {
return this.controllers[this.controllers.length-1];
},
/**
* Push a new controller onto the navigation stack. The new controller
* will automatically become the new visible view.
*
* @param {object} controller the controller to push on the stack.
*/
push: function(controller) {
var last = this.controllers[this.controllers.length - 1];
this.controllers.push(controller);
// Indicate we are switching controllers
var shouldSwitch = this.switchingController && this.switchingController(controller) || true;
// Return if navigation cancelled
if(shouldSwitch === false)
return;
// Actually switch the active controllers
if(last) {
last.isVisible = false;
last.visibilityChanged && last.visibilityChanged('push');
}
// Grab the top controller on the stack
var next = this.controllers[this.controllers.length - 1];
next.isVisible = true;
// Trigger visibility change, but send 'first' if this is the first page
next.visibilityChanged && next.visibilityChanged(last ? 'push' : 'first');
this._updateNavBar();
return controller;
},
/**
* Pop the top controller off the stack, and show the last one. This is the
* "back" operation.
*
* @return {object} the last popped controller
*/
pop: function() {
var next, last;
// Make sure we keep one on the stack at all times
if(this.controllers.length < 2) {
return;
}
// Grab the controller behind the top one on the stack
last = this.controllers.pop();
if(last) {
last.isVisible = false;
last.visibilityChanged && last.visibilityChanged('pop');
}
// Remove the old one
//last && last.detach();
next = this.controllers[this.controllers.length - 1];
// TODO: No DOM stuff here
//this.content.el.appendChild(next.el);
next.isVisible = true;
next.visibilityChanged && next.visibilityChanged('pop');
// Switch to it (TODO: Animate or such things here)
this._updateNavBar();
return last;
},
/**
* Show the NavBar (if any)
*/
showNavBar: function() {
if(this.navBar) {
this.navBar.show();
}
},
/**
* Hide the NavBar (if any)
*/
hideNavBar: function() {
if(this.navBar) {
this.navBar.hide();
}
},
// Update the nav bar after a push or pop
_updateNavBar: function() {
if(!this.getTopController() || !this.navBar) {
return;
}
this.navBar.setTitle(this.getTopController().title);
if(this.controllers.length > 1) {
this.navBar.showBackButton(true);
} else {
this.navBar.showBackButton(false);
}
}
});
})(window.ionic);