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)
212 // Check Load Status of Core
213 if ( typeof this.initCore !== 'undefined' ) {
217 // Check Load Status of HTML4 Support
218 if ( typeof this.initHtml4 !== 'undefined' ) {
225 this.enabled = !this.emulated.pushState;
227 if ( this.emulated.pushState ) {
231 this.pushState = emptyFunction;
232 this.replaceState = emptyFunction;
240 Roo.get(window).on('popstate',this.onPopState, this);
246 if ( this.sessionStorage ) {
249 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
254 this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
256 // For Other Browsers
257 Roo.get(window).on('beforeunload',this.onUnload,this);
258 Roo.get(window).on('unload',this.onUnload, this);
261 this.onUnload = emptyFunction;
265 this.normalizeStore();
267 * Clear Intervals on exit to prevent memory leaks
269 Roo.get(window).on('unload',this.clearAllIntervals, this);
272 * Create the initial State
274 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
278 // Non-Native pushState Implementation
279 if ( !this.emulated.pushState ) {
280 // Be aware, the following is only for native pushState implementations
281 // If you are wanting to include something for all browsers
282 // Then include it above this if block
287 if ( this.bugs.safariPoll ) {
288 this.intervalList.push(setInterval(this.safariStatePoll, this.safariPollInterval));
292 * Ensure Cross Browser Compatibility
294 //if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
299 * Fix Safari HashChange Issue
303 Roo.get(window).on('hashchange',function(){
304 Roo.get(window).fireEvent('popstate');
308 if ( this.getHash() ) {
309 Roo.onReady(function(){
310 Roo.get(window).fireEvent('hashchange');
315 } // !History.emulated.pushState
325 // ========================================================================
329 initCore : function(options){
331 this.intervalList = [];
335 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
336 this.sessionStorage.setItem('TEST', '1');
337 this.sessionStorage.removeItem('TEST');
339 this.sessionStorage = false;
343 if ( typeof this.initCore.initialized !== 'undefined' ) {
348 this.initCore.initialized = true;
357 * Clears all setInterval instances.
359 clearAllIntervals: function()
361 var i, il = this.intervalList;
362 if (typeof il !== "undefined" && il !== null) {
363 for (i = 0; i < il.length; i++) {
364 clearInterval(il[i]);
366 this.intervalList = null;
371 // ====================================================================
375 * debugLog(message,...)
376 * Logs the passed arguments if debug enabled
378 debugLog : function()
380 if ( (this.debug||false) ) {
381 Roo.log.apply(this,arguments);
387 // ====================================================================
391 * getInternetExplorerMajorVersion()
392 * Get's the major version of Internet Explorer
394 * @license Public Domain
395 * @author Benjamin Arthur Lupton <contact@balupton.com>
396 * @author James Padolsey <https://gist.github.com/527683>
398 getInternetExplorerMajorVersion : function(){
399 var result = this.getInternetExplorerMajorVersion.cached =
400 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
401 ? this.getInternetExplorerMajorVersion.cached
404 div = window.document.createElement('div'),
405 all = div.getElementsByTagName('i');
406 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
407 return (v > 4) ? v : false;
414 * isInternetExplorer()
415 * Are we using Internet Explorer?
417 * @license Public Domain
418 * @author Benjamin Arthur Lupton <contact@balupton.com>
420 isInternetExplorer : function(){
422 this.isInternetExplorer.cached =
423 (typeof this.isInternetExplorer.cached !== 'undefined')
424 ? this.isInternetExplorer.cached
425 : Boolean(this.getInternetExplorerMajorVersion())
431 * Which features require emulating?
439 initEmulated : function()
443 if (this.html4Mode) {
449 this.emulated.pushState = !Boolean(
450 window.history && window.history.pushState && window.history.replaceState
452 (/ 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) */
453 || (/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 */
456 this.emulated.hashChange = Boolean(
457 !(('onhashchange' in window) || ('onhashchange' in window.document))
459 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
464 initBugs : function ()
470 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
471 * https://bugs.webkit.org/show_bug.cgi?id=56249
473 this.bugs.setHash = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
474 && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
477 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
478 * https://bugs.webkit.org/show_bug.cgi?id=42940
480 this.bugs.safariPoll = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
481 && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
484 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
486 this.bugs.ieDoubleCheck = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8);
489 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
491 this.bugs.hashEscape = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 7);
497 * Checks to see if the Object is Empty
498 * @param {Object} obj
501 isEmptyObject : function(obj) {
502 for ( var name in obj ) {
503 if ( obj.hasOwnProperty(name) ) {
512 * Clones a object and eliminate all references to the original contexts
513 * @param {Object} obj
516 cloneObject : function(obj) {
519 hash = JSON.stringify(obj);
520 newObj = JSON.parse(hash);
529 // ====================================================================
534 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
535 * @return {String} rootUrl
537 getRootUrl : function(){
539 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
540 if ( window.document.location.port||false ) {
541 rootUrl += ':'+window.document.location.port;
551 * Fetches the `href` attribute of the `<base href="...">` element if it exists
552 * @return {String} baseHref
554 getBaseHref : function(){
557 baseElements = window.document.getElementsByTagName('base'),
561 // Test for Base Element
562 if ( baseElements.length === 1 ) {
563 // Prepare for Base Element
564 baseElement = baseElements[0];
565 baseHref = baseElement.href.replace(/[^\/]+$/,'');
568 // Adjust trailing slash
569 baseHref = baseHref.replace(/\/+$/,'');
570 if ( baseHref ) baseHref += '/';
578 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
579 * @return {String} baseUrl
581 getBaseUrl : function(){
583 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
591 * Fetches the URL of the current page
592 * @return {String} pageUrl
594 getPageUrl : function(){
597 State = this.getState(false,false),
598 stateUrl = (State||{}).url||this.getLocationHref(),
602 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
603 return (/\./).test(part) ? part : part+'/';
612 * Fetches the Url of the directory of the current page
613 * @return {String} basePageUrl
615 getBasePageUrl : function(){
617 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
618 return (/[^\/]$/).test(part) ? '' : part;
619 }).replace(/\/+$/,'')+'/';
627 * Ensures that we have an absolute URL and not a relative URL
628 * @param {string} url
629 * @param {Boolean} allowBaseHref
630 * @return {string} fullUrl
632 getFullUrl : function(url,allowBaseHref){
634 var fullUrl = url, firstChar = url.substring(0,1);
635 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
638 if ( /[a-z]+\:\/\//.test(url) ) {
641 else if ( firstChar === '/' ) {
643 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
645 else if ( firstChar === '#' ) {
647 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
649 else if ( firstChar === '?' ) {
651 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
655 if ( allowBaseHref ) {
656 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
658 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
660 // We have an if condition above as we do not want hashes
661 // which are relative to the baseHref in our URLs
662 // as if the baseHref changes, then all our bookmarks
663 // would now point to different locations
664 // whereas the basePageUrl will always stay the same
668 return fullUrl.replace(/\#$/,'');
673 * Ensures that we have a relative URL and not a absolute URL
674 * @param {string} url
675 * @return {string} url
677 getShortUrl : function(url){
679 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
682 if ( this.emulated.pushState ) {
683 // We are in a if statement as when pushState is not emulated
684 // The actual url these short urls are relative to can change
685 // So within the same session, we the url may end up somewhere different
686 shortUrl = shortUrl.replace(baseUrl,'');
690 shortUrl = shortUrl.replace(rootUrl,'/');
692 // Ensure we can still detect it as a state
693 if ( this.isTraditionalAnchor(shortUrl) ) {
694 shortUrl = './'+shortUrl;
698 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
705 * getLocationHref(document)
706 * Returns a normalized version of document.location.href
707 * accounting for browser inconsistencies, etc.
709 * This URL will be URI-encoded and will include the hash
711 * @param {object} document
712 * @return {string} url
714 getLocationHref : function(doc) {
715 doc = doc || window.document;
717 // most of the time, this will be true
718 if (doc.URL === doc.location.href)
719 return doc.location.href;
721 // some versions of webkit URI-decode document.location.href
722 // but they leave document.URL in an encoded state
723 if (doc.location.href === decodeURIComponent(doc.URL))
726 // FF 3.6 only updates document.URL when a page is reloaded
727 // document.location.href is updated correctly
728 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
729 return doc.location.href;
731 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
732 return doc.location.href;
734 return doc.URL || doc.location.href;
741 * Noramlize the store by adding necessary values
743 normalizeStore : function()
746 this.store.idToState = this.store.idToState||{};
747 this.store.urlToId = this.store.urlToId||{};
748 this.store.stateToId = this.store.stateToId||{};
753 * Get an object containing the data, title and url of the current state
754 * @param {Boolean} friendly
755 * @param {Boolean} create
756 * @return {Object} State
758 getState : function(friendly,create){
760 if ( typeof friendly === 'undefined' ) { friendly = true; }
761 if ( typeof create === 'undefined' ) { create = true; }
764 var State = this.getLastSavedState();
767 if ( !State && create ) {
768 State = this.createStateObject();
773 State = this.cloneObject(State);
774 State.url = this.cleanUrl||State.url;
782 * getIdByState(State)
783 * Gets a ID for a State
784 * @param {State} newState
785 * @return {String} id
787 getIdByState : function(newState){
790 var id = this.extractId(newState.url),
794 // Find ID via State String
795 str = this.getStateString(newState);
796 if ( typeof this.stateToId[str] !== 'undefined' ) {
797 id = this.stateToId[str];
799 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
800 id = this.store.stateToId[str];
805 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
806 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
811 // Apply the new State to the ID
812 this.stateToId[str] = id;
813 this.idToState[id] = newState;
822 * normalizeState(State)
823 * Expands a State Object
824 * @param {object} State
827 normalizeState : function(oldState){
829 var newState, dataNotEmpty;
832 if ( !oldState || (typeof oldState !== 'object') ) {
837 if ( typeof oldState.normalized !== 'undefined' ) {
842 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
846 // ----------------------------------------------------------------
850 newState.normalized = true;
851 newState.title = oldState.title||'';
852 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
853 newState.hash = this.getShortUrl(newState.url);
854 newState.data = this.cloneObject(oldState.data);
857 newState.id = this.getIdByState(newState);
859 // ----------------------------------------------------------------
862 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
863 newState.url = newState.cleanUrl;
865 // Check to see if we have more than just a url
866 dataNotEmpty = !this.isEmptyObject(newState.data);
869 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
871 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
872 if ( !/\?/.test(newState.hash) ) {
873 newState.hash += '?';
875 newState.hash += '&_suid='+newState.id;
878 // Create the Hashed URL
879 newState.hashedUrl = this.getFullUrl(newState.hash);
881 // ----------------------------------------------------------------
883 // Update the URL if we have a duplicate
884 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
885 newState.url = newState.hashedUrl;
888 // ----------------------------------------------------------------
895 * createStateObject(data,title,url)
896 * Creates a object based on the data, title and url state params
897 * @param {object} data
898 * @param {string} title
899 * @param {string} url
902 createStateObject : function(data,title,url){
911 State = this.normalizeState(State);
919 * Get a state by it's UID
922 getStateById : function(id){
927 var State = this.idToState[id] || this.store.idToState[id] || undefined;
934 * Get a State's String
935 * @param {State} passedState
937 getStateString : function(passedState){
939 var State, cleanedState, str;
942 State = this.normalizeState(passedState);
947 title: passedState.title,
952 str = JSON.stringify(cleanedState);
960 * @param {State} passedState
961 * @return {String} id
963 getStateId : function(passedState){
968 State = this.normalizeState(passedState);
978 * getHashByState(State)
979 * Creates a Hash for the State Object
980 * @param {State} passedState
981 * @return {String} hash
983 getHashByState : function(passedState){
988 State = this.normalizeState(passedState);
998 * extractId(url_or_hash)
999 * Get a State ID by it's URL or Hash
1000 * @param {string} url_or_hash
1001 * @return {string} id
1003 extractId : function ( url_or_hash ) {
1005 var id,parts,url, tmp;
1009 // If the URL has a #, use the id from before the #
1010 if (url_or_hash.indexOf('#') != -1)
1012 tmp = url_or_hash.split("#")[0];
1019 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
1020 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
1021 id = parts ? String(parts[2]||'') : '';
1028 * isTraditionalAnchor
1029 * Checks to see if the url is a traditional anchor or not
1030 * @param {String} url_or_hash
1033 isTraditionalAnchor : function(url_or_hash){
1035 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
1038 return isTraditional;
1043 * Get a State by it's URL or Hash
1044 * @param {String} url_or_hash
1045 * @return {State|null}
1047 extractState : function(url_or_hash,create){
1049 var State = null, id, url;
1050 create = create||false;
1053 id = this.extractId(url_or_hash);
1055 State = this.getStateById(id);
1058 // Fetch SUID returned no State
1061 url = this.getFullUrl(url_or_hash);
1064 id = this.getIdByUrl(url)||false;
1066 State = this.getStateById(id);
1070 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
1071 State = this.createStateObject(null,null,url);
1081 * Get a State ID by a State URL
1083 getIdByUrl : function(url){
1085 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1092 * getLastSavedState()
1093 * Get an object containing the data, title and url of the current state
1094 * @return {Object} State
1096 getLastSavedState : function(){
1097 return this.savedStates[this.savedStates.length-1]||undefined;
1101 * getLastStoredState()
1102 * Get an object containing the data, title and url of the current state
1103 * @return {Object} State
1105 getLastStoredState : function(){
1106 return this.storedStates[this.storedStates.length-1]||undefined;
1111 * Checks if a Url will have a url conflict
1112 * @param {Object} newState
1113 * @return {Boolean} hasDuplicate
1115 hasUrlDuplicate : function(newState) {
1117 var hasDuplicate = false,
1121 oldState = this.extractState(newState.url);
1124 hasDuplicate = oldState && oldState.id !== newState.id;
1127 return hasDuplicate;
1133 * @param {Object} newState
1134 * @return {Object} newState
1136 storeState : function(newState){
1138 this.urlToId[newState.url] = newState.id;
1141 this.storedStates.push(this.cloneObject(newState));
1148 * isLastSavedState(newState)
1149 * Tests to see if the state is the last state
1150 * @param {Object} newState
1151 * @return {boolean} isLast
1153 isLastSavedState : function(newState){
1156 newId, oldState, oldId;
1159 if ( this.savedStates.length ) {
1160 newId = newState.id;
1161 oldState = this.getLastSavedState();
1162 oldId = oldState.id;
1165 isLast = (newId === oldId);
1175 * @param {Object} newState
1176 * @return {boolean} changed
1178 saveState : function(newState){
1180 if ( this.isLastSavedState(newState) ) {
1185 this.savedStates.push(this.cloneObject(newState));
1193 * Gets a state by the index
1194 * @param {integer} index
1197 getStateByIndex : function(index){
1202 if ( typeof index === 'undefined' ) {
1203 // Get the last inserted
1204 State = this.savedStates[this.savedStates.length-1];
1206 else if ( index < 0 ) {
1208 State = this.savedStates[this.savedStates.length+index];
1211 // Get from the beginning
1212 State = this.savedStates[index];
1221 * Gets the current index
1224 getCurrentIndex : function(){
1229 if(this.savedStates.length < 1) {
1233 index = this.savedStates.length-1;
1238 // ====================================================================
1243 * @param {Location=} location
1244 * Gets the current document hash
1245 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1248 getHash : function(doc){
1249 var url = this.getLocationHref(doc),
1251 hash = this.getHashByUrl(url);
1257 * normalize and Unescape a Hash
1258 * @param {String} hash
1261 unescapeHash : function(hash){
1263 var result = this.normalizeHash(hash);
1266 result = decodeURIComponent(result);
1274 * normalize a hash across browsers
1277 normalizeHash : function(hash){
1279 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1287 * Sets the document hash
1288 * @param {string} hash
1289 * @return {Roo.History}
1291 setHash : function(hash,queue){
1296 if ( queue !== false && this.busy() ) {
1297 // Wait + Push to Queue
1298 //this.debug('this.setHash: we must wait', arguments);
1301 callback: this.setHash,
1309 //this.debug('this.setHash: called',hash);
1311 // Make Busy + Continue
1314 // Check if hash is a state
1315 State = this.extractState(hash,true);
1316 if ( State && !this.emulated.pushState ) {
1317 // Hash is a state so skip the setHash
1318 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1321 this.pushState(State.data,State.title,State.url,false);
1323 else if ( this.getHash() !== hash ) {
1324 // Hash is a proper hash, so apply it
1326 // Handle browser bugs
1327 if ( this.bugs.setHash ) {
1328 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1330 // Fetch the base page
1331 pageUrl = this.getPageUrl();
1333 // Safari hash apply
1334 this.pushState(null,null,pageUrl+'#'+hash,false);
1337 // Normal hash apply
1338 window.document.location.hash = hash;
1348 * normalize and Escape a Hash
1351 escapeHash : function(hash){
1353 var result = normalizeHash(hash);
1356 result = window.encodeURIComponent(result);
1359 if ( !this.bugs.hashEscape ) {
1360 // Restore common parts
1362 .replace(/\%21/g,'!')
1363 .replace(/\%26/g,'&')
1364 .replace(/\%3D/g,'=')
1365 .replace(/\%3F/g,'?');
1374 * Extracts the Hash from a URL
1375 * @param {string} url
1376 * @return {string} url
1378 getHashByUrl : function(url){
1380 var hash = String(url)
1381 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1385 hash = this.unescapeHash(hash);
1393 * Applies the title to the document
1394 * @param {State} newState
1397 setTitle : function(newState){
1399 var title = newState.title,
1404 firstState = this.getStateByIndex(0);
1405 if ( firstState && firstState.url === newState.url ) {
1406 title = firstState.title||this.initialTitle;
1412 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1414 catch ( Exception ) { }
1415 window.document.title = title;
1422 // ====================================================================
1428 * @param {boolean} value [optional]
1429 * @return {boolean} busy
1431 busy : function(value){
1435 if ( typeof value !== 'undefined' ) {
1436 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1437 this.busy_flag = value;
1440 else if ( typeof this.busy_flag === 'undefined' ) {
1441 this.busy_flag = false;
1445 if ( !this.busy_flag ) {
1449 // Execute the next item in the queue
1450 window.clearTimeout(this.busy.timeout);
1451 var fireNext = function(){
1453 if ( _this.busy_flag ) return;
1454 for ( i=_this.queues.length-1; i >= 0; --i ) {
1455 queue = _this.queues[i];
1456 if ( queue.length === 0 ) continue;
1457 item = queue.shift();
1458 _this.fireQueueItem(item);
1459 _this.busy.timeout = window.setTimeout(fireNext,_this.busyDelay);
1462 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1466 return this.busy_flag;
1472 * fireQueueItem(item)
1474 * @param {Object} item
1475 * @return {Mixed} result
1477 fireQueueItem : function(item){
1478 return item.callback.apply(item.scope||this,item.args||[]);
1482 * pushQueue(callback,args)
1483 * Add an item to the queue
1484 * @param {Object} item [scope,callback,args,queue]
1486 pushQueue : function(item){
1487 // Prepare the queue
1488 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1491 this.queues[item.queue||0].push(item);
1498 * queue (item,queue), (func,queue), (func), (item)
1499 * Either firs the item now if not busy, or adds it to the queue
1501 queue : function(item,queue){
1503 if ( typeof item === 'function' ) {
1508 if ( typeof queue !== 'undefined' ) {
1513 if ( this.busy() ) {
1514 this.pushQueue(item);
1516 this.fireQueueItem(item);
1527 clearQueue : function(){
1528 this.busy_flag = false;
1536 * doubleCheckComplete()
1537 * Complete a double check
1538 * @return {Roo.History}
1540 doubleCheckComplete : function(){
1542 this.stateChanged = true;
1545 this.doubleCheckClear();
1552 * doubleCheckClear()
1553 * Clear a double check
1554 * @return {Roo.History}
1556 doubleCheckClear : function(){
1558 if ( this.doubleChecker ) {
1559 window.clearTimeout(this.doubleChecker);
1560 this.doubleChecker = false;
1569 * Create a double check
1570 * @return {Roo.History}
1572 doubleCheck : function(tryAgain)
1576 this.stateChanged = false;
1577 this.doubleCheckClear();
1579 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1580 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1581 if ( this.bugs.ieDoubleCheck ) {
1583 this.doubleChecker = window.setTimeout(
1585 _this.doubleCheckClear();
1586 if ( !_this.stateChanged ) {
1587 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1593 this.doubleCheckInterval
1602 // ====================================================================
1607 * Poll the current state
1608 * @return {Roo.History}
1610 safariStatePoll : function(){
1613 // Get the Last State which has the new URL
1615 urlState = this.extractState(this.getLocationHref()),
1618 // Check for a difference
1619 if ( !this.isLastSavedState(urlState) ) {
1620 newState = urlState;
1626 // Check if we have a state with that url
1629 //this.debug('this.safariStatePoll: new');
1630 newState = this.createStateObject();
1633 // Apply the New State
1634 //this.debug('this.safariStatePoll: trigger');
1635 Roo.get(window).fireEvent('popstate');
1642 // ====================================================================
1647 * Send the browser history back one item
1648 * @param {Integer} queue [optional]
1650 back : function(queue)
1652 //this.debug('this.back: called', arguments);
1655 if ( queue !== false && this.busy() ) {
1656 // Wait + Push to Queue
1657 //this.debug('this.back: we must wait', arguments);
1660 callback: this.back,
1667 // Make Busy + Continue
1670 // Fix certain browser bugs that prevent the state from changing
1671 this.doubleCheck(function(){
1684 * Send the browser history forward one item
1685 * @param {Integer} queue [optional]
1687 forward : function(queue){
1688 //this.debug('this.forward: called', arguments);
1691 if ( queue !== false && this.busy() ) {
1692 // Wait + Push to Queue
1693 //this.debug('this.forward: we must wait', arguments);
1696 callback: this.forward,
1703 // Make Busy + Continue
1707 // Fix certain browser bugs that prevent the state from changing
1708 this.doubleCheck(function(){
1715 // End forward closure
1721 * Send the browser history back or forward index times
1722 * @param {Integer} queue [optional]
1724 go : function(index,queue){
1725 //this.debug('this.go: called', arguments);
1733 for ( i=1; i<=index; ++i ) {
1734 this.forward(queue);
1737 else if ( index < 0 ) {
1739 for ( i=-1; i>=index; --i ) {
1744 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1752 // ====================================================================
1753 // HTML5 State Support
1757 * Use native HTML5 History API Implementation
1761 * onPopState(event,extra)
1762 * Refresh the Current State
1764 onPopState : function(event,extra){
1766 var stateId = false, newState = false, currentHash, currentState;
1768 // Reset the double check
1769 this.doubleCheckComplete();
1771 // Check for a Hash, and handle apporiatly
1772 currentHash = this.getHash();
1773 if ( currentHash ) {
1775 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1776 if ( currentState ) {
1777 // We were able to parse it, it must be a State!
1778 // Let's forward to replaceState
1779 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1780 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1783 // Traditional Anchor
1784 //this.debug('this.onPopState: traditional anchor', currentHash);
1785 Roo.get(window).fireEvent('anchorchange');
1789 // We don't care for hashes
1790 this.expectedStateId = false;
1793 stateId = (event && event.browserEvent && event.browserEvent['state']) || (extra && extra['state']) || undefined;
1796 //stateId = this.Adapter.extractEventData('state',event,extra) || false;
1800 // Vanilla: Back/forward button was used
1801 newState = this.getStateById(stateId);
1803 else if ( this.expectedStateId ) {
1804 // Vanilla: A new state was pushed, and popstate was called manually
1805 newState = this.getStateById(this.expectedStateId);
1809 newState = this.extractState(this.getLocationHref());
1812 // The State did not exist in our store
1814 // Regenerate the State
1815 newState = this.createStateObject(null,null,this.getLocationHref());
1819 this.expectedStateId = false;
1821 // Check if we are the same state
1822 if ( this.isLastSavedState(newState) ) {
1823 // There has been no change (just the page's hash has finally propagated)
1824 //this.debug('this.onPopState: no change', newState, this.savedStates);
1830 this.storeState(newState);
1831 this.saveState(newState);
1833 // Force update of the title
1834 this.setTitle(newState);
1837 Roo.get(window).fireEvent('statechange');
1851 * pushState(data,title,url)
1852 * Add a new State to the history object, become it, and trigger onpopstate
1853 * We have to trigger for HTML4 compatibility
1854 * @param {object} data
1855 * @param {string} title
1856 * @param {string} url
1859 pushState : function(data,title,url,queue){
1860 //this.debug('this.pushState: called', arguments);
1863 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1864 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1868 if ( queue !== false && this.busy() ) {
1869 // Wait + Push to Queue
1870 //this.debug('this.pushState: we must wait', arguments);
1873 callback: this.pushState,
1880 // Make Busy + Continue
1883 // Create the newState
1884 var newState = this.createStateObject(data,title,url);
1887 if ( this.isLastSavedState(newState) ) {
1888 // Won't be a change
1892 // Store the newState
1893 this.storeState(newState);
1894 this.expectedStateId = newState.id;
1896 // Push the newState
1897 history.pushState(newState.id,newState.title,newState.url);
1900 Roo.get(window).fireEvent('popstate');
1903 // End pushState closure
1908 * replaceState(data,title,url)
1909 * Replace the State and trigger onpopstate
1910 * We have to trigger for HTML4 compatibility
1911 * @param {object} data
1912 * @param {string} title
1913 * @param {string} url
1916 replaceState : function(data,title,url,queue){
1917 //this.debug('this.replaceState: called', arguments);
1920 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1921 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1925 if ( queue !== false && this.busy() ) {
1926 // Wait + Push to Queue
1927 //this.debug('this.replaceState: we must wait', arguments);
1930 callback: this.replaceState,
1937 // Make Busy + Continue
1940 // Create the newState
1941 var newState = this.createStateObject(data,title,url);
1944 if ( this.isLastSavedState(newState) ) {
1945 // Won't be a change
1949 // Store the newState
1950 this.storeState(newState);
1951 this.expectedStateId = newState.id;
1953 // Push the newState
1954 history.replaceState(newState.id,newState.title,newState.url);
1957 Roo.get(window).fireEvent('popstate');
1960 // End replaceState closure
1965 // ====================================================================
1969 * Bind for Saving Store
1972 // When the page is closed
1973 onUnload : function(){
1975 var currentStore, item, currentStoreString;
1979 currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1986 currentStore.idToState = currentStore.idToState || {};
1987 currentStore.urlToId = currentStore.urlToId || {};
1988 currentStore.stateToId = currentStore.stateToId || {};
1991 for ( item in this.idToState ) {
1992 if ( !this.idToState.hasOwnProperty(item) ) {
1995 currentStore.idToState[item] = this.idToState[item];
1997 for ( item in this.urlToId ) {
1998 if ( !this.urlToId.hasOwnProperty(item) ) {
2001 currentStore.urlToId[item] = this.urlToId[item];
2003 for ( item in this.stateToId ) {
2004 if ( !this.stateToId.hasOwnProperty(item) ) {
2007 currentStore.stateToId[item] = this.stateToId[item];
2011 this.store = currentStore;
2012 this.normalizeStore();
2014 // In Safari, going into Private Browsing mode causes the
2015 // Session Storage object to still exist but if you try and use
2016 // or set any property/function of it it throws the exception
2017 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
2018 // add something to storage that exceeded the quota." infinitely
2020 currentStoreString = JSON.stringify(currentStore);
2023 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2026 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
2027 if (this.sessionStorage.length) {
2028 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
2029 // removing/resetting the storage can work.
2030 this.sessionStorage.removeItem('Roo.History.store');
2031 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2033 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.