This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Navigation.js
255 lines (214 loc) · 6.41 KB
/
Navigation.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
/**
* Classe responsável por gerenciar a navegação das telas adicionadas a Viewport
*
* @class Mba.ux.Viewport.Navigation
* @singleton
* @alternateClassName viewport.navigation
*/
Ext.define('Mba.ux.Viewport.Navigation', {
alternateClassName: 'viewport.navigation',
config: {
/**
* @cfg [Array} xtypesResetable
* Xtypes mapeados que são resetados {@link #clearNavigationStack} automicatimente,
* também pode ser feito no config da View com atribuito 'resettable'
*/
xtypesResetable: [],
/**
* @cfg {Object} closeApp
* @cfg {String} closeApp.message
* @cfg {Function} closeApp.fn (required)
*/
closeApp: {
message: 'Deseja realmente sair do aplicativo?',
fn: function() {
navigator.app.exitApp();
}
},
/**
* @cfg {Function} [appEmptyHistoryBackFn=Mba.ux.Viewport.Navigation.closeAppFn()]
* Callback utilizado na Viewport para tomar decisão o que fazer quando a navegação retorna para View inicial
*/
appEmptyHistoryBackFn: function() {
this.closeAppFn();
},
/**
* @cfg {Array} navigationStack
* Coleção de views adicionadas na Viewpor
*/
navigationStack: []
},
constructor: function (config) {
this.initConfig(config);
this.callParent([config]);
},
/**
* @method
* Antes de realizar atribuição para o objeto closeApp realiza validação se função de saida foi definida
* @param {Object} currentClose
*/
updateCloseApp: function(currentClose) {
if (!currentClose) {
return;
}
if (!Ext.isObject(currentClose)) {
throw 'CloseApp not object.';
}
if (!Ext.isFunction(currentClose.fn)) {
throw 'Callback Close is required.';
}
},
/**
* @method
* Ativa uma view na Viewport de acordo com xtype, permite setar configs e animação como segundo e terceiro argumento
* @param {xtype} viewXtype
* @param {Object} [options]
* @param {Object} [animation]
* @returns {Object} retorna o objeto de acordo com xtype passado no primeiro argumento
*/
activateView: function(viewXtype, options, animation) {
var view;
if (!(view = Ext.Viewport.child(viewXtype))) {
view = Ext.Viewport.add({xtype: viewXtype});
}
if(!animation && Ext.isFunction(view.getAnimation)) {
animation = view.getAnimation();
animation.direction = 'left';
}
this.setOptionsView(view, options);
if (view.isInnerItem()) {
if (animation) {
Ext.Viewport.animateActiveItem(view, animation);
} else {
Ext.Viewport.setActiveItem(view);
}
} else {
view.show();
}
this.orderHistory(viewXtype);
return view;
},
/**
* @method
* Atribui configs para View (cls, hidden)
* @param {Object} view
* @param {Object} options
* @private
*/
setOptionsView: function(view, options) {
for(var o in options) {
view['set' + Ext.String.capitalize(o)](options[o]);
}
},
/**
* @method
* Adiciona xtype para array {@link #navigationStack} de navegação
* @param {xtype} viewXtype
*/
orderHistory: function(viewXtype) {
var stack = this.getNavigationStack(),
pos;
if ((pos = stack.indexOf(viewXtype)) >= 0) {
stack.splice(pos, 1);
}
stack.push(viewXtype);
this.setNavigationStack(stack);
},
/**
* @method
* Remove item da stack pelo xtype
* @return {Boolean}
*/
removeStack: function(xtype) {
var stack = this.getNavigationStack(),
pos;
pos = stack.indexOf(xtype);
if (pos === -1) {
return false;
}
stack.splice(pos, 1);
return true;
},
/**
* @method
* Navegação entre views, remove a última view adicionada a coleção ativando a na viewport {@link #activateView()}
* @return {Boolean}
*/
back: function() {
var stack = this.getNavigationStack(),
view, xtype, animation;
if (stack.length <= 1) {
return false;
}
this.clearAutoNavigation(stack[stack.length-1]);
xtype = stack.pop();
view = Ext.Viewport.child(xtype);
if (!view.isInnerItem()) {
view.hide();
view.fireEvent('back', view, xtype);
return view;
}
animation = this.getAnimation(view);
if (animation) {
animation.direction = 'right';
}
xtype = stack.pop();
view.fireEvent('back', view, xtype);
if (view.fireEvent('beforeactivate', xtype) !== false) {
this.activateView(xtype, null, animation);
}
return view;
},
/**
* @private
* @param {Object} view
*/
clearAutoNavigation: function(xtype) {
var xtypes = this.getXtypesResetable();
if (xtypes.indexOf(xtype) != -1) {
this.clearNavigationStack();
return true;
}
var view = Ext.Viewport.child(xtype);
if (!view) {
return false;
}
if (view.resettable) {
this.clearNavigationStack();
return true;
}
return false;
},
/**
* @method
* Retorna a animação atribuida para View caso está possua
* @param {xtype} viewXtype
* @returns {Object}
*/
getAnimation: function(view) {
if (typeof view === 'string') {
view = Ext.Viewport.child(view);
}
return view.getAnimation ? view.getAnimation() : null;
},
/**
* @method
* Retira todos xtype atribuidos durante a navegação
*/
clearNavigationStack: function() {
this.setNavigationStack([]);
},
// @private
closeAppFn: function() {
var closeApp = this.getCloseApp();
if (closeApp.message) {
Ext.Msg.confirm(null, closeApp.message, (function(answer) {
if (answer == 'sim') {
closeApp.fn();
}
}).bind(this));
} else {
closeApp.fn();
}
}
});