interval-braining-ui-asset-pack 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +0 -0
  3. data/README.md +0 -0
  4. data/Rakefile +0 -0
  5. data/lib/asset_pack.rb +0 -0
  6. data/lib/asset_pack/engine.rb +0 -0
  7. data/lib/asset_pack/version.rb +1 -1
  8. data/lib/assets/javascripts/asset_pack/angular-animate.js +0 -0
  9. data/lib/assets/javascripts/asset_pack/angular-input-match.js +0 -0
  10. data/lib/assets/javascripts/asset_pack/angular-messages.js +0 -0
  11. data/lib/assets/javascripts/asset_pack/angular-mocks.js +0 -0
  12. data/lib/assets/javascripts/asset_pack/angular-resource.js +0 -0
  13. data/lib/assets/javascripts/asset_pack/angular-sanitize.js +0 -0
  14. data/lib/assets/javascripts/asset_pack/angular-ui-router-breadcrumbs.js +0 -0
  15. data/lib/assets/javascripts/asset_pack/angular-ui-router-helpers.js +0 -0
  16. data/lib/assets/javascripts/asset_pack/angular-ui-router-hooks-before-state.js +0 -0
  17. data/lib/assets/javascripts/asset_pack/angular-ui-router.js +0 -0
  18. data/lib/assets/javascripts/asset_pack/angular-validate-in-set.js +0 -0
  19. data/lib/assets/javascripts/asset_pack/angular.js +0 -0
  20. data/lib/assets/javascripts/asset_pack/bootstrap.js +0 -0
  21. data/lib/assets/javascripts/asset_pack/cardigan.js +0 -0
  22. data/lib/assets/javascripts/asset_pack/growl-notifications.js +0 -0
  23. data/lib/assets/javascripts/asset_pack/jquery.js +0 -0
  24. data/lib/assets/javascripts/asset_pack/localforage.js +1 -0
  25. data/lib/assets/javascripts/asset_pack/lodash.js +1 -0
  26. data/lib/assets/javascripts/asset_pack/lodash.underscore.js +1 -0
  27. data/lib/assets/stylesheets/asset_pack/OpenSans-Light.css +0 -0
  28. data/lib/assets/stylesheets/asset_pack/animate.css +0 -0
  29. data/lib/assets/stylesheets/asset_pack/bootstrap.css +0 -0
  30. data/lib/assets/stylesheets/asset_pack/cardigan.css +0 -0
  31. data/lib/assets/stylesheets/asset_pack/font-awesome.css +0 -0
  32. data/vendor/assets/bower_components/angular-animate/angular-animate.js +0 -0
  33. data/vendor/assets/bower_components/angular-growl-notifications/dist/growl-notifications.js +0 -0
  34. data/vendor/assets/bower_components/angular-input-match/dist/angular-input-match.js +0 -0
  35. data/vendor/assets/bower_components/angular-messages/angular-messages.js +0 -0
  36. data/vendor/assets/bower_components/angular-mocks/angular-mocks.js +0 -0
  37. data/vendor/assets/bower_components/angular-resource/angular-resource.js +0 -0
  38. data/vendor/assets/bower_components/angular-sanitize/angular-sanitize.js +0 -0
  39. data/vendor/assets/bower_components/angular-ui-router-breadcrumbs/dist/angular-ui-router-breadcrumbs.js +0 -0
  40. data/vendor/assets/bower_components/angular-ui-router-helpers/dist/angular-ui-router-helpers.js +0 -0
  41. data/vendor/assets/bower_components/angular-ui-router-hooks-before-state/dist/angular-ui-router-hooks-before-state.js +0 -0
  42. data/vendor/assets/bower_components/angular-ui-router/release/angular-ui-router.js +0 -0
  43. data/vendor/assets/bower_components/angular-validate-in-set/dist/angular-validate-in-set.js +0 -0
  44. data/vendor/assets/bower_components/angular/angular.js +0 -0
  45. data/vendor/assets/bower_components/bootstrap/dist/css/bootstrap.css +0 -0
  46. data/vendor/assets/bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.eot +0 -0
  47. data/vendor/assets/bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.svg +0 -0
  48. data/vendor/assets/bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf +0 -0
  49. data/vendor/assets/bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.woff +0 -0
  50. data/vendor/assets/bower_components/bootstrap/dist/js/bootstrap.js +0 -0
  51. data/vendor/assets/bower_components/cardigan/dist/cardigan.css +0 -0
  52. data/vendor/assets/bower_components/cardigan/dist/cardigan.js +0 -0
  53. data/vendor/assets/bower_components/font-awesome-bower/css/font-awesome.css +0 -0
  54. data/vendor/assets/bower_components/font-awesome-bower/fonts/FontAwesome.otf +0 -0
  55. data/vendor/assets/bower_components/font-awesome-bower/fonts/fontawesome-webfont.eot +0 -0
  56. data/vendor/assets/bower_components/font-awesome-bower/fonts/fontawesome-webfont.svg +0 -0
  57. data/vendor/assets/bower_components/font-awesome-bower/fonts/fontawesome-webfont.ttf +0 -0
  58. data/vendor/assets/bower_components/font-awesome-bower/fonts/fontawesome-webfont.woff +0 -0
  59. data/vendor/assets/bower_components/jquery/dist/jquery.js +0 -0
  60. data/vendor/assets/bower_components/localforage/dist/localforage.js +2227 -0
  61. data/vendor/assets/bower_components/lodash/dist/lodash.js +6785 -0
  62. data/vendor/assets/bower_components/lodash/dist/lodash.underscore.js +4979 -0
  63. data/vendor/assets/bower_components/webfont-OpenSans-Light/dist/css/OpenSans-Light.css +0 -0
  64. data/vendor/assets/bower_components/webfont-OpenSans-Light/dist/fonts/OpenSans-Light.eot +0 -0
  65. data/vendor/assets/bower_components/webfont-OpenSans-Light/dist/fonts/OpenSans-Light.svg +0 -0
  66. data/vendor/assets/bower_components/webfont-OpenSans-Light/dist/fonts/OpenSans-Light.ttf +0 -0
  67. data/vendor/assets/bower_components/webfont-OpenSans-Light/dist/fonts/OpenSans-Light.woff +0 -0
  68. metadata +8 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1a176db0fcafe143de3d3a4b65bce910786550d
4
- data.tar.gz: a5606f8c85b24d2b53569b5911fb151bb9471c3f
3
+ metadata.gz: 933797f0585ab9607658fc07b7a7712e5459349b
4
+ data.tar.gz: d5837be6bf046bc2855edc065f5c84ad24f4dfd0
5
5
  SHA512:
