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