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