6
- metadata.gz: a7dae2fd9574f8d1f0f7d94142733d5b4fc6c430021207af80fa090e6f7b434a9de6570faf037589db4e1d68339e5ea19012908e3d61ba7a4c1846c8da0cd65e
7
- data.tar.gz: 7dda0ffcda0b21711793962dbc4c41631a73015994b6b3f4d94fca418fb9bd0b871f3c556b49b0f2eb225396c3a88e0ae43fde6f472f0c27ae5121ac7d9be712
6
+ metadata.gz: 5e0adf8b3af6e58b75e932833e7821ab604a6a35a4004a6da94b165fa2f67184f38cd71bd668269c56613832054f6b1d5b44f7861682fa4c1735eba68066e000
7
+ data.tar.gz: 48a4c51127d23061ef9dcb2854f250a992f2a439842da4714c2d648a1e687c6f1b782d2be74b4fb032f966989137799897e42d820a44686b60503ddd597e7440
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
data/lib/asset_pack.rb CHANGED
File without changes
File without changes
@@ -1,3 +1,3 @@
1
1
  module AssetPack
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ //= require localforage/dist/localforage
@@ -0,0 +1 @@
1
+ //= require lodash/dist/lodash
@@ -0,0 +1 @@
1
+ //= require lodash/dist/lodash.underscore.js
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,2227 @@
1
+ /*!
2
+ localForage -- Offline Storage, Improved
3
+ Version 0.9.2
4
+ http://mozilla.github.io/localForage
5
+ (c) 2013-2014 Mozilla, Apache License 2.0
6
+ */
7
+ (function() {
8
+ var define, requireModule, require, requirejs;
9
+
10
+ (function() {
11
+ var registry = {}, seen = {};
12
+
13
+ define = function(name, deps, callback) {
14
+ registry[name] = { deps: deps, callback: callback };
15
+ };
16
+
17
+ requirejs = require = requireModule = function(name) {
18
+ requirejs._eak_seen = registry;
19
+
20
+ if (seen[name]) { return seen[name]; }
21
+ seen[name] = {};
22
+
23
+ if (!registry[name]) {
24
+ throw new Error("Could not find module " + name);
25
+ }
26
+
27
+ var mod = registry[name],
28
+ deps = mod.deps,
29
+ callback = mod.callback,
30
+ reified = [],
31
+ exports;
32
+
33
+ for (var i=0, l=deps.length; i<l; i++) {
34
+ if (deps[i] === 'exports') {
35
+ reified.push(exports = {});
36
+ } else {
37
+ reified.push(requireModule(resolve(deps[i])));
38
+ }
39
+ }
40
+
41
+ var value = callback.apply(this, reified);
42
+ return seen[name] = exports || value;
43
+
44
+ function resolve(child) {
45
+ if (child.charAt(0) !== '.') { return child; }
46
+ var parts = child.split("/");
47
+ var parentBase = name.split("/").slice(0, -1);
48
+
49
+ for (var i=0, l=parts.length; i<l; i++) {
50
+ var part = parts[i];
51
+
52
+ if (part === '..') { parentBase.pop(); }
53
+ else if (part === '.') { continue; }
54
+ else { parentBase.push(part); }
55
+ }
56
+
57
+ return parentBase.join("/");
58
+ }
59
+ };
60
+ })();
61
+
62
+ define("promise/all",
63
+ ["./utils","exports"],
64
+ function(__dependency1__, __exports__) {
65
+ "use strict";
66
+ /* global toString */
67
+
68
+ var isArray = __dependency1__.isArray;
69
+ var isFunction = __dependency1__.isFunction;
70
+
71
+ /**
72
+ Returns a promise that is fulfilled when all the given promises have been
73
+ fulfilled, or rejected if any of them become rejected. The return promise
74
+ is fulfilled with an array that gives all the values in the order they were
75
+ passed in the `promises` array argument.
76
+
77
+ Example:
78
+
79
+ ```javascript
80
+ var promise1 = RSVP.resolve(1);
81
+ var promise2 = RSVP.resolve(2);
82
+ var promise3 = RSVP.resolve(3);
83
+ var promises = [ promise1, promise2, promise3 ];
84
+
85
+ RSVP.all(promises).then(function(array){
86
+ // The array here would be [ 1, 2, 3 ];
87
+ });
88
+ ```
89
+
90
+ If any of the `promises` given to `RSVP.all` are rejected, the first promise
91
+ that is rejected will be given as an argument to the returned promises's
92
+ rejection handler. For example:
93
+
94
+ Example:
95
+
96
+ ```javascript
97
+ var promise1 = RSVP.resolve(1);
98
+ var promise2 = RSVP.reject(new Error("2"));
99
+ var promise3 = RSVP.reject(new Error("3"));
100
+ var promises = [ promise1, promise2, promise3 ];
101
+
102
+ RSVP.all(promises).then(function(array){
103
+ // Code here never runs because there are rejected promises!
104
+ }, function(error) {
105
+ // error.message === "2"
106
+ });
107
+ ```
108
+
109
+ @method all
110
+ @for RSVP
111
+ @param {Array} promises
112
+ @param {String} label
113
+ @return {Promise} promise that is fulfilled when all `promises` have been
114
+ fulfilled, or rejected if any of them become rejected.
115
+ */
116
+ function all(promises) {
117
+ /*jshint validthis:true */
118
+ var Promise = this;
119
+
120
+ if (!isArray(promises)) {
121
+ throw new TypeError('You must pass an array to all.');
122
+ }
123
+
124
+ return new Promise(function(resolve, reject) {
125
+ var results = [], remaining = promises.length,
126
+ promise;
127
+
128
+ if (remaining === 0) {
129
+ resolve([]);
130
+ }
131
+
132
+ function resolver(index) {
133
+ return function(value) {
134
+ resolveAll(index, value);
135
+ };
136
+ }
137
+
138
+ function resolveAll(index, value) {
139
+ results[index] = value;
140
+ if (--remaining === 0) {
141
+ resolve(results);
142
+ }
143
+ }
144
+
145
+ for (var i = 0; i < promises.length; i++) {
146
+ promise = promises[i];
147
+
148
+ if (promise && isFunction(promise.then)) {
149
+ promise.then(resolver(i), reject);
150
+ } else {
151
+ resolveAll(i, promise);
152
+ }
153
+ }
154
+ });
155
+ }
156
+
157
+ __exports__.all = all;
158
+ });
159
+ define("promise/asap",
160
+ ["exports"],
161
+ function(__exports__) {
162
+ "use strict";
163
+ var browserGlobal = (typeof window !== 'undefined') ? window : {};
164
+ var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
165
+ var local = (typeof global !== 'undefined') ? global : (this === undefined? window:this);
166
+
167
+ // node
168
+ function useNextTick() {
169
+ return function() {
170
+ process.nextTick(flush);
171
+ };
172
+ }
173
+
174
+ function useMutationObserver() {
175
+ var iterations = 0;
176
+ var observer = new BrowserMutationObserver(flush);
177
+ var node = document.createTextNode('');
178
+ observer.observe(node, { characterData: true });
179
+
180
+ return function() {
181
+ node.data = (iterations = ++iterations % 2);
182
+ };
183
+ }
184
+
185
+ function useSetTimeout() {
186
+ return function() {
187
+ local.setTimeout(flush, 1);
188
+ };
189
+ }
190
+
191
+ var queue = [];
192
+ function flush() {
193
+ for (var i = 0; i < queue.length; i++) {
194
+ var tuple = queue[i];
195
+ var callback = tuple[0], arg = tuple[1];
196
+ callback(arg);
197
+ }
198
+ queue = [];
199
+ }
200
+
201
+ var scheduleFlush;
202
+
203
+ // Decide what async method to use to triggering processing of queued callbacks:
204
+ if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
205
+ scheduleFlush = useNextTick();
206
+ } else if (BrowserMutationObserver) {
207
+ scheduleFlush = useMutationObserver();
208
+ } else {
209
+ scheduleFlush = useSetTimeout();
210
+ }
211
+
212
+ function asap(callback, arg) {
213
+ var length = queue.push([callback, arg]);
214
+ if (length === 1) {
215
+ // If length is 1, that means that we need to schedule an async flush.
216
+ // If additional callbacks are queued before the queue is flushed, they
217
+ // will be processed by this flush that we are scheduling.
218
+ scheduleFlush();
219
+ }
220
+ }
221
+
222
+ __exports__.asap = asap;
223
+ });
224
+ define("promise/config",
225
+ ["exports"],
226
+ function(__exports__) {
227
+ "use strict";
228
+ var config = {
229
+ instrument: false
230
+ };
231
+
232
+ function configure(name, value) {
233
+ if (arguments.length === 2) {
234
+ config[name] = value;
235
+ } else {
236
+ return config[name];
237
+ }
238
+ }
239
+
240
+ __exports__.config = config;
241
+ __exports__.configure = configure;
242
+ });
243
+ define("promise/polyfill",
244
+ ["./promise","./utils","exports"],
245
+ function(__dependency1__, __dependency2__, __exports__) {
246
+ "use strict";
247
+ /*global self*/
248
+ var RSVPPromise = __dependency1__.Promise;
249
+ var isFunction = __dependency2__.isFunction;
250
+
251
+ function polyfill() {
252
+ var local;
253
+
254
+ if (typeof global !== 'undefined') {
255
+ local = global;
256
+ } else if (typeof window !== 'undefined' && window.document) {
257
+ local = window;
258
+ } else {
259
+ local = self;
260
+ }
261
+
262
+ var es6PromiseSupport =
263
+ "Promise" in local &&
264
+ // Some of these methods are missing from
265
+ // Firefox/Chrome experimental implementations
266
+ "resolve" in local.Promise &&
267
+ "reject" in local.Promise &&
268
+ "all" in local.Promise &&
269
+ "race" in local.Promise &&
270
+ // Older version of the spec had a resolver object
271
+ // as the arg rather than a function
272
+ (function() {
273
+ var resolve;
274
+ new local.Promise(function(r) { resolve = r; });
275
+ return isFunction(resolve);
276
+ }());
277
+
278
+ if (!es6PromiseSupport) {
279
+ local.Promise = RSVPPromise;
280
+ }
281
+ }
282
+
283
+ __exports__.polyfill = polyfill;
284
+ });
285
+ define("promise/promise",
286
+ ["./config","./utils","./all","./race","./resolve","./reject","./asap","exports"],
287
+ function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
288
+ "use strict";
289
+ var config = __dependency1__.config;
290
+ var configure = __dependency1__.configure;
291
+ var objectOrFunction = __dependency2__.objectOrFunction;
292
+ var isFunction = __dependency2__.isFunction;
293
+ var now = __dependency2__.now;
294
+ var all = __dependency3__.all;
295
+ var race = __dependency4__.race;
296
+ var staticResolve = __dependency5__.resolve;
297
+ var staticReject = __dependency6__.reject;
298
+ var asap = __dependency7__.asap;
299
+
300
+ var counter = 0;
301
+
302
+ config.async = asap; // default async is asap;
303
+
304
+ function Promise(resolver) {
305
+ if (!isFunction(resolver)) {
306
+ throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
307
+ }
308
+
309
+ if (!(this instanceof Promise)) {
310
+ throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
311
+ }
312
+
313
+ this._subscribers = [];
314
+
315
+ invokeResolver(resolver, this);
316
+ }
317
+
318
+ function invokeResolver(resolver, promise) {
319
+ function resolvePromise(value) {
320
+ resolve(promise, value);
321
+ }
322
+
323
+ function rejectPromise(reason) {
324
+ reject(promise, reason);
325
+ }
326
+
327
+ try {
328
+ resolver(resolvePromise, rejectPromise);
329
+ } catch(e) {
330
+ rejectPromise(e);
331
+ }
332
+ }
333
+
334
+ function invokeCallback(settled, promise, callback, detail) {
335
+ var hasCallback = isFunction(callback),
336
+ value, error, succeeded, failed;
337
+
338
+ if (hasCallback) {
339
+ try {
340
+ value = callback(detail);
341
+ succeeded = true;
342
+ } catch(e) {
343
+ failed = true;
344
+ error = e;
345
+ }
346
+ } else {
347
+ value = detail;
348
+ succeeded = true;
349
+ }
350
+
351
+ if (handleThenable(promise, value)) {
352
+ return;
353
+ } else if (hasCallback && succeeded) {
354
+ resolve(promise, value);
355
+ } else if (failed) {
356
+ reject(promise, error);
357
+ } else if (settled === FULFILLED) {
358
+ resolve(promise, value);
359
+ } else if (settled === REJECTED) {
360
+ reject(promise, value);
361
+ }
362
+ }
363
+
364
+ var PENDING = void 0;
365
+ var SEALED = 0;
366
+ var FULFILLED = 1;
367
+ var REJECTED = 2;
368
+
369
+ function subscribe(parent, child, onFulfillment, onRejection) {
370
+ var subscribers = parent._subscribers;
371
+ var length = subscribers.length;
372
+
373
+ subscribers[length] = child;
374
+ subscribers[length + FULFILLED] = onFulfillment;
375
+ subscribers[length + REJECTED] = onRejection;
376
+ }
377
+
378
+ function publish(promise, settled) {
379
+ var child, callback, subscribers = promise._subscribers, detail = promise._detail;
380
+
381
+ for (var i = 0; i < subscribers.length; i += 3) {
382
+ child = subscribers[i];
383
+ callback = subscribers[i + settled];
384
+
385
+ invokeCallback(settled, child, callback, detail);
386
+ }
387
+
388
+ promise._subscribers = null;
389
+ }
390
+
391
+ Promise.prototype = {
392
+ constructor: Promise,
393
+
394
+ _state: undefined,
395
+ _detail: undefined,
396
+ _subscribers: undefined,
397
+
398
+ then: function(onFulfillment, onRejection) {
399
+ var promise = this;
400
+
401
+ var thenPromise = new this.constructor(function() {});
402
+
403
+ if (this._state) {
404
+ var callbacks = arguments;
405
+ config.async(function invokePromiseCallback() {
406
+ invokeCallback(promise._state, thenPromise, callbacks[promise._state - 1], promise._detail);
407
+ });
408
+ } else {
409
+ subscribe(this, thenPromise, onFulfillment, onRejection);
410
+ }
411
+
412
+ return thenPromise;
413
+ },
414
+
415
+ 'catch': function(onRejection) {
416
+ return this.then(null, onRejection);
417
+ }
418
+ };
419
+
420
+ Promise.all = all;
421
+ Promise.race = race;
422
+ Promise.resolve = staticResolve;
423
+ Promise.reject = staticReject;
424
+
425
+ function handleThenable(promise, value) {
426
+ var then = null,
427
+ resolved;
428
+
429
+ try {
430
+ if (promise === value) {
431
+ throw new TypeError("A promises callback cannot return that same promise.");
432
+ }
433
+
434
+ if (objectOrFunction(value)) {
435
+ then = value.then;
436
+
437
+ if (isFunction(then)) {
438
+ then.call(value, function(val) {
439
+ if (resolved) { return true; }
440
+ resolved = true;
441
+
442
+ if (value !== val) {
443
+ resolve(promise, val);
444
+ } else {
445
+ fulfill(promise, val);
446
+ }
447
+ }, function(val) {
448
+ if (resolved) { return true; }
449
+ resolved = true;
450
+
451
+ reject(promise, val);
452
+ });
453
+
454
+ return true;
455
+ }
456
+ }
457
+ } catch (error) {
458
+ if (resolved) { return true; }
459
+ reject(promise, error);
460
+ return true;
461
+ }
462
+
463
+ return false;
464
+ }
465
+
466
+ function resolve(promise, value) {
467
+ if (promise === value) {
468
+ fulfill(promise, value);
469
+ } else if (!handleThenable(promise, value)) {
470
+ fulfill(promise, value);
471
+ }
472
+ }
473
+
474
+ function fulfill(promise, value) {
475
+ if (promise._state !== PENDING) { return; }
476
+ promise._state = SEALED;
477
+ promise._detail = value;
478
+
479
+ config.async(publishFulfillment, promise);
480
+ }
481
+
482
+ function reject(promise, reason) {
483
+ if (promise._state !== PENDING) { return; }
484
+ promise._state = SEALED;
485
+ promise._detail = reason;
486
+
487
+ config.async(publishRejection, promise);
488
+ }
489
+
490
+ function publishFulfillment(promise) {
491
+ publish(promise, promise._state = FULFILLED);
492
+ }
493
+
494
+ function publishRejection(promise) {
495
+ publish(promise, promise._state = REJECTED);
496
+ }
497
+
498
+ __exports__.Promise = Promise;
499
+ });
500
+ define("promise/race",
501
+ ["./utils","exports"],
502
+ function(__dependency1__, __exports__) {
503
+ "use strict";
504
+ /* global toString */
505
+ var isArray = __dependency1__.isArray;
506
+
507
+ /**
508
+ `RSVP.race` allows you to watch a series of promises and act as soon as the
509
+ first promise given to the `promises` argument fulfills or rejects.
510
+
511
+ Example:
512
+
513
+ ```javascript
514
+ var promise1 = new RSVP.Promise(function(resolve, reject){
515
+ setTimeout(function(){
516
+ resolve("promise 1");
517
+ }, 200);
518
+ });
519
+
520
+ var promise2 = new RSVP.Promise(function(resolve, reject){
521
+ setTimeout(function(){
522
+ resolve("promise 2");
523
+ }, 100);
524
+ });
525
+
526
+ RSVP.race([promise1, promise2]).then(function(result){
527
+ // result === "promise 2" because it was resolved before promise1
528
+ // was resolved.
529
+ });
530
+ ```
531
+
532
+ `RSVP.race` is deterministic in that only the state of the first completed
533
+ promise matters. For example, even if other promises given to the `promises`
534
+ array argument are resolved, but the first completed promise has become
535
+ rejected before the other promises became fulfilled, the returned promise
536
+ will become rejected:
537
+
538
+ ```javascript
539
+ var promise1 = new RSVP.Promise(function(resolve, reject){
540
+ setTimeout(function(){
541
+ resolve("promise 1");
542
+ }, 200);
543
+ });
544
+
545
+ var promise2 = new RSVP.Promise(function(resolve, reject){
546
+ setTimeout(function(){
547
+ reject(new Error("promise 2"));
548
+ }, 100);
549
+ });
550
+
551
+ RSVP.race([promise1, promise2]).then(function(result){
552
+ // Code here never runs because there are rejected promises!
553
+ }, function(reason){
554
+ // reason.message === "promise2" because promise 2 became rejected before
555
+ // promise 1 became fulfilled
556
+ });
557
+ ```
558
+
559
+ @method race
560
+ @for RSVP
561
+ @param {Array} promises array of promises to observe
562
+ @param {String} label optional string for describing the promise returned.
563
+ Useful for tooling.
564
+ @return {Promise} a promise that becomes fulfilled with the value the first
565
+ completed promises is resolved with if the first completed promise was
566
+ fulfilled, or rejected with the reason that the first completed promise
567
+ was rejected with.
568
+ */
569
+ function race(promises) {
570
+ /*jshint validthis:true */
571
+ var Promise = this;
572
+
573
+ if (!isArray(promises)) {
574
+ throw new TypeError('You must pass an array to race.');
575
+ }
576
+ return new Promise(function(resolve, reject) {
577
+ var results = [], promise;
578
+
579
+ for (var i = 0; i < promises.length; i++) {
580
+ promise = promises[i];
581
+
582
+ if (promise && typeof promise.then === 'function') {
583
+ promise.then(resolve, reject);
584
+ } else {
585
+ resolve(promise);
586
+ }
587
+ }
588
+ });
589
+ }
590
+
591
+ __exports__.race = race;
592
+ });
593
+ define("promise/reject",
594
+ ["exports"],
595
+ function(__exports__) {
596
+ "use strict";
597
+ /**
598
+ `RSVP.reject` returns a promise that will become rejected with the passed
599
+ `reason`. `RSVP.reject` is essentially shorthand for the following:
600
+
601
+ ```javascript
602
+ var promise = new RSVP.Promise(function(resolve, reject){
603
+ reject(new Error('WHOOPS'));
604
+ });
605
+
606
+ promise.then(function(value){
607
+ // Code here doesn't run because the promise is rejected!
608
+ }, function(reason){
609
+ // reason.message === 'WHOOPS'
610
+ });
611
+ ```
612
+
613
+ Instead of writing the above, your code now simply becomes the following:
614
+
615
+ ```javascript
616
+ var promise = RSVP.reject(new Error('WHOOPS'));
617
+
618
+ promise.then(function(value){
619
+ // Code here doesn't run because the promise is rejected!
620
+ }, function(reason){
621
+ // reason.message === 'WHOOPS'
622
+ });
623
+ ```
624
+
625
+ @method reject
626
+ @for RSVP
627
+ @param {Any} reason value that the returned promise will be rejected with.
628
+ @param {String} label optional string for identifying the returned promise.
629
+ Useful for tooling.
630
+ @return {Promise} a promise that will become rejected with the given
631
+ `reason`.
632
+ */
633
+ function reject(reason) {
634
+ /*jshint validthis:true */
635
+ var Promise = this;
636
+
637
+ return new Promise(function (resolve, reject) {
638
+ reject(reason);
639
+ });
640
+ }
641
+
642
+ __exports__.reject = reject;
643
+ });
644
+ define("promise/resolve",
645
+ ["exports"],
646
+ function(__exports__) {
647
+ "use strict";
648
+ function resolve(value) {
649
+ /*jshint validthis:true */
650
+ if (value && typeof value === 'object' && value.constructor === this) {
651
+ return value;
652
+ }
653
+
654
+ var Promise = this;
655
+
656
+ return new Promise(function(resolve) {
657
+ resolve(value);
658
+ });
659
+ }
660
+
661
+ __exports__.resolve = resolve;
662
+ });
663
+ define("promise/utils",
664
+ ["exports"],
665
+ function(__exports__) {
666
+ "use strict";
667
+ function objectOrFunction(x) {
668
+ return isFunction(x) || (typeof x === "object" && x !== null);
669
+ }
670
+
671
+ function isFunction(x) {
672
+ return typeof x === "function";
673
+ }
674
+
675
+ function isArray(x) {
676
+ return Object.prototype.toString.call(x) === "[object Array]";
677
+ }
678
+
679
+ // Date.now is not available in browsers < IE9
680
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Compatibility
681
+ var now = Date.now || function() { return new Date().getTime(); };
682
+
683
+
684
+ __exports__.objectOrFunction = objectOrFunction;
685
+ __exports__.isFunction = isFunction;
686
+ __exports__.isArray = isArray;
687
+ __exports__.now = now;
688
+ });
689
+ requireModule('promise/polyfill').polyfill();
690
+ }());// Some code originally from async_storage.js in
691
+ // [Gaia](https://github.com/mozilla-b2g/gaia).
692
+ (function() {
693
+ 'use strict';
694
+
695
+ // Originally found in https://github.com/mozilla-b2g/gaia/blob/e8f624e4cc9ea945727278039b3bc9bcb9f8667a/shared/js/async_storage.js
696
+
697
+ // Promises!
698
+ var Promise = (typeof module !== 'undefined' && module.exports) ?
699
+ require('promise') : this.Promise;
700
+
701
+ var db = null;
702
+ var dbInfo = {};
703
+
704
+ // Initialize IndexedDB; fall back to vendor-prefixed versions if needed.
705
+ var indexedDB = indexedDB || this.indexedDB || this.webkitIndexedDB ||
706
+ this.mozIndexedDB || this.OIndexedDB ||
707
+ this.msIndexedDB;
708
+
709
+ // If IndexedDB isn't available, we get outta here!
710
+ if (!indexedDB) {
711
+ return;
712
+ }
713
+
714
+ // Open the IndexedDB database (automatically creates one if one didn't
715
+ // previously exist), using any options set in the config.
716
+ function _initStorage(options) {
717
+ if (options) {
718
+ for (var i in options) {
719
+ dbInfo[i] = options[i];
720
+ }
721
+ }
722
+
723
+ return new Promise(function(resolve, reject) {
724
+ var openreq = indexedDB.open(dbInfo.name, dbInfo.version);
725
+ openreq.onerror = function() {
726
+ reject(openreq.error);
727
+ };
728
+ openreq.onupgradeneeded = function() {
729
+ // First time setup: create an empty object store
730
+ openreq.result.createObjectStore(dbInfo.storeName);
731
+ };
732
+ openreq.onsuccess = function() {
733
+ db = openreq.result;
734
+ resolve();
735
+ };
736
+ });
737
+ }
738
+
739
+ function getItem(key, callback) {
740
+ var _this = this;
741
+ return new Promise(function(resolve, reject) {
742
+ _this.ready().then(function() {
743
+ var store = db.transaction(dbInfo.storeName, 'readonly')
744
+ .objectStore(dbInfo.storeName);
745
+ var req = store.get(key);
746
+
747
+ req.onsuccess = function() {
748
+ var value = req.result;
749
+ if (value === undefined) {
750
+ value = null;
751
+ }
752
+
753
+ deferCallback(callback,value);
754
+
755
+ resolve(value);
756
+ };
757
+
758
+ req.onerror = function() {
759
+ if (callback) {
760
+ callback(null, req.error);
761
+ }
762
+
763
+ reject(req.error);
764
+ };
765
+ }, reject);
766
+ });
767
+ }
768
+
769
+ function setItem(key, value, callback) {
770
+ var _this = this;
771
+ return new Promise(function(resolve, reject) {
772
+ _this.ready().then(function() {
773
+ var store = db.transaction(dbInfo.storeName, 'readwrite')
774
+ .objectStore(dbInfo.storeName);
775
+
776
+ // The reason we don't _save_ null is because IE 10 does
777
+ // not support saving the `null` type in IndexedDB. How
778
+ // ironic, given the bug below!
779
+ // See: https://github.com/mozilla/localForage/issues/161
780
+ if (value === null) {
781
+ value = undefined;
782
+ }
783
+
784
+ var req = store.put(value, key);
785
+ req.onsuccess = function() {
786
+ // Cast to undefined so the value passed to
787
+ // callback/promise is the same as what one would get out
788
+ // of `getItem()` later. This leads to some weirdness
789
+ // (setItem('foo', undefined) will return `null`), but
790
+ // it's not my fault localStorage is our baseline and that
791
+ // it's weird.
792
+ if (value === undefined) {
793
+ value = null;
794
+ }
795
+
796
+ deferCallback(callback, value);
797
+
798
+ resolve(value);
799
+ };
800
+ req.onerror = function() {
801
+ if (callback) {
802
+ callback(null, req.error);
803
+ }
804
+
805
+ reject(req.error);
806
+ };
807
+ }, reject);
808
+ });
809
+ }
810
+
811
+ function removeItem(key, callback) {
812
+ var _this = this;
813
+ return new Promise(function(resolve, reject) {
814
+ _this.ready().then(function() {
815
+ var store = db.transaction(dbInfo.storeName, 'readwrite')
816
+ .objectStore(dbInfo.storeName);
817
+
818
+ // We use a Grunt task to make this safe for IE and some
819
+ // versions of Android (including those used by Cordova).
820
+ // Normally IE won't like `.delete()` and will insist on
821
+ // using `['delete']()`, but we have a build step that
822
+ // fixes this for us now.
823
+ var req = store["delete"](key);
824
+ req.onsuccess = function() {
825
+
826
+ deferCallback(callback);
827
+
828
+ resolve();
829
+ };
830
+
831
+ req.onerror = function() {
832
+ if (callback) {
833
+ callback(req.error);
834
+ }
835
+
836
+ reject(req.error);
837
+ };
838
+
839
+ // The request will be aborted if we've exceeded our storage
840
+ // space. In this case, we will reject with a specific
841
+ // "QuotaExceededError".
842
+ req.onabort = function(event) {
843
+ var error = event.target.error;
844
+ if (error === 'QuotaExceededError') {
845
+ if (callback) {
846
+ callback(error);
847
+ }
848
+
849
+ reject(error);
850
+ }
851
+ };
852
+ }, reject);
853
+ });
854
+ }
855
+
856
+ function clear(callback) {
857
+ var _this = this;
858
+ return new Promise(function(resolve, reject) {
859
+ _this.ready().then(function() {
860
+ var store = db.transaction(dbInfo.storeName, 'readwrite')
861
+ .objectStore(dbInfo.storeName);
862
+ var req = store.clear();
863
+
864
+ req.onsuccess = function() {
865
+ deferCallback(callback);
866
+
867
+ resolve();
868
+ };
869
+
870
+ req.onerror = function() {
871
+ if (callback) {
872
+ callback(null, req.error);
873
+ }
874
+
875
+ reject(req.error);
876
+ };
877
+ }, reject);
878
+ });
879
+ }
880
+
881
+ function length(callback) {
882
+ var _this = this;
883
+ return new Promise(function(resolve, reject) {
884
+ _this.ready().then(function() {
885
+ var store = db.transaction(dbInfo.storeName, 'readonly')
886
+ .objectStore(dbInfo.storeName);
887
+ var req = store.count();
888
+
889
+ req.onsuccess = function() {
890
+ if (callback) {
891
+ callback(req.result);
892
+ }
893
+
894
+ resolve(req.result);
895
+ };
896
+
897
+ req.onerror = function() {
898
+ if (callback) {
899
+ callback(null, req.error);
900
+ }
901
+
902
+ reject(req.error);
903
+ };
904
+ }, reject);
905
+ });
906
+ }
907
+
908
+ function key(n, callback) {
909
+ var _this = this;
910
+ return new Promise(function(resolve, reject) {
911
+ if (n < 0) {
912
+ if (callback) {
913
+ callback(null);
914
+ }
915
+
916
+ resolve(null);
917
+
918
+ return;
919
+ }
920
+
921
+ _this.ready().then(function() {
922
+ var store = db.transaction(dbInfo.storeName, 'readonly')
923
+ .objectStore(dbInfo.storeName);
924
+
925
+ var advanced = false;
926
+ var req = store.openCursor();
927
+ req.onsuccess = function() {
928
+ var cursor = req.result;
929
+ if (!cursor) {
930
+ // this means there weren't enough keys
931
+ if (callback) {
932
+ callback(null);
933
+ }
934
+
935
+ resolve(null);
936
+
937
+ return;
938
+ }
939
+
940
+ if (n === 0) {
941
+ // We have the first key, return it if that's what they
942
+ // wanted.
943
+ if (callback) {
944
+ callback(cursor.key);
945
+ }
946
+
947
+ resolve(cursor.key);
948
+ } else {
949
+ if (!advanced) {
950
+ // Otherwise, ask the cursor to skip ahead n
951
+ // records.
952
+ advanced = true;
953
+ cursor.advance(n);
954
+ } else {
955
+ // When we get here, we've got the nth key.
956
+ if (callback) {
957
+ callback(cursor.key);
958
+ }
959
+
960
+ resolve(cursor.key);
961
+ }
962
+ }
963
+ };
964
+
965
+ req.onerror = function() {
966
+ if (callback) {
967
+ callback(null, req.error);
968
+ }
969
+
970
+ reject(req.error);
971
+ };
972
+ }, reject);
973
+ });
974
+ }
975
+
976
+ function keys(callback) {
977
+ var _this = this;
978
+
979
+ return new Promise(function(resolve, reject) {
980
+ _this.ready().then(function() {
981
+ var store = db.transaction(dbInfo.storeName, 'readonly')
982
+ .objectStore(dbInfo.storeName);
983
+
984
+ var req = store.openCursor();
985
+ var keys = [];
986
+
987
+ req.onsuccess = function() {
988
+ var cursor = req.result;
989
+
990
+ if (!cursor) {
991
+ if (callback) {
992
+ callback(keys);
993
+ }
994
+
995
+ resolve(keys);
996
+ return;
997
+ }
998
+
999
+ keys.push(cursor.key);
1000
+ cursor["continue"]();
1001
+ };
1002
+
1003
+ req.onerror = function() {
1004
+ if (callback) {
1005
+ callback(null, req.error);
1006
+ }
1007
+
1008
+ reject(req.error);
1009
+ };
1010
+ }, reject);
1011
+ });
1012
+ }
1013
+
1014
+ // Under Chrome the callback is called before the changes (save, clear)
1015
+ // are actually made. So we use a defer function which wait that the
1016
+ // call stack to be empty.
1017
+ // For more info : https://github.com/mozilla/localForage/issues/175
1018
+ // Pull request : https://github.com/mozilla/localForage/pull/178
1019
+ function deferCallback(callback, value) {
1020
+ if (callback) {
1021
+ return setTimeout(function() {
1022
+ return callback(value);
1023
+ }, 0);
1024
+ }
1025
+ }
1026
+
1027
+ var asyncStorage = {
1028
+ _driver: 'asyncStorage',
1029
+ _initStorage: _initStorage,
1030
+ getItem: getItem,
1031
+ setItem: setItem,
1032
+ removeItem: removeItem,
1033
+ clear: clear,
1034
+ length: length,
1035
+ key: key,
1036
+ keys: keys
1037
+ };
1038
+
1039
+ if (typeof define === 'function' && define.amd) {
1040
+ define('asyncStorage', function() {
1041
+ return asyncStorage;
1042
+ });
1043
+ } else if (typeof module !== 'undefined' && module.exports) {
1044
+ module.exports = asyncStorage;
1045
+ } else {
1046
+ this.asyncStorage = asyncStorage;
1047
+ }
1048
+ }).call(this);
1049
+ // If IndexedDB isn't available, we'll fall back to localStorage.
1050
+ // Note that this will have considerable performance and storage
1051
+ // side-effects (all data will be serialized on save and only data that
1052
+ // can be converted to a string via `JSON.stringify()` will be saved).
1053
+ (function() {
1054
+ 'use strict';
1055
+
1056
+ var keyPrefix = '';
1057
+ var dbInfo = {};
1058
+ // Promises!
1059
+ var Promise = (typeof module !== 'undefined' && module.exports) ?
1060
+ require('promise') : this.Promise;
1061
+ var localStorage = null;
1062
+
1063
+ // If the app is running inside a Google Chrome packaged webapp, or some
1064
+ // other context where localStorage isn't available, we don't use
1065
+ // localStorage. This feature detection is preferred over the old
1066
+ // `if (window.chrome && window.chrome.runtime)` code.
1067
+ // See: https://github.com/mozilla/localForage/issues/68
1068
+ try {
1069
+ // If localStorage isn't available, we get outta here!
1070
+ // This should be inside a try catch
1071
+ if (!this.localStorage || !('setItem' in this.localStorage)) {
1072
+ return;
1073
+ }
1074
+ // Initialize localStorage and create a variable to use throughout
1075
+ // the code.
1076
+ localStorage = this.localStorage;
1077
+ } catch (e) {
1078
+ return;
1079
+ }
1080
+
1081
+ // Config the localStorage backend, using options set in the config.
1082
+ function _initStorage(options) {
1083
+ if (options) {
1084
+ for (var i in options) {
1085
+ dbInfo[i] = options[i];
1086
+ }
1087
+ }
1088
+
1089
+ keyPrefix = dbInfo.name + '/';
1090
+
1091
+ return Promise.resolve();
1092
+ }
1093
+
1094
+ var SERIALIZED_MARKER = '__lfsc__:';
1095
+ var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length;
1096
+
1097
+ // OMG the serializations!
1098
+ var TYPE_ARRAYBUFFER = 'arbf';
1099
+ var TYPE_BLOB = 'blob';
1100
+ var TYPE_INT8ARRAY = 'si08';
1101
+ var TYPE_UINT8ARRAY = 'ui08';
1102
+ var TYPE_UINT8CLAMPEDARRAY = 'uic8';
1103
+ var TYPE_INT16ARRAY = 'si16';
1104
+ var TYPE_INT32ARRAY = 'si32';
1105
+ var TYPE_UINT16ARRAY = 'ur16';
1106
+ var TYPE_UINT32ARRAY = 'ui32';
1107
+ var TYPE_FLOAT32ARRAY = 'fl32';
1108
+ var TYPE_FLOAT64ARRAY = 'fl64';
1109
+ var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH +
1110
+ TYPE_ARRAYBUFFER.length;
1111
+
1112
+ // Remove all keys from the datastore, effectively destroying all data in
1113
+ // the app's key/value store!
1114
+ function clear(callback) {
1115
+ var _this = this;
1116
+ return new Promise(function(resolve, reject) {
1117
+ _this.ready().then(function() {
1118
+ localStorage.clear();
1119
+
1120
+ if (callback) {
1121
+ callback();
1122
+ }
1123
+
1124
+ resolve();
1125
+ }, reject);
1126
+ });
1127
+ }
1128
+
1129
+ // Retrieve an item from the store. Unlike the original async_storage
1130
+ // library in Gaia, we don't modify return values at all. If a key's value
1131
+ // is `undefined`, we pass that value to the callback function.
1132
+ function getItem(key, callback) {
1133
+ var _this = this;
1134
+ return new Promise(function(resolve, reject) {
1135
+ _this.ready().then(function() {
1136
+ try {
1137
+ var result = localStorage.getItem(keyPrefix + key);
1138
+
1139
+ // If a result was found, parse it from the serialized
1140
+ // string into a JS object. If result isn't truthy, the key
1141
+ // is likely undefined and we'll pass it straight to the
1142
+ // callback.
1143
+ if (result) {
1144
+ result = _deserialize(result);
1145
+ }
1146
+
1147
+ if (callback) {
1148
+ callback(result);
1149
+ }
1150
+
1151
+ resolve(result);
1152
+ } catch (e) {
1153
+ if (callback) {
1154
+ callback(null, e);
1155
+ }
1156
+
1157
+ reject(e);
1158
+ }
1159
+ }, reject);
1160
+ });
1161
+ }
1162
+
1163
+ // Same as localStorage's key() method, except takes a callback.
1164
+ function key(n, callback) {
1165
+ var _this = this;
1166
+ return new Promise(function(resolve, reject) {
1167
+ _this.ready().then(function() {
1168
+ var result;
1169
+ try {
1170
+ result = localStorage.key(n);
1171
+ } catch (error) {
1172
+ result = null;
1173
+ }
1174
+
1175
+ // Remove the prefix from the key, if a key is found.
1176
+ if (result) {
1177
+ result = result.substring(keyPrefix.length);
1178
+ }
1179
+
1180
+ if (callback) {
1181
+ callback(result);
1182
+ }
1183
+ resolve(result);
1184
+ }, reject);
1185
+ });
1186
+ }
1187
+
1188
+ function keys(callback) {
1189
+ var _this = this;
1190
+ return new Promise(function(resolve, reject) {
1191
+ _this.ready().then(function() {
1192
+ var length = localStorage.length;
1193
+ var keys = [];
1194
+
1195
+ for (var i = 0; i < length; i++) {
1196
+ keys.push(localStorage.key(i).substring(keyPrefix.length));
1197
+ }
1198
+
1199
+ if (callback) {
1200
+ callback(keys);
1201
+ }
1202
+
1203
+ resolve(keys);
1204
+ }, reject);
1205
+ });
1206
+ }
1207
+
1208
+ // Supply the number of keys in the datastore to the callback function.
1209
+ function length(callback) {
1210
+ var _this = this;
1211
+ return new Promise(function(resolve, reject) {
1212
+ _this.ready().then(function() {
1213
+ var result = localStorage.length;
1214
+
1215
+ if (callback) {
1216
+ callback(result);
1217
+ }
1218
+
1219
+ resolve(result);
1220
+ }, reject);
1221
+ });
1222
+ }
1223
+
1224
+ // Remove an item from the store, nice and simple.
1225
+ function removeItem(key, callback) {
1226
+ var _this = this;
1227
+ return new Promise(function(resolve, reject) {
1228
+ _this.ready().then(function() {
1229
+ localStorage.removeItem(keyPrefix + key);
1230
+
1231
+ if (callback) {
1232
+ callback();
1233
+ }
1234
+
1235
+ resolve();
1236
+ }, reject);
1237
+ });
1238
+ }
1239
+
1240
+ // Deserialize data we've inserted into a value column/field. We place
1241
+ // special markers into our strings to mark them as encoded; this isn't
1242
+ // as nice as a meta field, but it's the only sane thing we can do whilst
1243
+ // keeping localStorage support intact.
1244
+ //
1245
+ // Oftentimes this will just deserialize JSON content, but if we have a
1246
+ // special marker (SERIALIZED_MARKER, defined above), we will extract
1247
+ // some kind of arraybuffer/binary data/typed array out of the string.
1248
+ function _deserialize(value) {
1249
+ // If we haven't marked this string as being specially serialized (i.e.
1250
+ // something other than serialized JSON), we can just return it and be
1251
+ // done with it.
1252
+ if (value.substring(0,
1253
+ SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) {
1254
+ return JSON.parse(value);
1255
+ }
1256
+
1257
+ // The following code deals with deserializing some kind of Blob or
1258
+ // TypedArray. First we separate out the type of data we're dealing
1259
+ // with from the data itself.
1260
+ var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH);
1261
+ var type = value.substring(SERIALIZED_MARKER_LENGTH,
1262
+ TYPE_SERIALIZED_MARKER_LENGTH);
1263
+
1264
+ // Fill the string into a ArrayBuffer.
1265
+ // 2 bytes for each char.
1266
+ var buffer = new ArrayBuffer(serializedString.length * 2);
1267
+ var bufferView = new Uint16Array(buffer);
1268
+ for (var i = serializedString.length - 1; i >= 0; i--) {
1269
+ bufferView[i] = serializedString.charCodeAt(i);
1270
+ }
1271
+
1272
+ // Return the right type based on the code/type set during
1273
+ // serialization.
1274
+ switch (type) {
1275
+ case TYPE_ARRAYBUFFER:
1276
+ return buffer;
1277
+ case TYPE_BLOB:
1278
+ return new Blob([buffer]);
1279
+ case TYPE_INT8ARRAY:
1280
+ return new Int8Array(buffer);
1281
+ case TYPE_UINT8ARRAY:
1282
+ return new Uint8Array(buffer);
1283
+ case TYPE_UINT8CLAMPEDARRAY:
1284
+ return new Uint8ClampedArray(buffer);
1285
+ case TYPE_INT16ARRAY:
1286
+ return new Int16Array(buffer);
1287
+ case TYPE_UINT16ARRAY:
1288
+ return new Uint16Array(buffer);
1289
+ case TYPE_INT32ARRAY:
1290
+ return new Int32Array(buffer);
1291
+ case TYPE_UINT32ARRAY:
1292
+ return new Uint32Array(buffer);
1293
+ case TYPE_FLOAT32ARRAY:
1294
+ return new Float32Array(buffer);
1295
+ case TYPE_FLOAT64ARRAY:
1296
+ return new Float64Array(buffer);
1297
+ default:
1298
+ throw new Error('Unkown type: ' + type);
1299
+ }
1300
+ }
1301
+
1302
+ // Converts a buffer to a string to store, serialized, in the backend
1303
+ // storage library.
1304
+ function _bufferToString(buffer) {
1305
+ var str = '';
1306
+ var uint16Array = new Uint16Array(buffer);
1307
+
1308
+ try {
1309
+ str = String.fromCharCode.apply(null, uint16Array);
1310
+ } catch (e) {
1311
+ // This is a fallback implementation in case the first one does
1312
+ // not work. This is required to get the phantomjs passing...
1313
+ for (var i = 0; i < uint16Array.length; i++) {
1314
+ str += String.fromCharCode(uint16Array[i]);
1315
+ }
1316
+ }
1317
+
1318
+ return str;
1319
+ }
1320
+
1321
+ // Serialize a value, afterwards executing a callback (which usually
1322
+ // instructs the `setItem()` callback/promise to be executed). This is how
1323
+ // we store binary data with localStorage.
1324
+ function _serialize(value, callback) {
1325
+ var valueString = '';
1326
+ if (value) {
1327
+ valueString = value.toString();
1328
+ }
1329
+
1330
+ // Cannot use `value instanceof ArrayBuffer` or such here, as these
1331
+ // checks fail when running the tests using casper.js...
1332
+ //
1333
+ // TODO: See why those tests fail and use a better solution.
1334
+ if (value && (value.toString() === '[object ArrayBuffer]' ||
1335
+ value.buffer && value.buffer.toString() === '[object ArrayBuffer]')) {
1336
+ // Convert binary arrays to a string and prefix the string with
1337
+ // a special marker.
1338
+ var buffer;
1339
+ var marker = SERIALIZED_MARKER;
1340
+
1341
+ if (value instanceof ArrayBuffer) {
1342
+ buffer = value;
1343
+ marker += TYPE_ARRAYBUFFER;
1344
+ } else {
1345
+ buffer = value.buffer;
1346
+
1347
+ if (valueString === '[object Int8Array]') {
1348
+ marker += TYPE_INT8ARRAY;
1349
+ } else if (valueString === '[object Uint8Array]') {
1350
+ marker += TYPE_UINT8ARRAY;
1351
+ } else if (valueString === '[object Uint8ClampedArray]') {
1352
+ marker += TYPE_UINT8CLAMPEDARRAY;
1353
+ } else if (valueString === '[object Int16Array]') {
1354
+ marker += TYPE_INT16ARRAY;
1355
+ } else if (valueString === '[object Uint16Array]') {
1356
+ marker += TYPE_UINT16ARRAY;
1357
+ } else if (valueString === '[object Int32Array]') {
1358
+ marker += TYPE_INT32ARRAY;
1359
+ } else if (valueString === '[object Uint32Array]') {
1360
+ marker += TYPE_UINT32ARRAY;
1361
+ } else if (valueString === '[object Float32Array]') {
1362
+ marker += TYPE_FLOAT32ARRAY;
1363
+ } else if (valueString === '[object Float64Array]') {
1364
+ marker += TYPE_FLOAT64ARRAY;
1365
+ } else {
1366
+ callback(new Error("Failed to get type for BinaryArray"));
1367
+ }
1368
+ }
1369
+
1370
+ callback(marker + _bufferToString(buffer));
1371
+ } else if (valueString === "[object Blob]") {
1372
+ // Conver the blob to a binaryArray and then to a string.
1373
+ var fileReader = new FileReader();
1374
+
1375
+ fileReader.onload = function() {
1376
+ var str = _bufferToString(this.result);
1377
+
1378
+ callback(SERIALIZED_MARKER + TYPE_BLOB + str);
1379
+ };
1380
+
1381
+ fileReader.readAsArrayBuffer(value);
1382
+ } else {
1383
+ try {
1384
+ callback(JSON.stringify(value));
1385
+ } catch (e) {
1386
+ if (this.console && this.console.error) {
1387
+ this.console.error("Couldn't convert value into a JSON string: ", value);
1388
+ }
1389
+
1390
+ callback(null, e);
1391
+ }
1392
+ }
1393
+ }
1394
+
1395
+ // Set a key's value and run an optional callback once the value is set.
1396
+ // Unlike Gaia's implementation, the callback function is passed the value,
1397
+ // in case you want to operate on that value only after you're sure it
1398
+ // saved, or something like that.
1399
+ function setItem(key, value, callback) {
1400
+ var _this = this;
1401
+ return new Promise(function(resolve, reject) {
1402
+ _this.ready().then(function() {
1403
+ // Convert undefined values to null.
1404
+ // https://github.com/mozilla/localForage/pull/42
1405
+ if (value === undefined) {
1406
+ value = null;
1407
+ }
1408
+
1409
+ // Save the original value to pass to the callback.
1410
+ var originalValue = value;
1411
+
1412
+ _serialize(value, function(value, error) {
1413
+ if (error) {
1414
+ if (callback) {
1415
+ callback(null, error);
1416
+ }
1417
+
1418
+ reject(error);
1419
+ } else {
1420
+ try {
1421
+ localStorage.setItem(keyPrefix + key, value);
1422
+ } catch (e) {
1423
+ // localStorage capacity exceeded.
1424
+ // TODO: Make this a specific error/event.
1425
+ if (e.name === 'QuotaExceededError' ||
1426
+ e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
1427
+ if (callback) {
1428
+ callback(null, e);
1429
+ }
1430
+
1431
+ reject(e);
1432
+ }
1433
+ }
1434
+
1435
+ if (callback) {
1436
+ callback(originalValue);
1437
+ }
1438
+
1439
+ resolve(originalValue);
1440
+ }
1441
+ });
1442
+ }, reject);
1443
+ });
1444
+ }
1445
+
1446
+ var localStorageWrapper = {
1447
+ _driver: 'localStorageWrapper',
1448
+ _initStorage: _initStorage,
1449
+ // Default API, from Gaia/localStorage.
1450
+ getItem: getItem,
1451
+ setItem: setItem,
1452
+ removeItem: removeItem,
1453
+ clear: clear,
1454
+ length: length,
1455
+ key: key,
1456
+ keys: keys
1457
+ };
1458
+
1459
+ if (typeof define === 'function' && define.amd) {
1460
+ define('localStorageWrapper', function() {
1461
+ return localStorageWrapper;
1462
+ });
1463
+ } else if (typeof module !== 'undefined' && module.exports) {
1464
+ module.exports = localStorageWrapper;
1465
+ } else {
1466
+ this.localStorageWrapper = localStorageWrapper;
1467
+ }
1468
+ }).call(this);
1469
+ /*
1470
+ * Includes code from:
1471
+ *
1472
+ * base64-arraybuffer
1473
+ * https://github.com/niklasvh/base64-arraybuffer
1474
+ *
1475
+ * Copyright (c) 2012 Niklas von Hertzen
1476
+ * Licensed under the MIT license.
1477
+ */
1478
+ (function() {
1479
+ 'use strict';
1480
+
1481
+ // Sadly, the best way to save binary data in WebSQL is Base64 serializing
1482
+ // it, so this is how we store it to prevent very strange errors with less
1483
+ // verbose ways of binary <-> string data storage.
1484
+ var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
1485
+
1486
+ // Promises!
1487
+ var Promise = (typeof module !== 'undefined' && module.exports) ?
1488
+ require('promise') : this.Promise;
1489
+
1490
+ var openDatabase = this.openDatabase;
1491
+ var db = null;
1492
+ var dbInfo = {};
1493
+
1494
+ var SERIALIZED_MARKER = '__lfsc__:';
1495
+ var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length;
1496
+
1497
+ // OMG the serializations!
1498
+ var TYPE_ARRAYBUFFER = 'arbf';
1499
+ var TYPE_BLOB = 'blob';
1500
+ var TYPE_INT8ARRAY = 'si08';
1501
+ var TYPE_UINT8ARRAY = 'ui08';
1502
+ var TYPE_UINT8CLAMPEDARRAY = 'uic8';
1503
+ var TYPE_INT16ARRAY = 'si16';
1504
+ var TYPE_INT32ARRAY = 'si32';
1505
+ var TYPE_UINT16ARRAY = 'ur16';
1506
+ var TYPE_UINT32ARRAY = 'ui32';
1507
+ var TYPE_FLOAT32ARRAY = 'fl32';
1508
+ var TYPE_FLOAT64ARRAY = 'fl64';
1509
+ var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length;
1510
+
1511
+ // If WebSQL methods aren't available, we can stop now.
1512
+ if (!openDatabase) {
1513
+ return;
1514
+ }
1515
+
1516
+ // Open the WebSQL database (automatically creates one if one didn't
1517
+ // previously exist), using any options set in the config.
1518
+ function _initStorage(options) {
1519
+ var _this = this;
1520
+
1521
+ if (options) {
1522
+ for (var i in options) {
1523
+ dbInfo[i] = typeof(options[i]) !== 'string' ? options[i].toString() : options[i];
1524
+ }
1525
+ }
1526
+
1527
+ return new Promise(function(resolve, reject) {
1528
+ // Open the database; the openDatabase API will automatically
1529
+ // create it for us if it doesn't exist.
1530
+ try {
1531
+ db = openDatabase(dbInfo.name, dbInfo.version,
1532
+ dbInfo.description, dbInfo.size);
1533
+ } catch (e) {
1534
+ return _this.setDriver('localStorageWrapper').then(resolve, reject);
1535
+ }
1536
+
1537
+ // Create our key/value table if it doesn't exist.
1538
+ db.transaction(function(t) {
1539
+ t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName +
1540
+ ' (id INTEGER PRIMARY KEY, key unique, value)', [], function() {
1541
+ resolve();
1542
+ }, function(t, error) {
1543
+ reject(error);
1544
+ });
1545
+ });
1546
+ });
1547
+ }
1548
+
1549
+ function getItem(key, callback) {
1550
+ var _this = this;
1551
+ return new Promise(function(resolve, reject) {
1552
+ _this.ready().then(function() {
1553
+ db.transaction(function(t) {
1554
+ t.executeSql('SELECT * FROM ' + dbInfo.storeName +
1555
+ ' WHERE key = ? LIMIT 1', [key], function(t, results) {
1556
+ var result = results.rows.length ? results.rows.item(0).value : null;
1557
+
1558
+ // Check to see if this is serialized content we need to
1559
+ // unpack.
1560
+ if (result) {
1561
+ result = _deserialize(result);
1562
+ }
1563
+
1564
+ if (callback) {
1565
+ callback(result);
1566
+ }
1567
+
1568
+ resolve(result);
1569
+ }, function(t, error) {
1570
+ if (callback) {
1571
+ callback(null, error);
1572
+ }
1573
+
1574
+ reject(error);
1575
+ });
1576
+ });
1577
+ }, reject);
1578
+ });
1579
+ }
1580
+
1581
+ function setItem(key, value, callback) {
1582
+ var _this = this;
1583
+ return new Promise(function(resolve, reject) {
1584
+ _this.ready().then(function() {
1585
+ // The localStorage API doesn't return undefined values in an
1586
+ // "expected" way, so undefined is always cast to null in all
1587
+ // drivers. See: https://github.com/mozilla/localForage/pull/42
1588
+ if (value === undefined) {
1589
+ value = null;
1590
+ }
1591
+
1592
+ // Save the original value to pass to the callback.
1593
+ var originalValue = value;
1594
+
1595
+ _serialize(value, function(value, error) {
1596
+ if (error) {
1597
+ reject(error);
1598
+ } else {
1599
+ db.transaction(function(t) {
1600
+ t.executeSql('INSERT OR REPLACE INTO ' + dbInfo.storeName +
1601
+ ' (key, value) VALUES (?, ?)', [key, value], function() {
1602
+ if (callback) {
1603
+ callback(originalValue);
1604
+ }
1605
+
1606
+ resolve(originalValue);
1607
+ }, function(t, error) {
1608
+ if (callback) {
1609
+ callback(null, error);
1610
+ }
1611
+
1612
+ reject(error);
1613
+ });
1614
+ }, function(sqlError) { // The transaction failed; check
1615
+ // to see if it's a quota error.
1616
+ if (sqlError.code === sqlError.QUOTA_ERR) {
1617
+ // We reject the callback outright for now, but
1618
+ // it's worth trying to re-run the transaction.
1619
+ // Even if the user accepts the prompt to use
1620
+ // more storage on Safari, this error will
1621
+ // be called.
1622
+ //
1623
+ // TODO: Try to re-run the transaction.
1624
+ if (callback) {
1625
+ callback(null, sqlError);
1626
+ }
1627
+
1628
+ reject(sqlError);
1629
+ }
1630
+ });
1631
+ }
1632
+ });
1633
+ }, reject);
1634
+ });
1635
+ }
1636
+
1637
+ function removeItem(key, callback) {
1638
+ var _this = this;
1639
+ return new Promise(function(resolve, reject) {
1640
+ _this.ready().then(function() {
1641
+ db.transaction(function(t) {
1642
+ t.executeSql('DELETE FROM ' + dbInfo.storeName +
1643
+ ' WHERE key = ?', [key], function() {
1644
+ if (callback) {
1645
+ callback();
1646
+ }
1647
+
1648
+ resolve();
1649
+ }, function(t, error) {
1650
+ if (callback) {
1651
+ callback(error);
1652
+ }
1653
+
1654
+ reject(error);
1655
+ });
1656
+ });
1657
+ }, reject);
1658
+ });
1659
+ }
1660
+
1661
+ // Deletes every item in the table.
1662
+ // TODO: Find out if this resets the AUTO_INCREMENT number.
1663
+ function clear(callback) {
1664
+ var _this = this;
1665
+ return new Promise(function(resolve, reject) {
1666
+ _this.ready().then(function() {
1667
+ db.transaction(function(t) {
1668
+ t.executeSql('DELETE FROM ' + dbInfo.storeName, [], function() {
1669
+ if (callback) {
1670
+ callback();
1671
+ }
1672
+
1673
+ resolve();
1674
+ }, function(t, error) {
1675
+ if (callback) {
1676
+ callback(error);
1677
+ }
1678
+
1679
+ reject(error);
1680
+ });
1681
+ });
1682
+ }, reject);
1683
+ });
1684
+ }
1685
+
1686
+ // Does a simple `COUNT(key)` to get the number of items stored in
1687
+ // localForage.
1688
+ function length(callback) {
1689
+ var _this = this;
1690
+ return new Promise(function(resolve, reject) {
1691
+ _this.ready().then(function() {
1692
+ db.transaction(function(t) {
1693
+ // Ahhh, SQL makes this one soooooo easy.
1694
+ t.executeSql('SELECT COUNT(key) as c FROM ' +
1695
+ dbInfo.storeName, [], function(t, results) {
1696
+ var result = results.rows.item(0).c;
1697
+
1698
+ if (callback) {
1699
+ callback(result);
1700
+ }
1701
+
1702
+ resolve(result);
1703
+ }, function(t, error) {
1704
+ if (callback) {
1705
+ callback(null, error);
1706
+ }
1707
+
1708
+ reject(error);
1709
+ });
1710
+ });
1711
+ }, reject);
1712
+ });
1713
+ }
1714
+
1715
+ // Return the key located at key index X; essentially gets the key from a
1716
+ // `WHERE id = ?`. This is the most efficient way I can think to implement
1717
+ // this rarely-used (in my experience) part of the API, but it can seem
1718
+ // inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so
1719
+ // the ID of each key will change every time it's updated. Perhaps a stored
1720
+ // procedure for the `setItem()` SQL would solve this problem?
1721
+ // TODO: Don't change ID on `setItem()`.
1722
+ function key(n, callback) {
1723
+ var _this = this;
1724
+ return new Promise(function(resolve, reject) {
1725
+ _this.ready().then(function() {
1726
+ db.transaction(function(t) {
1727
+ t.executeSql('SELECT key FROM ' + dbInfo.storeName +
1728
+ ' WHERE id = ? LIMIT 1', [n + 1], function(t, results) {
1729
+ var result = results.rows.length ? results.rows.item(0).key : null;
1730
+
1731
+ if (callback) {
1732
+ callback(result);
1733
+ }
1734
+
1735
+ resolve(result);
1736
+ }, function(t, error) {
1737
+ if (callback) {
1738
+ callback(null, error);
1739
+ }
1740
+
1741
+ reject(error);
1742
+ });
1743
+ });
1744
+ }, reject);
1745
+ });
1746
+ }
1747
+
1748
+ function keys(callback) {
1749
+ var _this = this;
1750
+ return new Promise(function(resolve, reject) {
1751
+ _this.ready().then(function() {
1752
+ db.transaction(function(t) {
1753
+ t.executeSql('SELECT key FROM ' + dbInfo.storeName, [],
1754
+ function(t, results) {
1755
+ var length = results.rows.length;
1756
+ var keys = [];
1757
+
1758
+ for (var i = 0; i < length; i++) {
1759
+ keys.push(results.rows.item(i).key);
1760
+ }
1761
+
1762
+ if (callback) {
1763
+ callback(keys);
1764
+ }
1765
+
1766
+ resolve(keys);
1767
+ }, function(t, error) {
1768
+ if (callback) {
1769
+ callback(null, error);
1770
+ }
1771
+
1772
+ reject(error);
1773
+ });
1774
+ });
1775
+ }, reject);
1776
+ });
1777
+ }
1778
+
1779
+ // Converts a buffer to a string to store, serialized, in the backend
1780
+ // storage library.
1781
+ function _bufferToString(buffer) {
1782
+ // base64-arraybuffer
1783
+ var bytes = new Uint8Array(buffer);
1784
+ var i;
1785
+ var base64String = '';
1786
+
1787
+ for (i = 0; i < bytes.length; i += 3) {
1788
+ /*jslint bitwise: true */
1789
+ base64String += BASE_CHARS[bytes[i] >> 2];
1790
+ base64String += BASE_CHARS[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
1791
+ base64String += BASE_CHARS[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
1792
+ base64String += BASE_CHARS[bytes[i + 2] & 63];
1793
+ }
1794
+
1795
+ if ((bytes.length % 3) === 2) {
1796
+ base64String = base64String.substring(0, base64String.length - 1) + "=";
1797
+ } else if (bytes.length % 3 === 1) {
1798
+ base64String = base64String.substring(0, base64String.length - 2) + "==";
1799
+ }
1800
+
1801
+ return base64String;
1802
+ }
1803
+
1804
+ // Deserialize data we've inserted into a value column/field. We place
1805
+ // special markers into our strings to mark them as encoded; this isn't
1806
+ // as nice as a meta field, but it's the only sane thing we can do whilst
1807
+ // keeping localStorage support intact.
1808
+ //
1809
+ // Oftentimes this will just deserialize JSON content, but if we have a
1810
+ // special marker (SERIALIZED_MARKER, defined above), we will extract
1811
+ // some kind of arraybuffer/binary data/typed array out of the string.
1812
+ function _deserialize(value) {
1813
+ // If we haven't marked this string as being specially serialized (i.e.
1814
+ // something other than serialized JSON), we can just return it and be
1815
+ // done with it.
1816
+ if (value.substring(0, SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) {
1817
+ return JSON.parse(value);
1818
+ }
1819
+
1820
+ // The following code deals with deserializing some kind of Blob or
1821
+ // TypedArray. First we separate out the type of data we're dealing
1822
+ // with from the data itself.
1823
+ var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH);
1824
+ var type = value.substring(SERIALIZED_MARKER_LENGTH, TYPE_SERIALIZED_MARKER_LENGTH);
1825
+
1826
+ // Fill the string into a ArrayBuffer.
1827
+ var bufferLength = serializedString.length * 0.75;
1828
+ var len = serializedString.length;
1829
+ var i;
1830
+ var p = 0;
1831
+ var encoded1, encoded2, encoded3, encoded4;
1832
+
1833
+ if (serializedString[serializedString.length - 1] === "=") {
1834
+ bufferLength--;
1835
+ if (serializedString[serializedString.length - 2] === "=") {
1836
+ bufferLength--;
1837
+ }
1838
+ }
1839
+
1840
+ var buffer = new ArrayBuffer(bufferLength);
1841
+ var bytes = new Uint8Array(buffer);
1842
+
1843
+ for (i = 0; i < len; i+=4) {
1844
+ encoded1 = BASE_CHARS.indexOf(serializedString[i]);
1845
+ encoded2 = BASE_CHARS.indexOf(serializedString[i+1]);
1846
+ encoded3 = BASE_CHARS.indexOf(serializedString[i+2]);
1847
+ encoded4 = BASE_CHARS.indexOf(serializedString[i+3]);
1848
+
1849
+ /*jslint bitwise: true */
1850
+ bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
1851
+ bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
1852
+ bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
1853
+ }
1854
+
1855
+ // Return the right type based on the code/type set during
1856
+ // serialization.
1857
+ switch (type) {
1858
+ case TYPE_ARRAYBUFFER:
1859
+ return buffer;
1860
+ case TYPE_BLOB:
1861
+ return new Blob([buffer]);
1862
+ case TYPE_INT8ARRAY:
1863
+ return new Int8Array(buffer);
1864
+ case TYPE_UINT8ARRAY:
1865
+ return new Uint8Array(buffer);
1866
+ case TYPE_UINT8CLAMPEDARRAY:
1867
+ return new Uint8ClampedArray(buffer);
1868
+ case TYPE_INT16ARRAY:
1869
+ return new Int16Array(buffer);
1870
+ case TYPE_UINT16ARRAY:
1871
+ return new Uint16Array(buffer);
1872
+ case TYPE_INT32ARRAY:
1873
+ return new Int32Array(buffer);
1874
+ case TYPE_UINT32ARRAY:
1875
+ return new Uint32Array(buffer);
1876
+ case TYPE_FLOAT32ARRAY:
1877
+ return new Float32Array(buffer);
1878
+ case TYPE_FLOAT64ARRAY:
1879
+ return new Float64Array(buffer);
1880
+ default:
1881
+ throw new Error('Unkown type: ' + type);
1882
+ }
1883
+ }
1884
+
1885
+ // Serialize a value, afterwards executing a callback (which usually
1886
+ // instructs the `setItem()` callback/promise to be executed). This is how
1887
+ // we store binary data with localStorage.
1888
+ function _serialize(value, callback) {
1889
+ var valueString = '';
1890
+ if (value) {
1891
+ valueString = value.toString();
1892
+ }
1893
+
1894
+ // Cannot use `value instanceof ArrayBuffer` or such here, as these
1895
+ // checks fail when running the tests using casper.js...
1896
+ //
1897
+ // TODO: See why those tests fail and use a better solution.
1898
+ if (value && (value.toString() === '[object ArrayBuffer]' ||
1899
+ value.buffer && value.buffer.toString() === '[object ArrayBuffer]')) {
1900
+ // Convert binary arrays to a string and prefix the string with
1901
+ // a special marker.
1902
+ var buffer;
1903
+ var marker = SERIALIZED_MARKER;
1904
+
1905
+ if (value instanceof ArrayBuffer) {
1906
+ buffer = value;
1907
+ marker += TYPE_ARRAYBUFFER;
1908
+ } else {
1909
+ buffer = value.buffer;
1910
+
1911
+ if (valueString === '[object Int8Array]') {
1912
+ marker += TYPE_INT8ARRAY;
1913
+ } else if (valueString === '[object Uint8Array]') {
1914
+ marker += TYPE_UINT8ARRAY;
1915
+ } else if (valueString === '[object Uint8ClampedArray]') {
1916
+ marker += TYPE_UINT8CLAMPEDARRAY;
1917
+ } else if (valueString === '[object Int16Array]') {
1918
+ marker += TYPE_INT16ARRAY;
1919
+ } else if (valueString === '[object Uint16Array]') {
1920
+ marker += TYPE_UINT16ARRAY;
1921
+ } else if (valueString === '[object Int32Array]') {
1922
+ marker += TYPE_INT32ARRAY;
1923
+ } else if (valueString === '[object Uint32Array]') {
1924
+ marker += TYPE_UINT32ARRAY;
1925
+ } else if (valueString === '[object Float32Array]') {
1926
+ marker += TYPE_FLOAT32ARRAY;
1927
+ } else if (valueString === '[object Float64Array]') {
1928
+ marker += TYPE_FLOAT64ARRAY;
1929
+ } else {
1930
+ callback(new Error("Failed to get type for BinaryArray"));
1931
+ }
1932
+ }
1933
+
1934
+ callback(marker + _bufferToString(buffer));
1935
+ } else if (valueString === "[object Blob]") {
1936
+ // Conver the blob to a binaryArray and then to a string.
1937
+ var fileReader = new FileReader();
1938
+
1939
+ fileReader.onload = function() {
1940
+ var str = _bufferToString(this.result);
1941
+
1942
+ callback(SERIALIZED_MARKER + TYPE_BLOB + str);
1943
+ };
1944
+
1945
+ fileReader.readAsArrayBuffer(value);
1946
+ } else {
1947
+ try {
1948
+ callback(JSON.stringify(value));
1949
+ } catch (e) {
1950
+ if (this.console && this.console.error) {
1951
+ this.console.error("Couldn't convert value into a JSON string: ", value);
1952
+ }
1953
+
1954
+ callback(null, e);
1955
+ }
1956
+ }
1957
+ }
1958
+
1959
+ var webSQLStorage = {
1960
+ _driver: 'webSQLStorage',
1961
+ _initStorage: _initStorage,
1962
+ getItem: getItem,
1963
+ setItem: setItem,
1964
+ removeItem: removeItem,
1965
+ clear: clear,
1966
+ length: length,
1967
+ key: key,
1968
+ keys: keys
1969
+ };
1970
+
1971
+ if (typeof define === 'function' && define.amd) {
1972
+ define('webSQLStorage', function() {
1973
+ return webSQLStorage;
1974
+ });
1975
+ } else if (typeof module !== 'undefined' && module.exports) {
1976
+ module.exports = webSQLStorage;
1977
+ } else {
1978
+ this.webSQLStorage = webSQLStorage;
1979
+ }
1980
+ }).call(this);
1981
+ (function() {
1982
+ 'use strict';
1983
+
1984
+ // Promises!
1985
+ var Promise = (typeof module !== 'undefined' && module.exports) ?
1986
+ require('promise') : this.Promise;
1987
+
1988
+ // Avoid those magic constants!
1989
+ var MODULE_TYPE_DEFINE = 1;
1990
+ var MODULE_TYPE_EXPORT = 2;
1991
+ var MODULE_TYPE_WINDOW = 3;
1992
+
1993
+ // Attaching to window (i.e. no module loader) is the assumed,
1994
+ // simple default.
1995
+ var moduleType = MODULE_TYPE_WINDOW;
1996
+
1997
+ // Find out what kind of module setup we have; if none, we'll just attach
1998
+ // localForage to the main window.
1999
+ if (typeof define === 'function' && define.amd) {
2000
+ moduleType = MODULE_TYPE_DEFINE;
2001
+ } else if (typeof module !== 'undefined' && module.exports) {
2002
+ moduleType = MODULE_TYPE_EXPORT;
2003
+ }
2004
+
2005
+ // The actual localForage object that we expose as a module or via a
2006
+ // global. It's extended by pulling in one of our other libraries.
2007
+ var _this = this;
2008
+ var localForage = {
2009
+ INDEXEDDB: 'asyncStorage',
2010
+ LOCALSTORAGE: 'localStorageWrapper',
2011
+ WEBSQL: 'webSQLStorage',
2012
+
2013
+ _config: {
2014
+ description: '',
2015
+ name: 'localforage',
2016
+ // Default DB size is _JUST UNDER_ 5MB, as it's the highest size
2017
+ // we can use without a prompt.
2018
+ size: 4980736,
2019
+ storeName: 'keyvaluepairs',
2020
+ version: 1.0
2021
+ },
2022
+
2023
+ // Set any config values for localForage; can be called anytime before
2024
+ // the first API call (e.g. `getItem`, `setItem`).
2025
+ // We loop through options so we don't overwrite existing config
2026
+ // values.
2027
+ config: function(options) {
2028
+ // If the options argument is an object, we use it to set values.
2029
+ // Otherwise, we return either a specified config value or all
2030
+ // config values.
2031
+ if (typeof(options) === 'object') {
2032
+ // If localforage is ready and fully initialized, we can't set
2033
+ // any new configuration values. Instead, we return an error.
2034
+ if (this._ready) {
2035
+ return new Error("Can't call config() after localforage " +
2036
+ "has been used.");
2037
+ }
2038
+
2039
+ for (var i in options) {
2040
+ this._config[i] = options[i];
2041
+ }
2042
+
2043
+ return true;
2044
+ } else if (typeof(options) === 'string') {
2045
+ return this._config[options];
2046
+ } else {
2047
+ return this._config;
2048
+ }
2049
+ },
2050
+
2051
+ driver: function() {
2052
+ return this._driver || null;
2053
+ },
2054
+
2055
+ _ready: false,
2056
+
2057
+ _driverSet: null,
2058
+
2059
+ setDriver: function(drivers, callback, errorCallback) {
2060
+ var self = this;
2061
+
2062
+ var isArray = Array.isArray || function(arg) {
2063
+ return Object.prototype.toString.call(arg) === '[object Array]';
2064
+ };
2065
+
2066
+ if (!isArray(drivers) && typeof drivers === 'string') {
2067
+ drivers = [drivers];
2068
+ }
2069
+
2070
+ this._driverSet = new Promise(function(resolve, reject) {
2071
+ var driverName = self._getFirstSupportedDriver(drivers);
2072
+
2073
+ if (!driverName) {
2074
+ var error = new Error('No available storage method found.');
2075
+ self._driverSet = Promise.reject(error);
2076
+
2077
+ if (errorCallback) {
2078
+ errorCallback(error);
2079
+ }
2080
+
2081
+ reject(error);
2082
+
2083
+ return;
2084
+ }
2085
+
2086
+ self._ready = null;
2087
+
2088
+ // We allow localForage to be declared as a module or as a
2089
+ // library available without AMD/require.js.
2090
+ if (moduleType === MODULE_TYPE_DEFINE) {
2091
+ require([driverName], function(lib) {
2092
+ self._extend(lib);
2093
+
2094
+ if (callback) {
2095
+ callback();
2096
+ }
2097
+ resolve();
2098
+ });
2099
+
2100
+ return;
2101
+ } else if (moduleType === MODULE_TYPE_EXPORT) {
2102
+ // Making it browserify friendly
2103
+ var driver;
2104
+ switch (driverName) {
2105
+ case self.INDEXEDDB:
2106
+ driver = require('./drivers/indexeddb');
2107
+ break;
2108
+ case self.LOCALSTORAGE:
2109
+ driver = require('./drivers/localstorage');
2110
+ break;
2111
+ case self.WEBSQL:
2112
+ driver = require('./drivers/websql');
2113
+ }
2114
+
2115
+ self._extend(driver);
2116
+ } else {
2117
+ self._extend(_this[driverName]);
2118
+ }
2119
+
2120
+ if (callback) {
2121
+ callback();
2122
+ }
2123
+
2124
+ resolve();
2125
+ });
2126
+
2127
+ return this._driverSet;
2128
+ },
2129
+
2130
+ _getFirstSupportedDriver: function(drivers) {
2131
+ if (drivers) {
2132
+ for (var i = 0; i < drivers.length; i++) {
2133
+ var driver = drivers[i];
2134
+
2135
+ if (this.supports(driver)) {
2136
+ return driver;
2137
+ }
2138
+ }
2139
+ }
2140
+
2141
+ return null;
2142
+ },
2143
+
2144
+ supports: function(driverName) {
2145
+ return !!driverSupport[driverName];
2146
+ },
2147
+
2148
+ ready: function(callback) {
2149
+ var ready = new Promise(function(resolve, reject) {
2150
+ localForage._driverSet.then(function() {
2151
+ if (localForage._ready === null) {
2152
+ localForage._ready = localForage._initStorage(
2153
+ localForage._config);
2154
+ }
2155
+
2156
+ localForage._ready.then(resolve, reject);
2157
+ }, reject);
2158
+ });
2159
+
2160
+ ready.then(callback, callback);
2161
+
2162
+ return ready;
2163
+ },
2164
+
2165
+ _extend: function(libraryMethodsAndProperties) {
2166
+ for (var i in libraryMethodsAndProperties) {
2167
+ if (libraryMethodsAndProperties.hasOwnProperty(i)) {
2168
+ this[i] = libraryMethodsAndProperties[i];
2169
+ }
2170
+ }
2171
+ }
2172
+ };
2173
+
2174
+ // Check to see if IndexedDB is available and if it is the latest
2175
+ // implementation; it's our preferred backend library. We use "_spec_test"
2176
+ // as the name of the database because it's not the one we'll operate on,
2177
+ // but it's useful to make sure its using the right spec.
2178
+ // See: https://github.com/mozilla/localForage/issues/128
2179
+ var driverSupport = (function(_this) {
2180
+ // Initialize IndexedDB; fall back to vendor-prefixed versions
2181
+ // if needed.
2182
+ var indexedDB = indexedDB || _this.indexedDB || _this.webkitIndexedDB ||
2183
+ _this.mozIndexedDB || _this.OIndexedDB ||
2184
+ _this.msIndexedDB;
2185
+
2186
+ var result = {};
2187
+
2188
+ result[localForage.WEBSQL] = !!_this.openDatabase;
2189
+ result[localForage.INDEXEDDB] = !!(
2190
+ indexedDB &&
2191
+ typeof indexedDB.open === 'function' &&
2192
+ indexedDB.open('_localforage_spec_test', 1)
2193
+ .onupgradeneeded === null
2194
+ );
2195
+
2196
+ result[localForage.LOCALSTORAGE] = !!(function() {
2197
+ try {
2198
+ return (localStorage &&
2199
+ typeof localStorage.setItem === 'function');
2200
+ } catch (e) {
2201
+ return false;
2202
+ }
2203
+ })();
2204
+
2205
+ return result;
2206
+ })(this);
2207
+
2208
+ var driverTestOrder = [
2209
+ localForage.INDEXEDDB,
2210
+ localForage.WEBSQL,
2211
+ localForage.LOCALSTORAGE
2212
+ ];
2213
+
2214
+ localForage.setDriver(driverTestOrder);
2215
+
2216
+ // We allow localForage to be declared as a module or as a library
2217
+ // available without AMD/require.js.
2218
+ if (moduleType === MODULE_TYPE_DEFINE) {
2219
+ define(function() {
2220
+ return localForage;
2221
+ });
2222
+ } else if (moduleType === MODULE_TYPE_EXPORT) {
2223
+ module.exports = localForage;
2224
+ } else {
2225
+ this.localforage = localForage;
2226
+ }
2227
+ }).call(this);