-
Notifications
You must be signed in to change notification settings - Fork 0
/
orbit-common-local-storage.js
89 lines (70 loc) · 2.4 KB
/
orbit-common-local-storage.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
(function() {
// Share loader properties from globalized Orbit package
var define = window.Orbit.__define__;
var requireModule = window.Orbit.__requireModule__;
define('orbit-common/local-storage-source', ['exports', 'orbit/lib/assert', 'orbit-common/memory-source'], function (exports, assert, MemorySource) {
'use strict';
var supportsLocalStorage = function() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch(e) {
return false;
}
};
/**
Source for storing data in local storage
@class LocalStorageSource
@extends MemorySource
@namespace OC
@param {OC.Schema} schema
@param {Object} [options]
@constructor
*/
var LocalStorageSource = MemorySource['default'].extend({
init: function(schema, options) {
assert.assert('Your browser does not support local storage!', supportsLocalStorage());
this._super.apply(this, arguments);
options = options || {};
this.namespace = options['namespace'] || 'orbit'; // local storage key
this._autosave = options['autosave'] !== undefined ? options['autosave'] : true;
var autoload = options['autoload'] !== undefined ? options['autoload'] : true;
this._isDirty = false;
this.on('didTransform', function() {
this._saveData();
}, this);
if (autoload) this.load();
},
load: function() {
var storage = window.localStorage.getItem(this.namespace);
if (storage) {
this.reset(JSON.parse(storage));
}
},
enableAutosave: function() {
if (!this._autosave) {
this._autosave = true;
if (this._isDirty) this._saveData();
}
},
disableAutosave: function() {
if (this._autosave) {
this._autosave = false;
}
},
/////////////////////////////////////////////////////////////////////////////
// Internals
/////////////////////////////////////////////////////////////////////////////
_saveData: function(forceSave) {
if (!this._autosave && !forceSave) {
this._isDirty = true;
return;
}
window.localStorage.setItem(this.namespace, JSON.stringify(this.retrieve()));
this._isDirty = false;
}
});
exports['default'] = LocalStorageSource;
});
window.OC.LocalStorageSource = requireModule("orbit-common/local-storage-source")["default"];
})();
//# sourceMappingURL=orbit-common-local-storage.map