aboutsummaryrefslogtreecommitdiffstats
path: root/ui/ui.history.js
blob: 40848ebe56e17482b6f1e9750a159ca3a3a01733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * jQuery UI History
 *
 * Copyright (c) 2008 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/History
 *
 * Depends:
 *  ui.core.js
 */
(function($) {

// TODO lazy loading singleton
$.ui.hmanager = new function() {
    var states = {}, def = function() {};
    
    var $window = $(window), hash = location.hash;        

    function getState() {
        return hash.replace('#', '');
    } 
    
    var iframe;
    // var keepHistoryIn = iframe || window;
        
    return {

        enable: function() {
            
            if ($.browser.msie && parseInt($.browser.version) < 8) {
                $(function() {
                    // create hidden iframe for hash change tracking
                    iframe = $('<iframe id="ui-history-iframe" style="display: none;"></iframe>').
                                    prependTo(document.body)[0];
                    
                    // create initial history entry
                    iframe.contentWindow.document.open();
                    iframe.contentWindow.document.close();
                    
                    if (getState())
                        iframe.contentWindow.document.location.hash = getState();
                    
                });
            }
            
            $window.bind('hashchange', function(e) {
                // Prevent IE 8 from fireing an event twice,
                // one from true event, one from trigger...
                if (!iframe && hash == location.hash || iframe && hash == iframe.contentWindow.document.location.hash)
                    return false;

                if ($.browser.msie && parseInt($.browser.version) < 8) {
                    hash = iframe.contentWindow.document.location.hash;
                }
                else
                    hash = location.hash;
                
                if (getState())
                    states[getState()]();
                else
                    // TODO invoke default
                    ;
            });
            
            if (!($.browser.msie && parseInt($.browser.version) >= 8)) {
                setInterval(
                    ($.browser.msie ?
                        function() {
                            if (hash != iframe.contentWindow.document.location.hash)
                                $window.trigger('hashchange');
                        } : 
                        function() {
                            if (hash != location.hash)
                                $window.trigger('hashchange');
                            else
                                // Do the history.length check hack for Safari 2
                                ;
                        }
                    )
                    , 200
                );
            }
        },

        add: function(state, handler) {
            states[state] = handler;
        },
        
        go: function(state) {
            if (state) {
                if ($.browser.msie && parseInt($.browser.version) < 8) {
                    iframe.contentWindow.document.open();
                    iframe.contentWindow.document.close();
                    iframe.contentWindow.document.location.hash = state;                        
                }
                location.hash = state;
                $window.trigger('hashchange');
            }
            else 
                console.log('TODO do default state');
        }
    }
};

$.ui.history = function() { 
    var args = Array.prototype.slice.call(arguments, 1);
    $.ui.hmanager[arguments[0]].apply($.ui.hmanager, args);
};

})(jQuery);