forked from HazemKhaled/TiWindowStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowStack.js
259 lines (218 loc) · 6.95 KB
/
WindowStack.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
/**
* An Alloy widget to manage windows stack in same code for iOS and Android, with drawer support
*
* more https://github.com/HazemKhaled/TiWindowStack
*/
function WindowStack() {
// The navigation object for iOS
var navigationWindow = null,
// Windows array for history pepuse
windows = [],
that = this,
ANDROID = Ti.Platform.name === 'android',
// Since iOS 10 Ti.Platform.name return 'iOS' instead of 'iPhone OS'
// https://jira.appcelerator.org/browse/TIDOC-2611
IOS = Ti.Platform.name === 'iPhone OS' || Ti.Platform.name === 'iOS';
/**
* Who am i?
* @type {String}
*/
this.apiName = 'ti-window-stack';
/**
* Constant to open the window into the center of the drawer
* @type {Number}
*/
this.CENTER_WINDOW = 101;
/**
* Constant to open the window into the right of the drawer
* @type {Number}
*/
this.RIGHT_WINDOW = 102;
/**
* Constant to open the window into the left of the drawer
* @type {Number}
*/
this.LEFT_WINDOW = 103;
/**
* Which side to open new window in the drawer, center is default
* @type {NUMBER}
*/
this.targetInDrawer = this.CENTER_WINDOW;
/**
* Set external created NavigationWindow
* @param {Ti.UI.NavigationWindow} _navigationWindow NavigationWindow to set
*/
this.setNavigationWindow = function(_navigationWindow) {
navigationWindow = _navigationWindow;
};
/**
* targetInDrawer setter
* @param {NUMBER} targetInDrawer WindowStack.CENTER_WINDOW, WindowStack.RIGHT_WINDOW or WindowStack.LEFT_WINDOW
*/
this.setTargetInDrawer = function (targetInDrawer) {
if ([that.CENTER_WINDOW, that.RIGHT_WINDOW, that.LEFT_WINDOW].indexOf(targetInDrawer) !== -1){
this.targetInDrawer = targetInDrawer;
}
};
/**
* Open window into the stack, you can pass instance from nl.fokkezb.drawer to open this
* window into drawer center window.
*
* nl.fokkezb.drawer require Ti.UI.View for Android and Window for iOS
* @param {Ti.UI.Window/Ti.UI.View} _window Window/View to open
* @param {nl.fokkezb.drawer} drawer nl.fokkezb.drawer instance
* @return {VOID}
*/
this.open = function(_window, drawer, opts) {
if (IOS) {
// Create navigationWindow if we don't have, or if we have side menu
if (navigationWindow === null || drawer) {
navigationWindow = Ti.UI.iOS.createNavigationWindow({
window: _window
});
if (drawer) {
// Open the window in center, right or left?
var openIn = 'setCenterWindow';
if (that.targetInDrawer === that.RIGHT_WINDOW) {
openIn = 'setRightWindow';
} else if (that.targetInDrawer === that.LEFT_WINDOW) {
openIn = 'setLeftWindow';
}
// Open the window into the drawer
drawer[openIn](navigationWindow);
} else {
navigationWindow.open(_.extend({}, opts));
}
// Reset our local stack refrance
windows = [];
} else {
// Or just push new window to the stack
navigationWindow.openWindow(_window);
// Add this window to my stack refrance
windows.push(_window);
}
} else {
if (drawer) {
// Extend some properties to drawer containner
_.extend(drawer.window, _.pick(_window, ['title', 'keepScreenOn', 'exitOnClose']));
// Since android center item is view not a window, we have to fire it ourselves
_window.fireEvent('open');
// Generate Android menu
var activity = drawer.window.getActivity();
activity.onCreateOptionsMenu = function(e) {
// Clean up all items from any other controller
e.menu.clear();
if (_window.hasOwnProperty('onCreateOptionsMenu')) {
_window.onCreateOptionsMenu(e);
}
};
// Try to refresh menus if the activity created
try {
activity.invalidateOptionsMenu();
} catch (e) {
Ti.API.warn('Maybe we still do not have activity to update the menu, it works now by the way');
}
// Open the window in center, right or left?
var openIn = 'setCenterWindow';
if (that.targetInDrawer === that.RIGHT_WINDOW) {
openIn = 'setRightWindow';
} else if (that.targetInDrawer === that.LEFT_WINDOW) {
openIn = 'setLeftWindow';
}
// Open the window into the drawer
drawer[openIn](_window);
// Reset our local stack refrance
windows = [];
} else {
_window.open();
// Add this window to my stack refrance
windows.push(_window);
}
}
// On close the window update the windows array
_window.addEventListener('close', function() {
windows = _.without(windows, _window);
});
};
/**
* Pop window from the stack
* @param {Ti.UI.Window}/{Ti.UI.View} _window Window/View to open
*/
this.close = function(_window) {
if (IOS) {
navigationWindow.closeWindow(_window);
} else {
_window.close();
}
};
this.back = function() {
// Get last window in the stack
var _window = _.last(windows);
// In case assign this function directly to UI item, this will pass to the UI item it self, better use that
that.close(_window);
};
/**
* By default, closes all the windows, one after the other, starting for the current window.
* User sees all the windows getting closed.
*
* @param bool _direct Closes all other windows except the last one (so users don't see the
* other windows getting closed) and finally closes the current window.
* @return void
*/
this.home = function(_params) {
_params = _params || { animated: true };
var lastLength = windows.length,
interval;
if (_params.animated) {
Alloy.Globals.homeInterval = interval = setInterval(function() {
if (lastLength === windows.length) {
Alloy.Globals.windowStack.back();
lastLength--;
if (lastLength === 0 ||
// Center window is actually view on Android
windows[lastLength - 1].apiName === 'Ti.UI.View') {
clearInterval(Alloy.Globals.homeInterval);
}
}
}, 100);
} else {
var lastWindow = _.last(windows);
var rest = _.without(windows, lastWindow);
rest.forEach(function(window, index){
if (index === 0 && window.apiName === 'Ti.UI.View') {
return;
}
that.close(window);
});
that.close(lastWindow);
}
};
/**
* Close all Windows, close the navigationWindow or drawer
* @param {nl.fokkezb.drawer} drawer (Optional) Drwer to close
* @param {function} closeCallBack (Optional) Will call it after close last screen
*/
this.destroy = function(drawer, closeCallBack) {
if (drawer) {
if (closeCallBack) {
IOS ? drawer.addEventListener('close', closeCallBack) : drawer.window.addEventListener('close', closeCallBack);
}
drawer.close();
} else if (IOS) {
closeCallBack && navigationWindow.addEventListener('close', closeCallBack);
navigationWindow.close();
} else {
closeCallBack && _.last(windows).addEventListener('close', closeCallBack);
_.each(windows, function(_window) {
_window.close();
});
}
};
}
/**
* Create new instance
* @return {WindowStack} New instance of WindowStack
*/
exports.createWindowStack = function() {
return new WindowStack();
};