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){
190 initialTitle : window.document.title,
195 this.storedStates=[];
199 Roo.apply(this,options)
201 // Check Load Status of Adapter
202 //if ( typeof this.Adapter === 'undefined' ) {
206 // Check Load Status of Core
207 if ( typeof this.initCore !== 'undefined' ) {
211 // Check Load Status of HTML4 Support
212 if ( typeof this.initHtml4 !== 'undefined' ) {
218 this.enabled = !this.emulated.pushState;
220 if ( this.emulated.pushState ) {
223 var emptyFunction = function(){};
224 this.pushState = emptyFunction;
225 this.replaceState = emptyFunction;
228 this.Adapter.bind(window,'popstate',this.onPopState);
234 if ( this.sessionStorage ) {
237 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
244 this.normalizeStore();
246 * Clear Intervals on exit to prevent memory leaks
248 History.Adapter.bind(window,"unload",History.clearAllIntervals);
251 * Create the initial State
253 History.saveState(History.storeState(History.extractState(History.getLocationHref(),true)));
262 // ========================================================================
266 initCore : function(options){
268 this.intervalList = [];
272 this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
273 this.sessionStorage.setItem('TEST', '1');
274 this.sessionStorage.removeItem('TEST');
276 this.sessionStorage = false;
280 if ( typeof this.initCore.initialized !== 'undefined' ) {
285 this.initCore.initialized = true;
294 * Clears all setInterval instances.
296 clearAllIntervals: function()
298 var i, il = this.intervalList;
299 if (typeof il !== "undefined" && il !== null) {
300 for (i = 0; i < il.length; i++) {
301 clearInterval(il[i]);
303 this.intervalList = null;
308 // ====================================================================
312 * debugLog(message,...)
313 * Logs the passed arguments if debug enabled
315 debugLog : function()
317 if ( (this.debug||false) ) {
318 Roo.log.apply(this,arguments);
324 // ====================================================================
328 * getInternetExplorerMajorVersion()
329 * Get's the major version of Internet Explorer
331 * @license Public Domain
332 * @author Benjamin Arthur Lupton <contact@balupton.com>
333 * @author James Padolsey <https://gist.github.com/527683>
335 getInternetExplorerMajorVersion : function(){
336 var result = this.getInternetExplorerMajorVersion.cached =
337 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
338 ? this.getInternetExplorerMajorVersion.cached
341 div = window.document.createElement('div'),
342 all = div.getElementsByTagName('i');
343 while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
344 return (v > 4) ? v : false;
351 * isInternetExplorer()
352 * Are we using Internet Explorer?
354 * @license Public Domain
355 * @author Benjamin Arthur Lupton <contact@balupton.com>
357 isInternetExplorer : function(){
359 this.isInternetExplorer.cached =
360 (typeof this.isInternetExplorer.cached !== 'undefined')
361 ? this.isInternetExplorer.cached
362 : Boolean(this.getInternetExplorerMajorVersion())
368 * Which features require emulating?
376 initEmulated : function()
380 if (this.html4Mode) {
386 this.emulated.pushState = !Boolean(
387 window.history && window.history.pushState && window.history.replaceState
389 (/ 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) */
390 || (/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 */
393 this.emulated.hashChange = Boolean(
394 !(('onhashchange' in window) || ('onhashchange' in window.document))
396 (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
405 * Checks to see if the Object is Empty
406 * @param {Object} obj
409 isEmptyObject = function(obj) {
410 for ( var name in obj ) {
411 if ( obj.hasOwnProperty(name) ) {
420 * Clones a object and eliminate all references to the original contexts
421 * @param {Object} obj
424 cloneObject = function(obj) {
427 hash = JSON.stringify(obj);
428 newObj = JSON.parse(hash);
437 // ====================================================================
442 * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
443 * @return {String} rootUrl
445 getRootUrl = function(){
447 var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
448 if ( window.document.location.port||false ) {
449 rootUrl += ':'+window.document.location.port;
459 * Fetches the `href` attribute of the `<base href="...">` element if it exists
460 * @return {String} baseHref
462 getBaseHref = function(){
465 baseElements = window.document.getElementsByTagName('base'),
469 // Test for Base Element
470 if ( baseElements.length === 1 ) {
471 // Prepare for Base Element
472 baseElement = baseElements[0];
473 baseHref = baseElement.href.replace(/[^\/]+$/,'');
476 // Adjust trailing slash
477 baseHref = baseHref.replace(/\/+$/,'');
478 if ( baseHref ) baseHref += '/';
486 * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
487 * @return {String} baseUrl
489 getBaseUrl = function(){
491 var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
499 * Fetches the URL of the current page
500 * @return {String} pageUrl
502 getPageUrl = function(){
505 State = this.getState(false,false),
506 stateUrl = (State||{}).url||this.getLocationHref(),
510 pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
511 return (/\./).test(part) ? part : part+'/';
520 * Fetches the Url of the directory of the current page
521 * @return {String} basePageUrl
523 getBasePageUrl = function(){
525 var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
526 return (/[^\/]$/).test(part) ? '' : part;
527 }).replace(/\/+$/,'')+'/';
535 * Ensures that we have an absolute URL and not a relative URL
536 * @param {string} url
537 * @param {Boolean} allowBaseHref
538 * @return {string} fullUrl
540 getFullUrl = function(url,allowBaseHref){
542 var fullUrl = url, firstChar = url.substring(0,1);
543 allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
546 if ( /[a-z]+\:\/\//.test(url) ) {
549 else if ( firstChar === '/' ) {
551 fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
553 else if ( firstChar === '#' ) {
555 fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
557 else if ( firstChar === '?' ) {
559 fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
563 if ( allowBaseHref ) {
564 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
566 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
568 // We have an if condition above as we do not want hashes
569 // which are relative to the baseHref in our URLs
570 // as if the baseHref changes, then all our bookmarks
571 // would now point to different locations
572 // whereas the basePageUrl will always stay the same
576 return fullUrl.replace(/\#$/,'');
581 * Ensures that we have a relative URL and not a absolute URL
582 * @param {string} url
583 * @return {string} url
585 getShortUrl = function(url){
587 var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
590 if ( this.emulated.pushState ) {
591 // We are in a if statement as when pushState is not emulated
592 // The actual url these short urls are relative to can change
593 // So within the same session, we the url may end up somewhere different
594 shortUrl = shortUrl.replace(baseUrl,'');
598 shortUrl = shortUrl.replace(rootUrl,'/');
600 // Ensure we can still detect it as a state
601 if ( this.isTraditionalAnchor(shortUrl) ) {
602 shortUrl = './'+shortUrl;
606 shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
613 * getLocationHref(document)
614 * Returns a normalized version of document.location.href
615 * accounting for browser inconsistencies, etc.
617 * This URL will be URI-encoded and will include the hash
619 * @param {object} document
620 * @return {string} url
622 getLocationHref = function(doc) {
623 doc = doc || window.document;
625 // most of the time, this will be true
626 if (doc.URL === doc.location.href)
627 return doc.location.href;
629 // some versions of webkit URI-decode document.location.href
630 // but they leave document.URL in an encoded state
631 if (doc.location.href === decodeURIComponent(doc.URL))
634 // FF 3.6 only updates document.URL when a page is reloaded
635 // document.location.href is updated correctly
636 if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
637 return doc.location.href;
639 if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
640 return doc.location.href;
642 return doc.URL || doc.location.href;
649 * Noramlize the store by adding necessary values
651 normalizeStore = function(){
652 this.store.idToState = this.store.idToState||{};
653 this.store.urlToId = this.store.urlToId||{};
654 this.store.stateToId = this.store.stateToId||{};
659 * Get an object containing the data, title and url of the current state
660 * @param {Boolean} friendly
661 * @param {Boolean} create
662 * @return {Object} State
664 getState : function(friendly,create){
666 if ( typeof friendly === 'undefined' ) { friendly = true; }
667 if ( typeof create === 'undefined' ) { create = true; }
670 var State = this.getLastSavedState();
673 if ( !State && create ) {
674 State = this.createStateObject();
679 State = this.cloneObject(State);
680 State.url = this.cleanUrl||State.url;
688 * getIdByState(State)
689 * Gets a ID for a State
690 * @param {State} newState
691 * @return {String} id
693 getIdByState = function(newState){
696 var id = this.extractId(newState.url),
700 // Find ID via State String
701 str = this.getStateString(newState);
702 if ( typeof this.stateToId[str] !== 'undefined' ) {
703 id = this.stateToId[str];
705 else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
706 id = this.store.stateToId[str];
711 id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
712 if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
717 // Apply the new State to the ID
718 this.stateToId[str] = id;
719 this.idToState[id] = newState;
728 * normalizeState(State)
729 * Expands a State Object
730 * @param {object} State
733 normalizeState = function(oldState){
735 var newState, dataNotEmpty;
738 if ( !oldState || (typeof oldState !== 'object') ) {
743 if ( typeof oldState.normalized !== 'undefined' ) {
748 if ( !oldState.data || (typeof oldState.data !== 'object') ) {
752 // ----------------------------------------------------------------
756 newState.normalized = true;
757 newState.title = oldState.title||'';
758 newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
759 newState.hash = this.getShortUrl(newState.url);
760 newState.data = this.cloneObject(oldState.data);
763 newState.id = this.getIdByState(newState);
765 // ----------------------------------------------------------------
768 newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
769 newState.url = newState.cleanUrl;
771 // Check to see if we have more than just a url
772 dataNotEmpty = !this.isEmptyObject(newState.data);
775 if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
777 newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
778 if ( !/\?/.test(newState.hash) ) {
779 newState.hash += '?';
781 newState.hash += '&_suid='+newState.id;
784 // Create the Hashed URL
785 newState.hashedUrl = this.getFullUrl(newState.hash);
787 // ----------------------------------------------------------------
789 // Update the URL if we have a duplicate
790 if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
791 newState.url = newState.hashedUrl;
794 // ----------------------------------------------------------------
801 * createStateObject(data,title,url)
802 * Creates a object based on the data, title and url state params
803 * @param {object} data
804 * @param {string} title
805 * @param {string} url
808 createStateObject = function(data,title,url){
817 State = this.normalizeState(State);
825 * Get a state by it's UID
828 getStateById = function(id){
833 var State = this.idToState[id] || this.store.idToState[id] || undefined;
840 * Get a State's String
841 * @param {State} passedState
843 getStateString = function(passedState){
845 var State, cleanedState, str;
848 State = this.normalizeState(passedState);
853 title: passedState.title,
858 str = JSON.stringify(cleanedState);
866 * @param {State} passedState
867 * @return {String} id
869 getStateId = function(passedState){
874 State = this.normalizeState(passedState);
884 * getHashByState(State)
885 * Creates a Hash for the State Object
886 * @param {State} passedState
887 * @return {String} hash
889 getHashByState = function(passedState){
894 State = this.normalizeState(passedState);
904 * extractId(url_or_hash)
905 * Get a State ID by it's URL or Hash
906 * @param {string} url_or_hash
907 * @return {string} id
909 this.extractId = function ( url_or_hash ) {
911 var id,parts,url, tmp;
915 // If the URL has a #, use the id from before the #
916 if (url_or_hash.indexOf('#') != -1)
918 tmp = url_or_hash.split("#")[0];
925 parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
926 url = parts ? (parts[1]||url_or_hash) : url_or_hash;
927 id = parts ? String(parts[2]||'') : '';
934 * isTraditionalAnchor
935 * Checks to see if the url is a traditional anchor or not
936 * @param {String} url_or_hash
939 isTraditionalAnchor = function(url_or_hash){
941 var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
944 return isTraditional;
949 * Get a State by it's URL or Hash
950 * @param {String} url_or_hash
951 * @return {State|null}
953 extractState = function(url_or_hash,create){
955 var State = null, id, url;
956 create = create||false;
959 id = this.extractId(url_or_hash);
961 State = this.getStateById(id);
964 // Fetch SUID returned no State
967 url = this.getFullUrl(url_or_hash);
970 id = this.getIdByUrl(url)||false;
972 State = this.getStateById(id);
976 if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
977 State = this.createStateObject(null,null,url);
987 * Get a State ID by a State URL
989 getIdByUrl = function(url){
991 var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
998 * getLastSavedState()
999 * Get an object containing the data, title and url of the current state
1000 * @return {Object} State
1002 getLastSavedState = function(){
1003 return this.savedStates[this.savedStates.length-1]||undefined;
1007 * getLastStoredState()
1008 * Get an object containing the data, title and url of the current state
1009 * @return {Object} State
1011 getLastStoredState = function(){
1012 return this.storedStates[this.storedStates.length-1]||undefined;
1017 * Checks if a Url will have a url conflict
1018 * @param {Object} newState
1019 * @return {Boolean} hasDuplicate
1021 hasUrlDuplicate = function(newState) {
1023 var hasDuplicate = false,
1027 oldState = this.extractState(newState.url);
1030 hasDuplicate = oldState && oldState.id !== newState.id;
1033 return hasDuplicate;
1039 * @param {Object} newState
1040 * @return {Object} newState
1042 storeState = function(newState){
1044 this.urlToId[newState.url] = newState.id;
1047 this.storedStates.push(this.cloneObject(newState));
1054 * isLastSavedState(newState)
1055 * Tests to see if the state is the last state
1056 * @param {Object} newState
1057 * @return {boolean} isLast
1059 isLastSavedState = function(newState){
1062 newId, oldState, oldId;
1065 if ( this.savedStates.length ) {
1066 newId = newState.id;
1067 oldState = this.getLastSavedState();
1068 oldId = oldState.id;
1071 isLast = (newId === oldId);
1081 * @param {Object} newState
1082 * @return {boolean} changed
1084 saveState = function(newState){
1086 if ( this.isLastSavedState(newState) ) {
1091 this.savedStates.push(this.cloneObject(newState));
1099 * Gets a state by the index
1100 * @param {integer} index
1103 getStateByIndex = function(index){
1108 if ( typeof index === 'undefined' ) {
1109 // Get the last inserted
1110 State = this.savedStates[this.savedStates.length-1];
1112 else if ( index < 0 ) {
1114 State = this.savedStates[this.savedStates.length+index];
1117 // Get from the beginning
1118 State = this.savedStates[index];
1127 * Gets the current index
1130 getCurrentIndex = function(){
1135 if(this.savedStates.length < 1) {
1139 index = this.savedStates.length-1;
1144 // ====================================================================
1149 * @param {Location=} location
1150 * Gets the current document hash
1151 * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1154 getHash = function(doc){
1155 var url = this.getLocationHref(doc),
1157 hash = this.getHashByUrl(url);
1163 * normalize and Unescape a Hash
1164 * @param {String} hash
1167 unescapeHash = function(hash){
1169 var result = this.normalizeHash(hash);
1172 result = decodeURIComponent(result);
1180 * normalize a hash across browsers
1183 normalizeHash = function(hash){
1185 var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1193 * Sets the document hash
1194 * @param {string} hash
1195 * @return {Roo.History}
1197 setHash = function(hash,queue){
1202 if ( queue !== false && this.busy() ) {
1203 // Wait + Push to Queue
1204 //this.debug('this.setHash: we must wait', arguments);
1207 callback: this.setHash,
1215 //this.debug('this.setHash: called',hash);
1217 // Make Busy + Continue
1220 // Check if hash is a state
1221 State = this.extractState(hash,true);
1222 if ( State && !this.emulated.pushState ) {
1223 // Hash is a state so skip the setHash
1224 //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1227 this.pushState(State.data,State.title,State.url,false);
1229 else if ( this.getHash() !== hash ) {
1230 // Hash is a proper hash, so apply it
1232 // Handle browser bugs
1233 if ( this.bugs.setHash ) {
1234 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1236 // Fetch the base page
1237 pageUrl = this.getPageUrl();
1239 // Safari hash apply
1240 this.pushState(null,null,pageUrl+'#'+hash,false);
1243 // Normal hash apply
1244 window.document.location.hash = hash;
1254 * normalize and Escape a Hash
1257 escapeHash = function(hash){
1259 var result = normalizeHash(hash);
1262 result = window.encodeURIComponent(result);
1265 if ( !this.bugs.hashEscape ) {
1266 // Restore common parts
1268 .replace(/\%21/g,'!')
1269 .replace(/\%26/g,'&')
1270 .replace(/\%3D/g,'=')
1271 .replace(/\%3F/g,'?');
1280 * Extracts the Hash from a URL
1281 * @param {string} url
1282 * @return {string} url
1284 getHashByUrl = function(url){
1286 var hash = String(url)
1287 .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1291 hash = this.unescapeHash(hash);
1299 * Applies the title to the document
1300 * @param {State} newState
1303 setTitle = function(newState){
1305 var title = newState.title,
1310 firstState = this.getStateByIndex(0);
1311 if ( firstState && firstState.url === newState.url ) {
1312 title = firstState.title||this.initialTitle;
1318 window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','<').replace('>','>').replace(' & ',' & ');
1320 catch ( Exception ) { }
1321 window.document.title = title;
1328 // ====================================================================
1334 * @param {boolean} value [optional]
1335 * @return {boolean} busy
1337 busy = function(value){
1339 if ( typeof value !== 'undefined' ) {
1340 //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1341 this.busy_flag = value;
1344 else if ( typeof this.busy_flag === 'undefined' ) {
1345 this.busy_flag = false;
1349 if ( !this.busy_flag ) {
1350 // Execute the next item in the queue
1351 window.clearTimeout(this.busy.timeout);
1352 var fireNext = function(){
1354 if ( this.busy_flag ) return;
1355 for ( i=this.queues.length-1; i >= 0; --i ) {
1356 queue = this.queues[i];
1357 if ( queue.length === 0 ) continue;
1358 item = queue.shift();
1359 this.fireQueueItem(item);
1360 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1363 this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1367 return this.busy_flag;
1373 * fireQueueItem(item)
1375 * @param {Object} item
1376 * @return {Mixed} result
1378 fireQueueItem = function(item){
1379 return item.callback.apply(item.scope||this,item.args||[]);
1383 * pushQueue(callback,args)
1384 * Add an item to the queue
1385 * @param {Object} item [scope,callback,args,queue]
1387 pushQueue = function(item){
1388 // Prepare the queue
1389 this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1392 this.queues[item.queue||0].push(item);
1399 * queue (item,queue), (func,queue), (func), (item)
1400 * Either firs the item now if not busy, or adds it to the queue
1402 queue = function(item,queue){
1404 if ( typeof item === 'function' ) {
1409 if ( typeof queue !== 'undefined' ) {
1414 if ( this.busy() ) {
1415 this.pushQueue(item);
1417 this.fireQueueItem(item);
1428 clearQueue = function(){
1429 this.busy_flag = false;
1437 * doubleCheckComplete()
1438 * Complete a double check
1439 * @return {Roo.History}
1441 doubleCheckComplete = function(){
1443 this.stateChanged = true;
1446 this.doubleCheckClear();
1453 * doubleCheckClear()
1454 * Clear a double check
1455 * @return {Roo.History}
1457 doubleCheckClear = function(){
1459 if ( this.doubleChecker ) {
1460 window.clearTimeout(this.doubleChecker);
1461 this.doubleChecker = false;
1470 * Create a double check
1471 * @return {Roo.History}
1473 doubleCheck = function(tryAgain){
1475 this.stateChanged = false;
1476 this.doubleCheckClear();
1478 // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1479 // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1480 if ( this.bugs.ieDoubleCheck ) {
1482 this.doubleChecker = window.setTimeout(
1484 this.doubleCheckClear();
1485 if ( !this.stateChanged ) {
1486 //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1492 this.doubleCheckInterval
1501 // ====================================================================
1506 * Poll the current state
1507 * @return {Roo.History}
1509 safariStatePoll = function(){
1512 // Get the Last State which has the new URL
1514 urlState = this.extractState(this.getLocationHref()),
1517 // Check for a difference
1518 if ( !this.isLastSavedState(urlState) ) {
1519 newState = urlState;
1525 // Check if we have a state with that url
1528 //this.debug('this.safariStatePoll: new');
1529 newState = this.createStateObject();
1532 // Apply the New State
1533 //this.debug('this.safariStatePoll: trigger');
1534 this.Adapter.trigger(window,'popstate');
1541 // ====================================================================
1546 * Send the browser history back one item
1547 * @param {Integer} queue [optional]
1549 back = function(queue){
1550 //this.debug('this.back: called', arguments);
1553 if ( queue !== false && this.busy() ) {
1554 // Wait + Push to Queue
1555 //this.debug('this.back: we must wait', arguments);
1558 callback: this.back,
1565 // Make Busy + Continue
1568 // Fix certain browser bugs that prevent the state from changing
1569 this.doubleCheck(function(){
1582 * Send the browser history forward one item
1583 * @param {Integer} queue [optional]
1585 forward = function(queue){
1586 //this.debug('this.forward: called', arguments);
1589 if ( queue !== false && this.busy() ) {
1590 // Wait + Push to Queue
1591 //this.debug('this.forward: we must wait', arguments);
1594 callback: this.forward,
1601 // Make Busy + Continue
1605 // Fix certain browser bugs that prevent the state from changing
1606 this.doubleCheck(function(){
1613 // End forward closure
1619 * Send the browser history back or forward index times
1620 * @param {Integer} queue [optional]
1622 go = function(index,queue){
1623 //this.debug('this.go: called', arguments);
1631 for ( i=1; i<=index; ++i ) {
1632 this.forward(queue);
1635 else if ( index < 0 ) {
1637 for ( i=-1; i>=index; --i ) {
1642 throw new Error('History.go: History.go requires a positive or negative integer passed.');
1650 // ====================================================================
1651 // HTML5 State Support
1655 * Use native HTML5 History API Implementation
1659 * onPopState(event,extra)
1660 * Refresh the Current State
1662 onPopState = function(event,extra){
1664 var stateId = false, newState = false, currentHash, currentState;
1666 // Reset the double check
1667 this.doubleCheckComplete();
1669 // Check for a Hash, and handle apporiatly
1670 currentHash = this.getHash();
1671 if ( currentHash ) {
1673 currentState = this.extractState(currentHash||this.getLocationHref(),true);
1674 if ( currentState ) {
1675 // We were able to parse it, it must be a State!
1676 // Let's forward to replaceState
1677 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1678 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1681 // Traditional Anchor
1682 //this.debug('this.onPopState: traditional anchor', currentHash);
1683 this.Adapter.trigger(window,'anchorchange');
1687 // We don't care for hashes
1688 this.expectedStateId = false;
1693 stateId = this.Adapter.extractEventData('state',event,extra) || false;
1697 // Vanilla: Back/forward button was used
1698 newState = this.getStateById(stateId);
1700 else if ( this.expectedStateId ) {
1701 // Vanilla: A new state was pushed, and popstate was called manually
1702 newState = this.getStateById(this.expectedStateId);
1706 newState = this.extractState(this.getLocationHref());
1709 // The State did not exist in our store
1711 // Regenerate the State
1712 newState = this.createStateObject(null,null,this.getLocationHref());
1716 this.expectedStateId = false;
1718 // Check if we are the same state
1719 if ( this.isLastSavedState(newState) ) {
1720 // There has been no change (just the page's hash has finally propagated)
1721 //this.debug('this.onPopState: no change', newState, this.savedStates);
1727 this.storeState(newState);
1728 this.saveState(newState);
1730 // Force update of the title
1731 this.setTitle(newState);
1734 this.Adapter.trigger(window,'statechange');
1748 * pushState(data,title,url)
1749 * Add a new State to the history object, become it, and trigger onpopstate
1750 * We have to trigger for HTML4 compatibility
1751 * @param {object} data
1752 * @param {string} title
1753 * @param {string} url
1756 pushState = function(data,title,url,queue){
1757 //this.debug('this.pushState: called', arguments);
1760 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1761 throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1765 if ( queue !== false && this.busy() ) {
1766 // Wait + Push to Queue
1767 //this.debug('this.pushState: we must wait', arguments);
1770 callback: this.pushState,
1777 // Make Busy + Continue
1780 // Create the newState
1781 var newState = this.createStateObject(data,title,url);
1784 if ( this.isLastSavedState(newState) ) {
1785 // Won't be a change
1789 // Store the newState
1790 this.storeState(newState);
1791 this.expectedStateId = newState.id;
1793 // Push the newState
1794 history.pushState(newState.id,newState.title,newState.url);
1797 this.Adapter.trigger(window,'popstate');
1800 // End pushState closure
1805 * replaceState(data,title,url)
1806 * Replace the State and trigger onpopstate
1807 * We have to trigger for HTML4 compatibility
1808 * @param {object} data
1809 * @param {string} title
1810 * @param {string} url
1813 replaceState = function(data,title,url,queue){
1814 //this.debug('this.replaceState: called', arguments);
1817 if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1818 throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1822 if ( queue !== false && this.busy() ) {
1823 // Wait + Push to Queue
1824 //this.debug('this.replaceState: we must wait', arguments);
1827 callback: this.replaceState,
1834 // Make Busy + Continue
1837 // Create the newState
1838 var newState = this.createStateObject(data,title,url);
1841 if ( this.isLastSavedState(newState) ) {
1842 // Won't be a change
1846 // Store the newState
1847 this.storeState(newState);
1848 this.expectedStateId = newState.id;
1850 // Push the newState
1851 history.replaceState(newState.id,newState.title,newState.url);
1854 this.Adapter.trigger(window,'popstate');
1857 // End replaceState closure
1862 // ====================================================================
1866 * Bind for Saving Store
1868 if ( sessionStorage ) {
1869 // When the page is closed
1870 History.onUnload = function(){
1872 var currentStore, item, currentStoreString;
1876 currentStore = JSON.parse(sessionStorage.getItem('History.store'))||{};
1883 currentStore.idToState = currentStore.idToState || {};
1884 currentStore.urlToId = currentStore.urlToId || {};
1885 currentStore.stateToId = currentStore.stateToId || {};
1888 for ( item in History.idToState ) {
1889 if ( !History.idToState.hasOwnProperty(item) ) {
1892 currentStore.idToState[item] = History.idToState[item];
1894 for ( item in History.urlToId ) {
1895 if ( !History.urlToId.hasOwnProperty(item) ) {
1898 currentStore.urlToId[item] = History.urlToId[item];
1900 for ( item in History.stateToId ) {
1901 if ( !History.stateToId.hasOwnProperty(item) ) {
1904 currentStore.stateToId[item] = History.stateToId[item];
1908 History.store = currentStore;
1909 History.normalizeStore();
1911 // In Safari, going into Private Browsing mode causes the
1912 // Session Storage object to still exist but if you try and use
1913 // or set any property/function of it it throws the exception
1914 // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
1915 // add something to storage that exceeded the quota." infinitely
1917 currentStoreString = JSON.stringify(currentStore);
1920 sessionStorage.setItem('History.store', currentStoreString);
1923 if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
1924 if (sessionStorage.length) {
1925 // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
1926 // removing/resetting the storage can work.
1927 sessionStorage.removeItem('History.store');
1928 sessionStorage.setItem('History.store', currentStoreString);
1930 // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
1938 // For Internet Explorer
1939 History.intervalList.push(setInterval(History.onUnload,this.storeInterval));
1941 // For Other Browsers
1942 History.Adapter.bind(window,'beforeunload',History.onUnload);
1943 History.Adapter.bind(window,'unload',History.onUnload);
1945 // Both are enabled for consistency
1948 // Non-Native pushState Implementation
1949 if ( !History.emulated.pushState ) {
1950 // Be aware, the following is only for native pushState implementations
1951 // If you are wanting to include something for all browsers
1952 // Then include it above this if block
1957 if ( History.bugs.safariPoll ) {
1958 History.intervalList.push(setInterval(History.safariStatePoll, this.safariPollInterval));
1962 * Ensure Cross Browser Compatibility
1964 if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
1966 * Fix Safari HashChange Issue
1970 History.Adapter.bind(window,'hashchange',function(){
1971 History.Adapter.trigger(window,'popstate');
1975 if ( History.getHash() ) {
1976 History.Adapter.onDomLoad(function(){
1977 History.Adapter.trigger(window,'hashchange');
1982 } // !History.emulated.pushState
1985 }; // History.initCore
1987 // Try to Initialise History
1988 if (!History.options || !History.options.delayInit) {