diff --git a/kfet/static/kfet/js/kfet.js b/kfet/static/kfet/js/kfet.js index f0e7a316..bdf5b69b 100644 --- a/kfet/static/kfet/js/kfet.js +++ b/kfet/static/kfet/js/kfet.js @@ -27,6 +27,53 @@ $(document).ready(function() { } }); +/* + * Generic Websocket class and k-psul ws instanciation + */ + +class KfetWebsocket { + + static get defaults() { + return {"relative_url": "", "default_msg": {}, "handlers": []}; + } + + constructor(data) { + $.extend(this, this.constructor.defaults, data); + } + + get url() { + var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws'; + var location_host = window.location.host; + var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; + + return websocket_protocol+"://" + location_url + this.relative_url ; + } + + add_handler(handler) { + if (!this.socket) + this.listen(); + + this.handlers.push(handler); + } + + listen() { + var that = this; + this.socket = new ReconnectingWebSocket(this.url); + + this.socket.onmessage = function(e) { + var data = $.extend({}, that.default_msg, JSON.parse(e.data)); + for (let handler of that.handlers) { + handler(data); + } + } + } +} + +var OperationWebSocket = new KfetWebsocket({ + 'relative_url': '/ws/k-fet/k-psul/', + 'default_msg': {'opegroups':[],'opes':[],'checkouts':[],'articles':[]}, +}); + function dateUTCToParis(date) { return moment.tz(date, 'UTC').tz('Europe/Paris'); } diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index d3b148c9..4ac1354c 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -12,7 +12,6 @@ - {% endblock %} @@ -1321,15 +1320,7 @@ $(document).ready(function() { // Synchronization // ----- - websocket_msg_default = {'opegroups':[],'opes':[],'checkouts':[],'articles':[]} - - var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws'; - var location_host = window.location.host; - var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; - socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/"); - socket.onmessage = function(e) { - data = $.extend({}, websocket_msg_default, JSON.parse(e.data)); - + OperationWebSocket.add_handler(function(data) { for (var i=0; i