annotate libervia/pages/_browser/bridge.py @ 1309:9344ca3b21a6

browser (slideshow): fixed scrolling issue with comment panel: all elements present in body before slideshow is added are hidden, and comments panel is now added to slideshow itself.
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:47:21 +0200
parents 999dccf0093e
children 8998f01088ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import window
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
2 import javascript
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
3
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
4
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
5 class Bridge:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
6
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
7 def __getattr__(self, attr):
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
8 return lambda *args, **kwargs: self.call(attr, *args, **kwargs)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
9
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
10 def on_load(self, xhr, ev, callback, errback):
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
11 if xhr.status == 200:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
12 ret = javascript.JSON.parse(xhr.response)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
13 if ret is None:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
14 callback()
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
15 else:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
16 callback(ret)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
17 elif xhr.status == 502:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # PROXY_ERROR is used for bridge error
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
19 ret = javascript.JSON.parse(xhr.response)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
20 errback(ret)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
21 else:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
22 print(f"bridge called failed: code: {xhr.response}, text: {xhr.statusText}")
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
23 errback({"fullname": "InternalError", "message": xhr.statusText})
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
24
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
25 def call(self, method_name, *args, callback, errback, **kwargs):
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
26 xhr = window.XMLHttpRequest.new()
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
27 xhr.bind('load', lambda ev: self.on_load(xhr, ev, callback, errback))
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
28 xhr.bind('error', lambda ev: errback(
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
29 {"fullname": "ConnectionError", "message": xhr.statusText}))
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
30 xhr.open("POST", f"/_bridge/{method_name}", True)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
31 data = javascript.JSON.stringify({
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
32 "args": args,
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
33 "kwargs": kwargs,
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
34 })
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
35 xhr.setRequestHeader('X-Csrf-Token', window.csrf_token)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
36 xhr.send(data)