1;(function(window) { 2 if (window.BX.localStorage) 3 { 4 return; 5 } 6 7 const BX = window.BX; 8 let localStorageInstance = null; 9 let _prefix = null; 10 11 BX.localStorage = function() { 12 BX.bind(window, 'storage', BX.proxy(this._onchange, this)); 13 setInterval(BX.delegate(this._clear, this), 5000); 14 }; 15 16 /* localStorage public interface */ 17 18 BX.localStorage.checkBrowser = function() { 19 return true; 20 }; 21 22 BX.localStorage.set = function(key, value, ttl) { 23 return BX.localStorage.instance().set(key, value, ttl); 24 }; 25 26 BX.localStorage.get = function(key) { 27 return BX.localStorage.instance().get(key); 28 }; 29 30 BX.localStorage.remove = function(key) { 31 return BX.localStorage.instance().remove(key); 32 }; 33 34 BX.localStorage.instance = function() { 35 if (!localStorageInstance) 36 { 37 localStorageInstance = new BX.localStorage(); 38 } 39 40 return localStorageInstance; 41 }; 42 43 /* localStorage prototype */ 44 BX.localStorage.prototype.prefix = function() { 45 if (!_prefix) 46 { 47 _prefix = 'bx' + BX.message('USER_ID') + '-' + (BX.message.SITE_ID ? BX.message('SITE_ID') : 'admin') + '-'; 48 } 49 50 return _prefix; 51 }; 52 53 BX.localStorage.prototype._onchange = function(e) { 54 e = e || window.event; 55 56 if (!e.key) 57 { 58 return; 59 } 60 61 if (!!e.key && e.key.substring(0, this.prefix().length) == this.prefix()) 62 { 63 var d = { 64 key: e.key.substring(this.prefix().length, e.key.length), 65 value: !!e.newValue ? this._decode(e.newValue.substring(11, e.newValue.length)) : null, 66 oldValue: !!e.oldValue ? this._decode(e.oldValue.substring(11, e.oldValue.length)) : null, 67 }; 68 69 switch (d.key) 70 { 71 case 'BXGCE': // BX Global Custom Event 72 if (d.value) 73 { 74 BX.onCustomEvent(d.value.e, d.value.p); 75 } 76 break; 77 default: 78 // normal event handlers 79 if (e.newValue) 80 { 81 BX.onCustomEvent(window, 'onLocalStorageSet', [d]); 82 } 83 if (e.oldValue && !e.newValue) 84 { 85 BX.onCustomEvent(window, 'onLocalStorageRemove', [d]); 86 } 87 88 BX.onCustomEvent(window, 'onLocalStorageChange', [d]); 89 break; 90 } 91 } 92 }; 93 94 BX.localStorage.prototype._clear = function() { 95 var curDate = +new Date(), 96 key, 97 i; 98 99 for (i = 0; i < localStorage.length; i++) 100 { 101 key = localStorage.key(i); 102 if (key.substring(0, 2) == 'bx') 103 { 104 var ttl = localStorage.getItem(key).split(':', 1) * 1000; 105 if (curDate >= ttl) 106 { 107 localStorage.removeItem(key); 108 } 109 } 110 } 111 }; 112 113 BX.localStorage.prototype._encode = function(value) { 114 if (typeof (value) == 'object') 115 { 116 value = JSON.stringify(value); 117 } 118 else 119 { 120 value = value.toString(); 121 } 122 123 return value; 124 }; 125 126 BX.localStorage.prototype._decode = function(value) { 127 var answer = null; 128 if (!!value) 129 { 130 try 131 { 132 answer = JSON.parse(value); 133 } 134 catch (e) 135 { 136 answer = value; 137 } 138 } 139 140 return answer; 141 }; 142 143 BX.localStorage.prototype._trigger_error = function(e, key, value, ttl) { 144 BX.onCustomEvent(this, 'onLocalStorageError', [e, { key, value, ttl }]); 145 }; 146 147 BX.localStorage.prototype.set = function(key, value, ttl) { 148 if (!ttl || ttl <= 0) 149 { 150 ttl = 60; 151 } 152 153 if (key == undefined || key == null || value == undefined) 154 { 155 return false; 156 } 157 158 try 159 { 160 localStorage.setItem( 161 this.prefix() + key, 162 (Math.round((+new Date()) / 1000) + ttl) + ':' + this._encode(value), 163 ); 164 } 165 catch (e) 166 { 167 this._trigger_error(e, key, value, ttl); 168 } 169 }; 170 171 BX.localStorage.prototype.get = function(key) { 172 var storageAnswer = localStorage.getItem(this.prefix() + key); 173 174 if (storageAnswer) 175 { 176 var ttl = storageAnswer.split(':', 1) * 1000; 177 if ((+new Date()) <= ttl) 178 { 179 storageAnswer = storageAnswer.substring(11, storageAnswer.length); 180 return this._decode(storageAnswer); 181 } 182 } 183 184 return null; 185 }; 186 187 BX.localStorage.prototype.remove = function(key) { 188 localStorage.removeItem(this.prefix() + key); 189 }; 190 191 /* additional functions */ 192 193 BX.onGlobalCustomEvent = function(eventName, arEventParams, bSkipSelf) { 194 if (!!BX.localStorage.checkBrowser()) 195 { 196 BX.localStorage.set('BXGCE', { e: eventName, p: arEventParams }, 1); 197 } 198 199 if (!bSkipSelf) 200 {
201 BX.onCustomEvent(eventName, arEventParams); 202 } 203 }; 204 205 BX.localStorage.instance(); 206})(window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.