2 * Originally based of this code... - refactored for Roo...
5 * @author Benjamin Arthur Lupton <contact@balupton.com>
6 * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
7 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
13 // ====================================================================
19 * How long should the interval be before hashchange checks
21 thishashChangeInterval : 100,
25 * How long should the interval be before safari poll checks
27 safariPollInterval : 500,
31 * How long should the interval be before we perform a double check
33 doubleCheckInterval : 500,
37 * Force this.not to append suid
43 * How long should we wait between store calls
49 * How long should we wait between busy events
55 * If true will enable debug messages to be logged
61 * What is the title of the initial state
67 * If true, will force HTMl4 mode (hashtags)
73 * Want to override default options and call init manually.
79 * Which bugs are present
83 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
84 * https://bugs.webkit.org/show_bug.cgi?id=56249
89 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
90 * https://bugs.webkit.org/show_bug.cgi?id=42940
95 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
100 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
105 // ========================================================================
111 sessionStorage : false, // sessionStorage
113 intervalList : false, // array normally.
116 * Is History enabled?
120 // ====================================================================
125 * The store for all session specific data
131 * 1-1: State ID to State Object
137 * 1-1: State String to State ID
143 * 1-1: State URL to State ID
149 * Store the states in an array
151 storedStates : false,
155 * Saved the states in an array
161 * The list of queues to use
162 * First In, First Out
171 // ====================================================================
175 * History.stateChanged
176 * States whether or not the state has changed since the last double check was initialised
178 stateChanged : false,
181 * History.doubleChecker
182 * Contains the timeout used for the double checks
184 doubleChecker : false,
187 // Initialise History
188 init : function(options){
190 initialTitle : window.document.title,
195 this.storedStates=[];
199 Roo.apply(this,options)
201 // Check Load Status of Adapter
202 //if ( typeof this.Adapter === 'undefined' ) {
206 // Check Load Status of Core
207 if ( typeof this.initCore !== 'undefined' ) {
211 // Check Load Status of HTML4 Support
212 if ( typeof this.initHtml4 !== 'undefined' ) {
218 this.enabled = !this.emulated.pushState;
231 // ========================================================================
235 initCore : function(options){
237 this.intervalList = [];
241 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
242 this.sessionStorage.setItem('TEST', '1');
243 this.sessionStorage.removeItem('TEST');
245 this.sessionStorage = false;
249 if ( typeof this.initCore.initialized !== 'undefined' ) {
254 this.initCore.initialized = true;
263 * Clears all setInterval instances.
265 clearAllIntervals: function()
267 var i, il = this.intervalList;
268 if (typeof il !== "undefined" && il !== null) {
269 for (i = 0; i < il.length; i++) {
270 clearInterval(il[i]);
272 this.intervalList = null;
277 // ====================================================================
281 * debugLog(message,...)
282 * Logs the passed arguments if debug enabled
284 debugLog : function()
286 if ( (this.debug||false) ) {
287 Roo.log.apply(this,arguments);
293 // ====================================================================
297 * getInternetExplorerMajorVersion()
298 * Get's the major version of Internet Explorer
300 * @license Public Domain
301 * @author Benjamin Arthur Lupton <contact@balupton.com>
302 * @author James Padolsey <https://gist.github.com/527683>
304 getInternetExplorerMajorVersion : function(){
305 var result = this.getInternetExplorerMajorVersion.cached =
306 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
307 ? this.getInternetExplorerMajorVersion.cached
310 div = window.document.createElement('div'),
311 all = div.getElementsByTagName('i');
312 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
313 return (v > 4) ? v : false;
320 * isInternetExplorer()
321 * Are we using Internet Explorer?
323 * @license Public Domain
324 * @author Benjamin Arthur Lupton <contact@balupton.com>
326 isInternetExplorer : function(){
328 this.isInternetExplorer.cached =
329 (typeof this.isInternetExplorer.cached !== 'undefined')
330 ? this.isInternetExplorer.cached
331 : Boolean(this.getInternetExplorerMajorVersion())
337 * Which features require emulating?
345 initEmulated : function()
349 if (this.html4Mode) {
355 this.emulated.pushState = !Boolean(
356 window.history && window.history.pushState && window.history.replaceState
358 (/ Mobile\/([1-7][a-z]|(8([abcde]|f(1[0-8]))))/i).test(window.navigator.userAgent) /* disable for versions of iOS before version 4.3 (8F190) */
359 || (/AppleWebKit\/5([0-2]|3[0-2])/i).test(window.navigator.userAgent) /* disable for the mercury iOS browser, or at least older versions of the webkit engine */
362 this.emulated.hashChange = Boolean(
363 !(('onhashchange' in window) || ('onhashchange' in window.document))
365 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
374 * Checks to see if the Object is Empty
375 * @param {Object} obj
378 isEmptyObject = function(obj) {
379 for ( var name in obj ) {
380 if ( obj.hasOwnProperty(name) ) {
389 * Clones a object and eliminate all references to the original contexts
390 * @param {Object} obj
393 cloneObject = function(obj) {
396 hash = JSON.stringify(obj);
397 newObj = JSON.parse(hash);
406 // ====================================================================
411 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
412 * @return {String} rootUrl
414 getRootUrl = function(){
416 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
417 if ( window.document.location.port||false ) {
418 rootUrl += ':'+window.document.location.port;
428 * Fetches the `href` attribute of the `<base href="...">` element if it exists
429 * @return {String} baseHref
431 getBaseHref = function(){
434 baseElements = window.document.getElementsByTagName('base'),
438 // Test for Base Element
439 if ( baseElements.length === 1 ) {
440 // Prepare for Base Element
441 baseElement = baseElements[0];
442 baseHref = baseElement.href.replace(/[^\/]+$/,'');
445 // Adjust trailing slash
446 baseHref = baseHref.replace(/\/+$/,'');
447 if ( baseHref ) baseHref += '/';
455 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
456 * @return {String} baseUrl
458 getBaseUrl = function(){
460 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
468 * Fetches the URL of the current page
469 * @return {String} pageUrl
471 getPageUrl = function(){
474 State = this.getState(false,false),
475 stateUrl = (State||{}).url||this.getLocationHref(),
479 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
480 return (/\./).test(part) ? part : part+'/';
489 * Fetches the Url of the directory of the current page
490 * @return {String} basePageUrl
492 getBasePageUrl = function(){
494 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
495 return (/[^\/]$/).test(part) ? '' : part;
496 }).replace(/\/+$/,'')+'/';
504 * Ensures that we have an absolute URL and not a relative URL
505 * @param {string} url
506 * @param {Boolean} allowBaseHref
507 * @return {string} fullUrl
509 getFullUrl = function(url,allowBaseHref){
511 var fullUrl = url, firstChar = url.substring(0,1);
512 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
515 if ( /[a-z]+\:\/\//.test(url) ) {
518 else if ( firstChar === '/' ) {
520 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
522 else if ( firstChar === '#' ) {
524 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
526 else if ( firstChar === '?' ) {
528 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
532 if ( allowBaseHref ) {
533 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
535 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
537 // We have an if condition above as we do not want hashes
538 // which are relative to the baseHref in our URLs
539 // as if the baseHref changes, then all our bookmarks
540 // would now point to different locations
541 // whereas the basePageUrl will always stay the same
545 return fullUrl.replace(/\#$/,'');
550 * Ensures that we have a relative URL and not a absolute URL
551 * @param {string} url
552 * @return {string} url
554 getShortUrl = function(url){
556 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
559 if ( this.emulated.pushState ) {
560 // We are in a if statement as when pushState is not emulated
561 // The actual url these short urls are relative to can change
562 // So within the same session, we the url may end up somewhere different
563 shortUrl = shortUrl.replace(baseUrl,'');
567 shortUrl = shortUrl.replace(rootUrl,'/');
569 // Ensure we can still detect it as a state
570 if ( this.isTraditionalAnchor(shortUrl) ) {
571 shortUrl = './'+shortUrl;
575 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
582 * getLocationHref(document)
583 * Returns a normalized version of document.location.href
584 * accounting for browser inconsistencies, etc.
586 * This URL will be URI-encoded and will include the hash
588 * @param {object} document
589 * @return {string} url
591 getLocationHref = function(doc) {
592 doc = doc || window.document;
594 // most of the time, this will be true
595 if (doc.URL === doc.location.href)
596 return doc.location.href;
598 // some versions of webkit URI-decode document.location.href
599 // but they leave document.URL in an encoded state
600 if (doc.location.href === decodeURIComponent(doc.URL))
603 // FF 3.6 only updates document.URL when a page is reloaded
604 // document.location.href is updated correctly
605 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
606 return doc.location.href;
608 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
609 return doc.location.href;
611 return doc.URL || doc.location.href;
618 * Noramlize the store by adding necessary values
620 normalizeStore = function(){
621 this.store.idToState = this.store.idToState||{};
622 this.store.urlToId = this.store.urlToId||{};
623 this.store.stateToId = this.store.stateToId||{};
628 * Get an object containing the data, title and url of the current state
629 * @param {Boolean} friendly
630 * @param {Boolean} create
631 * @return {Object} State
633 getState : function(friendly,create){
635 if ( typeof friendly === 'undefined' ) { friendly = true; }
636 if ( typeof create === 'undefined' ) { create = true; }
639 var State = this.getLastSavedState();
642 if ( !State && create ) {
643 State = this.createStateObject();
648 State = this.cloneObject(State);
649 State.url = this.cleanUrl||State.url;
657 * getIdByState(State)
658 * Gets a ID for a State
659 * @param {State} newState
660 * @return {String} id
662 getIdByState = function(newState){
665 var id = this.extractId(newState.url),
669 // Find ID via State String
670 str = this.getStateString(newState);
671 if ( typeof this.stateToId[str] !== 'undefined' ) {
672 id = this.stateToId[str];
674 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
675 id = this.store.stateToId[str];
680 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
681 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
686 // Apply the new State to the ID
687 this.stateToId[str] = id;
688 this.idToState[id] = newState;
697 * normalizeState(State)
698 * Expands a State Object
699 * @param {object} State
702 normalizeState = function(oldState){
704 var newState, dataNotEmpty;
707 if ( !oldState || (typeof oldState !== 'object') ) {
712 if ( typeof oldState.normalized !== 'undefined' ) {
717 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
721 // ----------------------------------------------------------------
725 newState.normalized = true;
726 newState.title = oldState.title||'';
727 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
728 newState.hash = this.getShortUrl(newState.url);
729 newState.data = this.cloneObject(oldState.data);
732 newState.id = this.getIdByState(newState);
734 // ----------------------------------------------------------------
737 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
738 newState.url = newState.cleanUrl;
740 // Check to see if we have more than just a url
741 dataNotEmpty = !this.isEmptyObject(newState.data);
744 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
746 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
747 if ( !/\?/.test(newState.hash) ) {
748 newState.hash += '?';
750 newState.hash += '&_suid='+newState.id;
753 // Create the Hashed URL
754 newState.hashedUrl = this.getFullUrl(newState.hash);
756 // ----------------------------------------------------------------
758 // Update the URL if we have a duplicate
759 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
760 newState.url = newState.hashedUrl;
763 // ----------------------------------------------------------------
770 * createStateObject(data,title,url)
771 * Creates a object based on the data, title and url state params
772 * @param {object} data
773 * @param {string} title
774 * @param {string} url
777 createStateObject = function(data,title,url){
786 State = this.normalizeState(State);
794 * Get a state by it's UID
797 getStateById = function(id){
802 var State = this.idToState[id] || this.store.idToState[id] || undefined;
809 * Get a State's String
810 * @param {State} passedState
812 getStateString = function(passedState){
814 var State, cleanedState, str;
817 State = this.normalizeState(passedState);
822 title: passedState.title,
827 str = JSON.stringify(cleanedState);
835 * @param {State} passedState
836 * @return {String} id
838 getStateId = function(passedState){
843 State = this.normalizeState(passedState);
853 * getHashByState(State)
854 * Creates a Hash for the State Object
855 * @param {State} passedState
856 * @return {String} hash
858 getHashByState = function(passedState){
863 State = this.normalizeState(passedState);
873 * extractId(url_or_hash)
874 * Get a State ID by it's URL or Hash
875 * @param {string} url_or_hash
876 * @return {string} id
878 this.extractId = function ( url_or_hash ) {
880 var id,parts,url, tmp;
884 // If the URL has a #, use the id from before the #
885 if (url_or_hash.indexOf('#') != -1)
887 tmp = url_or_hash.split("#")[0];
894 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
895 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
896 id = parts ? String(parts[2]||'') : '';
903 * isTraditionalAnchor
904 * Checks to see if the url is a traditional anchor or not
905 * @param {String} url_or_hash
908 isTraditionalAnchor = function(url_or_hash){
910 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
913 return isTraditional;
918 * Get a State by it's URL or Hash
919 * @param {String} url_or_hash
920 * @return {State|null}
922 extractState = function(url_or_hash,create){
924 var State = null, id, url;
925 create = create||false;
928 id = this.extractId(url_or_hash);
930 State = this.getStateById(id);
933 // Fetch SUID returned no State
936 url = this.getFullUrl(url_or_hash);
939 id = this.getIdByUrl(url)||false;
941 State = this.getStateById(id);
945 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
946 State = this.createStateObject(null,null,url);
956 * Get a State ID by a State URL
958 getIdByUrl = function(url){
960 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
967 * getLastSavedState()
968 * Get an object containing the data, title and url of the current state
969 * @return {Object} State
971 getLastSavedState = function(){
972 return this.savedStates[this.savedStates.length-1]||undefined;
976 * getLastStoredState()
977 * Get an object containing the data, title and url of the current state
978 * @return {Object} State
980 getLastStoredState = function(){
981 return this.storedStates[this.storedStates.length-1]||undefined;
986 * Checks if a Url will have a url conflict
987 * @param {Object} newState
988 * @return {Boolean} hasDuplicate
990 hasUrlDuplicate = function(newState) {
992 var hasDuplicate = false,
996 oldState = this.extractState(newState.url);
999 hasDuplicate = oldState && oldState.id !== newState.id;
1002 return hasDuplicate;
1008 * @param {Object} newState
1009 * @return {Object} newState
1011 storeState = function(newState){
1013 this.urlToId[newState.url] = newState.id;
1016 this.storedStates.push(this.cloneObject(newState));
1023 * isLastSavedState(newState)
1024 * Tests to see if the state is the last state
1025 * @param {Object} newState
1026 * @return {boolean} isLast
1028 isLastSavedState = function(newState){
1031 newId, oldState, oldId;
1034 if ( this.savedStates.length ) {
1035 newId = newState.id;
1036 oldState = this.getLastSavedState();
1037 oldId = oldState.id;
1040 isLast = (newId === oldId);
1050 * @param {Object} newState
1051 * @return {boolean} changed
1053 saveState = function(newState){
1055 if ( this.isLastSavedState(newState) ) {
1060 this.savedStates.push(this.cloneObject(newState));
1068 * Gets a state by the index
1069 * @param {integer} index
1072 getStateByIndex = function(index){
1077 if ( typeof index === 'undefined' ) {
1078 // Get the last inserted
1079 State = this.savedStates[this.savedStates.length-1];
1081 else if ( index < 0 ) {
1083 State = this.savedStates[this.savedStates.length+index];
1086 // Get from the beginning
1087 State = this.savedStates[index];
1096 * Gets the current index
1099 getCurrentIndex = function(){
1104 if(this.savedStates.length < 1) {
1108 index = this.savedStates.length-1;
1113 // ====================================================================
1118 * @param {Location=} location
1119 * Gets the current document hash
1120 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1123 getHash = function(doc){
1124 var url = this.getLocationHref(doc),
1126 hash = this.getHashByUrl(url);
1132 * normalize and Unescape a Hash
1133 * @param {String} hash
1136 unescapeHash = function(hash){
1138 var result = this.normalizeHash(hash);
1141 result = decodeURIComponent(result);
1149 * normalize a hash across browsers
1152 normalizeHash = function(hash){
1154 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1162 * Sets the document hash
1163 * @param {string} hash
1164 * @return {Roo.History}
1166 setHash = function(hash,queue){
1171 if ( queue !== false && this.busy() ) {
1172 // Wait + Push to Queue
1173 //this.debug('this.setHash: we must wait', arguments);
1176 callback: this.setHash,
1184 //this.debug('this.setHash: called',hash);
1186 // Make Busy + Continue
1189 // Check if hash is a state
1190 State = this.extractState(hash,true);
1191 if ( State && !this.emulated.pushState ) {
1192 // Hash is a state so skip the setHash
1193 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1196 this.pushState(State.data,State.title,State.url,false);
1198 else if ( this.getHash() !== hash ) {
1199 // Hash is a proper hash, so apply it
1201 // Handle browser bugs
1202 if ( this.bugs.setHash ) {
1203 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1205 // Fetch the base page
1206 pageUrl = this.getPageUrl();
1208 // Safari hash apply
1209 this.pushState(null,null,pageUrl+'#'+hash,false);
1212 // Normal hash apply
1213 window.document.location.hash = hash;
1223 * normalize and Escape a Hash
1226 escapeHash = function(hash){
1228 var result = normalizeHash(hash);
1231 result = window.encodeURIComponent(result);
1234 if ( !this.bugs.hashEscape ) {
1235 // Restore common parts
1237 .replace(/\%21/g,'!')
1238 .replace(/\%26/g,'&')
1239 .replace(/\%3D/g,'=')
1240 .replace(/\%3F/g,'?');
1249 * Extracts the Hash from a URL
1250 * @param {string} url
1251 * @return {string} url
1253 getHashByUrl = function(url){
1255 var hash = String(url)
1256 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1260 hash = this.unescapeHash(hash);
1268 * Applies the title to the document
1269 * @param {State} newState
1272 setTitle = function(newState){
1274 var title = newState.title,
1279 firstState = this.getStateByIndex(0);
1280 if ( firstState && firstState.url === newState.url ) {
1281 title = firstState.title||this.initialTitle;
1287 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1289 catch ( Exception ) { }
1290 window.document.title = title;
1297 // ====================================================================
1303 * @param {boolean} value [optional]
1304 * @return {boolean} busy
1306 busy = function(value){
1308 if ( typeof value !== 'undefined' ) {
1309 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1310 this.busy_flag = value;
1313 else if ( typeof this.busy_flag === 'undefined' ) {
1314 this.busy_flag = false;
1318 if ( !this.busy_flag ) {
1319 // Execute the next item in the queue
1320 window.clearTimeout(this.busy.timeout);
1321 var fireNext = function(){
1323 if ( this.busy_flag ) return;
1324 for ( i=this.queues.length-1; i >= 0; --i ) {
1325 queue = this.queues[i];
1326 if ( queue.length === 0 ) continue;
1327 item = queue.shift();
1328 this.fireQueueItem(item);
1329 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1332 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1336 return this.busy_flag;
1342 * fireQueueItem(item)
1344 * @param {Object} item
1345 * @return {Mixed} result
1347 fireQueueItem = function(item){
1348 return item.callback.apply(item.scope||this,item.args||[]);
1352 * pushQueue(callback,args)
1353 * Add an item to the queue
1354 * @param {Object} item [scope,callback,args,queue]
1356 pushQueue = function(item){
1357 // Prepare the queue
1358 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1361 this.queues[item.queue||0].push(item);
1368 * queue (item,queue), (func,queue), (func), (item)
1369 * Either firs the item now if not busy, or adds it to the queue
1371 queue = function(item,queue){
1373 if ( typeof item === 'function' ) {
1378 if ( typeof queue !== 'undefined' ) {
1383 if ( this.busy() ) {
1384 this.pushQueue(item);
1386 this.fireQueueItem(item);
1397 clearQueue = function(){
1398 this.busy_flag = false;
1406 * doubleCheckComplete()
1407 * Complete a double check
1408 * @return {Roo.History}
1410 doubleCheckComplete = function(){
1412 this.stateChanged = true;
1415 this.doubleCheckClear();
1422 * doubleCheckClear()
1423 * Clear a double check
1424 * @return {Roo.History}
1426 doubleCheckClear = function(){
1428 if ( this.doubleChecker ) {
1429 window.clearTimeout(this.doubleChecker);
1430 this.doubleChecker = false;
1439 * Create a double check
1440 * @return {Roo.History}
1442 doubleCheck = function(tryAgain){
1444 this.stateChanged = false;
1445 this.doubleCheckClear();
1447 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1448 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1449 if ( this.bugs.ieDoubleCheck ) {
1451 this.doubleChecker = window.setTimeout(
1453 this.doubleCheckClear();
1454 if ( !this.stateChanged ) {
1455 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1461 this.doubleCheckInterval
1470 // ====================================================================
1475 * Poll the current state
1476 * @return {Roo.History}
1478 safariStatePoll = function(){
1481 // Get the Last State which has the new URL
1483 urlState = this.extractState(this.getLocationHref()),
1486 // Check for a difference
1487 if ( !this.isLastSavedState(urlState) ) {
1488 newState = urlState;
1494 // Check if we have a state with that url
1497 //this.debug('this.safariStatePoll: new');
1498 newState = this.createStateObject();
1501 // Apply the New State
1502 //this.debug('this.safariStatePoll: trigger');
1503 this.Adapter.trigger(window,'popstate');
1510 // ====================================================================
1515 * Send the browser history back one item
1516 * @param {Integer} queue [optional]
1518 back = function(queue){
1519 //this.debug('this.back: called', arguments);
1522 if ( queue !== false && this.busy() ) {
1523 // Wait + Push to Queue
1524 //this.debug('this.back: we must wait', arguments);
1527 callback: this.back,
1534 // Make Busy + Continue
1537 // Fix certain browser bugs that prevent the state from changing
1538 this.doubleCheck(function(){
1551 * Send the browser history forward one item
1552 * @param {Integer} queue [optional]
1554 forward = function(queue){
1555 //this.debug('this.forward: called', arguments);
1558 if ( queue !== false && this.busy() ) {
1559 // Wait + Push to Queue
1560 //this.debug('this.forward: we must wait', arguments);
1563 callback: this.forward,
1570 // Make Busy + Continue
1574 // Fix certain browser bugs that prevent the state from changing
1575 this.doubleCheck(function(){
1582 // End forward closure
1588 * Send the browser history back or forward index times
1589 * @param {Integer} queue [optional]
1591 go = function(index,queue){
1592 //this.debug('this.go: called', arguments);
1600 for ( i=1; i<=index; ++i ) {
1601 this.forward(queue);
1604 else if ( index < 0 ) {
1606 for ( i=-1; i>=index; --i ) {
1611 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1619 // ====================================================================
1620 // HTML5 State Support
1622 // Non-Native pushState Implementation
1623 if ( this.emulated.pushState ) {
1625 * Provide Skeleton for HTML4 Browsers
1629 var emptyFunction = function(){};
1630 History.pushState = History.pushState||emptyFunction;
1631 History.replaceState = History.replaceState||emptyFunction;
1632 } // History.emulated.pushState
1634 // Native pushState Implementation
1637 * Use native HTML5 History API Implementation
1641 * History.onPopState(event,extra)
1642 * Refresh the Current State
1644 History.onPopState = function(event,extra){
1646 var stateId = false, newState = false, currentHash, currentState;
1648 // Reset the double check
1649 History.doubleCheckComplete();
1651 // Check for a Hash, and handle apporiatly
1652 currentHash = History.getHash();
1653 if ( currentHash ) {
1655 currentState = History.extractState(currentHash||History.getLocationHref(),true);
1656 if ( currentState ) {
1657 // We were able to parse it, it must be a State!
1658 // Let's forward to replaceState
1659 //History.debug('History.onPopState: state anchor', currentHash, currentState);
1660 History.replaceState(currentState.data, currentState.title, currentState.url, false);
1663 // Traditional Anchor
1664 //History.debug('History.onPopState: traditional anchor', currentHash);
1665 History.Adapter.trigger(window,'anchorchange');
1666 History.busy(false);
1669 // We don't care for hashes
1670 History.expectedStateId = false;
1675 stateId = History.Adapter.extractEventData('state',event,extra) || false;
1679 // Vanilla: Back/forward button was used
1680 newState = History.getStateById(stateId);
1682 else if ( History.expectedStateId ) {
1683 // Vanilla: A new state was pushed, and popstate was called manually
1684 newState = History.getStateById(History.expectedStateId);
1688 newState = History.extractState(History.getLocationHref());
1691 // The State did not exist in our store
1693 // Regenerate the State
1694 newState = History.createStateObject(null,null,History.getLocationHref());
1698 History.expectedStateId = false;
1700 // Check if we are the same state
1701 if ( History.isLastSavedState(newState) ) {
1702 // There has been no change (just the page's hash has finally propagated)
1703 //History.debug('History.onPopState: no change', newState, History.savedStates);
1704 History.busy(false);
1709 History.storeState(newState);
1710 History.saveState(newState);
1712 // Force update of the title
1713 History.setTitle(newState);
1716 History.Adapter.trigger(window,'statechange');
1717 History.busy(false);
1722 History.Adapter.bind(window,'popstate',History.onPopState);
1725 * History.pushState(data,title,url)
1726 * Add a new State to the history object, become it, and trigger onpopstate
1727 * We have to trigger for HTML4 compatibility
1728 * @param {object} data
1729 * @param {string} title
1730 * @param {string} url
1733 History.pushState = function(data,title,url,queue){
1734 //History.debug('History.pushState: called', arguments);
1737 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1738 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1742 if ( queue !== false && History.busy() ) {
1743 // Wait + Push to Queue
1744 //History.debug('History.pushState: we must wait', arguments);
1747 callback: History.pushState,
1754 // Make Busy + Continue
1757 // Create the newState
1758 var newState = History.createStateObject(data,title,url);
1761 if ( History.isLastSavedState(newState) ) {
1762 // Won't be a change
1763 History.busy(false);
1766 // Store the newState
1767 History.storeState(newState);
1768 History.expectedStateId = newState.id;
1770 // Push the newState
1771 history.pushState(newState.id,newState.title,newState.url);
1774 History.Adapter.trigger(window,'popstate');
1777 // End pushState closure
1782 * History.replaceState(data,title,url)
1783 * Replace the State and trigger onpopstate
1784 * We have to trigger for HTML4 compatibility
1785 * @param {object} data
1786 * @param {string} title
1787 * @param {string} url
1790 History.replaceState = function(data,title,url,queue){
1791 //History.debug('History.replaceState: called', arguments);
1794 if ( History.getHashByUrl(url) && History.emulated.pushState ) {
1795 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1799 if ( queue !== false && History.busy() ) {
1800 // Wait + Push to Queue
1801 //History.debug('History.replaceState: we must wait', arguments);
1804 callback: History.replaceState,
1811 // Make Busy + Continue
1814 // Create the newState
1815 var newState = History.createStateObject(data,title,url);
1818 if ( History.isLastSavedState(newState) ) {
1819 // Won't be a change
1820 History.busy(false);
1823 // Store the newState
1824 History.storeState(newState);
1825 History.expectedStateId = newState.id;
1827 // Push the newState
1828 history.replaceState(newState.id,newState.title,newState.url);
1831 History.Adapter.trigger(window,'popstate');
1834 // End replaceState closure
1838 } // !History.emulated.pushState
1841 // ====================================================================
1847 if ( sessionStorage ) {
1850 History.store = JSON.parse(sessionStorage.getItem('History.store'))||{};
1857 History.normalizeStore();
1862 History.normalizeStore();
1866 * Clear Intervals on exit to prevent memory leaks
1868 History.Adapter.bind(window,"unload",History.clearAllIntervals);
1871 * Create the initial State
1873 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
1876 * Bind for Saving Store
1878 if ( sessionStorage ) {
1879 // When the page is closed
1880 History.onUnload = function(){
1882 var currentStore, item, currentStoreString;
1886 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1893 currentStore.idToState = currentStore.idToState || {};
1894 currentStore.urlToId = currentStore.urlToId || {};
1895 currentStore.stateToId = currentStore.stateToId || {};
1898 for ( item in History.idToState ) {
1899 if ( !History.idToState.hasOwnProperty(item) ) {
1902 currentStore.idToState[item] = History.idToState[item];
1904 for ( item in History.urlToId ) {
1905 if ( !History.urlToId.hasOwnProperty(item) ) {
1908 currentStore.urlToId[item] = History.urlToId[item];
1910 for ( item in History.stateToId ) {
1911 if ( !History.stateToId.hasOwnProperty(item) ) {
1914 currentStore.stateToId[item] = History.stateToId[item];
1918 History.store = currentStore;
1919 History.normalizeStore();
1921 // In Safari, going into Private Browsing mode causes the
1922 // Session Storage object to still exist but if you try and use
1923 // or set any property/function of it it throws the exception
1924 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1925 // add something to storage that exceeded the quota." infinitely
1927 currentStoreString = JSON.stringify(currentStore);
1930 sessionStorage.setItem('History.store', currentStoreString);
1933 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1934 if (sessionStorage.length) {
1935 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1936 // removing/resetting the storage can work.
1937 sessionStorage.removeItem('History.store');
1938 sessionStorage.setItem('History.store', currentStoreString);
1940 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1948 // For Internet Explorer
1949 History.intervalList.push(setInterval(History.onUnload,this.storeInterval));
1951 // For Other Browsers
1952 History.Adapter.bind(window,'beforeunload',History.onUnload);
1953 History.Adapter.bind(window,'unload',History.onUnload);
1955 // Both are enabled for consistency
1958 // Non-Native pushState Implementation
1959 if ( !History.emulated.pushState ) {
1960 // Be aware, the following is only for native pushState implementations
1961 // If you are wanting to include something for all browsers
1962 // Then include it above this if block
1967 if ( History.bugs.safariPoll ) {
1968 History.intervalList.push(setInterval(History.safariStatePoll, this.safariPollInterval));
1972 * Ensure Cross Browser Compatibility
1974 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1976 * Fix Safari HashChange Issue
1980 History.Adapter.bind(window,'hashchange',function(){
1981 History.Adapter.trigger(window,'popstate');
1985 if ( History.getHash() ) {
1986 History.Adapter.onDomLoad(function(){
1987 History.Adapter.trigger(window,'hashchange');
1992 } // !History.emulated.pushState
1995 }; // History.initCore
1997 // Try to Initialise History
1998 if (!History.options || !History.options.delayInit) {