1
2
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
41 print "add listener", listener
42 historyListeners.append(listener)
43
44
47
48
51
52
56
57
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
76
77
78
81
82
84 global historyToken
85 if historyToken == ht:
86 return
87 historyToken = ht
88 for listener in historyListeners:
89 listener.onHistoryChanged(ht)
90
91
94
98
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
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