| OLD | NEW |
| 1 | 1 |
| 2 | 2 |
| 3 Polymer({ | 3 Polymer({ |
| 4 is: 'iron-localstorage', | 4 is: 'iron-localstorage', |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * Fired when value loads from localStorage. | 7 * Fired when value loads from localStorage. |
| 8 * | 8 * |
| 9 * @param {Object} detail | 9 * @event paper-responsive-change |
| 10 * @param {Boolean} detail.externalChange true if change occured in differen
t window | 10 * @param {{externalChange: boolean}} detail - |
| 11 * @event iron-localstorage-load | 11 * externalChange: True if change occured in different window. |
| 12 */ | 12 */ |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Fired when loaded value is null. | 15 * Fired when loaded value is null. |
| 16 * You can use event handler to initialize default value. | 16 * You can use event handler to initialize default value. |
| 17 * | 17 * |
| 18 * @event iron-localstorage-load-empty | 18 * @event iron-localstorage-load-empty |
| 19 */ | 19 */ |
| 20 properties: { | 20 properties: { |
| 21 /** | 21 /** |
| 22 * The key to the data stored in localStorage. | 22 * The key to the data stored in localStorage. |
| 23 */ | 23 */ |
| 24 name: { | 24 name: { |
| 25 type: String, | 25 type: String, |
| 26 value: '' | 26 value: '' |
| 27 }, | 27 }, |
| 28 /** | 28 /** |
| 29 * The data associated with this storage. | 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 | 30 * If value is set to null, and storage is in useRaw mode, item will be de
leted |
| 31 * @type {*} |
| 31 */ | 32 */ |
| 32 value: { | 33 value: { |
| 33 type: Object, | 34 type: Object, |
| 34 notify: true | 35 notify: true |
| 35 }, | 36 }, |
| 36 | 37 |
| 37 /** | 38 /** |
| 38 * Value is stored and retrieved without JSON parse if true | 39 * Value is stored and retrieved without JSON parse if true |
| 39 */ | 40 */ |
| 40 useRaw: { | 41 useRaw: { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 * Loads the value again. Use if you modify | 103 * Loads the value again. Use if you modify |
| 103 * localStorage using DOM calls, and want to | 104 * localStorage using DOM calls, and want to |
| 104 * keep this element in sync. | 105 * keep this element in sync. |
| 105 */ | 106 */ |
| 106 reload: function() { | 107 reload: function() { |
| 107 this._load(); | 108 this._load(); |
| 108 }, | 109 }, |
| 109 | 110 |
| 110 /** | 111 /** |
| 111 * loads value from local storage | 112 * loads value from local storage |
| 112 * @param {Boolean} externalChange true if loading changes from a different
window | 113 * @param {boolean=} externalChange true if loading changes from a different
window |
| 113 */ | 114 */ |
| 114 _load: function(externalChange) { | 115 _load: function(externalChange) { |
| 115 var v = localStorage.getItem(this.name); | 116 var v = window.localStorage.getItem(this.name); |
| 116 | 117 |
| 117 if (v === null) { | 118 if (v === null) { |
| 118 this.fire('iron-localstorage-load-empty'); | 119 this.fire('iron-localstorage-load-empty'); |
| 119 } else if (!this.useRaw) { | 120 } else if (!this.useRaw) { |
| 120 try { | 121 try { |
| 121 v = JSON.parse(v); | 122 v = JSON.parse(v); |
| 122 } catch(x) { | 123 } catch(x) { |
| 123 this.errorMessage = "Could not parse local storage value"; | 124 this.errorMessage = "Could not parse local storage value"; |
| 124 console.error("could not parse local storage value", v); | 125 console.error("could not parse local storage value", v); |
| 125 } | 126 } |
| 126 } | 127 } |
| 127 | 128 |
| 128 this._justLoaded = true; | 129 this._justLoaded = true; |
| 129 this._loaded = true; | 130 this._loaded = true; |
| 130 this.value = v; | 131 this.value = v; |
| 131 this.fire('iron-localstorage-load', { externalChange: externalChange}); | 132 this.fire('iron-localstorage-load', { externalChange: externalChange}); |
| 132 }, | 133 }, |
| 133 | 134 |
| 134 /** | 135 /** |
| 135 * Saves the value to localStorage. Call to save if autoSaveDisabled is set. | 136 * Saves the value to localStorage. Call to save if autoSaveDisabled is set. |
| 136 * If `value` is null, deletes localStorage. | 137 * If `value` is null, deletes localStorage. |
| 137 */ | 138 */ |
| 138 save: function() { | 139 save: function() { |
| 139 var v = this.useRaw ? this.value : JSON.stringify(this.value); | 140 var v = this.useRaw ? this.value : JSON.stringify(this.value); |
| 140 try { | 141 try { |
| 141 if (this.value === null) { | 142 if (this.value === null) { |
| 142 localStorage.removeItem(this.name); | 143 window.localStorage.removeItem(this.name); |
| 143 } else { | 144 } else { |
| 144 localStorage.setItem(this.name, v); | 145 window.localStorage.setItem(this.name, /** @type {string} */ (v)); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 catch(ex) { | 148 catch(ex) { |
| 148 // Happens in Safari incognito mode, | 149 // Happens in Safari incognito mode, |
| 149 this.errorMessage = ex.message; | 150 this.errorMessage = ex.message; |
| 150 console.error("localStorage could not be saved. Safari incoginito mode?"
, ex); | 151 console.error("localStorage could not be saved. Safari incoginito mode?"
, ex); |
| 151 } | 152 } |
| 152 } | 153 } |
| 153 | 154 |
| 154 }); | 155 }); |
| 155 | 156 |
| OLD | NEW |