/library/History.py

http://pyjamas.googlecode.com/ · Python · 112 lines · 55 code · 8 blank · 49 comment · 5 complexity · a0ef06f908b066b3d1a13edf4fcca577 MD5 · raw file

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