2 * Originally based of this code... - refactored for Roo...
5 * @author Benjamin Arthur Lupton <contact@balupton.com>
6 * @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
7 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
13 // ====================================================================
19 * How long should the interval be before hashchange checks
21 thishashChangeInterval : 100,
25 * How long should the interval be before safari poll checks
27 safariPollInterval : 500,
31 * How long should the interval be before we perform a double check
33 doubleCheckInterval : 500,
37 * Force this.not to append suid
43 * How long should we wait between store calls
49 * How long should we wait between busy events
55 * If true will enable debug messages to be logged
61 * What is the title of the initial state
67 * If true, will force HTMl4 mode (hashtags)
73 * Want to override default options and call init manually.
79 * Which bugs are present
83 * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
84 * https://bugs.webkit.org/show_bug.cgi?id=56249
89 * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
90 * https://bugs.webkit.org/show_bug.cgi?id=42940
95 * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
100 * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
105 // ========================================================================
111 sessionStorage : false, // sessionStorage
113 intervalList : false, // array normally.
116 * Is History enabled?
120 // ====================================================================
125 * The store for all session specific data
131 * 1-1: State ID to State Object
137 * 1-1: State String to State ID
143 * 1-1: State URL to State ID
149 * Store the states in an array
151 storedStates : false,
155 * Saved the states in an array
161 * The list of queues to use
162 * First In, First Out
171 // ====================================================================
175 * History.stateChanged
176 * States whether or not the state has changed since the last double check was initialised
178 stateChanged : false,
181 * History.doubleChecker
182 * Contains the timeout used for the double checks
184 doubleChecker : false,
187 // Initialise History
188 init : function(options)
191 var emptyFunction = function(){};
195 this.initialTitle = window.document.title;
200 this.storedStates=[];
204 Roo.apply(this,options)
206 // Check Load Status of Adapter
207 //if ( typeof this.Adapter === 'undefined' ) {
211 // Check Load Status of Core
212 if ( typeof this.initCore !== 'undefined' ) {
216 // Check Load Status of HTML4 Support
217 if ( typeof this.initHtml4 !== 'undefined' ) {
223 this.enabled = !this.emulated.pushState;
225 if ( this.emulated.pushState ) {
229 this.pushState = emptyFunction;
230 this.replaceState = emptyFunction;
233 this.Adapter.bind(window,'popstate',this.onPopState);
239 if ( this.sessionStorage ) {
242 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
247 this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
249 // For Other Browsers
250 this.Adapter.bind(window,'beforeunload',this.onUnload);
251 this.Adapter.bind(window,'unload',this.onUnload);
254 this.onUnload = emptyFunction;
258 this.normalizeStore();
260 * Clear Intervals on exit to prevent memory leaks
262 this.Adapter.bind(window,"unload",this.clearAllIntervals);
265 * Create the initial State
267 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
276 // ========================================================================
280 initCore : function(options){
282 this.intervalList = [];
286 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
287 this.sessionStorage.setItem('TEST', '1');
288 this.sessionStorage.removeItem('TEST');
290 this.sessionStorage = false;
294 if ( typeof this.initCore.initialized !== 'undefined' ) {
299 this.initCore.initialized = true;
308 * Clears all setInterval instances.
310 clearAllIntervals: function()
312 var i, il = this.intervalList;
313 if (typeof il !== "undefined" && il !== null) {
314 for (i = 0; i < il.length; i++) {
315 clearInterval(il[i]);
317 this.intervalList = null;
322 // ====================================================================
326 * debugLog(message,...)
327 * Logs the passed arguments if debug enabled
329 debugLog : function()
331 if ( (this.debug||false) ) {
332 Roo.log.apply(this,arguments);
338 // ====================================================================
342 * getInternetExplorerMajorVersion()
343 * Get's the major version of Internet Explorer
345 * @license Public Domain
346 * @author Benjamin Arthur Lupton <contact@balupton.com>
347 * @author James Padolsey <https://gist.github.com/527683>
349 getInternetExplorerMajorVersion : function(){
350 var result = this.getInternetExplorerMajorVersion.cached =
351 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
352 ? this.getInternetExplorerMajorVersion.cached
355 div = window.document.createElement('div'),
356 all = div.getElementsByTagName('i');
357 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
358 return (v > 4) ? v : false;
365 * isInternetExplorer()
366 * Are we using Internet Explorer?
368 * @license Public Domain
369 * @author Benjamin Arthur Lupton <contact@balupton.com>
371 isInternetExplorer : function(){
373 this.isInternetExplorer.cached =
374 (typeof this.isInternetExplorer.cached !== 'undefined')
375 ? this.isInternetExplorer.cached
376 : Boolean(this.getInternetExplorerMajorVersion())
382 * Which features require emulating?
390 initEmulated : function()
394 if (this.html4Mode) {
400 this.emulated.pushState = !Boolean(
401 window.history && window.history.pushState && window.history.replaceState
403 (/ 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) */
404 || (/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 */
407 this.emulated.hashChange = Boolean(
408 !(('onhashchange' in window) || ('onhashchange' in window.document))
410 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
419 * Checks to see if the Object is Empty
420 * @param {Object} obj
423 isEmptyObject = function(obj) {
424 for ( var name in obj ) {
425 if ( obj.hasOwnProperty(name) ) {
434 * Clones a object and eliminate all references to the original contexts
435 * @param {Object} obj
438 cloneObject = function(obj) {
441 hash = JSON.stringify(obj);
442 newObj = JSON.parse(hash);
451 // ====================================================================
456 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
457 * @return {String} rootUrl
459 getRootUrl = function(){
461 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
462 if ( window.document.location.port||false ) {
463 rootUrl += ':'+window.document.location.port;
473 * Fetches the `href` attribute of the `<base href="...">` element if it exists
474 * @return {String} baseHref
476 getBaseHref = function(){
479 baseElements = window.document.getElementsByTagName('base'),
483 // Test for Base Element
484 if ( baseElements.length === 1 ) {
485 // Prepare for Base Element
486 baseElement = baseElements[0];
487 baseHref = baseElement.href.replace(/[^\/]+$/,'');
490 // Adjust trailing slash
491 baseHref = baseHref.replace(/\/+$/,'');
492 if ( baseHref ) baseHref += '/';
500 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
501 * @return {String} baseUrl
503 getBaseUrl = function(){
505 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
513 * Fetches the URL of the current page
514 * @return {String} pageUrl
516 getPageUrl = function(){
519 State = this.getState(false,false),
520 stateUrl = (State||{}).url||this.getLocationHref(),
524 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
525 return (/\./).test(part) ? part : part+'/';
534 * Fetches the Url of the directory of the current page
535 * @return {String} basePageUrl
537 getBasePageUrl = function(){
539 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
540 return (/[^\/]$/).test(part) ? '' : part;
541 }).replace(/\/+$/,'')+'/';
549 * Ensures that we have an absolute URL and not a relative URL
550 * @param {string} url
551 * @param {Boolean} allowBaseHref
552 * @return {string} fullUrl
554 getFullUrl = function(url,allowBaseHref){
556 var fullUrl = url, firstChar = url.substring(0,1);
557 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
560 if ( /[a-z]+\:\/\//.test(url) ) {
563 else if ( firstChar === '/' ) {
565 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
567 else if ( firstChar === '#' ) {
569 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
571 else if ( firstChar === '?' ) {
573 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
577 if ( allowBaseHref ) {
578 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
580 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
582 // We have an if condition above as we do not want hashes
583 // which are relative to the baseHref in our URLs
584 // as if the baseHref changes, then all our bookmarks
585 // would now point to different locations
586 // whereas the basePageUrl will always stay the same
590 return fullUrl.replace(/\#$/,'');
595 * Ensures that we have a relative URL and not a absolute URL
596 * @param {string} url
597 * @return {string} url
599 getShortUrl = function(url){
601 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
604 if ( this.emulated.pushState ) {
605 // We are in a if statement as when pushState is not emulated
606 // The actual url these short urls are relative to can change
607 // So within the same session, we the url may end up somewhere different
608 shortUrl = shortUrl.replace(baseUrl,'');
612 shortUrl = shortUrl.replace(rootUrl,'/');
614 // Ensure we can still detect it as a state
615 if ( this.isTraditionalAnchor(shortUrl) ) {
616 shortUrl = './'+shortUrl;
620 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
627 * getLocationHref(document)
628 * Returns a normalized version of document.location.href
629 * accounting for browser inconsistencies, etc.
631 * This URL will be URI-encoded and will include the hash
633 * @param {object} document
634 * @return {string} url
636 getLocationHref = function(doc) {
637 doc = doc || window.document;
639 // most of the time, this will be true
640 if (doc.URL === doc.location.href)
641 return doc.location.href;
643 // some versions of webkit URI-decode document.location.href
644 // but they leave document.URL in an encoded state
645 if (doc.location.href === decodeURIComponent(doc.URL))
648 // FF 3.6 only updates document.URL when a page is reloaded
649 // document.location.href is updated correctly
650 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
651 return doc.location.href;
653 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
654 return doc.location.href;
656 return doc.URL || doc.location.href;
663 * Noramlize the store by adding necessary values
665 normalizeStore = function(){
666 this.store.idToState = this.store.idToState||{};
667 this.store.urlToId = this.store.urlToId||{};
668 this.store.stateToId = this.store.stateToId||{};
673 * Get an object containing the data, title and url of the current state
674 * @param {Boolean} friendly
675 * @param {Boolean} create
676 * @return {Object} State
678 getState : function(friendly,create){
680 if ( typeof friendly === 'undefined' ) { friendly = true; }
681 if ( typeof create === 'undefined' ) { create = true; }
684 var State = this.getLastSavedState();
687 if ( !State && create ) {
688 State = this.createStateObject();
693 State = this.cloneObject(State);
694 State.url = this.cleanUrl||State.url;
702 * getIdByState(State)
703 * Gets a ID for a State
704 * @param {State} newState
705 * @return {String} id
707 getIdByState = function(newState){
710 var id = this.extractId(newState.url),
714 // Find ID via State String
715 str = this.getStateString(newState);
716 if ( typeof this.stateToId[str] !== 'undefined' ) {
717 id = this.stateToId[str];
719 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
720 id = this.store.stateToId[str];
725 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
726 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
731 // Apply the new State to the ID
732 this.stateToId[str] = id;
733 this.idToState[id] = newState;
742 * normalizeState(State)
743 * Expands a State Object
744 * @param {object} State
747 normalizeState = function(oldState){
749 var newState, dataNotEmpty;
752 if ( !oldState || (typeof oldState !== 'object') ) {
757 if ( typeof oldState.normalized !== 'undefined' ) {
762 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
766 // ----------------------------------------------------------------
770 newState.normalized = true;
771 newState.title = oldState.title||'';
772 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
773 newState.hash = this.getShortUrl(newState.url);
774 newState.data = this.cloneObject(oldState.data);
777 newState.id = this.getIdByState(newState);
779 // ----------------------------------------------------------------
782 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
783 newState.url = newState.cleanUrl;
785 // Check to see if we have more than just a url
786 dataNotEmpty = !this.isEmptyObject(newState.data);
789 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
791 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
792 if ( !/\?/.test(newState.hash) ) {
793 newState.hash += '?';
795 newState.hash += '&_suid='+newState.id;
798 // Create the Hashed URL
799 newState.hashedUrl = this.getFullUrl(newState.hash);
801 // ----------------------------------------------------------------
803 // Update the URL if we have a duplicate
804 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
805 newState.url = newState.hashedUrl;
808 // ----------------------------------------------------------------
815 * createStateObject(data,title,url)
816 * Creates a object based on the data, title and url state params
817 * @param {object} data
818 * @param {string} title
819 * @param {string} url
822 createStateObject = function(data,title,url){
831 State = this.normalizeState(State);
839 * Get a state by it's UID
842 getStateById = function(id){
847 var State = this.idToState[id] || this.store.idToState[id] || undefined;
854 * Get a State's String
855 * @param {State} passedState
857 getStateString = function(passedState){
859 var State, cleanedState, str;
862 State = this.normalizeState(passedState);
867 title: passedState.title,
872 str = JSON.stringify(cleanedState);
880 * @param {State} passedState
881 * @return {String} id
883 getStateId = function(passedState){
888 State = this.normalizeState(passedState);
898 * getHashByState(State)
899 * Creates a Hash for the State Object
900 * @param {State} passedState
901 * @return {String} hash
903 getHashByState = function(passedState){
908 State = this.normalizeState(passedState);
918 * extractId(url_or_hash)
919 * Get a State ID by it's URL or Hash
920 * @param {string} url_or_hash
921 * @return {string} id
923 this.extractId = function ( url_or_hash ) {
925 var id,parts,url, tmp;
929 // If the URL has a #, use the id from before the #
930 if (url_or_hash.indexOf('#') != -1)
932 tmp = url_or_hash.split("#")[0];
939 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
940 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
941 id = parts ? String(parts[2]||'') : '';
948 * isTraditionalAnchor
949 * Checks to see if the url is a traditional anchor or not
950 * @param {String} url_or_hash
953 isTraditionalAnchor = function(url_or_hash){
955 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
958 return isTraditional;
963 * Get a State by it's URL or Hash
964 * @param {String} url_or_hash
965 * @return {State|null}
967 extractState = function(url_or_hash,create){
969 var State = null, id, url;
970 create = create||false;
973 id = this.extractId(url_or_hash);
975 State = this.getStateById(id);
978 // Fetch SUID returned no State
981 url = this.getFullUrl(url_or_hash);
984 id = this.getIdByUrl(url)||false;
986 State = this.getStateById(id);
990 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
991 State = this.createStateObject(null,null,url);
1001 * Get a State ID by a State URL
1003 getIdByUrl = function(url){
1005 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1012 * getLastSavedState()
1013 * Get an object containing the data, title and url of the current state
1014 * @return {Object} State
1016 getLastSavedState = function(){
1017 return this.savedStates[this.savedStates.length-1]||undefined;
1021 * getLastStoredState()
1022 * Get an object containing the data, title and url of the current state
1023 * @return {Object} State
1025 getLastStoredState = function(){
1026 return this.storedStates[this.storedStates.length-1]||undefined;
1031 * Checks if a Url will have a url conflict
1032 * @param {Object} newState
1033 * @return {Boolean} hasDuplicate
1035 hasUrlDuplicate = function(newState) {
1037 var hasDuplicate = false,
1041 oldState = this.extractState(newState.url);
1044 hasDuplicate = oldState && oldState.id !== newState.id;
1047 return hasDuplicate;
1053 * @param {Object} newState
1054 * @return {Object} newState
1056 storeState = function(newState){
1058 this.urlToId[newState.url] = newState.id;
1061 this.storedStates.push(this.cloneObject(newState));
1068 * isLastSavedState(newState)
1069 * Tests to see if the state is the last state
1070 * @param {Object} newState
1071 * @return {boolean} isLast
1073 isLastSavedState = function(newState){
1076 newId, oldState, oldId;
1079 if ( this.savedStates.length ) {
1080 newId = newState.id;
1081 oldState = this.getLastSavedState();
1082 oldId = oldState.id;
1085 isLast = (newId === oldId);
1095 * @param {Object} newState
1096 * @return {boolean} changed
1098 saveState = function(newState){
1100 if ( this.isLastSavedState(newState) ) {
1105 this.savedStates.push(this.cloneObject(newState));
1113 * Gets a state by the index
1114 * @param {integer} index
1117 getStateByIndex = function(index){
1122 if ( typeof index === 'undefined' ) {
1123 // Get the last inserted
1124 State = this.savedStates[this.savedStates.length-1];
1126 else if ( index < 0 ) {
1128 State = this.savedStates[this.savedStates.length+index];
1131 // Get from the beginning
1132 State = this.savedStates[index];
1141 * Gets the current index
1144 getCurrentIndex = function(){
1149 if(this.savedStates.length < 1) {
1153 index = this.savedStates.length-1;
1158 // ====================================================================
1163 * @param {Location=} location
1164 * Gets the current document hash
1165 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1168 getHash = function(doc){
1169 var url = this.getLocationHref(doc),
1171 hash = this.getHashByUrl(url);
1177 * normalize and Unescape a Hash
1178 * @param {String} hash
1181 unescapeHash = function(hash){
1183 var result = this.normalizeHash(hash);
1186 result = decodeURIComponent(result);
1194 * normalize a hash across browsers
1197 normalizeHash = function(hash){
1199 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1207 * Sets the document hash
1208 * @param {string} hash
1209 * @return {Roo.History}
1211 setHash = function(hash,queue){
1216 if ( queue !== false && this.busy() ) {
1217 // Wait + Push to Queue
1218 //this.debug('this.setHash: we must wait', arguments);
1221 callback: this.setHash,
1229 //this.debug('this.setHash: called',hash);
1231 // Make Busy + Continue
1234 // Check if hash is a state
1235 State = this.extractState(hash,true);
1236 if ( State && !this.emulated.pushState ) {
1237 // Hash is a state so skip the setHash
1238 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1241 this.pushState(State.data,State.title,State.url,false);
1243 else if ( this.getHash() !== hash ) {
1244 // Hash is a proper hash, so apply it
1246 // Handle browser bugs
1247 if ( this.bugs.setHash ) {
1248 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1250 // Fetch the base page
1251 pageUrl = this.getPageUrl();
1253 // Safari hash apply
1254 this.pushState(null,null,pageUrl+'#'+hash,false);
1257 // Normal hash apply
1258 window.document.location.hash = hash;
1268 * normalize and Escape a Hash
1271 escapeHash = function(hash){
1273 var result = normalizeHash(hash);
1276 result = window.encodeURIComponent(result);
1279 if ( !this.bugs.hashEscape ) {
1280 // Restore common parts
1282 .replace(/\%21/g,'!')
1283 .replace(/\%26/g,'&')
1284 .replace(/\%3D/g,'=')
1285 .replace(/\%3F/g,'?');
1294 * Extracts the Hash from a URL
1295 * @param {string} url
1296 * @return {string} url
1298 getHashByUrl = function(url){
1300 var hash = String(url)
1301 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1305 hash = this.unescapeHash(hash);
1313 * Applies the title to the document
1314 * @param {State} newState
1317 setTitle = function(newState){
1319 var title = newState.title,
1324 firstState = this.getStateByIndex(0);
1325 if ( firstState && firstState.url === newState.url ) {
1326 title = firstState.title||this.initialTitle;
1332 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1334 catch ( Exception ) { }
1335 window.document.title = title;
1342 // ====================================================================
1348 * @param {boolean} value [optional]
1349 * @return {boolean} busy
1351 busy = function(value){
1353 if ( typeof value !== 'undefined' ) {
1354 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1355 this.busy_flag = value;
1358 else if ( typeof this.busy_flag === 'undefined' ) {
1359 this.busy_flag = false;
1363 if ( !this.busy_flag ) {
1364 // Execute the next item in the queue
1365 window.clearTimeout(this.busy.timeout);
1366 var fireNext = function(){
1368 if ( this.busy_flag ) return;
1369 for ( i=this.queues.length-1; i >= 0; --i ) {
1370 queue = this.queues[i];
1371 if ( queue.length === 0 ) continue;
1372 item = queue.shift();
1373 this.fireQueueItem(item);
1374 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1377 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1381 return this.busy_flag;
1387 * fireQueueItem(item)
1389 * @param {Object} item
1390 * @return {Mixed} result
1392 fireQueueItem = function(item){
1393 return item.callback.apply(item.scope||this,item.args||[]);
1397 * pushQueue(callback,args)
1398 * Add an item to the queue
1399 * @param {Object} item [scope,callback,args,queue]
1401 pushQueue = function(item){
1402 // Prepare the queue
1403 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1406 this.queues[item.queue||0].push(item);
1413 * queue (item,queue), (func,queue), (func), (item)
1414 * Either firs the item now if not busy, or adds it to the queue
1416 queue = function(item,queue){
1418 if ( typeof item === 'function' ) {
1423 if ( typeof queue !== 'undefined' ) {
1428 if ( this.busy() ) {
1429 this.pushQueue(item);
1431 this.fireQueueItem(item);
1442 clearQueue = function(){
1443 this.busy_flag = false;
1451 * doubleCheckComplete()
1452 * Complete a double check
1453 * @return {Roo.History}
1455 doubleCheckComplete = function(){
1457 this.stateChanged = true;
1460 this.doubleCheckClear();
1467 * doubleCheckClear()
1468 * Clear a double check
1469 * @return {Roo.History}
1471 doubleCheckClear = function(){
1473 if ( this.doubleChecker ) {
1474 window.clearTimeout(this.doubleChecker);
1475 this.doubleChecker = false;
1484 * Create a double check
1485 * @return {Roo.History}
1487 doubleCheck = function(tryAgain){
1489 this.stateChanged = false;
1490 this.doubleCheckClear();
1492 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1493 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1494 if ( this.bugs.ieDoubleCheck ) {
1496 this.doubleChecker = window.setTimeout(
1498 this.doubleCheckClear();
1499 if ( !this.stateChanged ) {
1500 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1506 this.doubleCheckInterval
1515 // ====================================================================
1520 * Poll the current state
1521 * @return {Roo.History}
1523 safariStatePoll = function(){
1526 // Get the Last State which has the new URL
1528 urlState = this.extractState(this.getLocationHref()),
1531 // Check for a difference
1532 if ( !this.isLastSavedState(urlState) ) {
1533 newState = urlState;
1539 // Check if we have a state with that url
1542 //this.debug('this.safariStatePoll: new');
1543 newState = this.createStateObject();
1546 // Apply the New State
1547 //this.debug('this.safariStatePoll: trigger');
1548 this.Adapter.trigger(window,'popstate');
1555 // ====================================================================
1560 * Send the browser history back one item
1561 * @param {Integer} queue [optional]
1563 back = function(queue){
1564 //this.debug('this.back: called', arguments);
1567 if ( queue !== false && this.busy() ) {
1568 // Wait + Push to Queue
1569 //this.debug('this.back: we must wait', arguments);
1572 callback: this.back,
1579 // Make Busy + Continue
1582 // Fix certain browser bugs that prevent the state from changing
1583 this.doubleCheck(function(){
1596 * Send the browser history forward one item
1597 * @param {Integer} queue [optional]
1599 forward = function(queue){
1600 //this.debug('this.forward: called', arguments);
1603 if ( queue !== false && this.busy() ) {
1604 // Wait + Push to Queue
1605 //this.debug('this.forward: we must wait', arguments);
1608 callback: this.forward,
1615 // Make Busy + Continue
1619 // Fix certain browser bugs that prevent the state from changing
1620 this.doubleCheck(function(){
1627 // End forward closure
1633 * Send the browser history back or forward index times
1634 * @param {Integer} queue [optional]
1636 go = function(index,queue){
1637 //this.debug('this.go: called', arguments);
1645 for ( i=1; i<=index; ++i ) {
1646 this.forward(queue);
1649 else if ( index < 0 ) {
1651 for ( i=-1; i>=index; --i ) {
1656 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1664 // ====================================================================
1665 // HTML5 State Support
1669 * Use native HTML5 History API Implementation
1673 * onPopState(event,extra)
1674 * Refresh the Current State
1676 onPopState = function(event,extra){
1678 var stateId = false, newState = false, currentHash, currentState;
1680 // Reset the double check
1681 this.doubleCheckComplete();
1683 // Check for a Hash, and handle apporiatly
1684 currentHash = this.getHash();
1685 if ( currentHash ) {
1687 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1688 if ( currentState ) {
1689 // We were able to parse it, it must be a State!
1690 // Let's forward to replaceState
1691 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1692 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1695 // Traditional Anchor
1696 //this.debug('this.onPopState: traditional anchor', currentHash);
1697 this.Adapter.trigger(window,'anchorchange');
1701 // We don't care for hashes
1702 this.expectedStateId = false;
1707 stateId = this.Adapter.extractEventData('state',event,extra) || false;
1711 // Vanilla: Back/forward button was used
1712 newState = this.getStateById(stateId);
1714 else if ( this.expectedStateId ) {
1715 // Vanilla: A new state was pushed, and popstate was called manually
1716 newState = this.getStateById(this.expectedStateId);
1720 newState = this.extractState(this.getLocationHref());
1723 // The State did not exist in our store
1725 // Regenerate the State
1726 newState = this.createStateObject(null,null,this.getLocationHref());
1730 this.expectedStateId = false;
1732 // Check if we are the same state
1733 if ( this.isLastSavedState(newState) ) {
1734 // There has been no change (just the page's hash has finally propagated)
1735 //this.debug('this.onPopState: no change', newState, this.savedStates);
1741 this.storeState(newState);
1742 this.saveState(newState);
1744 // Force update of the title
1745 this.setTitle(newState);
1748 this.Adapter.trigger(window,'statechange');
1762 * pushState(data,title,url)
1763 * Add a new State to the history object, become it, and trigger onpopstate
1764 * We have to trigger for HTML4 compatibility
1765 * @param {object} data
1766 * @param {string} title
1767 * @param {string} url
1770 pushState = function(data,title,url,queue){
1771 //this.debug('this.pushState: called', arguments);
1774 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1775 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1779 if ( queue !== false && this.busy() ) {
1780 // Wait + Push to Queue
1781 //this.debug('this.pushState: we must wait', arguments);
1784 callback: this.pushState,
1791 // Make Busy + Continue
1794 // Create the newState
1795 var newState = this.createStateObject(data,title,url);
1798 if ( this.isLastSavedState(newState) ) {
1799 // Won't be a change
1803 // Store the newState
1804 this.storeState(newState);
1805 this.expectedStateId = newState.id;
1807 // Push the newState
1808 history.pushState(newState.id,newState.title,newState.url);
1811 this.Adapter.trigger(window,'popstate');
1814 // End pushState closure
1819 * replaceState(data,title,url)
1820 * Replace the State and trigger onpopstate
1821 * We have to trigger for HTML4 compatibility
1822 * @param {object} data
1823 * @param {string} title
1824 * @param {string} url
1827 replaceState = function(data,title,url,queue){
1828 //this.debug('this.replaceState: called', arguments);
1831 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1832 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1836 if ( queue !== false && this.busy() ) {
1837 // Wait + Push to Queue
1838 //this.debug('this.replaceState: we must wait', arguments);
1841 callback: this.replaceState,
1848 // Make Busy + Continue
1851 // Create the newState
1852 var newState = this.createStateObject(data,title,url);
1855 if ( this.isLastSavedState(newState) ) {
1856 // Won't be a change
1860 // Store the newState
1861 this.storeState(newState);
1862 this.expectedStateId = newState.id;
1864 // Push the newState
1865 history.replaceState(newState.id,newState.title,newState.url);
1868 this.Adapter.trigger(window,'popstate');
1871 // End replaceState closure
1876 // ====================================================================
1880 * Bind for Saving Store
1883 // When the page is closed
1884 onUnload = function(){
1886 var currentStore, item, currentStoreString;
1890 currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1897 currentStore.idToState = currentStore.idToState || {};
1898 currentStore.urlToId = currentStore.urlToId || {};
1899 currentStore.stateToId = currentStore.stateToId || {};
1902 for ( item in this.idToState ) {
1903 if ( !this.idToState.hasOwnProperty(item) ) {
1906 currentStore.idToState[item] = this.idToState[item];
1908 for ( item in this.urlToId ) {
1909 if ( !this.urlToId.hasOwnProperty(item) ) {
1912 currentStore.urlToId[item] = this.urlToId[item];
1914 for ( item in this.stateToId ) {
1915 if ( !this.stateToId.hasOwnProperty(item) ) {
1918 currentStore.stateToId[item] = this.stateToId[item];
1922 this.store = currentStore;
1923 this.normalizeStore();
1925 // In Safari, going into Private Browsing mode causes the
1926 // Session Storage object to still exist but if you try and use
1927 // or set any property/function of it it throws the exception
1928 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1929 // add something to storage that exceeded the quota." infinitely
1931 currentStoreString = JSON.stringify(currentStore);
1934 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
1937 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1938 if (this.sessionStorage.length) {
1939 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1940 // removing/resetting the storage can work.
1941 this.sessionStorage.removeItem('Roo.History.store');
1942 this.sessionStorage.setItem('Roo.History.store', currentStoreString);
1944 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1952 // For Internet Explorer
1954 // Both are enabled for consistency
1957 // Non-Native pushState Implementation
1958 if ( !History.emulated.pushState ) {
1959 // Be aware, the following is only for native pushState implementations
1960 // If you are wanting to include something for all browsers
1961 // Then include it above this if block
1966 if ( History.bugs.safariPoll ) {
1967 History.intervalList.push(setInterval(History.safariStatePoll, this.safariPollInterval));
1971 * Ensure Cross Browser Compatibility
1973 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1975 * Fix Safari HashChange Issue
1979 History.Adapter.bind(window,'hashchange',function(){
1980 History.Adapter.trigger(window,'popstate');
1984 if ( History.getHash() ) {
1985 History.Adapter.onDomLoad(function(){
1986 History.Adapter.trigger(window,'hashchange');
1991 } // !History.emulated.pushState
1994 }; // History.initCore
1996 // Try to Initialise History
1997 if (!History.options || !History.options.delayInit) {