Merge branch 'master' into 2681-new-inactive-user-notification
[arvados.git] / apps / workbench / app / assets / javascripts / event_log.js
1 /*
2  * This js establishes a websockets connection with the API Server.
3  */
4
5 /* The subscribe method takes a window element id and object id.
6    Any log events for that particular object id are sent to that window element. */
7 function subscribeToEventLog (elementId) {
8   // if websockets are not supported by browser, do not subscribe for events
9   websocketsSupported = ('WebSocket' in window);
10   if (websocketsSupported == false) {
11     return;
12   }
13
14   // grab websocket connection from window, if one exists
15   event_log_disp = $(window).data("arv-websocket");
16   if (event_log_disp == null) {
17     // create the event log dispatcher
18     websocket_url = $('meta[name=arv-websocket-url]').attr("content");
19     if (websocket_url == null)
20       return;
21
22     event_log_disp = new WebSocket(websocket_url);
23
24     event_log_disp.onopen = onEventLogDispatcherOpen;
25     event_log_disp.onmessage = onEventLogDispatcherMessage;
26
27     // store websocket in window to allow reuse when multiple divs subscribe for events
28     $(window).data("arv-websocket", event_log_disp);
29   }
30 }
31
32 /* send subscribe message to the websockets server */
33 function onEventLogDispatcherOpen(event) {
34   this.send('{"method":"subscribe"}');
35 }
36
37 /* trigger event for all applicable elements waiting for this event */
38 function onEventLogDispatcherMessage(event) {
39   parsedData = JSON.parse(event.data);
40   object_uuid = parsedData.object_uuid;
41
42   // if there are any listeners for this object uuid or "all", trigger the event
43   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\"]";
44   $(matches).trigger('arv-log-event', event.data);
45 }