Roo/History.js
[roojs1] / Roo / History.js
1 /**
2  * Originally based of this code... - refactored for Roo...
3  *
4  * History.js Core
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/>
8  */
9
10
11 // this is not initialized automatically..
12 // must call Roo.History.init( { ... options... });
13
14
15 Roo.History = new Roo.util.Observable({
16          
17      
18     // ====================================================================
19         // Options
20
21     
22     /**
23      * hashChangeInterval
24      * How long should the interval be before hashchange checks
25      */
26     thishashChangeInterval  : 100,
27     
28     /**
29      * safariPollInterval
30      * How long should the interval be before safari poll checks
31      */
32     safariPollInterval : 500,
33     
34     /**
35      * doubleCheckInterval
36      * How long should the interval be before we perform a double check
37      */
38     doubleCheckInterval : 500,
39     
40     /**
41      * disableSuid
42      * Force this.not to append suid
43      */
44     disableSuid : false,
45     
46     /**
47      * storeInterval
48      * How long should we wait between store calls
49      */
50     storeInterval : 1000,
51     
52     /**
53      * busyDelay
54      * How long should we wait between busy events
55      */
56     busyDelay : 250,
57     
58     /**
59      * debug
60      * If true will enable debug messages to be logged
61      */
62     debug : false,
63     
64     /**
65      * initialTitle
66      * What is the title of the initial state
67      */
68     initialTitle : '',
69     
70     /**
71      * html4Mode
72      * If true, will force HTMl4 mode (hashtags)
73      */
74     html4Mode : false,
75     
76     /**
77      * delayInit
78      * Want to override default options and call init manually.
79      */
80     delayInit : false,
81     
82     /**
83     * History.bugs
84     * Which bugs are present
85     */
86     bugs : {
87         /**
88         * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
89         * https://bugs.webkit.org/show_bug.cgi?id=56249
90         */
91         setHash: false,
92
93        /**
94         * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
95         * https://bugs.webkit.org/show_bug.cgi?id=42940
96         */
97        safariPoll: false,
98
99        /**
100         * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
101         */
102        ieDoubleCheck: false,
103
104        /**
105         * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
106         */
107        hashEscape: false,
108     },
109      
110         // ========================================================================
111         // Initialise
112
113         // Localise Globals
114         
115                   
116         sessionStorage : false, // sessionStorage
117                  
118     intervalList : false, // array normally.
119     /**
120     * enabled
121     * Is History enabled?
122     */
123     enabled : false,
124
125     // ====================================================================
126                 // State Storage
127
128     /**
129      * store
130      * The store for all session specific data
131      */
132     store :false,
133
134     /**
135      * idToState
136      * 1-1: State ID to State Object
137      */
138     idToState : false,
139
140     /**
141      * stateToId
142      * 1-1: State String to State ID
143      */
144     stateToId :false,
145
146     /**
147      * urlToId
148      * 1-1: State URL to State ID
149      */
150     urlToId : false,
151
152     /**
153      * storedStates
154      * Store the states in an array
155      */
156     storedStates : false,
157
158     /**
159      * savedStates
160      * Saved the states in an array
161      */
162     savedStates : false,
163
164    /**
165     * queues
166     * The list of queues to use
167     * First In, First Out
168     */
169     queues : [],
170
171     /**
172     * busy.flag
173     */
174     busy_flag : false,
175     
176     // ====================================================================
177     // IE Bug Fix
178
179     /**
180      * History.stateChanged
181      * States whether or not the state has changed since the last double check was initialised
182      */
183     stateChanged : false,
184
185     /**
186      * History.doubleChecker
187      * Contains the timeout used for the double checks
188      */
189     doubleChecker : false,
190     
191     
192         // Initialise History
193         init : function(options)
194     {
195         
196         var emptyFunction = function(){};
197         var _this = this;
198         
199         
200         this.initialTitle = window.document.title;
201         this.store = {};
202         this.idToState={};
203                 this.stateToId={};
204                 this.urlToId={};
205                 this.storedStates=[];
206                 this.savedStates=[];
207         this.queues = [];
208         
209         Roo.apply(this,options)
210         
211                 // Check Load Status of Adapter
212                 //if ( typeof this.Adapter === 'undefined' ) {
213                 //      return false;
214                 //}
215
216                 // Check Load Status of Core
217                 if ( typeof this.initCore !== 'undefined' ) {
218                         this.initCore();
219                 }
220
221                 // Check Load Status of HTML4 Support
222                 if ( typeof this.initHtml4 !== 'undefined' ) {
223                         this.initHtml4();
224                 }
225         
226         this.initEmulated();
227         
228        
229                 this.enabled = !this.emulated.pushState;
230
231         if ( this.emulated.pushState ) {
232                          
233                         // Prepare
234                         
235                         this.pushState =  emptyFunction;
236                         this.replaceState = emptyFunction;
237                 }   
238
239         this.initBugs();
240         
241         
242         
243         
244         this.Adapter.bind(window,'popstate',this.onPopState);
245         
246          
247                 /**
248                  * Load the Store
249                  */
250                 if ( this.sessionStorage ) {
251                         // Fetch
252                         try {
253                                 this.store = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
254                         }
255                         catch ( err ) {
256                                 this.store = {};
257                         }
258             this.intervalList.push(setInterval(this.onUnload,this.storeInterval));
259
260                         // For Other Browsers
261                         this.Adapter.bind(window,'beforeunload',this.onUnload);
262                         this.Adapter.bind(window,'unload',this.onUnload);
263
264                 } else {
265             this.onUnload = emptyFunction;
266         }
267         
268         
269         this.normalizeStore();
270                 /**
271                  * Clear Intervals on exit to prevent memory leaks
272                  */
273                 this.Adapter.bind(window,"unload",this.clearAllIntervals);
274
275                 /**
276                  * Create the initial State
277                  */
278                 this.saveState(this.storeState(this.extractState(this.getLocationHref(),true)));
279
280         
281         
282         // Non-Native pushState Implementation
283                 if ( !this.emulated.pushState ) {
284                         // Be aware, the following is only for native pushState implementations
285                         // If you are wanting to include something for all browsers
286                         // Then include it above this if block
287
288                         /**
289                          * Setup Safari Fix
290                          */
291                         if ( this.bugs.safariPoll ) {
292                                 this.intervalList.push(setInterval(this.safariStatePoll, this.safariPollInterval));
293                         }
294
295                         /**
296                          * Ensure Cross Browser Compatibility
297                          */
298                         //if ( window.navigator.vendor === 'Apple Computer, Inc.' || (window.navigator.appCodeName||'') === 'Mozilla' ) {
299             if (Roo.isSafari) {
300                 //code
301             
302                                 /**
303                                  * Fix Safari HashChange Issue
304                                  */
305
306                                 // Setup Alias
307                                 this.Adapter.bind(window,'hashchange',function(){
308                                         Roo.get(window).fireEvent('popstate');
309                                 });
310
311                                 // Initialise Alias
312                                 if ( this.getHash() ) {
313                                         Roo.onReady(function(){
314                                                 Roo.get(window).fireEvent('hashchange');
315                                         });
316                                 }
317                         }
318
319                 } // !History.emulated.pushState
320
321        
322         
323
324                 // Return true
325                 return true;
326         },
327
328
329         // ========================================================================
330         // Initialise Core
331
332         // Initialise Core
333         initCore : function(options){
334                 // Initialise
335         this.intervalList = [];
336         
337         
338         try {
339             this.sessionStorage = window.sessionStorage; // This will throw an exception in some browsers when cookies/localStorage are explicitly disabled (i.e. Chrome)
340             this.sessionStorage.setItem('TEST', '1');
341             this.sessionStorage.removeItem('TEST');
342         } catch(e) {
343             this.sessionStorage = false;
344         }
345         
346         
347                 if ( typeof this.initCore.initialized !== 'undefined' ) {
348                         // Already Loaded
349                         return false;
350                 }
351                 else {
352                         this.initCore.initialized = true;
353                 }
354         
355
356     },
357                  
358
359     /**
360      * clearAllIntervals
361      * Clears all setInterval instances.
362      */
363     clearAllIntervals: function()
364     {
365         var i, il = this.intervalList;
366         if (typeof il !== "undefined" && il !== null) {
367             for (i = 0; i < il.length; i++) {
368                 clearInterval(il[i]);
369             }
370             this.intervalList = null;
371         }
372     },
373
374
375     // ====================================================================
376     // Debug
377
378     /**
379      * debugLog(message,...)
380      * Logs the passed arguments if debug enabled
381      */
382     debugLog : function()
383     {
384         if ( (this.debug||false) ) {
385             Roo.log.apply(this,arguments);
386         }
387     },
388
389                  
390
391     // ====================================================================
392     // Emulated Status
393
394     /**
395      * getInternetExplorerMajorVersion()
396      * Get's the major version of Internet Explorer
397      * @return {integer}
398      * @license Public Domain
399      * @author Benjamin Arthur Lupton <contact@balupton.com>
400      * @author James Padolsey <https://gist.github.com/527683>
401      */
402     getInternetExplorerMajorVersion : function(){
403         var result = this.getInternetExplorerMajorVersion.cached =
404                 (typeof this.getInternetExplorerMajorVersion.cached !== 'undefined')
405             ?   this.getInternetExplorerMajorVersion.cached
406             :   (function(){
407                     var v = 3,
408                             div = window.document.createElement('div'),
409                             all = div.getElementsByTagName('i');
410                     while ( (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->') && all[0] ) {}
411                     return (v > 4) ? v : false;
412                 })()
413             ;
414         return result;
415     },
416
417     /**
418      * isInternetExplorer()
419      * Are we using Internet Explorer?
420      * @return {boolean}
421      * @license Public Domain
422      * @author Benjamin Arthur Lupton <contact@balupton.com>
423      */
424     isInternetExplorer : function(){
425         var result =
426             this.isInternetExplorer.cached =
427             (typeof this.isInternetExplorer.cached !== 'undefined')
428                 ?       this.isInternetExplorer.cached
429                 :       Boolean(this.getInternetExplorerMajorVersion())
430             ;
431         return result;
432     },
433     /**
434      * emulated
435      * Which features require emulating?
436      */
437
438     emulated : {
439         pushState : true,
440         hashChange: true
441     },
442     
443     initEmulated : function()
444     {
445     
446         
447                 if (this.html4Mode) {
448                         return;
449                 }
450
451                 
452
453         this.emulated.pushState = !Boolean(
454                                         window.history && window.history.pushState && window.history.replaceState
455                                         && !(
456                                                 (/ 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) */
457                                                 || (/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 */
458                                         )
459                                 );
460         this.emulated.hashChange = Boolean(
461                                         !(('onhashchange' in window) || ('onhashchange' in window.document))
462                                         ||
463                                         (this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8)
464                                 );
465                         
466         },
467
468         initBugs : function ()
469     {
470         
471         
472        
473         /**
474          * Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a `replaceState` call
475          * https://bugs.webkit.org/show_bug.cgi?id=56249
476          */
477         this.bugs.setHash = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
478                                     && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
479
480         /**
481          * Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions
482          * https://bugs.webkit.org/show_bug.cgi?id=42940
483          */
484         this.bugs.safariPoll = Boolean(!this.emulated.pushState && navigator.vendor === 'Apple Computer, Inc.'
485                                        && /AppleWebKit\/5([0-2]|3[0-3])/.test(navigator.userAgent));
486
487         /**
488          * MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
489          */
490         this.bugs.ieDoubleCheck = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 8);
491
492         /**
493          * MSIE 6 requires the entire hash to be encoded for the hashes to trigger the onHashChange event
494          */
495         this.bugs.hashEscape = Boolean(this.isInternetExplorer() && this.getInternetExplorerMajorVersion() < 7);
496     },
497     
498
499     /**
500      * isEmptyObject(obj)
501      * Checks to see if the Object is Empty
502      * @param {Object} obj
503      * @return {boolean}
504      */
505     isEmptyObject : function(obj) {
506         for ( var name in obj ) {
507             if ( obj.hasOwnProperty(name) ) {
508                 return false;
509             }
510         }
511         return true;
512     },
513
514     /**
515      * cloneObject(obj)
516      * Clones a object and eliminate all references to the original contexts
517      * @param {Object} obj
518      * @return {Object}
519      */
520     cloneObject : function(obj) {
521         var hash,newObj;
522         if ( obj ) {
523             hash = JSON.stringify(obj);
524             newObj = JSON.parse(hash);
525         }
526         else {
527             newObj = {};
528         }
529         return newObj;
530     },
531
532
533     // ====================================================================
534     // URL Helpers
535
536     /**
537      * getRootUrl()
538      * Turns "http://mysite.com/dir/page.html?asd" into "http://mysite.com"
539      * @return {String} rootUrl
540      */
541     getRootUrl : function(){
542         // Create
543         var rootUrl = window.document.location.protocol+'//'+(window.document.location.hostname||window.document.location.host);
544         if ( window.document.location.port||false ) {
545             rootUrl += ':'+window.document.location.port;
546         }
547         rootUrl += '/';
548
549         // Return
550         return rootUrl;
551     },
552
553     /**
554      * getBaseHref()
555      * Fetches the `href` attribute of the `<base href="...">` element if it exists
556      * @return {String} baseHref
557      */
558     getBaseHref : function(){
559         // Create
560         var
561             baseElements = window.document.getElementsByTagName('base'),
562             baseElement = null,
563             baseHref = '';
564
565         // Test for Base Element
566         if ( baseElements.length === 1 ) {
567             // Prepare for Base Element
568             baseElement = baseElements[0];
569             baseHref = baseElement.href.replace(/[^\/]+$/,'');
570         }
571
572         // Adjust trailing slash
573         baseHref = baseHref.replace(/\/+$/,'');
574         if ( baseHref ) baseHref += '/';
575
576         // Return
577         return baseHref;
578     },
579
580     /**
581      * getBaseUrl()
582      * Fetches the baseHref or basePageUrl or rootUrl (whichever one exists first)
583      * @return {String} baseUrl
584      */
585     getBaseUrl : function(){
586         // Create
587         var baseUrl = this.getBaseHref()||this.getBasePageUrl()||this.getRootUrl();
588
589         // Return
590         return baseUrl;
591     },
592
593     /**
594      * getPageUrl()
595      * Fetches the URL of the current page
596      * @return {String} pageUrl
597      */
598     getPageUrl : function(){
599         // Fetch
600         var
601             State = this.getState(false,false),
602             stateUrl = (State||{}).url||this.getLocationHref(),
603             pageUrl;
604
605         // Create
606         pageUrl = stateUrl.replace(/\/+$/,'').replace(/[^\/]+$/,function(part,index,string){
607             return (/\./).test(part) ? part : part+'/';
608         });
609
610         // Return
611         return pageUrl;
612     },
613
614     /**
615      * getBasePageUrl()
616      * Fetches the Url of the directory of the current page
617      * @return {String} basePageUrl
618      */
619     getBasePageUrl : function(){
620         // Create
621         var basePageUrl = (this.getLocationHref()).replace(/[#\?].*/,'').replace(/[^\/]+$/,function(part,index,string){
622             return (/[^\/]$/).test(part) ? '' : part;
623         }).replace(/\/+$/,'')+'/';
624
625         // Return
626         return basePageUrl;
627     },
628
629     /**
630      * getFullUrl(url)
631      * Ensures that we have an absolute URL and not a relative URL
632      * @param {string} url
633      * @param {Boolean} allowBaseHref
634      * @return {string} fullUrl
635      */
636     getFullUrl : function(url,allowBaseHref){
637         // Prepare
638         var fullUrl = url, firstChar = url.substring(0,1);
639         allowBaseHref = (typeof allowBaseHref === 'undefined') ? true : allowBaseHref;
640
641         // Check
642         if ( /[a-z]+\:\/\//.test(url) ) {
643             // Full URL
644         }
645         else if ( firstChar === '/' ) {
646             // Root URL
647             fullUrl = this.getRootUrl()+url.replace(/^\/+/,'');
648         }
649         else if ( firstChar === '#' ) {
650             // Anchor URL
651             fullUrl = this.getPageUrl().replace(/#.*/,'')+url;
652         }
653         else if ( firstChar === '?' ) {
654             // Query URL
655             fullUrl = this.getPageUrl().replace(/[\?#].*/,'')+url;
656         }
657         else {
658             // Relative URL
659             if ( allowBaseHref ) {
660                 fullUrl = this.getBaseUrl()+url.replace(/^(\.\/)+/,'');
661             } else {
662                 fullUrl = this.getBasePageUrl()+url.replace(/^(\.\/)+/,'');
663             }
664             // We have an if condition above as we do not want hashes
665             // which are relative to the baseHref in our URLs
666             // as if the baseHref changes, then all our bookmarks
667             // would now point to different locations
668             // whereas the basePageUrl will always stay the same
669         }
670
671         // Return
672         return fullUrl.replace(/\#$/,'');
673     },
674
675     /**
676      * getShortUrl(url)
677      * Ensures that we have a relative URL and not a absolute URL
678      * @param {string} url
679      * @return {string} url
680      */
681     getShortUrl : function(url){
682         // Prepare
683         var shortUrl = url, baseUrl = this.getBaseUrl(), rootUrl = this.getRootUrl();
684
685         // Trim baseUrl
686         if ( this.emulated.pushState ) {
687             // We are in a if statement as when pushState is not emulated
688             // The actual url these short urls are relative to can change
689             // So within the same session, we the url may end up somewhere different
690             shortUrl = shortUrl.replace(baseUrl,'');
691         }
692
693         // Trim rootUrl
694         shortUrl = shortUrl.replace(rootUrl,'/');
695
696         // Ensure we can still detect it as a state
697         if ( this.isTraditionalAnchor(shortUrl) ) {
698             shortUrl = './'+shortUrl;
699         }
700
701         // Clean It
702         shortUrl = shortUrl.replace(/^(\.\/)+/g,'./').replace(/\#$/,'');
703
704         // Return
705         return shortUrl;
706     },
707
708     /**
709      * getLocationHref(document)
710      * Returns a normalized version of document.location.href
711      * accounting for browser inconsistencies, etc.
712      *
713      * This URL will be URI-encoded and will include the hash
714      *
715      * @param {object} document
716      * @return {string} url
717      */
718     getLocationHref : function(doc) {
719         doc = doc || window.document;
720
721         // most of the time, this will be true
722         if (doc.URL === doc.location.href)
723             return doc.location.href;
724
725         // some versions of webkit URI-decode document.location.href
726         // but they leave document.URL in an encoded state
727         if (doc.location.href === decodeURIComponent(doc.URL))
728             return doc.URL;
729
730         // FF 3.6 only updates document.URL when a page is reloaded
731         // document.location.href is updated correctly
732         if (doc.location.hash && decodeURIComponent(doc.location.href.replace(/^[^#]+/, "")) === doc.location.hash)
733             return doc.location.href;
734
735         if (doc.URL.indexOf('#') == -1 && doc.location.href.indexOf('#') != -1)
736             return doc.location.href;
737         
738         return doc.URL || doc.location.href;
739     },
740
741
742                 
743     /**
744      * noramlizeStore()
745      * Noramlize the store by adding necessary values
746      */
747     normalizeStore : function()
748     {
749         
750         this.store.idToState = this.store.idToState||{};
751         this.store.urlToId = this.store.urlToId||{};
752         this.store.stateToId = this.store.stateToId||{};
753     },
754
755     /**
756      * getState()
757      * Get an object containing the data, title and url of the current state
758      * @param {Boolean} friendly
759      * @param {Boolean} create
760      * @return {Object} State
761      */
762     getState : function(friendly,create){
763         // Prepare
764         if ( typeof friendly === 'undefined' ) { friendly = true; }
765         if ( typeof create === 'undefined' ) { create = true; }
766
767         // Fetch
768         var State = this.getLastSavedState();
769
770         // Create
771         if ( !State && create ) {
772             State = this.createStateObject();
773         }
774
775         // Adjust
776         if ( friendly ) {
777             State = this.cloneObject(State);
778             State.url = this.cleanUrl||State.url;
779         }
780
781         // Return
782         return State;
783     },
784
785     /**
786      * getIdByState(State)
787      * Gets a ID for a State
788      * @param {State} newState
789      * @return {String} id
790      */
791     getIdByState : function(newState){
792
793         // Fetch ID
794         var id = this.extractId(newState.url),
795             str;
796
797         if ( !id ) {
798             // Find ID via State String
799             str = this.getStateString(newState);
800             if ( typeof this.stateToId[str] !== 'undefined' ) {
801                 id = this.stateToId[str];
802             }
803             else if ( typeof this.store.stateToId[str] !== 'undefined' ) {
804                 id = this.store.stateToId[str];
805             }
806             else {
807                 // Generate a new ID
808                 while ( true ) {
809                     id = (new Date()).getTime() + String(Math.random()).replace(/\D/g,'');
810                     if ( typeof this.idToState[id] === 'undefined' && typeof this.store.idToState[id] === 'undefined' ) {
811                         break;
812                     }
813                 }
814
815                 // Apply the new State to the ID
816                 this.stateToId[str] = id;
817                 this.idToState[id] = newState;
818             }
819         }
820
821         // Return ID
822         return id;
823     },
824
825     /**
826      * normalizeState(State)
827      * Expands a State Object
828      * @param {object} State
829      * @return {object}
830      */
831     normalizeState : function(oldState){
832         // Variables
833         var newState, dataNotEmpty;
834
835         // Prepare
836         if ( !oldState || (typeof oldState !== 'object') ) {
837             oldState = {};
838         }
839
840         // Check
841         if ( typeof oldState.normalized !== 'undefined' ) {
842             return oldState;
843         }
844
845         // Adjust
846         if ( !oldState.data || (typeof oldState.data !== 'object') ) {
847             oldState.data = {};
848         }
849
850         // ----------------------------------------------------------------
851
852         // Create
853         newState = {};
854         newState.normalized = true;
855         newState.title = oldState.title||'';
856         newState.url = this.getFullUrl(oldState.url?oldState.url:(this.getLocationHref()));
857         newState.hash = this.getShortUrl(newState.url);
858         newState.data = this.cloneObject(oldState.data);
859
860         // Fetch ID
861         newState.id = this.getIdByState(newState);
862
863         // ----------------------------------------------------------------
864
865         // Clean the URL
866         newState.cleanUrl = newState.url.replace(/\??\&_suid.*/,'');
867         newState.url = newState.cleanUrl;
868
869         // Check to see if we have more than just a url
870         dataNotEmpty = !this.isEmptyObject(newState.data);
871
872         // Apply
873         if ( (newState.title || dataNotEmpty) && this.disableSuid !== true ) {
874             // Add ID to Hash
875             newState.hash = this.getShortUrl(newState.url).replace(/\??\&_suid.*/,'');
876             if ( !/\?/.test(newState.hash) ) {
877                 newState.hash += '?';
878             }
879             newState.hash += '&_suid='+newState.id;
880         }
881
882         // Create the Hashed URL
883         newState.hashedUrl = this.getFullUrl(newState.hash);
884
885         // ----------------------------------------------------------------
886
887         // Update the URL if we have a duplicate
888         if ( (this.emulated.pushState || this.bugs.safariPoll) && this.hasUrlDuplicate(newState) ) {
889             newState.url = newState.hashedUrl;
890         }
891
892         // ----------------------------------------------------------------
893
894         // Return
895         return newState;
896     },
897
898     /**
899      * createStateObject(data,title,url)
900      * Creates a object based on the data, title and url state params
901      * @param {object} data
902      * @param {string} title
903      * @param {string} url
904      * @return {object}
905      */
906     createStateObject : function(data,title,url){
907         // Hashify
908         var State = {
909             'data': data,
910             'title': title,
911             'url': url
912         };
913
914         // Expand the State
915         State = this.normalizeState(State);
916
917         // Return object
918         return State;
919     },
920
921     /**
922      * getStateById(id)
923      * Get a state by it's UID
924      * @param {String} id
925      */
926     getStateById : function(id){
927         // Prepare
928         id = String(id);
929
930         // Retrieve
931         var State = this.idToState[id] || this.store.idToState[id] || undefined;
932
933         // Return State
934         return State;
935     },
936
937     /**
938      * Get a State's String
939      * @param {State} passedState
940      */
941     getStateString : function(passedState){
942         // Prepare
943         var State, cleanedState, str;
944
945         // Fetch
946         State = this.normalizeState(passedState);
947
948         // Clean
949         cleanedState = {
950             data: State.data,
951             title: passedState.title,
952             url: passedState.url
953         };
954
955         // Fetch
956         str = JSON.stringify(cleanedState);
957
958         // Return
959         return str;
960     },
961
962     /**
963      * Get a State's ID
964      * @param {State} passedState
965      * @return {String} id
966      */
967     getStateId : function(passedState){
968         // Prepare
969         var State, id;
970
971         // Fetch
972         State = this.normalizeState(passedState);
973
974         // Fetch
975         id = State.id;
976
977         // Return
978         return id;
979     },
980
981     /**
982      * getHashByState(State)
983      * Creates a Hash for the State Object
984      * @param {State} passedState
985      * @return {String} hash
986      */
987     getHashByState : function(passedState){
988         // Prepare
989         var State, hash;
990
991         // Fetch
992         State = this.normalizeState(passedState);
993
994         // Hash
995         hash = State.hash;
996
997         // Return
998         return hash;
999     },
1000
1001     /**
1002      * extractId(url_or_hash)
1003      * Get a State ID by it's URL or Hash
1004      * @param {string} url_or_hash
1005      * @return {string} id
1006      */
1007     extractId : function ( url_or_hash ) {
1008         // Prepare
1009         var id,parts,url, tmp;
1010
1011         // Extract
1012         
1013         // If the URL has a #, use the id from before the #
1014         if (url_or_hash.indexOf('#') != -1)
1015         {
1016             tmp = url_or_hash.split("#")[0];
1017         }
1018         else
1019         {
1020             tmp = url_or_hash;
1021         }
1022         
1023         parts = /(.*)\&_suid=([0-9]+)$/.exec(tmp);
1024         url = parts ? (parts[1]||url_or_hash) : url_or_hash;
1025         id = parts ? String(parts[2]||'') : '';
1026
1027         // Return
1028         return id||false;
1029     },
1030
1031     /**
1032      * isTraditionalAnchor
1033      * Checks to see if the url is a traditional anchor or not
1034      * @param {String} url_or_hash
1035      * @return {Boolean}
1036      */
1037     isTraditionalAnchor : function(url_or_hash){
1038         // Check
1039         var isTraditional = !(/[\/\?\.]/.test(url_or_hash));
1040
1041         // Return
1042         return isTraditional;
1043     },
1044
1045     /**
1046      * extractState
1047      * Get a State by it's URL or Hash
1048      * @param {String} url_or_hash
1049      * @return {State|null}
1050      */
1051     extractState : function(url_or_hash,create){
1052         // Prepare
1053         var State = null, id, url;
1054         create = create||false;
1055
1056         // Fetch SUID
1057         id = this.extractId(url_or_hash);
1058         if ( id ) {
1059             State = this.getStateById(id);
1060         }
1061
1062         // Fetch SUID returned no State
1063         if ( !State ) {
1064             // Fetch URL
1065             url = this.getFullUrl(url_or_hash);
1066
1067             // Check URL
1068             id = this.getIdByUrl(url)||false;
1069             if ( id ) {
1070                 State = this.getStateById(id);
1071             }
1072
1073             // Create State
1074             if ( !State && create && !this.isTraditionalAnchor(url_or_hash) ) {
1075                 State = this.createStateObject(null,null,url);
1076             }
1077         }
1078
1079         // Return
1080         return State;
1081     },
1082
1083     /**
1084      * getIdByUrl()
1085      * Get a State ID by a State URL
1086      */
1087     getIdByUrl : function(url){
1088         // Fetch
1089         var id = this.urlToId[url] || this.store.urlToId[url] || undefined;
1090
1091         // Return
1092         return id;
1093     },
1094
1095     /**
1096      * getLastSavedState()
1097      * Get an object containing the data, title and url of the current state
1098      * @return {Object} State
1099      */
1100     getLastSavedState : function(){
1101         return this.savedStates[this.savedStates.length-1]||undefined;
1102     },
1103
1104     /**
1105      * getLastStoredState()
1106      * Get an object containing the data, title and url of the current state
1107      * @return {Object} State
1108      */
1109     getLastStoredState : function(){
1110         return this.storedStates[this.storedStates.length-1]||undefined;
1111     },
1112
1113     /**
1114      * hasUrlDuplicate
1115      * Checks if a Url will have a url conflict
1116      * @param {Object} newState
1117      * @return {Boolean} hasDuplicate
1118      */
1119     hasUrlDuplicate : function(newState) {
1120         // Prepare
1121         var hasDuplicate = false,
1122             oldState;
1123
1124         // Fetch
1125         oldState = this.extractState(newState.url);
1126
1127         // Check
1128         hasDuplicate = oldState && oldState.id !== newState.id;
1129
1130         // Return
1131         return hasDuplicate;
1132     },
1133
1134     /**
1135      * storeState
1136      * Store a State
1137      * @param {Object} newState
1138      * @return {Object} newState
1139      */
1140     storeState : function(newState){
1141         // Store the State
1142         this.urlToId[newState.url] = newState.id;
1143
1144         // Push the State
1145         this.storedStates.push(this.cloneObject(newState));
1146
1147         // Return newState
1148         return newState;
1149     },
1150
1151     /**
1152      * isLastSavedState(newState)
1153      * Tests to see if the state is the last state
1154      * @param {Object} newState
1155      * @return {boolean} isLast
1156      */
1157     isLastSavedState : function(newState){
1158         // Prepare
1159         var isLast = false,
1160             newId, oldState, oldId;
1161
1162         // Check
1163         if ( this.savedStates.length ) {
1164             newId = newState.id;
1165             oldState = this.getLastSavedState();
1166             oldId = oldState.id;
1167
1168             // Check
1169             isLast = (newId === oldId);
1170         }
1171
1172         // Return
1173         return isLast;
1174     },
1175
1176     /**
1177      * saveState
1178      * Push a State
1179      * @param {Object} newState
1180      * @return {boolean} changed
1181      */
1182     saveState : function(newState){
1183         // Check Hash
1184         if ( this.isLastSavedState(newState) ) {
1185             return false;
1186         }
1187
1188         // Push the State
1189         this.savedStates.push(this.cloneObject(newState));
1190
1191         // Return true
1192         return true;
1193     },
1194
1195     /**
1196      * getStateByIndex()
1197      * Gets a state by the index
1198      * @param {integer} index
1199      * @return {Object}
1200      */
1201     getStateByIndex : function(index){
1202         // Prepare
1203         var State = null;
1204
1205         // Handle
1206         if ( typeof index === 'undefined' ) {
1207             // Get the last inserted
1208             State = this.savedStates[this.savedStates.length-1];
1209         }
1210         else if ( index < 0 ) {
1211             // Get from the end
1212             State = this.savedStates[this.savedStates.length+index];
1213         }
1214         else {
1215             // Get from the beginning
1216             State = this.savedStates[index];
1217         }
1218
1219         // Return State
1220         return State;
1221     },
1222     
1223     /**
1224      * getCurrentIndex()
1225      * Gets the current index
1226      * @return (integer)
1227     */
1228     getCurrentIndex : function(){
1229         // Prepare
1230         var index = null;
1231         
1232         // No states saved
1233         if(this.savedStates.length < 1) {
1234             index = 0;
1235         }
1236         else {
1237             index = this.savedStates.length-1;
1238         }
1239         return index;
1240     },
1241
1242     // ====================================================================
1243     // Hash Helpers
1244
1245     /**
1246      * getHash()
1247      * @param {Location=} location
1248      * Gets the current document hash
1249      * Note: unlike location.hash, this is guaranteed to return the escaped hash in all browsers
1250      * @return {string}
1251      */
1252     getHash : function(doc){
1253         var url = this.getLocationHref(doc),
1254             hash;
1255         hash = this.getHashByUrl(url);
1256         return hash;
1257     },
1258
1259     /**
1260      * unescapeHash()
1261      * normalize and Unescape a Hash
1262      * @param {String} hash
1263      * @return {string}
1264      */
1265     unescapeHash : function(hash){
1266         // Prepare
1267         var result = this.normalizeHash(hash);
1268
1269         // Unescape hash
1270         result = decodeURIComponent(result);
1271
1272         // Return result
1273         return result;
1274     },
1275
1276     /**
1277      * normalizeHash()
1278      * normalize a hash across browsers
1279      * @return {string}
1280      */
1281     normalizeHash : function(hash){
1282         // Prepare
1283         var result = hash.replace(/[^#]*#/,'').replace(/#.*/, '');
1284
1285         // Return result
1286         return result;
1287     },
1288
1289     /**
1290      * setHash(hash)
1291      * Sets the document hash
1292      * @param {string} hash
1293      * @return {Roo.History}
1294      */
1295     setHash : function(hash,queue){
1296         // Prepare
1297         var State, pageUrl;
1298
1299         // Handle Queueing
1300         if ( queue !== false && this.busy() ) {
1301             // Wait + Push to Queue
1302             //this.debug('this.setHash: we must wait', arguments);
1303             this.pushQueue({
1304                 scope: this,
1305                 callback: this.setHash,
1306                 args: arguments,
1307                 queue: queue
1308             });
1309             return false;
1310         }
1311
1312         // Log
1313         //this.debug('this.setHash: called',hash);
1314
1315         // Make Busy + Continue
1316         this.busy(true);
1317
1318         // Check if hash is a state
1319         State = this.extractState(hash,true);
1320         if ( State && !this.emulated.pushState ) {
1321             // Hash is a state so skip the setHash
1322             //this.debug('this.setHash: Hash is a state so skipping the hash set with a direct pushState call',arguments);
1323
1324             // PushState
1325             this.pushState(State.data,State.title,State.url,false);
1326         }
1327         else if ( this.getHash() !== hash ) {
1328             // Hash is a proper hash, so apply it
1329
1330             // Handle browser bugs
1331             if ( this.bugs.setHash ) {
1332                 // Fix Safari Bug https://bugs.webkit.org/show_bug.cgi?id=56249
1333
1334                 // Fetch the base page
1335                 pageUrl = this.getPageUrl();
1336
1337                 // Safari hash apply
1338                 this.pushState(null,null,pageUrl+'#'+hash,false);
1339             }
1340             else {
1341                 // Normal hash apply
1342                 window.document.location.hash = hash;
1343             }
1344         }
1345
1346         // Chain
1347         return this;
1348     },
1349
1350     /**
1351      * escape()
1352      * normalize and Escape a Hash
1353      * @return {string}
1354      */
1355     escapeHash : function(hash){
1356         // Prepare
1357         var result = normalizeHash(hash);
1358
1359         // Escape hash
1360         result = window.encodeURIComponent(result);
1361
1362         // IE6 Escape Bug
1363         if ( !this.bugs.hashEscape ) {
1364             // Restore common parts
1365             result = result
1366                 .replace(/\%21/g,'!')
1367                 .replace(/\%26/g,'&')
1368                 .replace(/\%3D/g,'=')
1369                 .replace(/\%3F/g,'?');
1370         }
1371
1372         // Return result
1373         return result;
1374     },
1375
1376     /**
1377      * getHashByUrl(url)
1378      * Extracts the Hash from a URL
1379      * @param {string} url
1380      * @return {string} url
1381      */
1382     getHashByUrl : function(url){
1383         // Extract the hash
1384         var hash = String(url)
1385             .replace(/([^#]*)#?([^#]*)#?(.*)/, '$2')
1386             ;
1387
1388         // Unescape hash
1389         hash = this.unescapeHash(hash);
1390
1391         // Return hash
1392         return hash;
1393     },
1394
1395     /**
1396      * setTitle(title)
1397      * Applies the title to the document
1398      * @param {State} newState
1399      * @return {Boolean}
1400      */
1401     setTitle : function(newState){
1402         // Prepare
1403         var title = newState.title,
1404             firstState;
1405
1406         // Initial
1407         if ( !title ) {
1408             firstState = this.getStateByIndex(0);
1409             if ( firstState && firstState.url === newState.url ) {
1410                 title = firstState.title||this.initialTitle;
1411             }
1412         }
1413
1414         // Apply
1415         try {
1416             window.document.getElementsByTagName('title')[0].innerHTML = title.replace('<','&lt;').replace('>','&gt;').replace(' & ',' &amp; ');
1417         }
1418         catch ( Exception ) { }
1419         window.document.title = title;
1420
1421         // Chain
1422         return this;
1423     },
1424
1425
1426     // ====================================================================
1427     // Queueing
1428
1429
1430     /**
1431      * busy(value)
1432      * @param {boolean} value [optional]
1433      * @return {boolean} busy
1434      */
1435     busy : function(value){
1436         // Apply
1437         
1438         var _this = this;
1439         if ( typeof value !== 'undefined' ) {
1440             //this.debug('this.busy: changing ['+(this.busy.flag||false)+'] to ['+(value||false)+']', this.queues.length);
1441             this.busy_flag = value;
1442         }
1443         // Default
1444         else if ( typeof this.busy_flag === 'undefined' ) {
1445             this.busy_flag = false;
1446         }
1447
1448         // Queue
1449         if ( !this.busy_flag ) {
1450             
1451             
1452             
1453             // Execute the next item in the queue
1454             window.clearTimeout(this.busy.timeout);
1455             var fireNext = function(){
1456                 var i, queue, item;
1457                 if ( _this.busy_flag ) return;
1458                 for ( i=_this.queues.length-1; i >= 0; --i ) {
1459                     queue = _this.queues[i];
1460                     if ( queue.length === 0 ) continue;
1461                     item = queue.shift();
1462                     _this.fireQueueItem(item);
1463                     _this.busy.timeout = window.setTimeout(fireNext,_this.busyDelay);
1464                 }
1465             };
1466             this.busy.timeout = window.setTimeout(fireNext,this.busyDelay);
1467         }
1468
1469         // Return
1470         return this.busy_flag;
1471     },
1472
1473     
1474
1475     /**
1476      * fireQueueItem(item)
1477      * Fire a Queue Item
1478      * @param {Object} item
1479      * @return {Mixed} result
1480      */
1481     fireQueueItem : function(item){
1482         return item.callback.apply(item.scope||this,item.args||[]);
1483     },
1484
1485     /**
1486      * pushQueue(callback,args)
1487      * Add an item to the queue
1488      * @param {Object} item [scope,callback,args,queue]
1489      */
1490     pushQueue : function(item){
1491         // Prepare the queue
1492         this.queues[item.queue||0] = this.queues[item.queue||0]||[];
1493
1494         // Add to the queue
1495         this.queues[item.queue||0].push(item);
1496
1497         // Chain
1498         return this;
1499     },
1500
1501     /**
1502      * queue (item,queue), (func,queue), (func), (item)
1503      * Either firs the item now if not busy, or adds it to the queue
1504      */
1505     queue : function(item,queue){
1506         // Prepare
1507         if ( typeof item === 'function' ) {
1508             item = {
1509                 callback: item
1510             };
1511         }
1512         if ( typeof queue !== 'undefined' ) {
1513             item.queue = queue;
1514         }
1515
1516         // Handle
1517         if ( this.busy() ) {
1518             this.pushQueue(item);
1519         } else {
1520             this.fireQueueItem(item);
1521         }
1522
1523         // Chain
1524         return this;
1525     },
1526
1527     /**
1528      * clearQueue()
1529      * Clears the Queue
1530      */
1531     clearQueue : function(){
1532         this.busy_flag = false;
1533         this.queues = [];
1534         return this;
1535     },
1536
1537
1538
1539     /**
1540      * doubleCheckComplete()
1541      * Complete a double check
1542      * @return {Roo.History}
1543      */
1544     doubleCheckComplete : function(){
1545         // Update
1546         this.stateChanged = true;
1547
1548         // Clear
1549         this.doubleCheckClear();
1550
1551         // Chain
1552         return this;;
1553     },
1554
1555     /**
1556      * doubleCheckClear()
1557      * Clear a double check
1558      * @return {Roo.History}
1559      */
1560     doubleCheckClear : function(){
1561         // Clear
1562         if ( this.doubleChecker ) {
1563             window.clearTimeout(this.doubleChecker);
1564             this.doubleChecker = false;
1565         }
1566
1567         // Chain
1568         return this;
1569     },
1570
1571     /**
1572      * doubleCheck()
1573      * Create a double check
1574      * @return {Roo.History}
1575      */
1576     doubleCheck : function(tryAgain)
1577     {
1578         var _this = this;
1579         // Reset
1580         this.stateChanged = false;
1581         this.doubleCheckClear();
1582
1583         // Fix IE6,IE7 bug where calling history.back or history.forward does not actually change the hash (whereas doing it manually does)
1584         // Fix Safari 5 bug where sometimes the state does not change: https://bugs.webkit.org/show_bug.cgi?id=42940
1585         if ( this.bugs.ieDoubleCheck ) {
1586             // Apply Check
1587             this.doubleChecker = window.setTimeout(
1588                 function(){
1589                     _this.doubleCheckClear();
1590                     if ( !_this.stateChanged ) {
1591                         //this.debug('History.doubleCheck: State has not yet changed, trying again', arguments);
1592                         // Re-Attempt
1593                         tryAgain();
1594                     }
1595                     return true;
1596                 },
1597                 this.doubleCheckInterval
1598             );
1599         }
1600
1601         // Chain
1602         return this;
1603     },
1604
1605
1606     // ====================================================================
1607     // Safari Bug Fix
1608
1609     /**
1610      * safariStatePoll()
1611      * Poll the current state
1612      * @return {Roo.History}
1613      */
1614     safariStatePoll : function(){
1615         // Poll the URL
1616
1617         // Get the Last State which has the new URL
1618         var
1619             urlState = this.extractState(this.getLocationHref()),
1620             newState;
1621
1622         // Check for a difference
1623         if ( !this.isLastSavedState(urlState) ) {
1624             newState = urlState;
1625         }
1626         else {
1627             return;
1628         }
1629
1630         // Check if we have a state with that url
1631         // If not create it
1632         if ( !newState ) {
1633             //this.debug('this.safariStatePoll: new');
1634             newState = this.createStateObject();
1635         }
1636
1637         // Apply the New State
1638         //this.debug('this.safariStatePoll: trigger');
1639         Roo.get(window).fireEvent('popstate');
1640
1641         // Chain
1642         return this;
1643     },
1644
1645
1646     // ====================================================================
1647     // State Aliases
1648
1649     /**
1650      * back(queue)
1651      * Send the browser history back one item
1652      * @param {Integer} queue [optional]
1653      */
1654     back : function(queue)
1655     {
1656         //this.debug('this.back: called', arguments);
1657         var _this = this;
1658         // Handle Queueing
1659         if ( queue !== false && this.busy() ) {
1660             // Wait + Push to Queue
1661             //this.debug('this.back: we must wait', arguments);
1662             this.pushQueue({
1663                 scope: this,
1664                 callback: this.back,
1665                 args: arguments,
1666                 queue: queue
1667             });
1668             return false;
1669         }
1670
1671         // Make Busy + Continue
1672         this.busy(true);
1673
1674         // Fix certain browser bugs that prevent the state from changing
1675         this.doubleCheck(function(){
1676             _this.back(false);
1677         });
1678
1679         // Go back
1680         history.go(-1);
1681
1682         // End back closure
1683         return true;
1684     },
1685
1686     /**
1687      * forward(queue)
1688      * Send the browser history forward one item
1689      * @param {Integer} queue [optional]
1690      */
1691     forward : function(queue){
1692         //this.debug('this.forward: called', arguments);
1693
1694         // Handle Queueing
1695         if ( queue !== false && this.busy() ) {
1696             // Wait + Push to Queue
1697             //this.debug('this.forward: we must wait', arguments);
1698             this.pushQueue({
1699                 scope: this,
1700                 callback: this.forward,
1701                 args: arguments,
1702                 queue: queue
1703             });
1704             return false;
1705         }
1706
1707         // Make Busy + Continue
1708         this.busy(true);
1709         
1710         var _t = this;
1711         // Fix certain browser bugs that prevent the state from changing
1712         this.doubleCheck(function(){
1713             _t.forward(false);
1714         });
1715
1716         // Go forward
1717         history.go(1);
1718
1719         // End forward closure
1720         return true;
1721     },
1722
1723     /**
1724      * go(index,queue)
1725      * Send the browser history back or forward index times
1726      * @param {Integer} queue [optional]
1727      */
1728     go : function(index,queue){
1729         //this.debug('this.go: called', arguments);
1730
1731         // Prepare
1732         var i;
1733
1734         // Handle
1735         if ( index > 0 ) {
1736             // Forward
1737             for ( i=1; i<=index; ++i ) {
1738                 this.forward(queue);
1739             }
1740         }
1741         else if ( index < 0 ) {
1742             // Backward
1743             for ( i=-1; i>=index; --i ) {
1744                 this.back(queue);
1745             }
1746         }
1747         else {
1748             throw new Error('History.go: History.go requires a positive or negative integer passed.');
1749         }
1750
1751         // Chain
1752         return this;
1753     },
1754
1755
1756     // ====================================================================
1757     // HTML5 State Support
1758
1759      
1760     /*
1761      * Use native HTML5 History API Implementation
1762      */
1763
1764     /**
1765      * onPopState(event,extra)
1766      * Refresh the Current State
1767      */
1768     onPopState : function(event,extra){
1769         // Prepare
1770         var stateId = false, newState = false, currentHash, currentState;
1771
1772         // Reset the double check
1773         this.doubleCheckComplete();
1774
1775         // Check for a Hash, and handle apporiatly
1776         currentHash = this.getHash();
1777         if ( currentHash ) {
1778             // Expand Hash
1779             currentState = this.extractState(currentHash||this.getLocationHref(),true);
1780             if ( currentState ) {
1781                 // We were able to parse it, it must be a State!
1782                 // Let's forward to replaceState
1783                 //this.debug('this.onPopState: state anchor', currentHash, currentState);
1784                 this.replaceState(currentState.data, currentState.title, currentState.url, false);
1785             }
1786             else {
1787                 // Traditional Anchor
1788                 //this.debug('this.onPopState: traditional anchor', currentHash);
1789                 Roo.get(window).fireEvent('anchorchange');
1790                 this.busy(false);
1791             }
1792
1793             // We don't care for hashes
1794             this.expectedStateId = false;
1795             return false;
1796         }
1797         stateId = (event && event.browserEvent && event.browserEvent['state']) || (extra && extra['state']) || undefined;
1798
1799         // Ensure
1800         //stateId = this.Adapter.extractEventData('state',event,extra) || false;
1801
1802         // Fetch State
1803         if ( stateId ) {
1804             // Vanilla: Back/forward button was used
1805             newState = this.getStateById(stateId);
1806         }
1807         else if ( this.expectedStateId ) {
1808             // Vanilla: A new state was pushed, and popstate was called manually
1809             newState = this.getStateById(this.expectedStateId);
1810         }
1811         else {
1812             // Initial State
1813             newState = this.extractState(this.getLocationHref());
1814         }
1815
1816         // The State did not exist in our store
1817         if ( !newState ) {
1818             // Regenerate the State
1819             newState = this.createStateObject(null,null,this.getLocationHref());
1820         }
1821
1822         // Clean
1823         this.expectedStateId = false;
1824
1825         // Check if we are the same state
1826         if ( this.isLastSavedState(newState) ) {
1827             // There has been no change (just the page's hash has finally propagated)
1828             //this.debug('this.onPopState: no change', newState, this.savedStates);
1829             this.busy(false);
1830             return false;
1831         }
1832
1833         // Store the State
1834         this.storeState(newState);
1835         this.saveState(newState);
1836
1837         // Force update of the title
1838         this.setTitle(newState);
1839
1840         // Fire Our Event
1841         Roo.get(window).fireEvent('statechange');
1842         this.busy(false);
1843
1844         // Return true
1845         return true;
1846     },
1847     
1848     
1849     
1850     
1851     
1852         
1853
1854     /**
1855      * pushState(data,title,url)
1856      * Add a new State to the history object, become it, and trigger onpopstate
1857      * We have to trigger for HTML4 compatibility
1858      * @param {object} data
1859      * @param {string} title
1860      * @param {string} url
1861      * @return {true}
1862      */
1863     pushState : function(data,title,url,queue){
1864         //this.debug('this.pushState: called', arguments);
1865
1866         // Check the State
1867         if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1868             throw new Error('History.js does not support states with fragement-identifiers (hashes/anchors).');
1869         }
1870
1871         // Handle Queueing
1872         if ( queue !== false && this.busy() ) {
1873             // Wait + Push to Queue
1874             //this.debug('this.pushState: we must wait', arguments);
1875             this.pushQueue({
1876                 scope: this,
1877                 callback: this.pushState,
1878                 args: arguments,
1879                 queue: queue
1880             });
1881             return false;
1882         }
1883
1884         // Make Busy + Continue
1885         this.busy(true);
1886
1887         // Create the newState
1888         var newState = this.createStateObject(data,title,url);
1889
1890         // Check it
1891         if ( this.isLastSavedState(newState) ) {
1892             // Won't be a change
1893             this.busy(false);
1894         }
1895         else {
1896             // Store the newState
1897             this.storeState(newState);
1898             this.expectedStateId = newState.id;
1899
1900             // Push the newState
1901             history.pushState(newState.id,newState.title,newState.url);
1902
1903             // Fire HTML5 Event
1904             Roo.get(window).fireEvent('popstate');
1905         }
1906
1907         // End pushState closure
1908         return true;
1909     },
1910
1911     /**
1912      * replaceState(data,title,url)
1913      * Replace the State and trigger onpopstate
1914      * We have to trigger for HTML4 compatibility
1915      * @param {object} data
1916      * @param {string} title
1917      * @param {string} url
1918      * @return {true}
1919      */
1920     replaceState : function(data,title,url,queue){
1921         //this.debug('this.replaceState: called', arguments);
1922
1923         // Check the State
1924         if ( this.getHashByUrl(url) && this.emulated.pushState ) {
1925             throw new Error('this.js does not support states with fragement-identifiers (hashes/anchors).');
1926         }
1927
1928         // Handle Queueing
1929         if ( queue !== false && this.busy() ) {
1930             // Wait + Push to Queue
1931             //this.debug('this.replaceState: we must wait', arguments);
1932             this.pushQueue({
1933                 scope: this,
1934                 callback: this.replaceState,
1935                 args: arguments,
1936                 queue: queue
1937             });
1938             return false;
1939         }
1940
1941         // Make Busy + Continue
1942         this.busy(true);
1943
1944         // Create the newState
1945         var newState = this.createStateObject(data,title,url);
1946
1947         // Check it
1948         if ( this.isLastSavedState(newState) ) {
1949             // Won't be a change
1950             this.busy(false);
1951         }
1952         else {
1953             // Store the newState
1954             this.storeState(newState);
1955             this.expectedStateId = newState.id;
1956
1957             // Push the newState
1958             history.replaceState(newState.id,newState.title,newState.url);
1959
1960             // Fire HTML5 Event
1961             Roo.get(window).fireEvent('popstate');
1962         }
1963
1964         // End replaceState closure
1965         return true;
1966     },
1967
1968
1969                 // ====================================================================
1970                 // Initialise
1971
1972                 /**
1973                  * Bind for Saving Store
1974                  */
1975     
1976     // When the page is closed
1977     onUnload : function(){
1978         // Prepare
1979         var     currentStore, item, currentStoreString;
1980     
1981         // Fetch
1982         try {
1983             currentStore = JSON.parse(this.sessionStorage.getItem('Roo.History.store'))||{};
1984         }
1985         catch ( err ) {
1986             currentStore = {};
1987         }
1988     
1989         // Ensure
1990         currentStore.idToState = currentStore.idToState || {};
1991         currentStore.urlToId = currentStore.urlToId || {};
1992         currentStore.stateToId = currentStore.stateToId || {};
1993     
1994         // Sync
1995         for ( item in this.idToState ) {
1996             if ( !this.idToState.hasOwnProperty(item) ) {
1997                 continue;
1998             }
1999             currentStore.idToState[item] = this.idToState[item];
2000         }
2001         for ( item in this.urlToId ) {
2002             if ( !this.urlToId.hasOwnProperty(item) ) {
2003                 continue;
2004             }
2005             currentStore.urlToId[item] = this.urlToId[item];
2006         }
2007         for ( item in this.stateToId ) {
2008             if ( !this.stateToId.hasOwnProperty(item) ) {
2009                 continue;
2010             }
2011             currentStore.stateToId[item] = this.stateToId[item];
2012         }
2013     
2014         // Update
2015         this.store = currentStore;
2016         this.normalizeStore();
2017     
2018         // In Safari, going into Private Browsing mode causes the
2019         // Session Storage object to still exist but if you try and use
2020         // or set any property/function of it it throws the exception
2021         // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to
2022         // add something to storage that exceeded the quota." infinitely
2023         // every second.
2024         currentStoreString = JSON.stringify(currentStore);
2025         try {
2026             // Store
2027             this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2028         }
2029         catch (e) {
2030             if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
2031                 if (this.sessionStorage.length) {
2032                     // Workaround for a bug seen on iPads. Sometimes the quota exceeded error comes up and simply
2033                     // removing/resetting the storage can work.
2034                     this.sessionStorage.removeItem('Roo.History.store');
2035                     this.sessionStorage.setItem('Roo.History.store', currentStoreString);
2036                 } else {
2037                     // Otherwise, we're probably private browsing in Safari, so we'll ignore the exception.
2038                 }
2039             } else {
2040                 throw e;
2041             }
2042         }
2043     }
2044 });
2045
2046