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