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... });
14 // TOTALLY UNTESTED...
19 // ====================================================================
25 * How long should the interval be before hashchange checks
27 thishashChangeInterval : 100,
31 * How long should the interval be before safari poll checks
33 safariPollInterval : 500,
37 * How long should the interval be before we perform a double check
39 doubleCheckInterval : 500,
43 * Force this.not to append suid
49 * How long should we wait between store calls
55 * How long should we wait between busy events
61 * If true will enable debug messages to be logged
67 * What is the title of the initial state
73 * If true, will force HTMl4 mode (hashtags)
79 * Want to override default options and call init manually.
85 * Which bugs are present
89 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
90 * https://bugs.webkit.org/show_bug.cgi?id=56249
95 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
96 * https://bugs.webkit.org/show_bug.cgi?id=42940
101 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
103 ieDoubleCheck: false,
106 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
111 // ========================================================================
117 sessionStorage : false, // sessionStorage
119 intervalList : false, // array normally.
122 * Is History enabled?
126 // ====================================================================
131 * The store for all session specific data
137 * 1-1: State ID to State Object
143 * 1-1: State String to State ID
149 * 1-1: State URL to State ID
155 * Store the states in an array
157 storedStates : false,
161 * Saved the states in an array
167 * The list of queues to use
168 * First In, First Out
177 // ====================================================================
181 * History.stateChanged
182 * States whether or not the state has changed since the last double check was initialised
184 stateChanged : false,
187 * History.doubleChecker
188 * Contains the timeout used for the double checks
190 doubleChecker : false,
193 // Initialise History
194 init : function(options)
197 var emptyFunction = function(){};
201 this.initialTitle = window.document.title;
206 this.storedStates=[];
210 Roo.apply(this,options)
213 // Check Load Status of Core
216 // Check Load Status of HTML4 Support
217 //if ( typeof this.initHtml4 !== 'undefined' ) {
224 this.enabled = !this.emulated.pushState;
226 if ( this.emulated.pushState ) {
230 this.pushState = emptyFunction;
231 this.replaceState = emptyFunction;
239 Roo.get(window).on('popstate',this.onPopState, this);
245 if ( this.sessionStorage ) {
248 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
253 this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
255 // For Other Browsers
256 Roo.get(window).on('beforeunload',this.onUnload,this);
257 Roo.get(window).on('unload',this.onUnload, this);
260 this.onUnload = emptyFunction;
264 this.normalizeStore();
266 * Clear Intervals on exit to prevent memory leaks
268 Roo.get(window).on('unload',this.clearAllIntervals, this);
271 * Create the initial State
273 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
277 // Non-Native pushState Implementation
278 if ( !this.emulated.pushState ) {
279 // Be aware, the following is only for native pushState implementations
280 // If you are wanting to include something for all browsers
281 // Then include it above this if block
286 if ( this.bugs.safariPoll ) {
287 this.intervalList.push(setInterval(this.safariStatePoll, this.safariPollInterval));
291 * Ensure Cross Browser Compatibility
293 //if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
298 * Fix Safari HashChange Issue
302 Roo.get(window).on('hashchange',function(){
303 Roo.get(window).fireEvent('popstate');
307 if ( this.getHash() ) {
308 Roo.onReady(function(){
309 Roo.get(window).fireEvent('hashchange');
314 } // !History.emulated.pushState
324 // ========================================================================
328 initCore : function(options){
330 this.intervalList = [];
334 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
335 this.sessionStorage.setItem('TEST', '1');
336 this.sessionStorage.removeItem('TEST');
338 this.sessionStorage = false;
342 if ( typeof this.initCore.initialized !== 'undefined' ) {
347 this.initCore.initialized = true;
356 * Clears all setInterval instances.
358 clearAllIntervals: function()
360 var i, il = this.intervalList;
361 if (typeof il !== "undefined" && il !== null) {
362 for (i = 0; i < il.length; i++) {
363 clearInterval(il[i]);
365 this.intervalList = null;
370 // ====================================================================
374 * debugLog(message,...)
375 * Logs the passed arguments if debug enabled
377 debugLog : function()
379 if ( (this.debug||false) ) {
380 Roo.log.apply(this,arguments);
386 // ====================================================================
390 * getInternetExplorerMajorVersion()
391 * Get's the major version of Internet Explorer
393 * @license Public Domain
394 * @author Benjamin Arthur Lupton <contact@balupton.com>
395 * @author James Padolsey <https://gist.github.com/527683>
397 getInternetExplorerMajorVersion : function(){
398 var result = this.getInternetExplorerMajorVersion.cached =
399 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
400 ? this.getInternetExplorerMajorVersion.cached
403 div = window.document.createElement('div'),
404 all = div.getElementsByTagName('i');
405 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
406 return (v > 4) ? v : false;
413 * isInternetExplorer()
414 * Are we using Internet Explorer?
416 * @license Public Domain
417 * @author Benjamin Arthur Lupton <contact@balupton.com>
419 isInternetExplorer : function(){
421 this.isInternetExplorer.cached =
422 (typeof this.isInternetExplorer.cached !== 'undefined')
423 ? this.isInternetExplorer.cached
424 : Boolean(this.getInternetExplorerMajorVersion())
430 * Which features require emulating?
438 initEmulated : function()
442 if (this.html4Mode) {
448 this.emulated.pushState = !Boolean(
449 window.history && window.history.pushState && window.history.replaceState
451 (/ 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) */
452 || (/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 */
455 this.emulated.hashChange = Boolean(
456 !(('onhashchange' in window) || ('onhashchange' in window.document))
458 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
463 initBugs : function ()
469 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
470 * https://bugs.webkit.org/show_bug.cgi?id=56249
472 this.bugs.setHash = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
473 && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
476 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
477 * https://bugs.webkit.org/show_bug.cgi?id=42940
479 this.bugs.safariPoll = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
480 && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
483 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
485 this.bugs.ieDoubleCheck = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8);
488 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
490 this.bugs.hashEscape = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 7);
496 * Checks to see if the Object is Empty
497 * @param {Object} obj
500 isEmptyObject : function(obj) {
501 for ( var name in obj ) {
502 if ( obj.hasOwnProperty(name) ) {
511 * Clones a object and eliminate all references to the original contexts
512 * @param {Object} obj
515 cloneObject : function(obj) {
518 hash = JSON.stringify(obj);
519 newObj = JSON.parse(hash);
528 // ====================================================================
533 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
534 * @return {String} rootUrl
536 getRootUrl : function(){
538 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
539 if ( window.document.location.port||false ) {
540 rootUrl += ':'+window.document.location.port;
550 * Fetches the `href` attribute of the `<base href="...">` element if it exists
551 * @return {String} baseHref
553 getBaseHref : function(){
556 baseElements = window.document.getElementsByTagName('base'),
560 // Test for Base Element
561 if ( baseElements.length === 1 ) {
562 // Prepare for Base Element
563 baseElement = baseElements[0];
564 baseHref = baseElement.href.replace(/[^\/]+$/,'');
567 // Adjust trailing slash
568 baseHref = baseHref.replace(/\/+$/,'');
569 if ( baseHref ) baseHref += '/';
577 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
578 * @return {String} baseUrl
580 getBaseUrl : function(){
582 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
590 * Fetches the URL of the current page
591 * @return {String} pageUrl
593 getPageUrl : function(){
596 State = this.getState(false,false),
597 stateUrl = (State||{}).url||this.getLocationHref(),
601 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
602 return (/\./).test(part) ? part : part+'/';
611 * Fetches the Url of the directory of the current page
612 * @return {String} basePageUrl
614 getBasePageUrl : function(){
616 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
617 return (/[^\/]$/).test(part) ? '' : part;
618 }).replace(/\/+$/,'')+'/';
626 * Ensures that we have an absolute URL and not a relative URL
627 * @param {string} url
628 * @param {Boolean} allowBaseHref
629 * @return {string} fullUrl
631 getFullUrl : function(url,allowBaseHref){
633 var fullUrl = url, firstChar = url.substring(0,1);
634 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
637 if ( /[a-z]+\:\/\//.test(url) ) {
640 else if ( firstChar === '/' ) {
642 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
644 else if ( firstChar === '#' ) {
646 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
648 else if ( firstChar === '?' ) {
650 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
654 if ( allowBaseHref ) {
655 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
657 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
659 // We have an if condition above as we do not want hashes
660 // which are relative to the baseHref in our URLs
661 // as if the baseHref changes, then all our bookmarks
662 // would now point to different locations
663 // whereas the basePageUrl will always stay the same
667 return fullUrl.replace(/\#$/,'');
672 * Ensures that we have a relative URL and not a absolute URL
673 * @param {string} url
674 * @return {string} url
676 getShortUrl : function(url){
678 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
681 if ( this.emulated.pushState ) {
682 // We are in a if statement as when pushState is not emulated
683 // The actual url these short urls are relative to can change
684 // So within the same session, we the url may end up somewhere different
685 shortUrl = shortUrl.replace(baseUrl,'');
689 shortUrl = shortUrl.replace(rootUrl,'/');
691 // Ensure we can still detect it as a state
692 if ( this.isTraditionalAnchor(shortUrl) ) {
693 shortUrl = './'+shortUrl;
697 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
704 * getLocationHref(document)
705 * Returns a normalized version of document.location.href
706 * accounting for browser inconsistencies, etc.
708 * This URL will be URI-encoded and will include the hash
710 * @param {object} document
711 * @return {string} url
713 getLocationHref : function(doc) {
714 doc = doc || window.document;
716 // most of the time, this will be true
717 if (doc.URL === doc.location.href)
718 return doc.location.href;
720 // some versions of webkit URI-decode document.location.href
721 // but they leave document.URL in an encoded state
722 if (doc.location.href === decodeURIComponent(doc.URL))
725 // FF 3.6 only updates document.URL when a page is reloaded
726 // document.location.href is updated correctly
727 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
728 return doc.location.href;
730 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
731 return doc.location.href;
733 return doc.URL || doc.location.href;
740 * Noramlize the store by adding necessary values
742 normalizeStore : function()
745 this.store.idToState = this.store.idToState||{};
746 this.store.urlToId = this.store.urlToId||{};
747 this.store.stateToId = this.store.stateToId||{};
752 * Get an object containing the data, title and url of the current state
753 * @param {Boolean} friendly
754 * @param {Boolean} create
755 * @return {Object} State
757 getState : function(friendly,create){
759 if ( typeof friendly === 'undefined' ) { friendly = true; }
760 if ( typeof create === 'undefined' ) { create = true; }
763 var State = this.getLastSavedState();
766 if ( !State && create ) {
767 State = this.createStateObject();
772 State = this.cloneObject(State);
773 State.url = this.cleanUrl||State.url;
781 * getIdByState(State)
782 * Gets a ID for a State
783 * @param {State} newState
784 * @return {String} id
786 getIdByState : function(newState){
789 var id = this.extractId(newState.url),
793 // Find ID via State String
794 str = this.getStateString(newState);
795 if ( typeof this.stateToId[str] !== 'undefined' ) {
796 id = this.stateToId[str];
798 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
799 id = this.store.stateToId[str];
804 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
805 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
810 // Apply the new State to the ID
811 this.stateToId[str] = id;
812 this.idToState[id] = newState;
821 * normalizeState(State)
822 * Expands a State Object
823 * @param {object} State
826 normalizeState : function(oldState){
828 var newState, dataNotEmpty;
831 if ( !oldState || (typeof oldState !== 'object') ) {
836 if ( typeof oldState.normalized !== 'undefined' ) {
841 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
845 // ----------------------------------------------------------------
849 newState.normalized = true;
850 newState.title = oldState.title||'';
851 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
852 newState.hash = this.getShortUrl(newState.url);
853 newState.data = this.cloneObject(oldState.data);
856 newState.id = this.getIdByState(newState);
858 // ----------------------------------------------------------------
861 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
862 newState.url = newState.cleanUrl;
864 // Check to see if we have more than just a url
865 dataNotEmpty = !this.isEmptyObject(newState.data);
868 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
870 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
871 if ( !/\?/.test(newState.hash) ) {
872 newState.hash += '?';
874 newState.hash += '&_suid='+newState.id;
877 // Create the Hashed URL
878 newState.hashedUrl = this.getFullUrl(newState.hash);
880 // ----------------------------------------------------------------
882 // Update the URL if we have a duplicate
883 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
884 newState.url = newState.hashedUrl;
887 // ----------------------------------------------------------------
894 * createStateObject(data,title,url)
895 * Creates a object based on the data, title and url state params
896 * @param {object} data
897 * @param {string} title
898 * @param {string} url
901 createStateObject : function(data,title,url){
910 State = this.normalizeState(State);
918 * Get a state by it's UID
921 getStateById : function(id){
926 var State = this.idToState[id] || this.store.idToState[id] || undefined;
933 * Get a State's String
934 * @param {State} passedState
936 getStateString : function(passedState){
938 var State, cleanedState, str;
941 State = this.normalizeState(passedState);
946 title: passedState.title,
951 str = JSON.stringify(cleanedState);
959 * @param {State} passedState
960 * @return {String} id
962 getStateId : function(passedState){
967 State = this.normalizeState(passedState);
977 * getHashByState(State)
978 * Creates a Hash for the State Object
979 * @param {State} passedState
980 * @return {String} hash
982 getHashByState : function(passedState){
987 State = this.normalizeState(passedState);
997 * extractId(url_or_hash)
998 * Get a State ID by it's URL or Hash
999 * @param {string} url_or_hash
1000 * @return {string} id
1002 extractId : function ( url_or_hash ) {
1004 var id,parts,url, tmp;
1008 // If the URL has a #, use the id from before the #
1009 if (url_or_hash.indexOf('#') != -1)
1011 tmp = url_or_hash.split("#")[0];
1018 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
1019 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
1020 id = parts ? String(parts[2]||'') : '';
1027 * isTraditionalAnchor
1028 * Checks to see if the url is a traditional anchor or not
1029 * @param {String} url_or_hash
1032 isTraditionalAnchor : function(url_or_hash){
1034 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
1037 return isTraditional;
1042 * Get a State by it's URL or Hash
1043 * @param {String} url_or_hash
1044 * @return {State|null}
1046 extractState : function(url_or_hash,create){
1048 var State = null, id, url;
1049 create = create||false;
1052 id = this.extractId(url_or_hash);
1054 State = this.getStateById(id);
1057 // Fetch SUID returned no State
1060 url = this.getFullUrl(url_or_hash);
1063 id = this.getIdByUrl(url)||false;
1065 State = this.getStateById(id);
1069 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
1070 State = this.createStateObject(null,null,url);
1080 * Get a State ID by a State URL
1082 getIdByUrl : function(url){
1084 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1091 * getLastSavedState()
1092 * Get an object containing the data, title and url of the current state
1093 * @return {Object} State
1095 getLastSavedState : function(){
1096 return this.savedStates[this.savedStates.length-1]||undefined;
1100 * getLastStoredState()
1101 * Get an object containing the data, title and url of the current state
1102 * @return {Object} State
1104 getLastStoredState : function(){
1105 return this.storedStates[this.storedStates.length-1]||undefined;
1110 * Checks if a Url will have a url conflict
1111 * @param {Object} newState
1112 * @return {Boolean} hasDuplicate
1114 hasUrlDuplicate : function(newState) {
1116 var hasDuplicate = false,
1120 oldState = this.extractState(newState.url);
1123 hasDuplicate = oldState && oldState.id !== newState.id;
1126 return hasDuplicate;
1132 * @param {Object} newState
1133 * @return {Object} newState
1135 storeState : function(newState){
1137 this.urlToId[newState.url] = newState.id;
1140 this.storedStates.push(this.cloneObject(newState));
1147 * isLastSavedState(newState)
1148 * Tests to see if the state is the last state
1149 * @param {Object} newState
1150 * @return {boolean} isLast
1152 isLastSavedState : function(newState){
1155 newId, oldState, oldId;
1158 if ( this.savedStates.length ) {
1159 newId = newState.id;
1160 oldState = this.getLastSavedState();
1161 oldId = oldState.id;
1164 isLast = (newId === oldId);
1174 * @param {Object} newState
1175 * @return {boolean} changed
1177 saveState : function(newState){
1179 if ( this.isLastSavedState(newState) ) {
1184 this.savedStates.push(this.cloneObject(newState));
1192 * Gets a state by the index
1193 * @param {integer} index
1196 getStateByIndex : function(index){
1201 if ( typeof index === 'undefined' ) {
1202 // Get the last inserted
1203 State = this.savedStates[this.savedStates.length-1];
1205 else if ( index < 0 ) {
1207 State = this.savedStates[this.savedStates.length+index];
1210 // Get from the beginning
1211 State = this.savedStates[index];
1220 * Gets the current index
1223 getCurrentIndex : function(){
1228 if(this.savedStates.length < 1) {
1232 index = this.savedStates.length-1;
1237 // ====================================================================
1242 * @param {Location=} location
1243 * Gets the current document hash
1244 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1247 getHash : function(doc){
1248 var url = this.getLocationHref(doc),
1250 hash = this.getHashByUrl(url);
1256 * normalize and Unescape a Hash
1257 * @param {String} hash
1260 unescapeHash : function(hash){
1262 var result = this.normalizeHash(hash);
1265 result = decodeURIComponent(result);
1273 * normalize a hash across browsers
1276 normalizeHash : function(hash){
1278 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1286 * Sets the document hash
1287 * @param {string} hash
1288 * @return {Roo.History}
1290 setHash : function(hash,queue){
1295 if ( queue !== false && this.busy() ) {
1296 // Wait + Push to Queue
1297 //this.debug('this.setHash: we must wait', arguments);
1300 callback: this.setHash,
1308 //this.debug('this.setHash: called',hash);
1310 // Make Busy + Continue
1313 // Check if hash is a state
1314 State = this.extractState(hash,true);
1315 if ( State && !this.emulated.pushState ) {
1316 // Hash is a state so skip the setHash
1317 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1320 this.pushState(State.data,State.title,State.url,false);
1322 else if ( this.getHash() !== hash ) {
1323 // Hash is a proper hash, so apply it
1325 // Handle browser bugs
1326 if ( this.bugs.setHash ) {
1327 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1329 // Fetch the base page
1330 pageUrl = this.getPageUrl();
1332 // Safari hash apply
1333 this.pushState(null,null,pageUrl+'#'+hash,false);
1336 // Normal hash apply
1337 window.document.location.hash = hash;
1347 * normalize and Escape a Hash
1350 escapeHash : function(hash){
1352 var result = normalizeHash(hash);
1355 result = window.encodeURIComponent(result);
1358 if ( !this.bugs.hashEscape ) {
1359 // Restore common parts
1361 .replace(/\%21/g,'!')
1362 .replace(/\%26/g,'&')
1363 .replace(/\%3D/g,'=')
1364 .replace(/\%3F/g,'?');
1373 * Extracts the Hash from a URL
1374 * @param {string} url
1375 * @return {string} url
1377 getHashByUrl : function(url){
1379 var hash = String(url)
1380 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1384 hash = this.unescapeHash(hash);
1392 * Applies the title to the document
1393 * @param {State} newState
1396 setTitle : function(newState){
1398 var title = newState.title,
1403 firstState = this.getStateByIndex(0);
1404 if ( firstState && firstState.url === newState.url ) {
1405 title = firstState.title||this.initialTitle;
1411 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1413 catch ( Exception ) { }
1414 window.document.title = title;
1421 // ====================================================================
1427 * @param {boolean} value [optional]
1428 * @return {boolean} busy
1430 busy : function(value){
1434 if ( typeof value !== 'undefined' ) {
1435 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1436 this.busy_flag = value;
1439 else if ( typeof this.busy_flag === 'undefined' ) {
1440 this.busy_flag = false;
1444 if ( !this.busy_flag ) {
1448 // Execute the next item in the queue
1449 window.clearTimeout(this.busy.timeout);
1450 var fireNext = function(){
1452 if ( _this.busy_flag ) return;
1453 for ( i=_this.queues.length-1; i >= 0; --i ) {
1454 queue = _this.queues[i];
1455 if ( queue.length === 0 ) continue;
1456 item = queue.shift();
1457 _this.fireQueueItem(item);
1458 _this.busy.timeout = window.setTimeout(fireNext,_this.busyDelay);
1461 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1465 return this.busy_flag;
1471 * fireQueueItem(item)
1473 * @param {Object} item
1474 * @return {Mixed} result
1476 fireQueueItem : function(item){
1477 return item.callback.apply(item.scope||this,item.args||[]);
1481 * pushQueue(callback,args)
1482 * Add an item to the queue
1483 * @param {Object} item [scope,callback,args,queue]
1485 pushQueue : function(item){
1486 // Prepare the queue
1487 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1490 this.queues[item.queue||0].push(item);
1497 * queue (item,queue), (func,queue), (func), (item)
1498 * Either firs the item now if not busy, or adds it to the queue
1500 queue : function(item,queue){
1502 if ( typeof item === 'function' ) {
1507 if ( typeof queue !== 'undefined' ) {
1512 if ( this.busy() ) {
1513 this.pushQueue(item);
1515 this.fireQueueItem(item);
1526 clearQueue : function(){
1527 this.busy_flag = false;
1535 * doubleCheckComplete()
1536 * Complete a double check
1537 * @return {Roo.History}
1539 doubleCheckComplete : function(){
1541 this.stateChanged = true;
1544 this.doubleCheckClear();
1551 * doubleCheckClear()
1552 * Clear a double check
1553 * @return {Roo.History}
1555 doubleCheckClear : function(){
1557 if ( this.doubleChecker ) {
1558 window.clearTimeout(this.doubleChecker);
1559 this.doubleChecker = false;
1568 * Create a double check
1569 * @return {Roo.History}
1571 doubleCheck : function(tryAgain)
1575 this.stateChanged = false;
1576 this.doubleCheckClear();
1578 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1579 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1580 if ( this.bugs.ieDoubleCheck ) {
1582 this.doubleChecker = window.setTimeout(
1584 _this.doubleCheckClear();
1585 if ( !_this.stateChanged ) {
1586 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1592 this.doubleCheckInterval
1601 // ====================================================================
1606 * Poll the current state
1607 * @return {Roo.History}
1609 safariStatePoll : function(){
1612 // Get the Last State which has the new URL
1614 urlState = this.extractState(this.getLocationHref()),
1617 // Check for a difference
1618 if ( !this.isLastSavedState(urlState) ) {
1619 newState = urlState;
1625 // Check if we have a state with that url
1628 //this.debug('this.safariStatePoll: new');
1629 newState = this.createStateObject();
1632 // Apply the New State
1633 //this.debug('this.safariStatePoll: trigger');
1634 Roo.get(window).fireEvent('popstate');
1641 // ====================================================================
1646 * Send the browser history back one item
1647 * @param {Integer} queue [optional]
1649 back : function(queue)
1651 //this.debug('this.back: called', arguments);
1654 if ( queue !== false && this.busy() ) {
1655 // Wait + Push to Queue
1656 //this.debug('this.back: we must wait', arguments);
1659 callback: this.back,
1666 // Make Busy + Continue
1669 // Fix certain browser bugs that prevent the state from changing
1670 this.doubleCheck(function(){
1683 * Send the browser history forward one item
1684 * @param {Integer} queue [optional]
1686 forward : function(queue){
1687 //this.debug('this.forward: called', arguments);
1690 if ( queue !== false && this.busy() ) {
1691 // Wait + Push to Queue
1692 //this.debug('this.forward: we must wait', arguments);
1695 callback: this.forward,
1702 // Make Busy + Continue
1706 // Fix certain browser bugs that prevent the state from changing
1707 this.doubleCheck(function(){
1714 // End forward closure
1720 * Send the browser history back or forward index times
1721 * @param {Integer} queue [optional]
1723 go : function(index,queue){
1724 //this.debug('this.go: called', arguments);
1732 for ( i=1; i<=index; ++i ) {
1733 this.forward(queue);
1736 else if ( index < 0 ) {
1738 for ( i=-1; i>=index; --i ) {
1743 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1751 // ====================================================================
1752 // HTML5 State Support
1756 * Use native HTML5 History API Implementation
1760 * onPopState(event,extra)
1761 * Refresh the Current State
1763 onPopState : function(event,extra){
1765 var stateId = false, newState = false, currentHash, currentState;
1767 // Reset the double check
1768 this.doubleCheckComplete();
1770 // Check for a Hash, and handle apporiatly
1771 currentHash = this.getHash();
1772 if ( currentHash ) {
1774 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1775 if ( currentState ) {
1776 // We were able to parse it, it must be a State!
1777 // Let's forward to replaceState
1778 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1779 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1782 // Traditional Anchor
1783 //this.debug('this.onPopState: traditional anchor', currentHash);
1784 Roo.get(window).fireEvent('anchorchange');
1788 // We don't care for hashes
1789 this.expectedStateId = false;
1792 stateId = (event && event.browserEvent && event.browserEvent['state']) || (extra && extra['state']) || undefined;
1795 //stateId = this.Adapter.extractEventData('state',event,extra) || false;
1799 // Vanilla: Back/forward button was used
1800 newState = this.getStateById(stateId);
1802 else if ( this.expectedStateId ) {
1803 // Vanilla: A new state was pushed, and popstate was called manually
1804 newState = this.getStateById(this.expectedStateId);
1808 newState = this.extractState(this.getLocationHref());
1811 // The State did not exist in our store
1813 // Regenerate the State
1814 newState = this.createStateObject(null,null,this.getLocationHref());
1818 this.expectedStateId = false;
1820 // Check if we are the same state
1821 if ( this.isLastSavedState(newState) ) {
1822 // There has been no change (just the page's hash has finally propagated)
1823 //this.debug('this.onPopState: no change', newState, this.savedStates);
1829 this.storeState(newState);
1830 this.saveState(newState);
1832 // Force update of the title
1833 this.setTitle(newState);
1836 Roo.get(window).fireEvent('statechange');
1850 * pushState(data,title,url)
1851 * Add a new State to the history object, become it, and trigger onpopstate
1852 * We have to trigger for HTML4 compatibility
1853 * @param {object} data
1854 * @param {string} title
1855 * @param {string} url
1858 pushState : function(data,title,url,queue){
1859 //this.debug('this.pushState: called', arguments);
1862 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1863 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1867 if ( queue !== false && this.busy() ) {
1868 // Wait + Push to Queue
1869 //this.debug('this.pushState: we must wait', arguments);
1872 callback: this.pushState,
1879 // Make Busy + Continue
1882 // Create the newState
1883 var newState = this.createStateObject(data,title,url);
1886 if ( this.isLastSavedState(newState) ) {
1887 // Won't be a change
1891 // Store the newState
1892 this.storeState(newState);
1893 this.expectedStateId = newState.id;
1895 // Push the newState
1896 history.pushState(newState.id,newState.title,newState.url);
1899 Roo.get(window).fireEvent('popstate');
1902 // End pushState closure
1907 * replaceState(data,title,url)
1908 * Replace the State and trigger onpopstate
1909 * We have to trigger for HTML4 compatibility
1910 * @param {object} data
1911 * @param {string} title
1912 * @param {string} url
1915 replaceState : function(data,title,url,queue){
1916 //this.debug('this.replaceState: called', arguments);
1919 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1920 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1924 if ( queue !== false && this.busy() ) {
1925 // Wait + Push to Queue
1926 //this.debug('this.replaceState: we must wait', arguments);
1929 callback: this.replaceState,
1936 // Make Busy + Continue
1939 // Create the newState
1940 var newState = this.createStateObject(data,title,url);
1943 if ( this.isLastSavedState(newState) ) {
1944 // Won't be a change
1948 // Store the newState
1949 this.storeState(newState);
1950 this.expectedStateId = newState.id;
1952 // Push the newState
1953 history.replaceState(newState.id,newState.title,newState.url);
1956 Roo.get(window).fireEvent('popstate');
1959 // End replaceState closure
1964 // ====================================================================
1968 * Bind for Saving Store
1971 // When the page is closed
1972 onUnload : function(){
1974 var currentStore, item, currentStoreString;
1978 currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1985 currentStore.idToState = currentStore.idToState || {};
1986 currentStore.urlToId = currentStore.urlToId || {};
1987 currentStore.stateToId = currentStore.stateToId || {};
1990 for ( item in this.idToState ) {
1991 if ( !this.idToState.hasOwnProperty(item) ) {
1994 currentStore.idToState[item] = this.idToState[item];
1996 for ( item in this.urlToId ) {
1997 if ( !this.urlToId.hasOwnProperty(item) ) {
2000 currentStore.urlToId[item] = this.urlToId[item];
2002 for ( item in this.stateToId ) {
2003 if ( !this.stateToId.hasOwnProperty(item) ) {
2006 currentStore.stateToId[item] = this.stateToId[item];
2010 this.store = currentStore;
2011 this.normalizeStore();
2013 // In Safari, going into Private Browsing mode causes the
2014 // Session Storage object to still exist but if you try and use
2015 // or set any property/function of it it throws the exception
2016 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
2017 // add something to storage that exceeded the quota." infinitely
2019 currentStoreString = JSON.stringify(currentStore);
2022 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2025 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
2026 if (this.sessionStorage.length) {
2027 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
2028 // removing/resetting the storage can work.
2029 this.sessionStorage.removeItem('Roo.History.store');
2030 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2032 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.