2 * Originally based of this code... - refactored for Roo...
3 * https://github.com/browserstate/history.js
6 * @author Benjamin Arthur Lupton <contact@balupton.com>
7 * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
8 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
10 * Hackily modifyed by alan@roojs.com
12 * this is not initialized automatically..
13 * must call Roo.History.init( { ... options... });
17 * Documentation to be done....
23 // ====================================================================
29 * How long should the interval be before hashchange checks
31 thishashChangeInterval : 100,
35 * How long should the interval be before safari poll checks
37 safariPollInterval : 500,
41 * How long should the interval be before we perform a double check
43 doubleCheckInterval : 500,
47 * Force this.not to append suid
53 * How long should we wait between store calls
59 * How long should we wait between busy events
65 * If true will enable debug messages to be logged
71 * What is the title of the initial state
77 * If true, will force HTMl4 mode (hashtags)
83 * Want to override default options and call init manually.
89 * Which bugs are present
93 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
94 * https://bugs.webkit.org/show_bug.cgi?id=56249
99 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
100 * https://bugs.webkit.org/show_bug.cgi?id=42940
105 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
107 ieDoubleCheck: false,
110 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
115 // ========================================================================
121 sessionStorage : false, // sessionStorage
123 intervalList : false, // array normally.
126 * Is History enabled?
130 // ====================================================================
135 * The store for all session specific data
141 * 1-1: State ID to State Object
147 * 1-1: State String to State ID
153 * 1-1: State URL to State ID
159 * Store the states in an array
161 storedStates : false,
165 * Saved the states in an array
171 * The list of queues to use
172 * First In, First Out
181 // ====================================================================
185 * History.stateChanged
186 * States whether or not the state has changed since the last double check was initialised
188 stateChanged : false,
191 * History.doubleChecker
192 * Contains the timeout used for the double checks
194 doubleChecker : false,
197 // Initialise History
198 init : function(options)
201 var emptyFunction = function(){};
205 this.initialTitle = window.document.title;
210 this.storedStates=[];
214 Roo.apply(this,options)
217 // Check Load Status of Core
220 // Check Load Status of HTML4 Support
221 //if ( typeof this.initHtml4 !== 'undefined' ) {
228 this.enabled = !this.emulated.pushState;
230 if ( this.emulated.pushState ) {
234 this.pushState = emptyFunction;
235 this.replaceState = emptyFunction;
243 Roo.get(window).on('popstate',this.onPopState, this);
249 if ( this.sessionStorage ) {
252 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
257 this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
259 // For Other Browsers
260 Roo.get(window).on('beforeunload',this.onUnload,this);
261 Roo.get(window).on('unload',this.onUnload, this);
264 this.onUnload = emptyFunction;
268 this.normalizeStore();
270 * Clear Intervals on exit to prevent memory leaks
272 Roo.get(window).on('unload',this.clearAllIntervals, this);
275 * Create the initial State
277 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
281 // Non-Native pushState Implementation
282 if ( !this.emulated.pushState ) {
283 // Be aware, the following is only for native pushState implementations
284 // If you are wanting to include something for all browsers
285 // Then include it above this if block
290 if ( this.bugs.safariPoll ) {
291 this.intervalList.push(setInterval(this.safariStatePoll, this.safariPollInterval));
295 * Ensure Cross Browser Compatibility
297 //if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
302 * Fix Safari HashChange Issue
306 Roo.get(window).on('hashchange',function(){
307 Roo.get(window).fireEvent('popstate');
311 if ( this.getHash() ) {
312 Roo.onReady(function(){
313 Roo.get(window).fireEvent('hashchange');
318 } // !History.emulated.pushState
328 // ========================================================================
332 initCore : function(options){
334 this.intervalList = [];
338 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
339 this.sessionStorage.setItem('TEST', '1');
340 this.sessionStorage.removeItem('TEST');
342 this.sessionStorage = false;
346 if ( typeof this.initCore.initialized !== 'undefined' ) {
351 this.initCore.initialized = true;
360 * Clears all setInterval instances.
362 clearAllIntervals: function()
364 var i, il = this.intervalList;
365 if (typeof il !== "undefined" && il !== null) {
366 for (i = 0; i < il.length; i++) {
367 clearInterval(il[i]);
369 this.intervalList = null;
374 // ====================================================================
378 * debugLog(message,...)
379 * Logs the passed arguments if debug enabled
381 debugLog : function()
383 if ( (this.debug||false) ) {
384 Roo.log.apply(this,arguments);
390 // ====================================================================
394 * getInternetExplorerMajorVersion()
395 * Get's the major version of Internet Explorer
397 * @license Public Domain
398 * @author Benjamin Arthur Lupton <contact@balupton.com>
399 * @author James Padolsey <https://gist.github.com/527683>
401 getInternetExplorerMajorVersion : function(){
402 var result = this.getInternetExplorerMajorVersion.cached =
403 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
404 ? this.getInternetExplorerMajorVersion.cached
407 div = window.document.createElement('div'),
408 all = div.getElementsByTagName('i');
409 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
410 return (v > 4) ? v : false;
417 * isInternetExplorer()
418 * Are we using Internet Explorer?
420 * @license Public Domain
421 * @author Benjamin Arthur Lupton <contact@balupton.com>
423 isInternetExplorer : function(){
425 this.isInternetExplorer.cached =
426 (typeof this.isInternetExplorer.cached !== 'undefined')
427 ? this.isInternetExplorer.cached
428 : Boolean(this.getInternetExplorerMajorVersion())
434 * Which features require emulating?
442 initEmulated : function()
446 if (this.html4Mode) {
452 this.emulated.pushState = !Boolean(
453 window.history && window.history.pushState && window.history.replaceState
455 (/ 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) */
456 || (/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 */
459 this.emulated.hashChange = Boolean(
460 !(('onhashchange' in window) || ('onhashchange' in window.document))
462 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
467 initBugs : function ()
473 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
474 * https://bugs.webkit.org/show_bug.cgi?id=56249
476 this.bugs.setHash = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
477 && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
480 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
481 * https://bugs.webkit.org/show_bug.cgi?id=42940
483 this.bugs.safariPoll = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
484 && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
487 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
489 this.bugs.ieDoubleCheck = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8);
492 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
494 this.bugs.hashEscape = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 7);
500 * Checks to see if the Object is Empty
501 * @param {Object} obj
504 isEmptyObject : function(obj) {
505 for ( var name in obj ) {
506 if ( obj.hasOwnProperty(name) ) {
515 * Clones a object and eliminate all references to the original contexts
516 * @param {Object} obj
519 cloneObject : function(obj) {
522 hash = JSON.stringify(obj);
523 newObj = JSON.parse(hash);
532 // ====================================================================
537 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
538 * @return {String} rootUrl
540 getRootUrl : function(){
542 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
543 if ( window.document.location.port||false ) {
544 rootUrl += ':'+window.document.location.port;
554 * Fetches the `href` attribute of the `<base href="...">` element if it exists
555 * @return {String} baseHref
557 getBaseHref : function(){
560 baseElements = window.document.getElementsByTagName('base'),
564 // Test for Base Element
565 if ( baseElements.length === 1 ) {
566 // Prepare for Base Element
567 baseElement = baseElements[0];
568 baseHref = baseElement.href.replace(/[^\/]+$/,'');
571 // Adjust trailing slash
572 baseHref = baseHref.replace(/\/+$/,'');
573 if ( baseHref ) baseHref += '/';
581 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
582 * @return {String} baseUrl
584 getBaseUrl : function(){
586 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
594 * Fetches the URL of the current page
595 * @return {String} pageUrl
597 getPageUrl : function(){
600 State = this.getState(false,false),
601 stateUrl = (State||{}).url||this.getLocationHref(),
605 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
606 return (/\./).test(part) ? part : part+'/';
615 * Fetches the Url of the directory of the current page
616 * @return {String} basePageUrl
618 getBasePageUrl : function(){
620 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
621 return (/[^\/]$/).test(part) ? '' : part;
622 }).replace(/\/+$/,'')+'/';
630 * Ensures that we have an absolute URL and not a relative URL
631 * @param {string} url
632 * @param {Boolean} allowBaseHref
633 * @return {string} fullUrl
635 getFullUrl : function(url,allowBaseHref){
637 var fullUrl = url, firstChar = url.substring(0,1);
638 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
641 if ( /[a-z]+\:\/\//.test(url) ) {
644 else if ( firstChar === '/' ) {
646 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
648 else if ( firstChar === '#' ) {
650 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
652 else if ( firstChar === '?' ) {
654 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
658 if ( allowBaseHref ) {
659 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
661 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
663 // We have an if condition above as we do not want hashes
664 // which are relative to the baseHref in our URLs
665 // as if the baseHref changes, then all our bookmarks
666 // would now point to different locations
667 // whereas the basePageUrl will always stay the same
671 return fullUrl.replace(/\#$/,'');
676 * Ensures that we have a relative URL and not a absolute URL
677 * @param {string} url
678 * @return {string} url
680 getShortUrl : function(url){
682 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
685 if ( this.emulated.pushState ) {
686 // We are in a if statement as when pushState is not emulated
687 // The actual url these short urls are relative to can change
688 // So within the same session, we the url may end up somewhere different
689 shortUrl = shortUrl.replace(baseUrl,'');
693 shortUrl = shortUrl.replace(rootUrl,'/');
695 // Ensure we can still detect it as a state
696 if ( this.isTraditionalAnchor(shortUrl) ) {
697 shortUrl = './'+shortUrl;
701 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
708 * getLocationHref(document)
709 * Returns a normalized version of document.location.href
710 * accounting for browser inconsistencies, etc.
712 * This URL will be URI-encoded and will include the hash
714 * @param {object} document
715 * @return {string} url
717 getLocationHref : function(doc) {
718 doc = doc || window.document;
720 // most of the time, this will be true
721 if (doc.URL === doc.location.href)
722 return doc.location.href;
724 // some versions of webkit URI-decode document.location.href
725 // but they leave document.URL in an encoded state
726 if (doc.location.href === decodeURIComponent(doc.URL))
729 // FF 3.6 only updates document.URL when a page is reloaded
730 // document.location.href is updated correctly
731 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
732 return doc.location.href;
734 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
735 return doc.location.href;
737 return doc.URL || doc.location.href;
744 * Noramlize the store by adding necessary values
746 normalizeStore : function()
749 this.store.idToState = this.store.idToState||{};
750 this.store.urlToId = this.store.urlToId||{};
751 this.store.stateToId = this.store.stateToId||{};
756 * Get an object containing the data, title and url of the current state
757 * @param {Boolean} friendly
758 * @param {Boolean} create
759 * @return {Object} State
761 getState : function(friendly,create){
763 if ( typeof friendly === 'undefined' ) { friendly = true; }
764 if ( typeof create === 'undefined' ) { create = true; }
767 var State = this.getLastSavedState();
770 if ( !State && create ) {
771 State = this.createStateObject();
776 State = this.cloneObject(State);
777 State.url = this.cleanUrl||State.url;
785 * getIdByState(State)
786 * Gets a ID for a State
787 * @param {State} newState
788 * @return {String} id
790 getIdByState : function(newState){
793 var id = this.extractId(newState.url),
797 // Find ID via State String
798 str = this.getStateString(newState);
799 if ( typeof this.stateToId[str] !== 'undefined' ) {
800 id = this.stateToId[str];
802 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
803 id = this.store.stateToId[str];
808 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
809 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
814 // Apply the new State to the ID
815 this.stateToId[str] = id;
816 this.idToState[id] = newState;
825 * normalizeState(State)
826 * Expands a State Object
827 * @param {object} State
830 normalizeState : function(oldState){
832 var newState, dataNotEmpty;
835 if ( !oldState || (typeof oldState !== 'object') ) {
840 if ( typeof oldState.normalized !== 'undefined' ) {
845 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
849 // ----------------------------------------------------------------
853 newState.normalized = true;
854 newState.title = oldState.title||'';
855 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
856 newState.hash = this.getShortUrl(newState.url);
857 newState.data = this.cloneObject(oldState.data);
860 newState.id = this.getIdByState(newState);
862 // ----------------------------------------------------------------
865 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
866 newState.url = newState.cleanUrl;
868 // Check to see if we have more than just a url
869 dataNotEmpty = !this.isEmptyObject(newState.data);
872 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
874 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
875 if ( !/\?/.test(newState.hash) ) {
876 newState.hash += '?';
878 newState.hash += '&_suid='+newState.id;
881 // Create the Hashed URL
882 newState.hashedUrl = this.getFullUrl(newState.hash);
884 // ----------------------------------------------------------------
886 // Update the URL if we have a duplicate
887 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
888 newState.url = newState.hashedUrl;
891 // ----------------------------------------------------------------
898 * createStateObject(data,title,url)
899 * Creates a object based on the data, title and url state params
900 * @param {object} data
901 * @param {string} title
902 * @param {string} url
905 createStateObject : function(data,title,url){
914 State = this.normalizeState(State);
922 * Get a state by it's UID
925 getStateById : function(id){
930 var State = this.idToState[id] || this.store.idToState[id] || undefined;
937 * Get a State's String
938 * @param {State} passedState
940 getStateString : function(passedState){
942 var State, cleanedState, str;
945 State = this.normalizeState(passedState);
950 title: passedState.title,
955 str = JSON.stringify(cleanedState);
963 * @param {State} passedState
964 * @return {String} id
966 getStateId : function(passedState){
971 State = this.normalizeState(passedState);
981 * getHashByState(State)
982 * Creates a Hash for the State Object
983 * @param {State} passedState
984 * @return {String} hash
986 getHashByState : function(passedState){
991 State = this.normalizeState(passedState);
1001 * extractId(url_or_hash)
1002 * Get a State ID by it's URL or Hash
1003 * @param {string} url_or_hash
1004 * @return {string} id
1006 extractId : function ( url_or_hash ) {
1008 var id,parts,url, tmp;
1012 // If the URL has a #, use the id from before the #
1013 if (url_or_hash.indexOf('#') != -1)
1015 tmp = url_or_hash.split("#")[0];
1022 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
1023 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
1024 id = parts ? String(parts[2]||'') : '';
1031 * isTraditionalAnchor
1032 * Checks to see if the url is a traditional anchor or not
1033 * @param {String} url_or_hash
1036 isTraditionalAnchor : function(url_or_hash){
1038 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
1041 return isTraditional;
1046 * Get a State by it's URL or Hash
1047 * @param {String} url_or_hash
1048 * @return {State|null}
1050 extractState : function(url_or_hash,create){
1052 var State = null, id, url;
1053 create = create||false;
1056 id = this.extractId(url_or_hash);
1058 State = this.getStateById(id);
1061 // Fetch SUID returned no State
1064 url = this.getFullUrl(url_or_hash);
1067 id = this.getIdByUrl(url)||false;
1069 State = this.getStateById(id);
1073 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
1074 State = this.createStateObject(null,null,url);
1084 * Get a State ID by a State URL
1086 getIdByUrl : function(url){
1088 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1095 * getLastSavedState()
1096 * Get an object containing the data, title and url of the current state
1097 * @return {Object} State
1099 getLastSavedState : function(){
1100 return this.savedStates[this.savedStates.length-1]||undefined;
1104 * getLastStoredState()
1105 * Get an object containing the data, title and url of the current state
1106 * @return {Object} State
1108 getLastStoredState : function(){
1109 return this.storedStates[this.storedStates.length-1]||undefined;
1114 * Checks if a Url will have a url conflict
1115 * @param {Object} newState
1116 * @return {Boolean} hasDuplicate
1118 hasUrlDuplicate : function(newState) {
1120 var hasDuplicate = false,
1124 oldState = this.extractState(newState.url);
1127 hasDuplicate = oldState && oldState.id !== newState.id;
1130 return hasDuplicate;
1136 * @param {Object} newState
1137 * @return {Object} newState
1139 storeState : function(newState){
1141 this.urlToId[newState.url] = newState.id;
1144 this.storedStates.push(this.cloneObject(newState));
1151 * isLastSavedState(newState)
1152 * Tests to see if the state is the last state
1153 * @param {Object} newState
1154 * @return {boolean} isLast
1156 isLastSavedState : function(newState){
1159 newId, oldState, oldId;
1162 if ( this.savedStates.length ) {
1163 newId = newState.id;
1164 oldState = this.getLastSavedState();
1165 oldId = oldState.id;
1168 isLast = (newId === oldId);
1178 * @param {Object} newState
1179 * @return {boolean} changed
1181 saveState : function(newState){
1183 if ( this.isLastSavedState(newState) ) {
1188 this.savedStates.push(this.cloneObject(newState));
1196 * Gets a state by the index
1197 * @param {integer} index
1200 getStateByIndex : function(index){
1205 if ( typeof index === 'undefined' ) {
1206 // Get the last inserted
1207 State = this.savedStates[this.savedStates.length-1];
1209 else if ( index < 0 ) {
1211 State = this.savedStates[this.savedStates.length+index];
1214 // Get from the beginning
1215 State = this.savedStates[index];
1224 * Gets the current index
1227 getCurrentIndex : function(){
1232 if(this.savedStates.length < 1) {
1236 index = this.savedStates.length-1;
1241 // ====================================================================
1246 * @param {Location=} location
1247 * Gets the current document hash
1248 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1251 getHash : function(doc){
1252 var url = this.getLocationHref(doc),
1254 hash = this.getHashByUrl(url);
1260 * normalize and Unescape a Hash
1261 * @param {String} hash
1264 unescapeHash : function(hash){
1266 var result = this.normalizeHash(hash);
1269 result = decodeURIComponent(result);
1277 * normalize a hash across browsers
1280 normalizeHash : function(hash){
1282 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1290 * Sets the document hash
1291 * @param {string} hash
1292 * @return {Roo.History}
1294 setHash : function(hash,queue){
1299 if ( queue !== false && this.busy() ) {
1300 // Wait + Push to Queue
1301 //this.debug('this.setHash: we must wait', arguments);
1304 callback: this.setHash,
1312 //this.debug('this.setHash: called',hash);
1314 // Make Busy + Continue
1317 // Check if hash is a state
1318 State = this.extractState(hash,true);
1319 if ( State && !this.emulated.pushState ) {
1320 // Hash is a state so skip the setHash
1321 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1324 this.pushState(State.data,State.title,State.url,false);
1326 else if ( this.getHash() !== hash ) {
1327 // Hash is a proper hash, so apply it
1329 // Handle browser bugs
1330 if ( this.bugs.setHash ) {
1331 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1333 // Fetch the base page
1334 pageUrl = this.getPageUrl();
1336 // Safari hash apply
1337 this.pushState(null,null,pageUrl+'#'+hash,false);
1340 // Normal hash apply
1341 window.document.location.hash = hash;
1351 * normalize and Escape a Hash
1354 escapeHash : function(hash){
1356 var result = normalizeHash(hash);
1359 result = window.encodeURIComponent(result);
1362 if ( !this.bugs.hashEscape ) {
1363 // Restore common parts
1365 .replace(/\%21/g,'!')
1366 .replace(/\%26/g,'&')
1367 .replace(/\%3D/g,'=')
1368 .replace(/\%3F/g,'?');
1377 * Extracts the Hash from a URL
1378 * @param {string} url
1379 * @return {string} url
1381 getHashByUrl : function(url){
1383 var hash = String(url)
1384 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1388 hash = this.unescapeHash(hash);
1396 * Applies the title to the document
1397 * @param {State} newState
1400 setTitle : function(newState){
1402 var title = newState.title,
1407 firstState = this.getStateByIndex(0);
1408 if ( firstState && firstState.url === newState.url ) {
1409 title = firstState.title||this.initialTitle;
1415 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1417 catch ( Exception ) { }
1418 window.document.title = title;
1425 // ====================================================================
1431 * @param {boolean} value [optional]
1432 * @return {boolean} busy
1434 busy : function(value){
1438 if ( typeof value !== 'undefined' ) {
1439 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1440 this.busy_flag = value;
1443 else if ( typeof this.busy_flag === 'undefined' ) {
1444 this.busy_flag = false;
1448 if ( !this.busy_flag ) {
1452 // Execute the next item in the queue
1453 window.clearTimeout(this.busy.timeout);
1454 var fireNext = function(){
1456 if ( _this.busy_flag ) return;
1457 for ( i=_this.queues.length-1; i >= 0; --i ) {
1458 queue = _this.queues[i];
1459 if ( queue.length === 0 ) continue;
1460 item = queue.shift();
1461 _this.fireQueueItem(item);
1462 _this.busy.timeout = window.setTimeout(fireNext,_this.busyDelay);
1465 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1469 return this.busy_flag;
1475 * fireQueueItem(item)
1477 * @param {Object} item
1478 * @return {Mixed} result
1480 fireQueueItem : function(item){
1481 return item.callback.apply(item.scope||this,item.args||[]);
1485 * pushQueue(callback,args)
1486 * Add an item to the queue
1487 * @param {Object} item [scope,callback,args,queue]
1489 pushQueue : function(item){
1490 // Prepare the queue
1491 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1494 this.queues[item.queue||0].push(item);
1501 * queue (item,queue), (func,queue), (func), (item)
1502 * Either firs the item now if not busy, or adds it to the queue
1504 queue : function(item,queue){
1506 if ( typeof item === 'function' ) {
1511 if ( typeof queue !== 'undefined' ) {
1516 if ( this.busy() ) {
1517 this.pushQueue(item);
1519 this.fireQueueItem(item);
1530 clearQueue : function(){
1531 this.busy_flag = false;
1539 * doubleCheckComplete()
1540 * Complete a double check
1541 * @return {Roo.History}
1543 doubleCheckComplete : function(){
1545 this.stateChanged = true;
1548 this.doubleCheckClear();
1555 * doubleCheckClear()
1556 * Clear a double check
1557 * @return {Roo.History}
1559 doubleCheckClear : function(){
1561 if ( this.doubleChecker ) {
1562 window.clearTimeout(this.doubleChecker);
1563 this.doubleChecker = false;
1572 * Create a double check
1573 * @return {Roo.History}
1575 doubleCheck : function(tryAgain)
1579 this.stateChanged = false;
1580 this.doubleCheckClear();
1582 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1583 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1584 if ( this.bugs.ieDoubleCheck ) {
1586 this.doubleChecker = window.setTimeout(
1588 _this.doubleCheckClear();
1589 if ( !_this.stateChanged ) {
1590 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1596 this.doubleCheckInterval
1605 // ====================================================================
1610 * Poll the current state
1611 * @return {Roo.History}
1613 safariStatePoll : function(){
1616 // Get the Last State which has the new URL
1618 urlState = this.extractState(this.getLocationHref()),
1621 // Check for a difference
1622 if ( !this.isLastSavedState(urlState) ) {
1623 newState = urlState;
1629 // Check if we have a state with that url
1632 //this.debug('this.safariStatePoll: new');
1633 newState = this.createStateObject();
1636 // Apply the New State
1637 //this.debug('this.safariStatePoll: trigger');
1638 Roo.get(window).fireEvent('popstate');
1645 // ====================================================================
1650 * Send the browser history back one item
1651 * @param {Integer} queue [optional]
1653 back : function(queue)
1655 //this.debug('this.back: called', arguments);
1658 if ( queue !== false && this.busy() ) {
1659 // Wait + Push to Queue
1660 //this.debug('this.back: we must wait', arguments);
1663 callback: this.back,
1670 // Make Busy + Continue
1673 // Fix certain browser bugs that prevent the state from changing
1674 this.doubleCheck(function(){
1687 * Send the browser history forward one item
1688 * @param {Integer} queue [optional]
1690 forward : function(queue){
1691 //this.debug('this.forward: called', arguments);
1694 if ( queue !== false && this.busy() ) {
1695 // Wait + Push to Queue
1696 //this.debug('this.forward: we must wait', arguments);
1699 callback: this.forward,
1706 // Make Busy + Continue
1710 // Fix certain browser bugs that prevent the state from changing
1711 this.doubleCheck(function(){
1718 // End forward closure
1724 * Send the browser history back or forward index times
1725 * @param {Integer} queue [optional]
1727 go : function(index,queue){
1728 //this.debug('this.go: called', arguments);
1736 for ( i=1; i<=index; ++i ) {
1737 this.forward(queue);
1740 else if ( index < 0 ) {
1742 for ( i=-1; i>=index; --i ) {
1747 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1755 // ====================================================================
1756 // HTML5 State Support
1760 * Use native HTML5 History API Implementation
1764 * onPopState(event,extra)
1765 * Refresh the Current State
1767 onPopState : function(event,extra){
1769 var stateId = false, newState = false, currentHash, currentState;
1771 // Reset the double check
1772 this.doubleCheckComplete();
1774 // Check for a Hash, and handle apporiatly
1775 currentHash = this.getHash();
1776 if ( currentHash ) {
1778 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1779 if ( currentState ) {
1780 // We were able to parse it, it must be a State!
1781 // Let's forward to replaceState
1782 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1783 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1786 // Traditional Anchor
1787 //this.debug('this.onPopState: traditional anchor', currentHash);
1788 Roo.get(window).fireEvent('anchorchange');
1792 // We don't care for hashes
1793 this.expectedStateId = false;
1796 stateId = (event && event.browserEvent && event.browserEvent['state']) || (extra && extra['state']) || undefined;
1799 //stateId = this.Adapter.extractEventData('state',event,extra) || false;
1803 // Vanilla: Back/forward button was used
1804 newState = this.getStateById(stateId);
1806 else if ( this.expectedStateId ) {
1807 // Vanilla: A new state was pushed, and popstate was called manually
1808 newState = this.getStateById(this.expectedStateId);
1812 newState = this.extractState(this.getLocationHref());
1815 // The State did not exist in our store
1817 // Regenerate the State
1818 newState = this.createStateObject(null,null,this.getLocationHref());
1822 this.expectedStateId = false;
1824 // Check if we are the same state
1825 if ( this.isLastSavedState(newState) ) {
1826 // There has been no change (just the page's hash has finally propagated)
1827 //this.debug('this.onPopState: no change', newState, this.savedStates);
1833 this.storeState(newState);
1834 this.saveState(newState);
1836 // Force update of the title
1837 this.setTitle(newState);
1840 Roo.get(window).fireEvent('statechange');
1854 * pushState(data,title,url)
1855 * Add a new State to the history object, become it, and trigger onpopstate
1856 * We have to trigger for HTML4 compatibility
1857 * @param {object} data
1858 * @param {string} title
1859 * @param {string} url
1862 pushState : function(data,title,url,queue){
1863 //this.debug('this.pushState: called', arguments);
1866 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1867 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1871 if ( queue !== false && this.busy() ) {
1872 // Wait + Push to Queue
1873 //this.debug('this.pushState: we must wait', arguments);
1876 callback: this.pushState,
1883 // Make Busy + Continue
1886 // Create the newState
1887 var newState = this.createStateObject(data,title,url);
1890 if ( this.isLastSavedState(newState) ) {
1891 // Won't be a change
1895 // Store the newState
1896 this.storeState(newState);
1897 this.expectedStateId = newState.id;
1899 // Push the newState
1900 history.pushState(newState.id,newState.title,newState.url);
1903 Roo.get(window).fireEvent('popstate');
1906 // End pushState closure
1911 * replaceState(data,title,url)
1912 * Replace the State and trigger onpopstate
1913 * We have to trigger for HTML4 compatibility
1914 * @param {object} data
1915 * @param {string} title
1916 * @param {string} url
1919 replaceState : function(data,title,url,queue){
1920 //this.debug('this.replaceState: called', arguments);
1923 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1924 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1928 if ( queue !== false && this.busy() ) {
1929 // Wait + Push to Queue
1930 //this.debug('this.replaceState: we must wait', arguments);
1933 callback: this.replaceState,
1940 // Make Busy + Continue
1943 // Create the newState
1944 var newState = this.createStateObject(data,title,url);
1947 if ( this.isLastSavedState(newState) ) {
1948 // Won't be a change
1952 // Store the newState
1953 this.storeState(newState);
1954 this.expectedStateId = newState.id;
1956 // Push the newState
1957 history.replaceState(newState.id,newState.title,newState.url);
1960 Roo.get(window).fireEvent('popstate');
1963 // End replaceState closure
1968 // ====================================================================
1972 * Bind for Saving Store
1975 // When the page is closed
1976 onUnload : function(){
1978 var currentStore, item, currentStoreString;
1982 currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1989 currentStore.idToState = currentStore.idToState || {};
1990 currentStore.urlToId = currentStore.urlToId || {};
1991 currentStore.stateToId = currentStore.stateToId || {};
1994 for ( item in this.idToState ) {
1995 if ( !this.idToState.hasOwnProperty(item) ) {
1998 currentStore.idToState[item] = this.idToState[item];
2000 for ( item in this.urlToId ) {
2001 if ( !this.urlToId.hasOwnProperty(item) ) {
2004 currentStore.urlToId[item] = this.urlToId[item];
2006 for ( item in this.stateToId ) {
2007 if ( !this.stateToId.hasOwnProperty(item) ) {
2010 currentStore.stateToId[item] = this.stateToId[item];
2014 this.store = currentStore;
2015 this.normalizeStore();
2017 // In Safari, going into Private Browsing mode causes the
2018 // Session Storage object to still exist but if you try and use
2019 // or set any property/function of it it throws the exception
2020 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
2021 // add something to storage that exceeded the quota." infinitely
2023 currentStoreString = JSON.stringify(currentStore);
2026 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2029 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
2030 if (this.sessionStorage.length) {
2031 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
2032 // removing/resetting the storage can work.
2033 this.sessionStorage.removeItem('Roo.History.store');
2034 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2036 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.