Package library :: Package pyjamas :: Module History
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.History

  1  # This is the gtk-dependent History module. 
  2  # For the pyjamas/javascript version, see platform/HistoryPyJS.py 
  3   
  4  from __pyjamas__ import JS, doc, wnd 
  5  import pyjd 
  6  if pyjd.is_desktop: 
  7      from __pyjamas__ import get_main_frame 
  8   
  9  global historyToken 
 10  historyToken = '' 
 11   
 12  historyListeners = [] 
 13   
 14   
 15  """ 
 16      Simple History management class for back/forward button support. 
 17   
 18      This class allows your AJAX application to use a history.  Each time you 
 19      call newItem(), a new item is added to the history and the history 
 20      listeners are notified.  If the user clicks the browser's forward or back 
 21      buttons, the appropriate item (a string passed to newItem) is fetched 
 22      from the history and the history listeners are notified. 
 23   
 24      The address bar of the browser contains the current token, using 
 25      the "#" seperator (for implementation reasons, not because we love 
 26      the # mark). 
 27   
 28      You may want to check whether the hash already contains a history 
 29      token when the page loads and use that to show appropriate content; 
 30      this allows users of the site to store direct links in their 
 31      bookmarks or send them in emails. 
 32   
 33      To make this work properly in all browsers, you must add a specially 
 34      named iframe to your html page, like this: 
 35   
 36      <iframe id='__pygwt_historyFrame' style='width:0;height:0;border:0' /> 
 37  """ 
 38   
 39   
40 -def addHistoryListener(listener):
41 print "add listener", listener 42 historyListeners.append(listener)
43 44
45 -def back():
46 wnd().history.back()
47 48
49 -def forward():
50 wnd().history.forward()
51 52
53 -def getToken():
54 global historyToken 55 return historyToken
56 57
58 -def newItem(ht):
59 global historyToken 60 if historyToken == ht: 61 return 62 onHistoryChanged(ht) 63 return 64 65 JS(""" 66 if(@{{historyToken}} == "" || @{{historyToken}} == null){ 67 @{{historyToken}} = "#"; 68 } 69 $wnd['location']['hash'] = encodeURI(@{{historyToken}})['replace']('#','%23'); 70 """)
71 72 73 # TODO - fireHistoryChangedAndCatch not implemented
74 -def onHistoryChanged(ht):
75 fireHistoryChangedImpl(ht)
76 77 78 # TODO
79 -def fireHistoryChangedAndCatch():
80 pass
81 82
83 -def fireHistoryChangedImpl(ht):
84 global historyToken 85 if historyToken == ht: 86 return 87 historyToken = ht 88 for listener in historyListeners: 89 listener.onHistoryChanged(ht)
90 91
92 -def removeHistoryListener(listener):
93 historyListeners.remove(listener)
94
95 -def _first_notify():
96 print "first notify", historyToken 97 onHistoryChanged(historyToken)
98
99 -def init():
100 print "init", get_main_frame(), pyjd.is_desktop 101 if get_main_frame() is None: 102 if pyjd.is_desktop: 103 pyjd.add_setup_callback(init) 104 # pyjd.add_pre_run_callback(_first_notify) 105 return 106 107 global historyToken 108 historyToken = '' 109 hash = wnd().location.hash 110 111 if hash and len(hash) > 0: 112 historyToken = hash[1:]
113 114 init() 115