/
Window.browser.py
135 lines (108 loc) · 3.05 KB
/
Window.browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def addWindowCloseListener(listener):
closingListeners.append(listener)
def addWindowResizeListener(listener):
resizeListeners.append(listener)
def getTitle():
return JS('$doc["title"]')
def getLocation():
global location
if not location:
l = JS('$wnd["location"]')
location = Location.Location(l)
return location
def setLocation(url):
w = JS('$wnd')
w.location = url
def getClientHeight():
return JS('$wnd["innerHeight"]')
def getClientWidth():
return JS('$wnd["innerWidth"]')
def setOnError(onError):
if (not callable(onError)):
raise TypeError("object is not callable")
JS("""\
$wnd['onerror']=function(msg, url, linenumber){
return @{{onError}}(msg, url, linenumber);
}
""")
def fireClosingImpl():
ret = None
for listener in closingListeners:
msg = listener.onWindowClosing()
if ret is None:
ret = msg
return ret
def fireResizedAndCatch(handler):
# FIXME - need implementation
pass
def fireResizedImpl():
for listener in resizeListeners:
listener.onWindowResized(getClientWidth(), getClientHeight())
# TODO: call fireClosedAndCatch
def onClosed():
fireClosedImpl()
# TODO: call fireClosingAndCatch
def onClosing():
return fireClosingImpl()
# TODO: call fireResizedAndCatch
def onResize():
fireResizedImpl()
def fireClosedAndCatch(handler):
# FIXME - need implementation
pass
def fireClosedImpl():
for listener in closingListeners:
listener.onWindowClosed()
def fireClosingAndCatch(handler):
# FIXME - need implementation
pass
def resize(width, height):
wnd().resizeTo(width, height)
def onError(msg, url, linenumber):
dialog=doc().createElement("div")
dialog.className='errordialog'
# Note: $pyjs.trackstack is a global javascript array
# XXX: we should not have a sys dependency here!
tracestr = sys.trackstackstr(JS("$pyjs['trackstack']['slice'](0,-1)"))
tracestr = tracestr.replace("\n", "<br />\n ")
dialog.innerHTML="""\
<b style="color:red">JavaScript Error: </b>
%s at line number %d. Please inform webmaster.
<br /> %s
""" % (msg, linenumber, tracestr)
doc().body.appendChild(dialog)
return True
def alert(msg):
wnd().alert(msg)
def confirm(msg):
return wnd().confirm(msg)
def prompt(msg, defaultReply=""):
return wnd().prompt(msg, defaultReply)
def init_listeners():
global closingListeners
global resizeListeners
if not closingListeners:
closingListeners = []
if not resizeListeners:
resizeListeners = []
def init():
global sys
import sys
init_listeners()
JS("""
$wnd['__pygwt_initHandlers'](
function() {
@{{onResize}}();
},
function() {
return @{{onClosing}}();
},
function() {
@{{onClosed}}();
/*$wnd['onresize'] = null;
$wnd['onbeforeclose'] = null;
$wnd['onclose'] = null;*/
}
);
""")
setOnError(onError)