Skip to content

Commit

Permalink
migrate session_contexts.coffee to plain js (#1740)
Browse files Browse the repository at this point in the history
* migrate session_contexts.coffee to plain js

* add a newline
  • Loading branch information
johrstrom authored Jan 4, 2022
1 parent 5f02b5b commit 5528160
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 84 deletions.

This file was deleted.

This file was deleted.

111 changes: 111 additions & 0 deletions apps/dashboard/app/javascript/packs/batch_connect_sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict';

const pollers = [];

class Timer {
constructor(callback, delay){
this.delay = delay;
this.remaining = delay;
this.active = true;
this.callback = callback;

this.resume();
}

resume() {
if(!this.active) { return; }

this.start = new Date();
this.clearTO();
this.timerId = setTimeout(this.callback, this.remaining);
}

restart() {
if(!this.active) { return; }

this.remaining = this.delay;
resume();
}

pause() {
if(!this.active) { return; }

this.clearTO();
this.remaining -= new Date() - this.start;
}

stop(){
if(!this.active) { return; }
this.clearTO();
this.active = false;
}

clearTO(){
if(this.timerId !== undefined) {
clearTimeout(this.timerId);
}
}

}

class Poller {
constructor(url, delay) {
this.url = url;
this.delay = delay;
this.poll();
}

poll(){
this.timer = new Timer(this.request.bind(this), this.delay);
}

pause() {
this.timer.pause();
}

resume() {
this.timer.resume();
}

request(){
const that = this;
$.getScript(this.url)
.done((_script, _textStatus, _jqxhr) => {
return;
}).fail((_jqxhr, textStatus, errorThrown) => {
if(textStatus) { console.log(`Failed to get session data. Server returned '${textStatus}'`); }
if(errorThrown) {
console.log("Failed to get session data because of error.");
console.log(errorThrown);
}
return;
}).always(() => {
that.poll();
return;
});
}
}

function makePollers(){
const obj = $('#batch_connect_sessions');
if(!obj) return;

const url = obj.data('url');
const delay = obj.data('delay');
if(url && delay) { pollers.push(new Poller(url, delay)); }

$(document)
.on('show.bs.modal', () => {
pollers.forEach((poller) => {
poller.pause();
});
}).on('hidden.bs.modal', () => {
pollers.forEach((poller) => {
poller.resume();
});
});
}

jQuery(function() {
makePollers();
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
any_apps = (@sys_app_groups + @usr_app_groups + @dev_app_groups).any?
-%>

<%= javascript_pack_tag 'batch_connect_sessions', nonce: true %>

<%= render partial: 'batch_connect/shared/breadcrumb',
locals: {
links: [
Expand Down Expand Up @@ -31,7 +33,7 @@ locals: {
%>
</div>
<div class="col-md-9">
<div class="batch-connect sessions" data-toggle="poll" data-url="<%= batch_connect_sessions_path(format: :js) %>" data-delay="<%= ENV["POLL_DELAY"] || 10000 %>">
<div id="batch_connect_sessions" class="batch-connect sessions" data-toggle="poll" data-url="<%= batch_connect_sessions_path(format: :js) %>" data-delay="<%= ENV["POLL_DELAY"] || 10000 %>">
<% if @sessions.empty? %>
<div class="ood-appkit markdown">
<p><%= t('dashboard.batch_connect_no_sessions') %></p>
Expand Down

0 comments on commit 5528160

Please sign in to comment.