OLD | NEW |
| (Empty) |
1 | |
2 | |
3 Polymer({ | |
4 is: 'iron-localstorage', | |
5 | |
6 /** | |
7 * Fired when value loads from localStorage. | |
8 * | |
9 * @event paper-responsive-change | |
10 * @param {{externalChange: boolean}} detail - | |
11 * externalChange: True if change occured in different window. | |
12 */ | |
13 | |
14 /** | |
15 * Fired when loaded value is null. | |
16 * You can use event handler to initialize default value. | |
17 * | |
18 * @event iron-localstorage-load-empty | |
19 */ | |
20 properties: { | |
21 /** | |
22 * The key to the data stored in localStorage. | |
23 */ | |
24 name: { | |
25 type: String, | |
26 value: '' | |
27 }, | |
28 /** | |
29 * The data associated with this storage. | |
30 * If value is set to null, and storage is in useRaw mode, item will be de
leted | |
31 * @type {*} | |
32 */ | |
33 value: { | |
34 type: Object, | |
35 notify: true | |
36 }, | |
37 | |
38 /** | |
39 * Value is stored and retrieved without JSON parse if true | |
40 */ | |
41 useRaw: { | |
42 type: Boolean, | |
43 value: false | |
44 }, | |
45 | |
46 /** | |
47 * Auto save is disabled if true. Default to false. | |
48 */ | |
49 autoSaveDisabled: { | |
50 type: Boolean, | |
51 value: false | |
52 }, | |
53 /** | |
54 * Last error encountered while saving/loading items. Null otherwise | |
55 */ | |
56 errorMessage: { | |
57 type: String, | |
58 notify: true | |
59 }, | |
60 /* | |
61 * True if value was loaded | |
62 */ | |
63 _loaded: { | |
64 type: Boolean, | |
65 value: false | |
66 } | |
67 }, | |
68 | |
69 observers: [ | |
70 'reload(name,useRaw)', | |
71 '_trySaveValue(value, _loaded, autoSaveDisabled)' | |
72 ], | |
73 | |
74 ready: function() { | |
75 this._boundHandleStorage = this._handleStorage.bind(this); | |
76 }, | |
77 | |
78 attached: function() { | |
79 window.addEventListener('storage', this._boundHandleStorage); | |
80 }, | |
81 | |
82 detached: function() { | |
83 window.removeEventListener('storage', this._boundHandleStorage); | |
84 }, | |
85 | |
86 _handleStorage: function(ev) { | |
87 if (ev.key == this.name) { | |
88 this._load(true); | |
89 } | |
90 }, | |
91 | |
92 _trySaveValue: function(value, _loaded, autoSaveDisabled) { | |
93 if (this._justLoaded) { // guard against saving after _load() | |
94 this._justLoaded = false; | |
95 return; | |
96 } | |
97 if (_loaded && !autoSaveDisabled) { | |
98 this.save(); | |
99 } | |
100 }, | |
101 | |
102 /** | |
103 * Loads the value again. Use if you modify | |
104 * localStorage using DOM calls, and want to | |
105 * keep this element in sync. | |
106 */ | |
107 reload: function() { | |
108 this._load(); | |
109 }, | |
110 | |
111 /** | |
112 * loads value from local storage | |
113 * @param {boolean=} externalChange true if loading changes from a different
window | |
114 */ | |
115 _load: function(externalChange) { | |
116 var v = window.localStorage.getItem(this.name); | |
117 | |
118 if (v === null) { | |
119 this.fire('iron-localstorage-load-empty'); | |
120 } else if (!this.useRaw) { | |
121 try { | |
122 v = JSON.parse(v); | |
123 } catch(x) { | |
124 this.errorMessage = "Could not parse local storage value"; | |
125 console.error("could not parse local storage value", v); | |
126 } | |
127 } | |
128 | |
129 this._justLoaded = true; | |
130 this._loaded = true; | |
131 this.value = v; | |
132 this.fire('iron-localstorage-load', { externalChange: externalChange}); | |
133 }, | |
134 | |
135 /** | |
136 * Saves the value to localStorage. Call to save if autoSaveDisabled is set. | |
137 * If `value` is null, deletes localStorage. | |
138 */ | |
139 save: function() { | |
140 var v = this.useRaw ? this.value : JSON.stringify(this.value); | |
141 try { | |
142 if (this.value === null) { | |
143 window.localStorage.removeItem(this.name); | |
144 } else { | |
145 window.localStorage.setItem(this.name, /** @type {string} */ (v)); | |
146 } | |
147 } | |
148 catch(ex) { | |
149 // Happens in Safari incognito mode, | |
150 this.errorMessage = ex.message; | |
151 console.error("localStorage could not be saved. Safari incoginito mode?"
, ex); | |
152 } | |
153 } | |
154 | |
155 }); | |
156 | |
OLD | NEW |