-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFramework.js
More file actions
279 lines (227 loc) · 8.04 KB
/
Framework.js
File metadata and controls
279 lines (227 loc) · 8.04 KB
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
var Alloy = require('alloy'),
_ = Alloy._,
Backbone = Alloy.Backbone;
/**
* {String} The status of this app (i.e. loggedout, loggedin, activated)
* @private
*/
var _status;
/**
* @class Framework
* @singleton
*
* RebelFrame's general functions, defined as singleton
*/
var Framework = module.exports = _.extend({
/**
* @property {String} LOGGEDOUT User has installed App, but not yet loggedin
*/
LOGGEDOUT: 'loggedout',
/**
* @property {String} LOGGEDOUT User is loggedin, but has not yet activated
*/
LOGGEDIN: 'loggedin',
/**
* @property {String} ACTIVATED User is loggedin, activated and ready to use all the features
*/
ACTIVATED: 'activated',
/**
* @property {Number} deviceWidth Width of the device in dip. We need to devide by the logicalDensityFactor on Android to get the dip
*/
deviceWidth: OS_IOS ? Ti.Platform.displayCaps.platformWidth : Ti.Platform.displayCaps.platformWidth / Ti.Platform.displayCaps.logicalDensityFactor,
/**
* @property {Number} deviceHeight Height of the device in dip. We need to devide by the logicalDensityFactor on Android to get the dip
*/
deviceHeight: OS_IOS ? Ti.Platform.displayCaps.platformHeight : Ti.Platform.displayCaps.platformHeight / Ti.Platform.displayCaps.logicalDensityFactor,
/**
* Sets the status of this app
*
* @param {String} status The status to set
*/
setStatus: function(status) {
var oldStatus = _status;
_status = status;
// Persist new App Status
Ti.App.Properties.setString(Ti.App.id + '.status', status);
// Trigger event, so App can route based on new App Status
Framework.trigger('status', {
oldStatus: oldStatus,
newStatus: _status
});
},
/**
* Returns the status of this app
*
* @return {String} The status
*/
getStatus: function() {
// Ti.API.error('stored status', Ti.App.Properties.getString(Ti.App.id + '.status'));
if (!_status)
_status = Ti.App.Properties.getString(Ti.App.id + '.status', Framework.LOGGEDOUT);
// Ti.API.error('returned status', _status);
return _status;
},
/**
* Extend Alloy's createController en createWidget functions to call construct function when controller is initialized.
*/
extendAlloy: function() {
// Wrap some Alloy functions, so they call construct and destruct methods.
var _alloy_createController = Alloy.createController,
_alloy_createModel = Alloy.createModel,
_alloy_createCollection = Alloy.createCollection,
_alloy_createWidget = Alloy.createWidget,
WM = require('RebelFrame/WindowManager'),
Context = require('RebelFrame/Context');
/**
* Adds openWin en closeWin functions to Alloy Controllers. This way it is easy to call $.openWin() and $.closeWin and are needed close eventlisteners automatically added and removed.
*
* @param {Alloy.Controller} controller Controller to add the functions to
*/
var addFunctions = function(controller, config) {
_.extend(controller, {
/**
* Opens the Window. Also adds a `clse` eventListener, to clean up the controller when the Window is closed.
*
* @param {Ti.UI.Window} [win] Window to open. If not provided, the Controller's top level view is used.
*/
openWin: function(win) {
win = win || controller.getView();
// When Controller is 'root' Window and should show hamburger, make it so
if (!_.isUndefined(config.showSideMenu))
win.showSideMenu = config.showSideMenu;
// When Controller is opened from the XML of a TabGroup, do nothing
else if (!_.isUndefined(config.tabGroupRoot))
return;
function onOpenWin(evt) {
this.removeEventListener('open', onOpenWin);
if(OS_ANDROID) {
Context.on(win.id, this.activity);
}
// If there is a onOpen callback, call it
if(_.isFunction(config.onOpen))
config.onOpen(controller);
}
/**
* Handle `close` event of Window. Removes eventlistener and calls both RebelFrame's destruct as Alloy's destroy functions.
*
* @param {Object} evt Event details
*/
function onCloseWin(evt) {
this.removeEventListener('close', onCloseWin);
if(OS_ANDROID)
Context.off(win.activity);
// If there is a destruct function, call it.
if (_.isFunction(controller.destruct)) {
Ti.API.debug('destruct() called');
controller.destruct.call(controller, evt);
} else
Ti.API.warn('destruct() NOT called');
// Call Aloy's own destroy function
controller.destroy.call(controller, evt);
// If there is a onClose callback, call it
if(_.isFunction(config.onClose))
config.onClose(controller);
// Cleanup possible panning:
if(OS_IOS)
evt.source.keyboardPanning = false;
}
win.addEventListener('open', onOpenWin);
win.addEventListener('close', onCloseWin);
// Open the window
WM.openWin(win);
},
/**
* Close the Window
*
* @param {Ti.UI.Window} [win] Window to close. If not provided, the Controller's top level view is used.
*/
closeWin: function(win) {
win = win || controller.getView();
WM.closeWin(win);
}
});
};
/**
* Call original Alloy.createController function and then construct is it exists. Also track this new screen in Google Analytics
*
* @param {String} name Controller name
* @param {Object} config Controller configuration
*
* @return {Alloy.controller} Created controller, extended with RebelFrame fucntions
*/
Alloy.createController = function(name, config) {
config = config || {};
// Create controller using Alloy's original function
var controller = _alloy_createController(name, config);
// Add custom RebelFrame functions to controller
addFunctions(controller, config);
// Call constructor, if exists
if (controller.construct)
controller.construct.call(controller, config || {});
// Track screen
// if (name !== 'index')
// require('RebelFrame/Tracker').trackScreen(name);
return controller;
};
/**
* Call original Alloy.createWidget function and then construct is it exists. Also track this new screen in Google Analytics
*
* @param {String} name Controller name
* @param {Object} config Controller configuration
*
* @return {Alloy.controller} Created controller, extended with RebelFrame fucntions
*/
Alloy.createWidget = function(name, controller, config) {
config = config || {};
// Create controller using Alloy's original function
var widget = _alloy_createWidget(name, controller, config);
// Also support name, config as arguments, leaving out controller, but do this only after calling the original method.
// Copied from Alloy.js definition
if ("undefined" != typeof controller && null !== controller && _.isObject(controller) && !_.isString(controller)) {
config = controller;
}
// Add custom RebelFrame functions to controller
addFunctions(widget, config);
// Call constructor, if exists
if (widget.construct)
widget.construct.call(widget, config || {});
return widget;
};
},
/**
* Scan the contents of the supplied object
*
* For debugging purposes only
*
* @param {Object} obj The object to scan
* @private
*/
scan: function(obj) {
var key, type;
Ti.API.error('Contents of object:');
for (key in obj) {
type = typeof(obj[key]);
if (type != 'object' && type != 'function')
Ti.API.error(' - ' + key + ': ' + type + ' (' + obj[key] + ')');
else
Ti.API.error(' - ' + key + ': ' + type);
}
},
iosVersion: function(asArray) {
if(!OS_IOS)
return false;
var version = Ti.Platform.version.split(".");
if(asArray)
return version;
var asFloat = parseInt(version[0]);
if(version[1])
asFloat += parseInt(version[1])/10;
if(version[2])
asFloat += parseInt(version[2])/100;
return asFloat;
}
}, Backbone.Events);
// Create some basic globals that can be used in TSS
Alloy.Globals.deviceHeight = Framework.deviceHeight;
Alloy.Globals.screenHeight = Framework.deviceHeight - (OS_IOS ? 64 : 72);
Alloy.Globals.screenWidth = Framework.deviceWidth;