refactor: Avoid importing all js functions.

This commit is contained in:
tecnovert 2022-09-28 21:42:38 +02:00
parent ef0f5ea1ea
commit eb30369bd4
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
3 changed files with 45 additions and 53 deletions

View file

@ -33,20 +33,8 @@ from .basicswap_util import (
strAddressType, strAddressType,
) )
from .js_server import ( from .js_server import (
js_coins,
js_error, js_error,
js_wallets, js_url_to_function,
js_offers,
js_sentoffers,
js_bids,
js_sentbids,
js_network,
js_revokeoffer,
js_smsgaddresses,
js_rates_list,
js_rates,
js_rate,
js_index,
) )
from .ui.util import ( from .ui.util import (
PAGE_LIMIT, PAGE_LIMIT,
@ -665,22 +653,7 @@ class HttpHandler(BaseHTTPRequestHandler):
if len(url_split) > 1 and url_split[1] == 'json': if len(url_split) > 1 and url_split[1] == 'json':
try: try:
self.putHeaders(status_code, 'text/plain') self.putHeaders(status_code, 'text/plain')
func = js_index func = js_url_to_function(url_split)
if len(url_split) > 2:
func = {
'coins': js_coins,
'wallets': js_wallets,
'offers': js_offers,
'sentoffers': js_sentoffers,
'bids': js_bids,
'sentbids': js_sentbids,
'network': js_network,
'revokeoffer': js_revokeoffer,
'smsgaddresses': js_smsgaddresses,
'rate': js_rate,
'rates': js_rates,
'rateslist': js_rates_list,
}.get(url_split[2], js_index)
return func(self, url_split, post_string, is_json) return func(self, url_split, post_string, is_json)
except Exception as ex: except Exception as ex:
if self.server.swap_client.debug is True: if self.server.swap_client.debug is True:

View file

@ -389,3 +389,22 @@ def js_rate(self, url_split, post_string, is_json):
def js_index(self, url_split, post_string, is_json): def js_index(self, url_split, post_string, is_json):
return bytes(json.dumps(self.server.swap_client.getSummary()), 'UTF-8') return bytes(json.dumps(self.server.swap_client.getSummary()), 'UTF-8')
def js_url_to_function(url_split):
if len(url_split) > 2:
return {
'coins': js_coins,
'wallets': js_wallets,
'offers': js_offers,
'sentoffers': js_sentoffers,
'bids': js_bids,
'sentbids': js_sentbids,
'network': js_network,
'revokeoffer': js_revokeoffer,
'smsgaddresses': js_smsgaddresses,
'rate': js_rate,
'rates': js_rates,
'rateslist': js_rates_list,
}.get(url_split[2], js_index)
return js_index

View file

@ -22,31 +22,31 @@
{% if ws_url %} {% if ws_url %}
<script> <script>
var ws = new WebSocket("{{ ws_url }}"), var ws = new WebSocket("{{ ws_url }}"),
floating_div = document.createElement('div'); floating_div = document.createElement('div');
floating_div.classList.add('floatright'); floating_div.classList.add('floatright');
messages = document.createElement('ul'); messages = document.createElement('ul');
messages.setAttribute('id', 'ul_updates'); messages.setAttribute('id', 'ul_updates');
ws.onmessage = function (event) { ws.onmessage = function (event) {
let json = JSON.parse(event.data); let json = JSON.parse(event.data);
let event_message = 'Unknown event'; let event_message = 'Unknown event';
if (json['event'] == 'new_offer') { if (json['event'] == 'new_offer') {
event_message = '<a href=/offer/' + json['offer_id'] + '>New offer</a>'; event_message = '<a href=/offer/' + json['offer_id'] + '>New offer</a>';
} else } else
if (json['event'] == 'new_bid') { if (json['event'] == 'new_bid') {
event_message = '<a href=/bid/' + json['bid_id'] + '>New bid</a> on offer <a href=/offer/' + json['offer_id'] + '>' + json['offer_id'] + '</a>'; event_message = '<a href=/bid/' + json['bid_id'] + '>New bid</a> on offer <a href=/offer/' + json['offer_id'] + '>' + json['offer_id'] + '</a>';
} else } else
if (json['event'] == 'bid_accepted') { if (json['event'] == 'bid_accepted') {
event_message = '<a href=/bid/' + json['bid_id'] + '>Bid accepted</a>'; event_message = '<a href=/bid/' + json['bid_id'] + '>Bid accepted</a>';
} }
let messages = document.getElementById('ul_updates'), let messages = document.getElementById('ul_updates'),
message = document.createElement('li'); message = document.createElement('li');
message.innerHTML = event_message; message.innerHTML = event_message;
messages.appendChild(message); messages.appendChild(message);
}; };
floating_div.appendChild(messages); floating_div.appendChild(messages);
document.body.appendChild(floating_div); document.body.appendChild(floating_div);
</script> </script>
{% endif %} {% endif %}