/
core.js
675 lines (611 loc) · 19.9 KB
/
core.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/**
* @file overview This file contains the core framework class CBMVC_Alloy.
* @author Winson winsonet@gmail.com
* @version v1.5
* @copyright Winson http://www.coderblog.in
* @license MIT License http://www.opensource.org/licenses/mit-license.php
*
* @disclaimer THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var Alloy = require('alloy'),
_ = require('alloy/underscore')._;
/**
* CB app core namespace
* @type {JSON}
*/
var CB = {
/**
* Define all libs namespace
*/
UI: require('ui'),
Util: require('util'),
Network: require('net'),
WP: require('wp'),
Debug: require('debug'),
Cache: require('cache'),
Date: require('date'),
Youtube: require('youtube'),
Animator: require('animator'),
//Social: require('social'),
/**
* Holds data from the JSON config file
*/
ID: null,
VERSION: null,
CVERSION: '1.5.0.0612131440',
LEGAL: {
COPYRIGHT: null,
TOS: null,
PRIVACY: null
},
ConfigurationURL: null,
Nodes: [],
Plugins: null,
Settings: null,
CurrentPage: null,
/**
* The loading view
*/
Loading: Alloy.createWidget("com.chariti.loading").getView(),
cancelLoading: false,
loadingOpen: false,
/**
* Tabs Widget
*/
Tabs: null,
MainContent: null,
_mainContent: null,
_innerContent: null,
_currentController: null,
_previousController: null,
_staticControllers: [],
_backAnimation: null,
_contentWidth: Ti.Platform.displayCaps.platformWidth,
_mainContentwidth: Ti.Platform.displayCaps.platformHeight,
/**
* Device information
*/
Device: {
isHandheld: Alloy.isHandheld,
isTablet: Alloy.isTablet,
type: Alloy.isHandheld ? "handheld" : "tablet",
os: null,
name: null,
version: Titanium.Platform.version,
versionMajor: null,
versionMinor: null,
width: Ti.Platform.displayCaps.platformWidth > Ti.Platform.displayCaps.platformHeight ? Ti.Platform.displayCaps.platformHeight : Ti.Platform.displayCaps.platformWidth,
height: Ti.Platform.displayCaps.platformWidth > Ti.Platform.displayCaps.platformHeight ? Ti.Platform.displayCaps.platformWidth : Ti.Platform.displayCaps.platformHeight,
dpi: Ti.Platform.displayCaps.dpi,
orientation: Ti.Gesture.orientation == Ti.UI.LANDSCAPE_LEFT || Ti.Gesture.orientation == Ti.UI.LANDSCAPE_RIGHT ? "LANDSCAPE" : "PORTRAIT",
statusBarOrientation: null,
isNexus7: (OS_ANDROID && (Ti.Platform.displayCaps.platformWidth == 1280 || (Ti.Platform.displayCaps.platformWidth == 800 && Ti.Platform.displayCaps.platformHeight > 1200)))
},
/**
* Init the core app setting
* @param {object} mainContent view
*/
init: function(content) {
// Global system Events
Ti.Network.addEventListener("change", CB.networkObserver);
Ti.Gesture.addEventListener("orientationchange", CB.orientationObserver);
Ti.App.addEventListener("pause", CB.exitObserver);
Ti.App.addEventListener("close", CB.exitObserver);
Ti.App.addEventListener("resumed", CB.resumeObserver);
// Determine device characteristics
CB.determineDevice();
// Migrate to newer version
require("migrate").init();
// Setup database
CB.setupDatabase();
// Init cache object
CB.Cache.init();
// Init utility object
CB.Util.init(content.index);
// The main content
CB.MainContent = content.main;
// The main content copy
CB._mainContent = content.main;
// Get the current controller
CB._currentController = Alloy.createController(Alloy.CFG.firstController);
},
/**
* Determines the device characteristics
*/
determineDevice: function() {
CB.Device.versionMajor = parseInt(CB.Device.version.split(".")[0], 10);
CB.Device.versionMinor = parseInt(CB.Device.version.split(".")[1], 10);
if(OS_IOS) {
CB.Device.os = "IOS";
if(Ti.Platform.osname.toUpperCase() == "IPHONE") {
CB.Device.name = "IPHONE";
} else if(Ti.Platform.osname.toUpperCase() == "IPAD") {
CB.Device.name = "IPAD";
}
} else if(OS_ANDROID) {
CB.Device.os = "ANDROID";
CB.Device.name = Ti.Platform.model.toUpperCase();
// Fix the display values
CB.Device.width = (CB.Device.width / (CB.Device.dpi / 160));
CB.Device.height = (CB.Device.height / (CB.Device.dpi / 160));
}
},
/**
* Setup the database bindings
*/
setupDatabase: function() {
Ti.API.debug("CB.setupDatabase");
var db = Ti.Database.open(Alloy.CFG.database);
db.execute("CREATE TABLE IF NOT EXISTS updates (url TEXT PRIMARY KEY, time TEXT);");
db.execute("CREATE TABLE IF NOT EXISTS log (time INTEGER, type TEXT, message TEXT);");
// Fill the log table with empty rows that we can 'update', providing a max row limit
var data = db.execute("SELECT time FROM log;");
if(data.rowCount === 0) {
db.execute("BEGIN TRANSACTION;");
for(var i = 0; i < 100; i++) {
db.execute("INSERT INTO log VALUES (" + i + ", \"\", \"\");");
}
db.execute("END TRANSACTION;");
}
data.close();
db.close();
},
/**
* Drops the entire database
*/
dropDatabase: function() {
Ti.API.debug("CB.dropDatabase");
var db = Ti.Database.open(Alloy.CFG.database);
db.remove();
},
/**
* Loads in the appropriate controller and config data
*/
loadContent: function() {
CB.log("debug", "CB.loadContent");
var contentFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "cbapp.json");
if(!contentFile.exists()) {
contentFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + "cbapp.json");
}
var content = contentFile.read();
var data;
try {
data = JSON.parse(content.text);
} catch(_error) {
CB.log("error", "Unable to parse downloaded JSON, reverting to packaged JSON");
contentFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + "cbapp.json");
if(contentFile.exists()) {
content = contentFile.read();
data = JSON.parse(content.text);
} else {
CB.log("error", "Unable to parse local JSON, dying");
alert("Unable to open the application");
return;
}
}
CB.ID = data.id;
CB.VERSION = data.version;
CB.LEGAL = {
COPYRIGHT: data.legal.copyright,
TOS: data.legal.terms,
PRIVACY: data.legal.privacy
};
CB.ConfigurationURL = data.configurationUrl && data.configurationUrl.length > 10 ? data.configurationUrl : false;
CB.Settings = data.settings;
CB.Plugins = data.plugins;
},
/**
* Get current controller
* @return {[type]} [description]
*/
getCurrentController: function(data) {
if(CB._currentController === undefined) {
CB._currentController = Alloy.createController(Alloy.CFG.firstController, data || {});
}
return CB._currentController;
},
/**
* Get Previous controller
* @return {[type]} [description]
*/
getPreviousController: function(data) {
return CB._previousController;
},
/**
* Push controller for page switch, and popup a confirm dialog before leave this page
* @param {string} args.controller , next controller's name
* @param {enum} args.animation , animation type, should be an animation enum object
* @param {int} args.duration , the animation duration
* @param {JSON} args.data , the data for pass to next controller, JSON format
* @param {Function} args.callback , callback function
* @param {bool} args.noTabs , whether don't use custom tabs widget
* @param {string} args.currTab , set the current tab name
* @param {bool} args.showInd , whether show an activity indicator
* @param {enum} args.action , what action to do when push the controller
* @param {Object} args.setInnerContent , whether need to set the inner content
* @param {Object} args.useInnerContent , whether use the inner content
* @param {Object} args.showConfirm , whether need to popup the confirm dialog box
*/
pushConfirmLeaveController: function(args) {
if(args.showConfirm) {
CB.Util.confirm('You have not submit, are you sure leave this page?', 'Leave Page', function(e) {
CB.pushController(args);
});
} else {
CB.pushController(args);
}
},
/**
* Push controller for page switch
* @param {string} args.controller , next controller's name
* @param {enum} args.animation , animation type, should be an animation enum object
* @param {int} args.duration , the animation duration
* @param {JSON} args.data , the data for pass to next controller, JSON format
* @param {Function} args.callback , callback function
* @param {bool} args.noTabs , whether don't use custom tabs widget
* @param {string} args.currTab , set the current tab name
* @param {bool} args.showInd , whether show an activity indicator
* @param {enum} args.action , what action to do when push the controller
* @param {Object} args.setInnerContent , whether need to set the inner content
* @param {Object} args.useInnerContent , whether use the inner content
*/
pushController: function(args) {
if(args.useMainContent) {
CB._mainContent = CB.MainContent;
}
if(args.showInd) {
CB.Util.actInd.show();
}
if(!args.duration) {
args.duration = Alloy.CFG.animationDuration;
}
var oldController = CB._currentController;
var oldView = oldController.getView();
CB._currentController = null;
oldController.onBeforeClose();
//keep the previous controller
//CB._previousController = oldView.name;
if(!oldView.name) {
oldView.name = Alloy.CFG.firstController;
}
if(args.action && args.action == UI.NavAction.Back && CB._previousController !== null) {
args.controller = CB._previousController;
args.currTab = CB._previousController;
if(CB._backAnimation) {
args.animation = CB._backAnimation;
}
CB.Debug.echo('CB._previousController:==' + CB._previousController, 97, 'core.js');
}
if(args.controller === undefined || args.controller === null) {
args.controller = oldView.name;
}
if(args.action && args.action == UI.NavAction.Back && CB._staticControllers.length > 0) {
CB._currentController = CB._staticControllers.pop();
} else {
CB._currentController = Alloy.createController(args.controller, args.data || {});
if(args.setInnerContent) {
CB._innerContent = CB._currentController.getView('content');
}
//init the controller, should be not overwrite
var returnObj = CB._currentController.init(args.controller, args.data);
//run the controller, can be overwrite
CB._currentController.onLoad(returnObj, args.controller);
}
CB.CurrentPage = args.controller;
if(args.static) {
CB._staticControllers.push(oldController);
}
CB._backAnimation = null;
var currentView = CB._currentController.getView();
currentView.name = args.controller;
CB._mainContent.width = CB._contentWidth;
currentView.width = CB._contentWidth;
//CB._mainContent.add(currentView);
//currentView.opacity = 0;
switch(args.animation) {
case CB.UI.AnimationStyle.FadeIn:
CB._backAnimation = CB.UI.AnimationStyle.FadeIn;
currentView.left = 0;
currentView.opacity = 0;
CB._mainContent.add(currentView);
currentView.animate({
opacity: 1,
duration: args.duration
}, function() {
CB.Debug.echo(CB._mainContent.children.length, 172, 'core.js=====');
CB.finishedPush(args, currentView, oldView, oldController);
});
break;
case CB.UI.AnimationStyle.FadeOut:
currentView.left = 0;
currentView.opacity = 0;
CB._mainContent.add(currentView);
oldView.opacity = 1;
oldView.animate({
opacity: 0,
duration: args.duration
}, function() {
currentView.animate({
opacity: 1,
duration: args.duration
}, function() {
CB.finishedPush(args, currentView, oldView, oldController);
});
});
break;
case CB.UI.AnimationStyle.NavLeft:
CB._backAnimation = CB.UI.AnimationStyle.NavRight;
currentView.left = CB._contentWidth;
CB._mainContent.add(currentView);
CB._mainContent.children[0].left = 0;
CB._mainContent.children[0].width = CB._contentWidth;
CB._mainContent.width = CB._mainContentwidth;
CB._mainContent.left = 0;
CB._mainContent.animate({
left: -currentView.left,
duration: args.duration
}, function() {
currentView.left = 0;
CB._mainContent.left = 0;
CB._mainContent.width = CB._contentWidth;
CB.finishedPush(args, currentView, oldView, oldController);
});
break;
case CB.UI.AnimationStyle.NavRight:
//CB._mainContent.children[0].left = 0;
CB._mainContent.width = CB._mainContentwidth;
CB._mainContent.left = -CB._contentWidth;
CB._mainContent.children[0].left = CB._contentWidth;
currentView.left = 0;
CB._mainContent.add(currentView);
CB._mainContent.animate({
left: 0,
duration: args.duration
}, function() {
CB._mainContent.left = 0;
CB._mainContent.width = CB._contentWidth;
CB.finishedPush(args, currentView, oldView, oldController);
});
break;
case CB.UI.AnimationStyle.SlideLeft:
CB._backAnimation = CB.UI.AnimationStyle.SlideRight;
CB._mainContent.left = 0;
currentView.left = CB._contentWidth;
CB._mainContent.width = CB._contentWidth * 2;
CB._mainContent.add(currentView);
CB._mainContent.animate({
left: -CB._contentWidth,
duration: OS_ANDROID ? args.duration + 700 : args.duration
}, function() {
currentView.left = 0;
CB.finishedPush(args, currentView, oldView, oldController);
CB._mainContent.left = 0;
CB._mainContent.width = CB._contentWidth;
});
break;
case CB.UI.AnimationStyle.SlideRight:
CB._mainContent.width = CB._contentWidth * 2;
CB._mainContent.left = -CB._contentWidth;
if(CB._mainContent.children[1]) {
CB._mainContent.children[1].left = CB._contentWidth;
} else {
CB._mainContent.children[0].left = CB._contentWidth;
}
currentView.left = 0;
CB._mainContent.add(currentView);
CB._mainContent.animate({
left: 0,
duration: OS_ANDROID ? args.duration + 700 : args.duration
}, function() {
CB.finishedPush(args, currentView, oldView, oldController);
});
break;
case CB.UI.AnimationStyle.SlideUp:
CB._backAnimation = CB.UI.AnimationStyle.SlideDown;
currentView.top = Ti.Platform.displayCaps.platformHeight * 2;
CB._mainContent.add(currentView);
currentView.animate({
top: 0,
duration: args.duration
}, function() {
CB.finishedPush(args, currentView, oldView, oldController);
});
break;
case CB.UI.AnimationStyle.SlideDown:
oldView.top = 0;
oldView.zIndex = 100;
CB._mainContent.add(currentView);
oldView.animate({
top: Ti.Platform.displayCaps.platformHeight * 2,
duration: args.duration
}, function() {
CB.finishedPush(args, currentView, oldView, oldController);
});
break;
case CB.UI.AnimationStyle.None:
CB._mainContent.add(currentView);
CB.finishedPush(args, currentView, oldView, oldController);
break;
default:
CB._mainContent.add(currentView);
CB.finishedPush(args, currentView, oldView, oldController);
break;
}
},
/**
* the private function after run when the pushController finished
* @param {[type]} args [description]
* @param {[type]} currentView [description]
* @param {[type]} oldView [description]
* @param {[type]} oldController [description]
* @return {[type]} [description]
*/
finishedPush: function(args, currentView, oldView, oldController) {
if(Alloy.CFG.hasCustomTabs && !args.noTabs) {
var tab = args.controller;
if(args.currTab) {
tab = args.currTab;
}
CB.Tabs.setTab(tab);
}
//keep the previous controller
if(args.action && args.action == UI.NavAction.KeepBack) {
CB._previousController = oldView.name;
CB.Debug.echo(CB._previousController, 299, 'core======');
}
oldController.onClose();
// if (Ti.Platform.name !== 'android') {
// CB._mainContent.remove(oldView);
// }
args.callback && args.callback(CB._currentController);
oldController = null;
if(CB._mainContent.children.length > 2) {
CB._mainContent.remove(CB._mainContent.children[CB._mainContent.children.length - 2]);
//CB._mainContent.width = CB._contentWidth;
}
oldView = null;
if(args.showInd) {
CB.Util.actInd.hide();
}
//handle difference mainContent
if(args.useInnerContent && CB._innerContent) {
CB._mainContent = CB._innerContent;
} else {
CB._mainContent = CB.MainContent;
}
},
/**
* Show the remote webserver error by error code
* @param {[type]} errCode [description]
*/
ShowError: function(errCode) {
var errorMsg = '';
switch(errCode) {
case 110:
errorMsg = CB.Util.L('noCatID');
break;
case 120:
errorMsg = 'There is no share event id!';
break;
case 130:
errorMsg = CB.Util.L('noFileObject');
break;
case 140:
errorMsg = CB.Util.L('noDefineLanguage');
break;
case 160:
errorMsg = CB.Util.L('paramsRequire');
break;
case 900:
errorMsg = CB.Util.L('noRecordFound');
break;
case -100:
errorMsg = CB.Util.L('networkErr');
break;
default:
errorMsg = CB.Util.L('unKnowError');
}
var alertDialog = Ti.UI.createAlertDialog({
title: CB.Util.L('error'),
message: errorMsg,
buttonNames: ['OK']
});
alertDialog.show();
},
/**
* Logs all console data
* @param {String} _severity A severity type (debug, error, info, log, trace, warn)
* @param {String} _text The text to log
*/
log: function(_severity, _text) {
switch(_severity.toLowerCase()) {
case "debug":
Ti.API.debug(_text);
break;
case "error":
Ti.API.error(_text);
break;
case "info":
Ti.API.info(_text);
break;
case "log":
Ti.API.log(_text);
break;
case "trace":
Ti.API.trace(_text);
break;
case "warn":
Ti.API.warn(_text);
break;
}
// var db = Ti.Database.open(Alloy.CFG.database);
// var time = new Date().getTime();
// var type = CB.Util.escapeString(_severity);
// var message = CB.Util.escapeString(_text);
// db.execute("UPDATE log SET time = " + time + ", type = " + type + ", message = " + message + " WHERE time = (SELECT min(time) FROM log);");
// db.close();
},
/**
* Global network event handler
* @param {Object} _event Standard Titanium event callback
*/
networkObserver: function(_event) {
CB.log("debug", "CB.networkObserver");
CB.Network.type = _event.networkTypeName;
CB.Network.online = _event.online;
Ti.App.fireEvent("CB:networkChange");
},
/**
* Exit event observer
* @param {Object} _event Standard Titanium event callback
*/
exitObserver: function(_event) {
CB.log("debug", "CB.exitObserver");
},
/**
* Resume event observer
* @param {Object} _event Standard Titanium event callback
*/
resumeObserver: function(_event) {
CB.log("debug", "CB.resumeObserver");
},
/**
* orientationObserver
* @param {Object} _event Standard Titanium event callback
*/
orientationObserver: function(_event) {
CB.log("debug", "CB.orientationObserver");
},
/**
* Back button observer
* @param {Object} _event Standard Titanium event callback
*/
backButtonObserver: function(_event) {
CB.log("debug", "CB.backButtonObserver");
if(CB.modalStack.length > 0) {
CB.removeChild(true);
return;
} else {
var stack;
if(CB.Device.isHandheld || !CB.hasDetail) {
stack = CB.controllerStacks[CB.currentStack];
} else {
stack = CB.detailStacks[CB.currentDetailStack];
}
if(stack.length > 1) {
CB.removeChild();
} else {
CB.MainWindow.close();
}
}
}
}
module.exports = CB;