PageRenderTime 169ms CodeModel.GetById 72ms app.highlight 7ms RepoModel.GetById 60ms app.codeStats 1ms

/library/platform/HistoryIE6.py

http://pyjamas.googlecode.com/
Python | 71 lines | 71 code | 0 blank | 0 comment | 0 complexity | cdae822abb6fa180650070de21ea3331 MD5 | raw file
 1def init():
 2    JS("""
 3    // Check for existence of the history frame.
 4    var historyFrame = $doc.getElementById('__pygwt_historyFrame');
 5    if (!historyFrame)
 6        return false;
 7
 8    // Get the initial token from the url's hash component.
 9    var hash = $wnd.location.hash;
10    if (hash.length > 0)
11        $wnd.__historyToken = decodeURIComponent(hash.substring(1));
12    else
13        $wnd.__historyToken = '';
14
15    // Initialize the history iframe.  If '__historyToken' already exists, then
16    // we're probably backing into the app, so _don't_ set the iframe's location.
17    var tokenElement = null;
18    if (historyFrame.contentWindow) {
19        var doc = historyFrame.contentWindow.document;
20        tokenElement = doc ? doc.getElementById('__historyToken') : null;
21    }
22
23    if (tokenElement)
24        $wnd.__historyToken = tokenElement.value;
25    else
26        historyFrame.src = 'history.html?' + $wnd.__historyToken;
27
28    // Expose the '__onHistoryChanged' function, which will be called by
29    // the history frame when it loads.
30    $wnd.__onHistoryChanged = function(token) {
31        // Change the URL and notify the application that its history frame
32        // is changing.  Note that setting location.hash does _not_ add a history
33        // frame on IE, so we don't have to do a 'location.replace()'.
34        if (token != $wnd.__historyToken) {
35            $wnd.__historyToken = token;
36            $wnd.location.hash = encodeURIComponent(token);
37            // TODO - move init back into History
38            // this.onHistoryChanged(token);
39            var h = new __History_History();
40            h.onHistoryChanged(token);
41        }
42    };
43
44    // This is the URL check timer.  It detects when an unexpected change
45    // occurs in the document's URL (e.g. when the user enters one manually
46    // or selects a 'favorite', but only the #hash part changes).  When this
47    // occurs, we _must_ reload the page.  This is because IE has a really
48    // nasty bug that totally mangles its history stack and causes the location
49    // bar in the UI to stop working under these circumstances.
50    var urlChecker = function() {
51        var hash = $wnd.location.hash;
52        if (hash.length > 0) {
53            var token = decodeURIComponent(hash.substring(1));
54            if ($wnd.__historyToken && (token != $wnd.__historyToken))
55                $wnd.location.reload();
56        }
57        $wnd.setTimeout(urlChecker, 250);
58    };
59    urlChecker();
60
61    return true;
62    """)
63
64
65class History:
66
67    def newItem(self, historyToken):
68        JS("""
69        var iframe = $doc.getElementById('__pygwt_historyFrame');
70        iframe.contentWindow.location.href = 'history.html?' + historyToken;
71        """)