This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
ionic-core.js
468 lines (412 loc) · 13.1 KB
/
ionic-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
angular.module('ionic.service.core', [])
/**
* @private
* Provides a safe interface to store objects in persistent memory
*/
.provider('persistentStorage', function() {
return {
$get: ['$q', '$window', function($q, $window) {
var objectCache = {};
var memoryLocks = {};
var persistenceStrategy = {
get: function(key) {
return $window.localStorage.getItem(key);
},
remove: function(key) {
return $window.localStorage.removeItem(key);
},
set: function(key, value) {
return $window.localStorage.setItem(key, value);
}
};
return {
/**
* Stores an object in local storage under the given key
*/
storeObject: function(key, object) {
// Convert object to JSON and store in localStorage
var json = JSON.stringify(object);
persistenceStrategy.set(key, json);
// Then store it in the object cache
objectCache[key] = object;
},
/**
* Either retrieves the cached copy of an object,
* or the object itself from localStorage.
* Returns null if the object couldn't be found.
*/
retrieveObject: function(key) {
// First check to see if it's the object cache
var cached = objectCache[key];
if (cached) {
return cached;
}
// Deserialize the object from JSON
var json = persistenceStrategy.get(key);
// null or undefined --> return null.
if (json == null) {
return null;
}
try {
return JSON.parse(json);
} catch (err) {
return null;
}
},
/**
* Locks the async call represented by the given promise and lock key.
* Only one asyncFunction given by the lockKey can be running at any time.
*
* @param lockKey should be a string representing the name of this async call.
* This is required for persistence.
* @param asyncFunction Returns a promise of the async call.
* @returns A new promise, identical to the one returned by asyncFunction,
* but with two new errors: 'in_progress', and 'last_call_interrupted'.
*/
lockedAsyncCall: function(lockKey, asyncFunction) {
var deferred = $q.defer();
// If the memory lock is set, error out.
if (memoryLocks[lockKey]) {
deferred.reject('in_progress');
return deferred.promise;
}
// If there is a stored lock but no memory lock, flag a persistence error
if (persistenceStrategy.get(lockKey) === 'locked') {
deferred.reject('last_call_interrupted');
deferred.promise.then(null, function() {
persistenceStrategy.remove(lockKey);
});
return deferred.promise;
}
// Set stored and memory locks
memoryLocks[lockKey] = true;
persistenceStrategy.set(lockKey, 'locked');
// Perform the async operation
asyncFunction().then(function(successData) {
deferred.resolve(successData);
// Remove stored and memory locks
delete memoryLocks[lockKey];
persistenceStrategy.remove(lockKey);
}, function(errorData) {
deferred.reject(errorData);
// Remove stored and memory locks
delete memoryLocks[lockKey];
persistenceStrategy.remove(lockKey);
}, function(notifyData) {
deferred.notify(notifyData);
});
return deferred.promise;
}
};
}]
};
})
/**
* A core Ionic account identity provider.
*
* Usage:
* angular.module('myApp', ['ionic', 'ionic.service.core'])
* .config(['$ionicAppProvider', function($ionicAccountProvider) {
* $ionicAppProvider.identify({
* app_id: 'x34dfxjydi23dx'
* });
* }]);
*/
.provider('$ionicApp', ['$httpProvider', function($httpProvider) {
var app = {};
var settings = {
'api_server': 'https://apps.ionic.io',
'push_api_server': 'https://push.ionic.io',
'analytics_api_server': 'https://analytics.ionic.io'
};
var _is_cordova_available = function() {
console.log('Ionic Core: searching for cordova.js');
try {
if (window.cordova || cordova) {
console.log('Ionic Core: cordova.js has already been loaded');
return true;
}
} catch(e) {}
var scripts = document.getElementsByTagName('script');
var len = scripts.length;
for(var i = 0; i < len; i++) {
var script = scripts[i].getAttribute('src');
if(script) {
var parts = script.split('/');
var partsLength = 0;
try {
partsLength = parts.length;
if (parts[partsLength-1] === 'cordova.js') {
console.log('Ionic Core: cordova.js has previously been included.');
return true;
}
} catch(e) {}
}
}
return false;
};
this.identify = function(opts) {
if (!opts.gcm_id){
opts.gcm_id = 'None';
}
app = opts;
};
/**
* Set a config property.
*/
this.set = function(k, v) {
settings[k] = v;
};
this.setApiServer = function(server) {
settings.api_server = server;
};
this.$get = [function() {
return {
getId: function() {
return app.app_id;
},
getGcmId: function(){
return app.gcm_id;
},
getValue: function(k) {
return settings[k];
},
getApiUrl: function() {
return this.getValue('api_server');
},
getApiKey: function() {
return app.api_key;
},
getApiEndpoint: function(service) {
var app = this.getApp();
if(!app) return null;
return this.getApiUrl() + '/api/v1/' + app.app_id + '/' + service;
},
/**
* Get the registered app for all commands.
*/
getApp: function() {
return app;
},
getDeviceTypeByNavigator: function() {
return (navigator.userAgent.match(/iPad/i)) == "iPad" ? "ipad" : (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "iphone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "blackberry" : "unknown";
},
loadCordova: function() {
if(!_is_cordova_available()) {
var cordova_script = document.createElement('script');
var cordova_src = 'cordova.js';
switch(this.getDeviceTypeByNavigator()) {
case 'android':
if (window.location.href.substring(0, 4) === "file") {
cordova_src = 'file:///android_asset/www/cordova.js';
}
break;
case 'ipad':
case 'iphone':
try {
var resource = window.location.search.match(/cordova_js_bootstrap_resource=(.*?)(&|#|$)/i);
if (resource) {
cordova_src = decodeURI(resource[1]);
}
} catch(e) {
console.log('Could not find cordova_js_bootstrap_resource query param');
console.log(e);
}
break;
case 'unknown':
return false;
default:
break;
}
cordova_script.setAttribute('src', cordova_src);
document.head.appendChild(cordova_script);
console.log('Ionic Core: injecting cordova.js');
}
},
bootstrap: function() {
this.loadCordova();
}
}
}];
}])
/**
* @ngdoc service
* @name $ionicUser
* @module ionic.service.core
* @description
*
* An interface for storing data to a user object which will be sent with many ionic services
*
* Add tracking data to the user by passing objects in to the identify function.
* The _id property identifies the user on this device and cannot be overwritten.
*
* @usage
* ```javascript
* $ionicUser.get();
*
* // Add info to user object
* $ionicUser.identify({
* username: "Timmy"
* });
*
* ```
*/
.factory('$ionicUser', [
'$q',
'$ionicCoreSettings',
'$timeout',
'$http',
'persistentStorage',
'$ionicApp',
function($q, $ionicCoreSettings, $timeout, $http, persistentStorage, $ionicApp) {
// User object we'll use to store all our user info
var storageKeyName = 'ionic_analytics_user_' + $ionicApp.getApp().app_id,
user = persistentStorage.retrieveObject(storageKeyName) || {},
deviceCordova = ionic.Platform.device(),
device = {
screen_width: window.innerWidth * (window.devicePixelRatio || 1),
screen_height: window.innerHeight * (window.devicePixelRatio || 1)
};
if (deviceCordova.model) device.model = deviceCordova.model;
if (deviceCordova.platform) device.platform = deviceCordova.platform;
if (deviceCordova.version) device.version = deviceCordova.version;
if (deviceCordova.uuid) device.uuid = deviceCordova.uuid;
// Flag if we've changed anything on our user
var dirty = false;
dirty = storeOrDirty('is_on_device', ionic.Platform.isWebView());
dirty = storeOrDirty('device', device);
if (!user._id) {
user._id = generateGuid();
dirty = true;
}
if (dirty) {
persistentStorage.storeObject(storageKeyName, user);
}
function generateGuid() {
// Some crazy bit-twiddling to generate a random guid
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
function storeOrDirty(key, value) {
// Store the key on the user object and return whether something changed
if (!angular.equals(user[key], value)) {
user[key] = value;
return true;
}
return false;
}
return {
/**
* Push a value to the array with the given key.
* @param key the key
* @param value the value
* @param isUnique whether to only push if it doesn't exist in the set
*
*/
_op: function(key, value, type) {
var u = user.user_id;
var appId = '';
if ($ionicCoreSettings.get('app_id')) {
appId = $ionicCoreSettings.get('app_id')
} else {
appId = $ionicApp.getId();
}
if(!u) {
throw new Error("Please call identify with a user_id before calling push");
}
var o = {};
o['user_id'] = u;
o[key] = value;
return $http.post($ionicApp.getApiUrl() + '/api/v1/app/' + appId + '/users/' + type, o);
},
/**
* Push the given value into the array field identified by the key.
* Pass true to isUnique to only push the value if the value does not
* already exist in the array.
*/
push: function(key, value, isUnique) {
if(isUnique) {
return this._op(key, value, 'pushUnique');
} else {
return this._op(key, value, 'push');
}
},
/**
* Pull a given value out of the array identified by key.
*/
pull: function(key, value) {
return this._op(key, value, 'pull');
},
/**
* Set the given value under the key in the user. This overwrites
* any other data under that field. To append data to list, use push above.
*/
set: function(key, value) {
return this._op(key, value, 'set');
},
/**
* Remove the field for the given key.
*/
unset: function(key) {
return this._op(key, '', 'unset');
},
generateGUID: function() {
return generateGuid();
},
identify: function(userData) {
var appId = '';
if ($ionicCoreSettings.get('app_id')) {
appId = $ionicCoreSettings.get('app_id')
} else {
appId = $ionicApp.getId();
}
if (!userData.user_id) {
var msg = 'You must supply a unique user_id field.';
throw new Error(msg)
}
// Copy all the data into our user object
angular.extend(user, userData);
// Write the user object to our local storage
persistentStorage.storeObject(storageKeyName, user);
return $http.post($ionicApp.getApiUrl() + '/api/v1/app/' + appId + '/users/identify', userData);
},
identifyAnonymous: function() {
var appId = '';
if ($ionicCoreSettings.get('app_id')) {
appId = $ionicCoreSettings.get('app_id')
} else {
appId = $ionicApp.getId();
}
userData = {};
userData['user_id'] = generateGuid();
userData['isAnonymous'] = true;
// Copy all the data into our user object
angular.extend(user, userData);
// Write the user object to our local storage
persistentStorage.storeObject(storageKeyName, user);
return $http.post($ionicApp.getApiUrl() + '/api/v1/app/' + appId + '/users/identify', userData);
},
get: function() {
return user;
}
}
}])
// Auto-generated configuration factory
.factory('$ionicCoreSettings', function() {
var settings = {};
return {
get: function(setting) {
if (settings[setting]) {
return settings[setting];
}
return null;
}
}
})
// Auto-generated configuration factory
.run(['$ionicApp', function($ionicApp) {
console.log('Ionic Core: init');
$ionicApp.bootstrap();
}]);