1;(function(window){ 2if (window.BX.localStorage) return; 3 4var 5 BX = window.BX, 6 localStorageInstance = null, 7 _prefix = null, 8 _key = '_bxCurrentKey', 9 _support = false; 10 11BX.localStorage = function() 12{ 13 this.keyChanges = {}; // flag to skip self changes in IE 14 BX.bind( 15 (BX.browser.IsIE() && !BX.browser.IsIE9()) ? document : window, // HATE! 16 'storage', 17 BX.proxy(this._onchange, this) 18 ); 19 20 setInterval(BX.delegate(this._clear, this), 5000); 21}; 22 23/* localStorage public interface */ 24 25BX.localStorage.checkBrowser = function() 26{ 27 return _support; 28}; 29 30BX.localStorage.set = function(key, value, ttl) 31{ 32 return BX.localStorage.instance().set(key, value, ttl); 33}; 34 35BX.localStorage.get = function(key) 36{ 37 return BX.localStorage.instance().get(key); 38}; 39 40BX.localStorage.remove = function(key) 41{ 42 return BX.localStorage.instance().remove(key); 43}; 44 45BX.localStorage.instance = function() 46{ 47 if (!localStorageInstance) 48 { 49 var support = BX.localStorage.checkBrowser(); 50 if (support == 'native') 51 localStorageInstance = new BX.localStorage(); 52 else if (support == 'ie8') 53 localStorageInstance = new BX.localStorageIE8(); 54 else if (support == 'ie7') 55 localStorageInstance = new BX.localStorageIE7(); 56 else 57 { 58 localStorageInstance = { 59 'set' : BX.DoNothing, 60 'get' : function(){return null}, 61 'remove' : BX.DoNothing 62 }; 63 } 64 } 65 return localStorageInstance; 66}; 67 68/* localStorage prototype */ 69BX.localStorage.prototype.prefix = function() 70{ 71 if (!_prefix) 72 { 73 _prefix = 'bx' + BX.message('USER_ID') + '-' + (BX.message.SITE_ID? BX.message('SITE_ID'): 'admin') + '-'; 74 } 75 76 return _prefix; 77}; 78 79BX.localStorage.prototype._onchange = function(e) 80{ 81 e = e || window.event; 82 83 if (!e.key) 84 return; 85 86 if (BX.browser.DetectIeVersion() > 0 && this.keyChanges[e.key]) 87 { 88 this.keyChanges[e.key] = false; 89 return; 90 } 91 92 if (!!e.key && e.key.substring(0,this.prefix().length) == this.prefix()) 93 { 94 var d = { 95 key: e.key.substring(this.prefix().length, e.key.length), 96 value: !!e.newValue? this._decode(e.newValue.substring(11, e.newValue.length)): null, 97 oldValue: !!e.oldValue? this._decode(e.oldValue.substring(11, e.oldValue.length)): null 98 }; 99 100 switch(d.key) 101 { 102 case 'BXGCE': // BX Global Custom Event 103 if (d.value) 104 { 105 BX.onCustomEvent(d.value.e, d.value.p); 106 } 107 break; 108 default: 109 // normal event handlers 110 if (e.newValue) 111 BX.onCustomEvent(window, 'onLocalStorageSet', [d]); 112 if (e.oldValue && !e.newValue) 113 BX.onCustomEvent(window, 'onLocalStorageRemove', [d]); 114 115 BX.onCustomEvent(window, 'onLocalStorageChange', [d]); 116 break; 117 } 118 } 119}; 120 121BX.localStorage.prototype._clear = function() 122{ 123 var curDate = +new Date(), key, i; 124 125 for (i=0; i<localStorage.length; i++) 126 { 127 key = localStorage.key(i); 128 if (key.substring(0,2) == 'bx') 129 { 130 var ttl = localStorage.getItem(key).split(':', 1)*1000; 131 if (curDate >= ttl) 132 localStorage.removeItem(key); 133 } 134 } 135}; 136 137BX.localStorage.prototype._encode = function(value) 138{ 139 if (typeof(value) == 'object') 140 value = JSON.stringify(value); 141 else 142 value = value.toString(); 143 return value; 144}; 145 146BX.localStorage.prototype._decode = function(value) 147{ 148 var answer = null; 149 if (!!value) 150 { 151 try {answer = JSON.parse(value);} 152 catch(e) { answer = value; } 153 } 154 return answer; 155}; 156 157BX.localStorage.prototype._trigger_error = function(e, key, value, ttl) 158{ 159 BX.onCustomEvent(this, 'onLocalStorageError', [e, {key: key, value: value, ttl: ttl}]); 160}; 161 162BX.localStorage.prototype.set = function(key, value, ttl) 163{ 164 if (!ttl || ttl <= 0) 165 ttl = 60; 166 167 if (key == undefined || key == null || value == undefined) 168 return false; 169 170 this.keyChanges[this.prefix()+key] = true; 171 try 172 { 173 localStorage.setItem( 174 this.prefix()+key, 175 (Math.round((+new Date())/1000)+ttl)+':'+this._encode(value) 176 ); 177 } 178 catch (e) 179 { 180 this._trigger_error(e, key, value, ttl); 181 } 182}; 183 184BX.localStorage.prototype.get = function(key) 185{ 186 var storageAnswer = localStorage.getItem(this.prefix()+key); 187 188 if (storageAnswer) 189 { 190 var ttl = storageAnswer.split(':', 1)*1000; 191 if ((+new Date()) <= ttl) 192 { 193 storageAnswer = storageAnswer.substring(11, storageAnswer.length); 194 return this._decode(storageAnswer); 195 } 196 } 197 198 return null; 199}; 200 201BX.localStorage.prototype.remove = function(key) 202{ 203 this.keyChanges[this.prefix()+key] = true; 204 localStorage.removeItem(this.prefix()+key); 205}; 206 207/************** IE 7 ******************/ 208 209BX.localStorageIE7 = function() 210{ 211 this.NS = 'BXLocalStorage'; 212 this.__current_state = {}; 213 this.keyChanges = {}; 214 215 BX.ready(BX.delegate(this._Init, this)); 216}; 217 218BX.extend(BX.localStorageIE7, BX.localStorage); 219 220BX.localStorageIE7.prototype._Init = function() 221{ 222 this.storage_element = document.body.appendChild(BX.create('DIV')); 223 this.storage_element.addBehavior('#default#userData'); 224 this.storage_element.load(this.NS); 225 226 var doc = this.storage_element.xmlDocument, 227 len = doc.firstChild.attributes.length; 228 229 for (var i = 0; i<len; i++) 230 { 231 if (!!doc.firstChild.attributes[i]) 232 { 233 var k = doc.firstChild.attributes[i].nodeName; 234 if (k.substring(0,this.prefix().length) == this.prefix()) 235 { 236 this.__current_state[k] = doc.firstChild.attributes[i].nodeValue; 237 } 238 } 239 } 240 241 setInterval(BX.delegate(this._Listener, this), 500); 242 setInterval(BX.delegate(this._clear, this), 5000); 243}; 244 245BX.localStorageIE7.prototype._Listener = function(bInit) 246{ 247 this.storage_element.load(this.NS); 248 249 var doc = this.storage_element.xmlDocument, 250 len = doc.firstChild.attributes.length, 251 i,k,v; 252 253 var new_state = {}, arChanges = []; 254 255 for (i = 0; i<len; i++) 256 { 257 if (!!doc.firstChild.attributes[i]) 258 { 259 k = doc.firstChild.attributes[i].nodeName; 260 if (k.substring(0,this.prefix().length) == this.prefix()) 261 { 262 v = doc.firstChild.attributes[i].nodeValue; 263 264 if (this.__current_state[k] != v) 265 { 266 arChanges.push({ 267 key: k, newValue: v, oldValue: this.__current_state[k] 268 }); 269 } 270 271 new_state[k] = v; 272 delete this.__current_state[k]; 273 } 274 } 275 } 276 277 for (i in this.__current_state) 278 { 279 if(this.__current_state.hasOwnProperty(i)) 280 { 281 arChanges.push({ 282 key: i, newValue: undefined, oldValue: this.__current_state[i] 283 }); 284 } 285 } 286 287 this.__current_state = new_state; 288 289 for (i=0; i<arChanges.length; i++) 290 { 291 this._onchange(arChanges[i]); 292 } 293}; 294 295BX.localStorageIE7.prototype._clear = function() 296{ 297 this.storage_element.load(this.NS); 298 299 var doc = this.storage_element.xmlDocument, 300 len = doc.firstChild.attributes.length, 301 curDate = +new Date(), 302 i,k,v,ttl; 303 304 for (i = 0; i<len; i++) 305 { 306 if (!!doc.firstChild.attributes[i]) 307 { 308 k = doc.firstChild.attributes[i].nodeName; 309 if (k.substring(0,2) == 'bx') 310 { 311 v = doc.firstChild.attributes[i].nodeValue; 312 ttl = v.split(':', 1)*1000; 313 if (curDate >= ttl) 314 { 315 doc.firstChild.removeAttribute(k) 316 } 317 } 318 } 319 } 320 321 this.storage_element.save(this.NS); 322}; 323 324BX.localStorageIE7.prototype.set = function(key, value, ttl) 325{ 326 if (!ttl || ttl <= 0) 327 ttl = 60; 328 329 try 330 { 331 this.storage_element.load(this.NS); 332 333 var doc = this.storage_element.xmlDocument; 334 335 this.keyChanges[this.prefix()+key] = true; 336 337 doc.firstChild.setAttribute( 338 this.prefix()+key, 339 (Math.round((+new Date())/1000)+ttl)+':'+this._encode(value) 340 ); 341 342 this.storage_element.save(this.NS); 343 } 344 catch(e) 345 { 346 this._trigger_error(e, key, value, ttl); 347 } 348}; 349 350BX.localStorageIE7.prototype.get = function(key) 351{ 352 this.storage_element.load(this.NS); 353 var doc = this.storage_element.xmlDocument; 354 355 var storageAnswer = doc.firstChild.getAttribute(this.prefix()+key); 356 357 if (storageAnswer) 358 { 359 var ttl = storageAnswer.split(':', 1)*1000; 360 if ((+new Date()) <= ttl) 361 { 362 storageAnswer = storageAnswer.substring(11, storageAnswer.length); 363 return this._decode(storageAnswer); 364 } 365 } 366 367 return null; 368}; 369 370BX.localStorageIE7.prototype.remove = function(key) 371{ 372 this.storage_element.load(this.NS); 373 374 var doc = this.storage_element.xmlDocument; 375 doc.firstChild.removeAttribute(this.prefix()+key); 376 377 this.keyChanges[this.prefix()+key] = true; 378 this.storage_element.save(this.NS); 379}; 380 381/************** IE 8 & FF 3.6 ***************/ 382 383BX.localStorageIE8 = function() 384{ 385 this.key = _key; 386 387 this.currentKey = null; 388 this.currentValue = null; 389 390 BX.localStorageIE8.superclass.constructor.apply(this); 391}; 392BX.extend(BX.localStorageIE8, BX.localStorage); 393 394BX.localStorageIE8.prototype._onchange = function(e) 395{ 396 if (null == this.currentKey) 397 { 398 this.currentKey = localStorage.getItem(this.key); 399 if (this.currentKey) 400 { 401 this.currentValue = localStorage.getItem(this.prefix() + this.currentKey); 402 } 403 } 404 else 405 { 406 e = { 407 key: this.prefix() + this.currentKey, 408 newValue: localStorage.getItem(this.prefix() + this.currentKey), 409 oldValue: this.currentValue 410 }; 411 412 this.currentKey = null; 413 this.currentValue = null; 414 415 // especially for FF3.6 416 if (this.keyChanges[e.key]) 417 { 418 this.keyChanges[e.key] = false;
419 return; 420 } 421 422 BX.localStorageIE8.superclass._onchange.apply(this, [e]); 423 } 424}; 425 426BX.localStorageIE8.prototype.set = function(key, value, ttl) 427{ 428 this.currentKey = null; 429 this.keyChanges[this.prefix()+key] = true; 430 431 try 432 { 433 localStorage.setItem(this.key, key); 434 BX.localStorageIE8.superclass.set.apply(this, arguments); 435 } 436 catch(e) 437 { 438 this._trigger_error(e, key, value, ttl); 439 } 440}; 441 442BX.localStorageIE8.prototype.remove = function(key) 443{ 444 this.currentKey = null; 445 this.keyChanges[this.prefix()+key] = true; 446 447 localStorage.setItem(this.key, key); 448 BX.localStorageIE8.superclass.remove.apply(this, arguments); 449}; 450 451/* additional functions */ 452 453BX.onGlobalCustomEvent = function(eventName, arEventParams, bSkipSelf) 454{ 455 if (!!BX.localStorage.checkBrowser()) 456 BX.localStorage.set('BXGCE', {e:eventName,p:arEventParams}, 1); 457 458 if (!bSkipSelf) 459 BX.onCustomEvent(eventName, arEventParams); 460}; 461 462/***************** initialize *********************/ 463 464try { 465 _support = !!localStorage.setItem; 466} catch(e) {} 467 468if (_support) 469{ 470 _support = 'native'; 471 472 // hack to check FF3.6 && IE8 473 var _target = (BX.browser.IsIE() && !BX.browser.IsIE9()) ? document : window, 474 _checkFFnIE8 = function(e) { 475 if (typeof(e||window.event).key == 'undefined') 476 _support = 'ie8'; 477 BX.unbind(_target, 'storage', _checkFFnIE8); 478 BX.localStorage.instance(); 479 }; 480 BX.bind(_target, 'storage', _checkFFnIE8); 481 482 try 483 { 484 localStorage.setItem(_key, null); 485 } 486 catch(e) 487 { 488 _support = false; 489 BX.localStorage.instance(); 490 } 491} 492else if (BX.browser.IsIE7()) 493{ 494 _support = 'ie7'; 495 BX.localStorage.instance(); 496} 497 498})(window); 499
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.