PageRenderTime 41ms CodeModel.GetById 22ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/library/History.py

http://pyjamas.googlecode.com/
Python | 112 lines | 64 code | 4 blank | 44 comment | 3 complexity | a0ef06f908b066b3d1a13edf4fcca577 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1from __pyjamas__ import JS
  2
  3def init():
  4    JS("""
  5    $wnd.__historyToken = '';
  6
  7    // Get the initial token from the url's hash component.
  8    var hash = $wnd.location.hash;
  9    if (hash.length > 0)
 10        $wnd.__historyToken = decodeURIComponent(hash.substring(1));
 11
 12    // Create the timer that checks the browser's url hash every 1/4 s.
 13    $wnd.__checkHistory = function() {
 14        var token = '', hash = $wnd.location.hash;
 15        if (hash.length > 0)
 16            token = decodeURIComponent(hash.substring(1));
 17
 18        if (token != $wnd.__historyToken) {
 19            $wnd.__historyToken = token;
 20            // TODO - move init back into History
 21            // this.onHistoryChanged(token);
 22            var h = new __History_History();
 23            h.onHistoryChanged(token);
 24        }
 25
 26        $wnd.setTimeout('__checkHistory()', 250);
 27    };
 28
 29    // Kick off the timer.
 30    $wnd.__checkHistory();
 31
 32    return true;
 33    """)
 34
 35historyListeners = []
 36init()
 37
 38
 39class History:
 40    """
 41        Simple History management class for back/forward button support.
 42        
 43        This class allows your AJAX application to use a history.  Each time you
 44        call newItem(), a new item is added to the history and the history
 45        listeners are notified.  If the user clicks the browser's forward or back 
 46        buttons, the appropriate item (a string passed to newItem) is fetched
 47        from the history and the history listeners are notified.
 48        
 49        The address bar of the browser contains the current token, using 
 50        the "#" seperator (for implementation reasons, not because we love 
 51        the # mark).
 52        
 53        You may want to check whether the hash already contains a history
 54        token when the page loads and use that to show appropriate content;
 55        this allows users of the site to store direct links in their
 56        bookmarks or send them in emails.
 57        
 58        To make this work properly in all browsers, you must add a specially
 59        named iframe to your html page, like this:
 60        
 61        <iframe id='__pygwt_historyFrame' style='width:0;height:0;border:0'></iframe>
 62    """
 63
 64    def addHistoryListener(self, listener):
 65        global historyListeners
 66        historyListeners.append(listener)
 67
 68    def back(self):
 69        JS("""
 70        $wnd.history.back();
 71        """)
 72
 73    def forward(self):
 74        JS("""
 75        $wnd.history.forward();
 76        """)
 77    
 78    def getToken(self):
 79        JS("""
 80        return $wnd.__historyToken;
 81        """)
 82    
 83    def newItem(self, historyToken):
 84        JS("""
 85        if(historyToken == "" || historyToken == null){
 86            historyToken = "#";
 87        }
 88        $wnd.location.hash = encodeURIComponent(historyToken);
 89        """)
 90
 91    # TODO - fireHistoryChangedAndCatch not implemented
 92    def onHistoryChanged(self, historyToken):
 93        #UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
 94        #if (handler != null)
 95        #   fireHistoryChangedAndCatch(historyToken, handler);
 96        #else
 97        self.fireHistoryChangedImpl(historyToken)
 98
 99    # TODO
100    def fireHistoryChangedAndCatch(self):
101        pass
102    
103    def fireHistoryChangedImpl(self, historyToken):
104        global historyListeners
105        
106        for listener in historyListeners:
107            listener.onHistoryChanged(historyToken)
108
109    def removeHistoryListener(self, listener):
110        global historyListeners
111        historyListeners.remove(listener)
112