| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 @license | 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> | 9 --> |
| 10 | 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> | 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 | 12 |
| 13 <!-- | 13 <!-- |
| 14 Element access to Web Storage API (window.localStorage). | 14 Element access to Web Storage API (window.localStorage). |
| 15 | 15 |
| 16 Keeps `value` is sync with a localStorage key. | 16 Keeps `value` in sync with a localStorage key. |
| 17 | 17 |
| 18 Direct assignments to `value` are automatically detected and saved. | 18 Direct assignments to `value` are automatically detected and saved. |
| 19 Subproperty assignments are not (ex: `value.bar='foo'`). | 19 Subproperty assignments are not (ex: `value.bar='foo'`). |
| 20 Call `save()` manually to commit your changes after modifying subproperties. | 20 Call `save()` manually to commit your changes after modifying subproperties. |
| 21 | 21 |
| 22 Value is saved in localStorage as JSON by default. | 22 Value is saved in localStorage as JSON by default. |
| 23 | 23 |
| 24 If you set the value to null, storage key will be deleted. | 24 If you set the value to null, storage key will be deleted. |
| 25 | 25 |
| 26 <iron-localstorage name="my-app-storage" value="{{value}}"> | 26 <iron-localstorage name="my-app-storage" value="{{value}}"> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 39 --> | 39 --> |
| 40 <dom-module id="iron-localstorage"></dom-module> | 40 <dom-module id="iron-localstorage"></dom-module> |
| 41 <script> | 41 <script> |
| 42 | 42 |
| 43 Polymer({ | 43 Polymer({ |
| 44 is: 'iron-localstorage', | 44 is: 'iron-localstorage', |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * Fired when value loads from localStorage. | 47 * Fired when value loads from localStorage. |
| 48 * | 48 * |
| 49 * @param {Object} detail | 49 * @event paper-responsive-change |
| 50 * @param {Boolean} detail.externalChange true if change occured in differen
t window | 50 * @param {{externalChange: boolean}} detail - |
| 51 * @event iron-localstorage-load | 51 * externalChange: True if change occured in different window. |
| 52 */ | 52 */ |
| 53 | 53 |
| 54 /** | 54 /** |
| 55 * Fired when loaded value is null. | 55 * Fired when loaded value is null. |
| 56 * You can use event handler to initialize default value. | 56 * You can use event handler to initialize default value. |
| 57 * | 57 * |
| 58 * @event iron-localstorage-load-empty | 58 * @event iron-localstorage-load-empty |
| 59 */ | 59 */ |
| 60 properties: { | 60 properties: { |
| 61 /** | 61 /** |
| 62 * The key to the data stored in localStorage. | 62 * The key to the data stored in localStorage. |
| 63 */ | 63 */ |
| 64 name: { | 64 name: { |
| 65 type: String, | 65 type: String, |
| 66 value: '' | 66 value: '' |
| 67 }, | 67 }, |
| 68 /** | 68 /** |
| 69 * The data associated with this storage. | 69 * The data associated with this storage. |
| 70 * If value is set to null, and storage is in useRaw mode, item will be de
leted | 70 * If value is set to null, and storage is in useRaw mode, item will be de
leted |
| 71 * @type {*} |
| 71 */ | 72 */ |
| 72 value: { | 73 value: { |
| 73 type: Object, | 74 type: Object, |
| 74 notify: true | 75 notify: true |
| 75 }, | 76 }, |
| 76 | 77 |
| 77 /** | 78 /** |
| 78 * Value is stored and retrieved without JSON parse if true | 79 * Value is stored and retrieved without JSON parse if true |
| 79 */ | 80 */ |
| 80 useRaw: { | 81 useRaw: { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 * Loads the value again. Use if you modify | 143 * Loads the value again. Use if you modify |
| 143 * localStorage using DOM calls, and want to | 144 * localStorage using DOM calls, and want to |
| 144 * keep this element in sync. | 145 * keep this element in sync. |
| 145 */ | 146 */ |
| 146 reload: function() { | 147 reload: function() { |
| 147 this._load(); | 148 this._load(); |
| 148 }, | 149 }, |
| 149 | 150 |
| 150 /** | 151 /** |
| 151 * loads value from local storage | 152 * loads value from local storage |
| 152 * @param {Boolean} externalChange true if loading changes from a different
window | 153 * @param {boolean=} externalChange true if loading changes from a different
window |
| 153 */ | 154 */ |
| 154 _load: function(externalChange) { | 155 _load: function(externalChange) { |
| 155 var v = localStorage.getItem(this.name); | 156 var v = window.localStorage.getItem(this.name); |
| 156 | 157 |
| 157 if (v === null) { | 158 if (v === null) { |
| 158 this.fire('iron-localstorage-load-empty'); | 159 this.fire('iron-localstorage-load-empty'); |
| 159 } else if (!this.useRaw) { | 160 } else if (!this.useRaw) { |
| 160 try { | 161 try { |
| 161 v = JSON.parse(v); | 162 v = JSON.parse(v); |
| 162 } catch(x) { | 163 } catch(x) { |
| 163 this.errorMessage = "Could not parse local storage value"; | 164 this.errorMessage = "Could not parse local storage value"; |
| 164 console.error("could not parse local storage value", v); | 165 console.error("could not parse local storage value", v); |
| 165 } | 166 } |
| 166 } | 167 } |
| 167 | 168 |
| 168 this._justLoaded = true; | 169 this._justLoaded = true; |
| 169 this._loaded = true; | 170 this._loaded = true; |
| 170 this.value = v; | 171 this.value = v; |
| 171 this.fire('iron-localstorage-load', { externalChange: externalChange}); | 172 this.fire('iron-localstorage-load', { externalChange: externalChange}); |
| 172 }, | 173 }, |
| 173 | 174 |
| 174 /** | 175 /** |
| 175 * Saves the value to localStorage. Call to save if autoSaveDisabled is set. | 176 * Saves the value to localStorage. Call to save if autoSaveDisabled is set. |
| 176 * If `value` is null, deletes localStorage. | 177 * If `value` is null, deletes localStorage. |
| 177 */ | 178 */ |
| 178 save: function() { | 179 save: function() { |
| 179 var v = this.useRaw ? this.value : JSON.stringify(this.value); | 180 var v = this.useRaw ? this.value : JSON.stringify(this.value); |
| 180 try { | 181 try { |
| 181 if (this.value === null) { | 182 if (this.value === null) { |
| 182 localStorage.removeItem(this.name); | 183 window.localStorage.removeItem(this.name); |
| 183 } else { | 184 } else { |
| 184 localStorage.setItem(this.name, v); | 185 window.localStorage.setItem(this.name, /** @type {string} */ (v)); |
| 185 } | 186 } |
| 186 } | 187 } |
| 187 catch(ex) { | 188 catch(ex) { |
| 188 // Happens in Safari incognito mode, | 189 // Happens in Safari incognito mode, |
| 189 this.errorMessage = ex.message; | 190 this.errorMessage = ex.message; |
| 190 console.error("localStorage could not be saved. Safari incoginito mode?"
, ex); | 191 console.error("localStorage could not be saved. Safari incoginito mode?"
, ex); |
| 191 } | 192 } |
| 192 } | 193 } |
| 193 | 194 |
| 194 }); | 195 }); |
| 195 | 196 |
| 196 </script> | 197 </script> |
| OLD | NEW |