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/>
11 // this is not initialized automatically..
12 // must call Roo.History.init( { ... options... });
18 // ====================================================================
24 * How long should the interval be before hashchange checks
26 thishashChangeInterval : 100,
30 * How long should the interval be before safari poll checks
32 safariPollInterval : 500,
36 * How long should the interval be before we perform a double check
38 doubleCheckInterval : 500,
42 * Force this.not to append suid
48 * How long should we wait between store calls
54 * How long should we wait between busy events
60 * If true will enable debug messages to be logged
66 * What is the title of the initial state
72 * If true, will force HTMl4 mode (hashtags)
78 * Want to override default options and call init manually.
84 * Which bugs are present
88 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
89 * https://bugs.webkit.org/show_bug.cgi?id=56249
94 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
95 * https://bugs.webkit.org/show_bug.cgi?id=42940
100 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
102 ieDoubleCheck: false,
105 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
110 // ========================================================================
116 sessionStorage : false, // sessionStorage
118 intervalList : false, // array normally.
121 * Is History enabled?
125 // ====================================================================
130 * The store for all session specific data
136 * 1-1: State ID to State Object
142 * 1-1: State String to State ID
148 * 1-1: State URL to State ID
154 * Store the states in an array
156 storedStates : false,
160 * Saved the states in an array
166 * The list of queues to use
167 * First In, First Out
176 // ====================================================================
180 * History.stateChanged
181 * States whether or not the state has changed since the last double check was initialised
183 stateChanged : false,
186 * History.doubleChecker
187 * Contains the timeout used for the double checks
189 doubleChecker : false,
192 // Initialise History
193 init : function(options)
196 var emptyFunction = function(){};
200 this.initialTitle = window.document.title;
205 this.storedStates=[];
209 Roo.apply(this,options)
211 // Check Load Status of Adapter
212 //if ( typeof this.Adapter === 'undefined' ) {
216 // Check Load Status of Core
217 if ( typeof this.initCore !== 'undefined' ) {
221 // Check Load Status of HTML4 Support
222 if ( typeof this.initHtml4 !== 'undefined' ) {
228 this.enabled = !this.emulated.pushState;
230 if ( this.emulated.pushState ) {
234 this.pushState = emptyFunction;
235 this.replaceState = emptyFunction;
238 this.Adapter.bind(window,'popstate',this.onPopState);
244 if ( this.sessionStorage ) {
247 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
252 this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
254 // For Other Browsers
255 this.Adapter.bind(window,'beforeunload',this.onUnload);
256 this.Adapter.bind(window,'unload',this.onUnload);
259 this.onUnload = emptyFunction;
263 this.normalizeStore();
265 * Clear Intervals on exit to prevent memory leaks
267 this.Adapter.bind(window,"unload",this.clearAllIntervals);
270 * Create the initial State
272 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
276 // Non-Native pushState Implementation
277 if ( !this.emulated.pushState ) {
278 // Be aware, the following is only for native pushState implementations
279 // If you are wanting to include something for all browsers
280 // Then include it above this if block
285 if ( this.bugs.safariPoll ) {
286 this.intervalList.push(setInterval(this.safariStatePoll, this.safariPollInterval));
290 * Ensure Cross Browser Compatibility
292 //if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
297 * Fix Safari HashChange Issue
301 this.Adapter.bind(window,'hashchange',function(){
302 _this.Adapter.trigger(window,'popstate');
306 if ( this.getHash() ) {
307 this.Adapter.onDomLoad(function(){
308 _this.Adapter.trigger(window,'hashchange');
313 } // !History.emulated.pushState
323 // ========================================================================
327 initCore : function(options){
329 this.intervalList = [];
333 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
334 this.sessionStorage.setItem('TEST', '1');
335 this.sessionStorage.removeItem('TEST');
337 this.sessionStorage = false;
341 if ( typeof this.initCore.initialized !== 'undefined' ) {
346 this.initCore.initialized = true;
355 * Clears all setInterval instances.
357 clearAllIntervals: function()
359 var i, il = this.intervalList;
360 if (typeof il !== "undefined" && il !== null) {
361 for (i = 0; i < il.length; i++) {
362 clearInterval(il[i]);
364 this.intervalList = null;
369 // ====================================================================
373 * debugLog(message,...)
374 * Logs the passed arguments if debug enabled
376 debugLog : function()
378 if ( (this.debug||false) ) {
379 Roo.log.apply(this,arguments);
385 // ====================================================================
389 * getInternetExplorerMajorVersion()
390 * Get's the major version of Internet Explorer
392 * @license Public Domain
393 * @author Benjamin Arthur Lupton <contact@balupton.com>
394 * @author James Padolsey <https://gist.github.com/527683>
396 getInternetExplorerMajorVersion : function(){
397 var result = this.getInternetExplorerMajorVersion.cached =
398 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
399 ? this.getInternetExplorerMajorVersion.cached
402 div = window.document.createElement('div'),
403 all = div.getElementsByTagName('i');
404 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
405 return (v > 4) ? v : false;
412 * isInternetExplorer()
413 * Are we using Internet Explorer?
415 * @license Public Domain
416 * @author Benjamin Arthur Lupton <contact@balupton.com>
418 isInternetExplorer : function(){
420 this.isInternetExplorer.cached =
421 (typeof this.isInternetExplorer.cached !== 'undefined')
422 ? this.isInternetExplorer.cached
423 : Boolean(this.getInternetExplorerMajorVersion())
429 * Which features require emulating?
437 initEmulated : function()
441 if (this.html4Mode) {
447 this.emulated.pushState = !Boolean(
448 window.history && window.history.pushState && window.history.replaceState
450 (/ 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) */
451 || (/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 */
454 this.emulated.hashChange = Boolean(
455 !(('onhashchange' in window) || ('onhashchange' in window.document))
457 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
462 initBugs : function ()
468 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
469 * https://bugs.webkit.org/show_bug.cgi?id=56249
471 this.bugs.setHash: Boolean(!History.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent)),
474 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
475 * https://bugs.webkit.org/show_bug.cgi?id=42940
477 this.bugs.safariPoll: Boolean(!History.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.' && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent)),
480 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
482 this.bugs.ieDoubleCheck: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 8),
485 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
487 this.bugs.hashEscape: Boolean(History.isInternetExplorer() && History.getInternetExplorerMajorVersion() < 7)
494 * Checks to see if the Object is Empty
495 * @param {Object} obj
498 isEmptyObject = function(obj) {
499 for ( var name in obj ) {
500 if ( obj.hasOwnProperty(name) ) {
509 * Clones a object and eliminate all references to the original contexts
510 * @param {Object} obj
513 cloneObject = function(obj) {
516 hash = JSON.stringify(obj);
517 newObj = JSON.parse(hash);
526 // ====================================================================
531 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
532 * @return {String} rootUrl
534 getRootUrl = function(){
536 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
537 if ( window.document.location.port||false ) {
538 rootUrl += ':'+window.document.location.port;
548 * Fetches the `href` attribute of the `<base href="...">` element if it exists
549 * @return {String} baseHref
551 getBaseHref = function(){
554 baseElements = window.document.getElementsByTagName('base'),
558 // Test for Base Element
559 if ( baseElements.length === 1 ) {
560 // Prepare for Base Element
561 baseElement = baseElements[0];
562 baseHref = baseElement.href.replace(/[^\/]+$/,'');
565 // Adjust trailing slash
566 baseHref = baseHref.replace(/\/+$/,'');
567 if ( baseHref ) baseHref += '/';
575 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
576 * @return {String} baseUrl
578 getBaseUrl = function(){
580 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
588 * Fetches the URL of the current page
589 * @return {String} pageUrl
591 getPageUrl = function(){
594 State = this.getState(false,false),
595 stateUrl = (State||{}).url||this.getLocationHref(),
599 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
600 return (/\./).test(part) ? part : part+'/';
609 * Fetches the Url of the directory of the current page
610 * @return {String} basePageUrl
612 getBasePageUrl = function(){
614 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
615 return (/[^\/]$/).test(part) ? '' : part;
616 }).replace(/\/+$/,'')+'/';
624 * Ensures that we have an absolute URL and not a relative URL
625 * @param {string} url
626 * @param {Boolean} allowBaseHref
627 * @return {string} fullUrl
629 getFullUrl = function(url,allowBaseHref){
631 var fullUrl = url, firstChar = url.substring(0,1);
632 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
635 if ( /[a-z]+\:\/\//.test(url) ) {
638 else if ( firstChar === '/' ) {
640 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
642 else if ( firstChar === '#' ) {
644 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
646 else if ( firstChar === '?' ) {
648 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
652 if ( allowBaseHref ) {
653 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
655 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
657 // We have an if condition above as we do not want hashes
658 // which are relative to the baseHref in our URLs
659 // as if the baseHref changes, then all our bookmarks
660 // would now point to different locations
661 // whereas the basePageUrl will always stay the same
665 return fullUrl.replace(/\#$/,'');
670 * Ensures that we have a relative URL and not a absolute URL
671 * @param {string} url
672 * @return {string} url
674 getShortUrl = function(url){
676 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
679 if ( this.emulated.pushState ) {
680 // We are in a if statement as when pushState is not emulated
681 // The actual url these short urls are relative to can change
682 // So within the same session, we the url may end up somewhere different
683 shortUrl = shortUrl.replace(baseUrl,'');
687 shortUrl = shortUrl.replace(rootUrl,'/');
689 // Ensure we can still detect it as a state
690 if ( this.isTraditionalAnchor(shortUrl) ) {
691 shortUrl = './'+shortUrl;
695 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
702 * getLocationHref(document)
703 * Returns a normalized version of document.location.href
704 * accounting for browser inconsistencies, etc.
706 * This URL will be URI-encoded and will include the hash
708 * @param {object} document
709 * @return {string} url
711 getLocationHref = function(doc) {
712 doc = doc || window.document;
714 // most of the time, this will be true
715 if (doc.URL === doc.location.href)
716 return doc.location.href;
718 // some versions of webkit URI-decode document.location.href
719 // but they leave document.URL in an encoded state
720 if (doc.location.href === decodeURIComponent(doc.URL))
723 // FF 3.6 only updates document.URL when a page is reloaded
724 // document.location.href is updated correctly
725 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
726 return doc.location.href;
728 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
729 return doc.location.href;
731 return doc.URL || doc.location.href;
738 * Noramlize the store by adding necessary values
740 normalizeStore = function(){
741 this.store.idToState = this.store.idToState||{};
742 this.store.urlToId = this.store.urlToId||{};
743 this.store.stateToId = this.store.stateToId||{};
748 * Get an object containing the data, title and url of the current state
749 * @param {Boolean} friendly
750 * @param {Boolean} create
751 * @return {Object} State
753 getState : function(friendly,create){
755 if ( typeof friendly === 'undefined' ) { friendly = true; }
756 if ( typeof create === 'undefined' ) { create = true; }
759 var State = this.getLastSavedState();
762 if ( !State && create ) {
763 State = this.createStateObject();
768 State = this.cloneObject(State);
769 State.url = this.cleanUrl||State.url;
777 * getIdByState(State)
778 * Gets a ID for a State
779 * @param {State} newState
780 * @return {String} id
782 getIdByState = function(newState){
785 var id = this.extractId(newState.url),
789 // Find ID via State String
790 str = this.getStateString(newState);
791 if ( typeof this.stateToId[str] !== 'undefined' ) {
792 id = this.stateToId[str];
794 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
795 id = this.store.stateToId[str];
800 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
801 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
806 // Apply the new State to the ID
807 this.stateToId[str] = id;
808 this.idToState[id] = newState;
817 * normalizeState(State)
818 * Expands a State Object
819 * @param {object} State
822 normalizeState = function(oldState){
824 var newState, dataNotEmpty;
827 if ( !oldState || (typeof oldState !== 'object') ) {
832 if ( typeof oldState.normalized !== 'undefined' ) {
837 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
841 // ----------------------------------------------------------------
845 newState.normalized = true;
846 newState.title = oldState.title||'';
847 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
848 newState.hash = this.getShortUrl(newState.url);
849 newState.data = this.cloneObject(oldState.data);
852 newState.id = this.getIdByState(newState);
854 // ----------------------------------------------------------------
857 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
858 newState.url = newState.cleanUrl;
860 // Check to see if we have more than just a url
861 dataNotEmpty = !this.isEmptyObject(newState.data);
864 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
866 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
867 if ( !/\?/.test(newState.hash) ) {
868 newState.hash += '?';
870 newState.hash += '&_suid='+newState.id;
873 // Create the Hashed URL
874 newState.hashedUrl = this.getFullUrl(newState.hash);
876 // ----------------------------------------------------------------
878 // Update the URL if we have a duplicate
879 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
880 newState.url = newState.hashedUrl;
883 // ----------------------------------------------------------------
890 * createStateObject(data,title,url)
891 * Creates a object based on the data, title and url state params
892 * @param {object} data
893 * @param {string} title
894 * @param {string} url
897 createStateObject = function(data,title,url){
906 State = this.normalizeState(State);
914 * Get a state by it's UID
917 getStateById = function(id){
922 var State = this.idToState[id] || this.store.idToState[id] || undefined;
929 * Get a State's String
930 * @param {State} passedState
932 getStateString = function(passedState){
934 var State, cleanedState, str;
937 State = this.normalizeState(passedState);
942 title: passedState.title,
947 str = JSON.stringify(cleanedState);
955 * @param {State} passedState
956 * @return {String} id
958 getStateId = function(passedState){
963 State = this.normalizeState(passedState);
973 * getHashByState(State)
974 * Creates a Hash for the State Object
975 * @param {State} passedState
976 * @return {String} hash
978 getHashByState = function(passedState){
983 State = this.normalizeState(passedState);
993 * extractId(url_or_hash)
994 * Get a State ID by it's URL or Hash
995 * @param {string} url_or_hash
996 * @return {string} id
998 this.extractId = function ( url_or_hash ) {
1000 var id,parts,url, tmp;
1004 // If the URL has a #, use the id from before the #
1005 if (url_or_hash.indexOf('#') != -1)
1007 tmp = url_or_hash.split("#")[0];
1014 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
1015 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
1016 id = parts ? String(parts[2]||'') : '';
1023 * isTraditionalAnchor
1024 * Checks to see if the url is a traditional anchor or not
1025 * @param {String} url_or_hash
1028 isTraditionalAnchor = function(url_or_hash){
1030 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
1033 return isTraditional;
1038 * Get a State by it's URL or Hash
1039 * @param {String} url_or_hash
1040 * @return {State|null}
1042 extractState = function(url_or_hash,create){
1044 var State = null, id, url;
1045 create = create||false;
1048 id = this.extractId(url_or_hash);
1050 State = this.getStateById(id);
1053 // Fetch SUID returned no State
1056 url = this.getFullUrl(url_or_hash);
1059 id = this.getIdByUrl(url)||false;
1061 State = this.getStateById(id);
1065 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
1066 State = this.createStateObject(null,null,url);
1076 * Get a State ID by a State URL
1078 getIdByUrl = function(url){
1080 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1087 * getLastSavedState()
1088 * Get an object containing the data, title and url of the current state
1089 * @return {Object} State
1091 getLastSavedState = function(){
1092 return this.savedStates[this.savedStates.length-1]||undefined;
1096 * getLastStoredState()
1097 * Get an object containing the data, title and url of the current state
1098 * @return {Object} State
1100 getLastStoredState = function(){
1101 return this.storedStates[this.storedStates.length-1]||undefined;
1106 * Checks if a Url will have a url conflict
1107 * @param {Object} newState
1108 * @return {Boolean} hasDuplicate
1110 hasUrlDuplicate = function(newState) {
1112 var hasDuplicate = false,
1116 oldState = this.extractState(newState.url);
1119 hasDuplicate = oldState && oldState.id !== newState.id;
1122 return hasDuplicate;
1128 * @param {Object} newState
1129 * @return {Object} newState
1131 storeState = function(newState){
1133 this.urlToId[newState.url] = newState.id;
1136 this.storedStates.push(this.cloneObject(newState));
1143 * isLastSavedState(newState)
1144 * Tests to see if the state is the last state
1145 * @param {Object} newState
1146 * @return {boolean} isLast
1148 isLastSavedState = function(newState){
1151 newId, oldState, oldId;
1154 if ( this.savedStates.length ) {
1155 newId = newState.id;
1156 oldState = this.getLastSavedState();
1157 oldId = oldState.id;
1160 isLast = (newId === oldId);
1170 * @param {Object} newState
1171 * @return {boolean} changed
1173 saveState = function(newState){
1175 if ( this.isLastSavedState(newState) ) {
1180 this.savedStates.push(this.cloneObject(newState));
1188 * Gets a state by the index
1189 * @param {integer} index
1192 getStateByIndex = function(index){
1197 if ( typeof index === 'undefined' ) {
1198 // Get the last inserted
1199 State = this.savedStates[this.savedStates.length-1];
1201 else if ( index < 0 ) {
1203 State = this.savedStates[this.savedStates.length+index];
1206 // Get from the beginning
1207 State = this.savedStates[index];
1216 * Gets the current index
1219 getCurrentIndex = function(){
1224 if(this.savedStates.length < 1) {
1228 index = this.savedStates.length-1;
1233 // ====================================================================
1238 * @param {Location=} location
1239 * Gets the current document hash
1240 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1243 getHash = function(doc){
1244 var url = this.getLocationHref(doc),
1246 hash = this.getHashByUrl(url);
1252 * normalize and Unescape a Hash
1253 * @param {String} hash
1256 unescapeHash = function(hash){
1258 var result = this.normalizeHash(hash);
1261 result = decodeURIComponent(result);
1269 * normalize a hash across browsers
1272 normalizeHash = function(hash){
1274 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1282 * Sets the document hash
1283 * @param {string} hash
1284 * @return {Roo.History}
1286 setHash = function(hash,queue){
1291 if ( queue !== false && this.busy() ) {
1292 // Wait + Push to Queue
1293 //this.debug('this.setHash: we must wait', arguments);
1296 callback: this.setHash,
1304 //this.debug('this.setHash: called',hash);
1306 // Make Busy + Continue
1309 // Check if hash is a state
1310 State = this.extractState(hash,true);
1311 if ( State && !this.emulated.pushState ) {
1312 // Hash is a state so skip the setHash
1313 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1316 this.pushState(State.data,State.title,State.url,false);
1318 else if ( this.getHash() !== hash ) {
1319 // Hash is a proper hash, so apply it
1321 // Handle browser bugs
1322 if ( this.bugs.setHash ) {
1323 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1325 // Fetch the base page
1326 pageUrl = this.getPageUrl();
1328 // Safari hash apply
1329 this.pushState(null,null,pageUrl+'#'+hash,false);
1332 // Normal hash apply
1333 window.document.location.hash = hash;
1343 * normalize and Escape a Hash
1346 escapeHash = function(hash){
1348 var result = normalizeHash(hash);
1351 result = window.encodeURIComponent(result);
1354 if ( !this.bugs.hashEscape ) {
1355 // Restore common parts
1357 .replace(/\%21/g,'!')
1358 .replace(/\%26/g,'&')
1359 .replace(/\%3D/g,'=')
1360 .replace(/\%3F/g,'?');
1369 * Extracts the Hash from a URL
1370 * @param {string} url
1371 * @return {string} url
1373 getHashByUrl = function(url){
1375 var hash = String(url)
1376 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1380 hash = this.unescapeHash(hash);
1388 * Applies the title to the document
1389 * @param {State} newState
1392 setTitle = function(newState){
1394 var title = newState.title,
1399 firstState = this.getStateByIndex(0);
1400 if ( firstState && firstState.url === newState.url ) {
1401 title = firstState.title||this.initialTitle;
1407 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1409 catch ( Exception ) { }
1410 window.document.title = title;
1417 // ====================================================================
1423 * @param {boolean} value [optional]
1424 * @return {boolean} busy
1426 busy = function(value){
1428 if ( typeof value !== 'undefined' ) {
1429 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1430 this.busy_flag = value;
1433 else if ( typeof this.busy_flag === 'undefined' ) {
1434 this.busy_flag = false;
1438 if ( !this.busy_flag ) {
1439 // Execute the next item in the queue
1440 window.clearTimeout(this.busy.timeout);
1441 var fireNext = function(){
1443 if ( this.busy_flag ) return;
1444 for ( i=this.queues.length-1; i >= 0; --i ) {
1445 queue = this.queues[i];
1446 if ( queue.length === 0 ) continue;
1447 item = queue.shift();
1448 this.fireQueueItem(item);
1449 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1452 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1456 return this.busy_flag;
1462 * fireQueueItem(item)
1464 * @param {Object} item
1465 * @return {Mixed} result
1467 fireQueueItem = function(item){
1468 return item.callback.apply(item.scope||this,item.args||[]);
1472 * pushQueue(callback,args)
1473 * Add an item to the queue
1474 * @param {Object} item [scope,callback,args,queue]
1476 pushQueue = function(item){
1477 // Prepare the queue
1478 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1481 this.queues[item.queue||0].push(item);
1488 * queue (item,queue), (func,queue), (func), (item)
1489 * Either firs the item now if not busy, or adds it to the queue
1491 queue = function(item,queue){
1493 if ( typeof item === 'function' ) {
1498 if ( typeof queue !== 'undefined' ) {
1503 if ( this.busy() ) {
1504 this.pushQueue(item);
1506 this.fireQueueItem(item);
1517 clearQueue = function(){
1518 this.busy_flag = false;
1526 * doubleCheckComplete()
1527 * Complete a double check
1528 * @return {Roo.History}
1530 doubleCheckComplete = function(){
1532 this.stateChanged = true;
1535 this.doubleCheckClear();
1542 * doubleCheckClear()
1543 * Clear a double check
1544 * @return {Roo.History}
1546 doubleCheckClear = function(){
1548 if ( this.doubleChecker ) {
1549 window.clearTimeout(this.doubleChecker);
1550 this.doubleChecker = false;
1559 * Create a double check
1560 * @return {Roo.History}
1562 doubleCheck = function(tryAgain){
1564 this.stateChanged = false;
1565 this.doubleCheckClear();
1567 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1568 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1569 if ( this.bugs.ieDoubleCheck ) {
1571 this.doubleChecker = window.setTimeout(
1573 this.doubleCheckClear();
1574 if ( !this.stateChanged ) {
1575 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1581 this.doubleCheckInterval
1590 // ====================================================================
1595 * Poll the current state
1596 * @return {Roo.History}
1598 safariStatePoll = function(){
1601 // Get the Last State which has the new URL
1603 urlState = this.extractState(this.getLocationHref()),
1606 // Check for a difference
1607 if ( !this.isLastSavedState(urlState) ) {
1608 newState = urlState;
1614 // Check if we have a state with that url
1617 //this.debug('this.safariStatePoll: new');
1618 newState = this.createStateObject();
1621 // Apply the New State
1622 //this.debug('this.safariStatePoll: trigger');
1623 this.Adapter.trigger(window,'popstate');
1630 // ====================================================================
1635 * Send the browser history back one item
1636 * @param {Integer} queue [optional]
1638 back = function(queue){
1639 //this.debug('this.back: called', arguments);
1642 if ( queue !== false && this.busy() ) {
1643 // Wait + Push to Queue
1644 //this.debug('this.back: we must wait', arguments);
1647 callback: this.back,
1654 // Make Busy + Continue
1657 // Fix certain browser bugs that prevent the state from changing
1658 this.doubleCheck(function(){
1671 * Send the browser history forward one item
1672 * @param {Integer} queue [optional]
1674 forward = function(queue){
1675 //this.debug('this.forward: called', arguments);
1678 if ( queue !== false && this.busy() ) {
1679 // Wait + Push to Queue
1680 //this.debug('this.forward: we must wait', arguments);
1683 callback: this.forward,
1690 // Make Busy + Continue
1694 // Fix certain browser bugs that prevent the state from changing
1695 this.doubleCheck(function(){
1702 // End forward closure
1708 * Send the browser history back or forward index times
1709 * @param {Integer} queue [optional]
1711 go = function(index,queue){
1712 //this.debug('this.go: called', arguments);
1720 for ( i=1; i<=index; ++i ) {
1721 this.forward(queue);
1724 else if ( index < 0 ) {
1726 for ( i=-1; i>=index; --i ) {
1731 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1739 // ====================================================================
1740 // HTML5 State Support
1744 * Use native HTML5 History API Implementation
1748 * onPopState(event,extra)
1749 * Refresh the Current State
1751 onPopState = function(event,extra){
1753 var stateId = false, newState = false, currentHash, currentState;
1755 // Reset the double check
1756 this.doubleCheckComplete();
1758 // Check for a Hash, and handle apporiatly
1759 currentHash = this.getHash();
1760 if ( currentHash ) {
1762 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1763 if ( currentState ) {
1764 // We were able to parse it, it must be a State!
1765 // Let's forward to replaceState
1766 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1767 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1770 // Traditional Anchor
1771 //this.debug('this.onPopState: traditional anchor', currentHash);
1772 this.Adapter.trigger(window,'anchorchange');
1776 // We don't care for hashes
1777 this.expectedStateId = false;
1782 stateId = this.Adapter.extractEventData('state',event,extra) || false;
1786 // Vanilla: Back/forward button was used
1787 newState = this.getStateById(stateId);
1789 else if ( this.expectedStateId ) {
1790 // Vanilla: A new state was pushed, and popstate was called manually
1791 newState = this.getStateById(this.expectedStateId);
1795 newState = this.extractState(this.getLocationHref());
1798 // The State did not exist in our store
1800 // Regenerate the State
1801 newState = this.createStateObject(null,null,this.getLocationHref());
1805 this.expectedStateId = false;
1807 // Check if we are the same state
1808 if ( this.isLastSavedState(newState) ) {
1809 // There has been no change (just the page's hash has finally propagated)
1810 //this.debug('this.onPopState: no change', newState, this.savedStates);
1816 this.storeState(newState);
1817 this.saveState(newState);
1819 // Force update of the title
1820 this.setTitle(newState);
1823 this.Adapter.trigger(window,'statechange');
1837 * pushState(data,title,url)
1838 * Add a new State to the history object, become it, and trigger onpopstate
1839 * We have to trigger for HTML4 compatibility
1840 * @param {object} data
1841 * @param {string} title
1842 * @param {string} url
1845 pushState = function(data,title,url,queue){
1846 //this.debug('this.pushState: called', arguments);
1849 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1850 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1854 if ( queue !== false && this.busy() ) {
1855 // Wait + Push to Queue
1856 //this.debug('this.pushState: we must wait', arguments);
1859 callback: this.pushState,
1866 // Make Busy + Continue
1869 // Create the newState
1870 var newState = this.createStateObject(data,title,url);
1873 if ( this.isLastSavedState(newState) ) {
1874 // Won't be a change
1878 // Store the newState
1879 this.storeState(newState);
1880 this.expectedStateId = newState.id;
1882 // Push the newState
1883 history.pushState(newState.id,newState.title,newState.url);
1886 this.Adapter.trigger(window,'popstate');
1889 // End pushState closure
1894 * replaceState(data,title,url)
1895 * Replace the State and trigger onpopstate
1896 * We have to trigger for HTML4 compatibility
1897 * @param {object} data
1898 * @param {string} title
1899 * @param {string} url
1902 replaceState = function(data,title,url,queue){
1903 //this.debug('this.replaceState: called', arguments);
1906 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1907 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1911 if ( queue !== false && this.busy() ) {
1912 // Wait + Push to Queue
1913 //this.debug('this.replaceState: we must wait', arguments);
1916 callback: this.replaceState,
1923 // Make Busy + Continue
1926 // Create the newState
1927 var newState = this.createStateObject(data,title,url);
1930 if ( this.isLastSavedState(newState) ) {
1931 // Won't be a change
1935 // Store the newState
1936 this.storeState(newState);
1937 this.expectedStateId = newState.id;
1939 // Push the newState
1940 history.replaceState(newState.id,newState.title,newState.url);
1943 this.Adapter.trigger(window,'popstate');
1946 // End replaceState closure
1951 // ====================================================================
1955 * Bind for Saving Store
1958 // When the page is closed
1959 onUnload = function(){
1961 var currentStore, item, currentStoreString;
1965 currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1972 currentStore.idToState = currentStore.idToState || {};
1973 currentStore.urlToId = currentStore.urlToId || {};
1974 currentStore.stateToId = currentStore.stateToId || {};
1977 for ( item in this.idToState ) {
1978 if ( !this.idToState.hasOwnProperty(item) ) {
1981 currentStore.idToState[item] = this.idToState[item];
1983 for ( item in this.urlToId ) {
1984 if ( !this.urlToId.hasOwnProperty(item) ) {
1987 currentStore.urlToId[item] = this.urlToId[item];
1989 for ( item in this.stateToId ) {
1990 if ( !this.stateToId.hasOwnProperty(item) ) {
1993 currentStore.stateToId[item] = this.stateToId[item];
1997 this.store = currentStore;
1998 this.normalizeStore();
2000 // In Safari, going into Private Browsing mode causes the
2001 // Session Storage object to still exist but if you try and use
2002 // or set any property/function of it it throws the exception
2003 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
2004 // add something to storage that exceeded the quota." infinitely
2006 currentStoreString = JSON.stringify(currentStore);
2009 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2012 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
2013 if (this.sessionStorage.length) {
2014 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
2015 // removing/resetting the storage can work.
2016 this.sessionStorage.removeItem('Roo.History.store');
2017 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2019 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.