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