Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / assets / javascripts / event_log.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 /*
6  * This js establishes a websockets connection with the API Server.
7  */
8
9 /* Subscribe to websockets event log.  Do nothing if already connected. */
10 function subscribeToEventLog () {
11     // if websockets are not supported by browser, do not subscribe for events
12     websocketsSupported = ('WebSocket' in window);
13     if (websocketsSupported == false) {
14         return;
15     }
16
17     // check if websocket connection is already stored on the window
18     event_log_disp = $(window).data("arv-websocket");
19     if (event_log_disp == null) {
20         // need to create new websocket and event log dispatcher
21         websocket_url = $('meta[name=arv-websocket-url]').attr("content");
22         if (websocket_url == null)
23             return;
24
25         event_log_disp = new WebSocket(websocket_url);
26
27         event_log_disp.onopen = onEventLogDispatcherOpen;
28         event_log_disp.onmessage = onEventLogDispatcherMessage;
29
30         // store websocket in window to allow reuse when multiple divs subscribe for events
31         $(window).data("arv-websocket", event_log_disp);
32     }
33 }
34
35 /* Send subscribe message to the websockets server.  Without any filters
36    arguments, this subscribes to all events */
37 function onEventLogDispatcherOpen(event) {
38     this.send('{"method":"subscribe"}');
39 }
40
41 /* Trigger event for all applicable elements waiting for this event */
42 function onEventLogDispatcherMessage(event) {
43     parsedData = JSON.parse(event.data);
44     object_uuid = parsedData.object_uuid;
45
46     if (!object_uuid) {
47         return;
48     }
49
50     // if there are any listeners for this object uuid or "all", trigger the event
51     matches = ".arv-log-event-listener[data-object-uuid=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuids~=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuid=\"all\"],.arv-log-event-listener[data-object-kind=\"" + parsedData.object_kind + "\"]";
52     $(matches).trigger('arv-log-event', parsedData);
53 }
54
55 /* Automatically connect if there are any elements on the page that want to
56    receive event log events. */
57 $(document).on('ajax:complete ready', function() {
58     var a = $('.arv-log-event-listener');
59     if (a.length > 0) {
60         subscribeToEventLog();
61     }
62 });