annotate libervia/pages/_browser/bridge.py @ 1344:472267dcd4d8

browser (alt_media_player): native player support + poster + flags + restricted area: - alt_media_player will now use native player when possible. This allows to use its controls and behaviour instead of native ones. - a poster can be specified when instanciated manually - video is not preloaded anymore - handle events propagation to plays nicely when used in slideshow - a "restricted area" mode can be used to let click propagation on video border, and thus catch only play/pause in the center. This is notably useful when used in the slideshow, as border can be used to show/hide slideshow controls - player can be reset, in which case the play button overlay is put back, and video is put at its beginning - once video is played at least once, a `in_use` class is added to the element, play button overlay is removed then. This fix a bug when the overlay was still appearing when using bottom play button. - VideoPlayer has been renamed to MediaPlayer
author Goffi <goffi@goffi.org>
date Mon, 24 Aug 2020 23:04:35 +0200
parents 8998f01088ac
children b28025a7cc28
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)
1314
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
13 if callback is not None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
14 if ret is None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
15 callback()
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
16 else:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
17 callback(ret)
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
18 elif xhr.status == 502:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
19 # 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
20 ret = javascript.JSON.parse(xhr.response)
1314
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
21 if errback is not None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
22 errback(ret)
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
23 else:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
24 print(f"bridge called failed: code: {xhr.response}, text: {xhr.statusText}")
1314
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
25 if errback is not None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
26 errback({"fullname": "InternalError", "message": xhr.statusText})
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
27
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
28 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
29 xhr = window.XMLHttpRequest.new()
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
30 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
31 xhr.bind('error', lambda ev: errback(
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
32 {"fullname": "ConnectionError", "message": xhr.statusText}))
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
33 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
34 data = javascript.JSON.stringify({
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "args": args,
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "kwargs": kwargs,
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
37 })
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
38 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
39 xhr.send(data)