Fix this shenanigans

This commit is contained in:
Ludovic Stephan 2020-09-15 16:33:41 +02:00
parent 2bc97a115c
commit 11d94ecba8

View file

@ -2,7 +2,7 @@ var OpenWS = new KfetWebsocket({
relative_url: "open/" relative_url: "open/"
}); });
var OpenKfet = function(force_close_url, admin) { var OpenKfet = function (force_close_url, admin) {
this.force_close_url = force_close_url; this.force_close_url = force_close_url;
this.admin = admin; this.admin = admin;
@ -13,9 +13,9 @@ var OpenKfet = function(force_close_url, admin) {
warning: $('.kfetopen .warning') warning: $('.kfetopen .warning')
}, },
this.dom.force_close_btn.click( () => this.toggle_force_close() ); this.dom.force_close_btn.click(() => this.toggle_force_close());
setInterval( () => this.refresh(), this.refresh_interval * 1000); setInterval(() => this.refresh(), this.refresh_interval * 1000);
OpenWS.add_handler( data => this.refresh(data) ); OpenWS.add_handler(data => this.refresh(data));
}; };
@ -53,7 +53,7 @@ OpenKfet.prototype = {
return this.last_update && moment().diff(this.last_update, 'minute') <= this.time_unknown; return this.last_update && moment().diff(this.last_update, 'minute') <= this.time_unknown;
}, },
refresh: function(data) { refresh: function (data) {
if (data) { if (data) {
$.extend(this, data); $.extend(this, data);
this.last_update = moment(); this.last_update = moment();
@ -63,7 +63,7 @@ OpenKfet.prototype = {
this.refresh_dom(); this.refresh_dom();
}, },
refresh_dom: function() { refresh_dom: function () {
let status = this.status; let status = this.status;
this.clear_class(); this.clear_class();
@ -83,31 +83,32 @@ OpenKfet.prototype = {
} }
}, },
toggle_force_close: function(password) { toggle_force_close: function (password) {
$.post({ $.post({
context: this,
url: this.force_close_url, url: this.force_close_url,
data: {force_close: !this.force_close}, data: { force_close: !this.force_close },
beforeSend: function ($xhr) { beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken); $xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password !== undefined) if (password !== undefined)
$xhr.setRequestHeader("KFetPassword", password); $xhr.setRequestHeader("KFetPassword", password);
} }
}) })
.fail(function($xhr) { .fail(function ($xhr) {
switch ($xhr.status) { switch ($xhr.status) {
case 403: case 403:
requestAuth({'errors': {}}, this.toggle_force_close); requestAuth({ 'errors': {} }, this.toggle_force_close.bind(this));
break; break;
} }
}); });
}, },
clear_class: function() { clear_class: function () {
let re = new RegExp('(^|\\s)' + this.class_prefix + '\\S+', 'g'); let re = new RegExp('(^|\\s)' + this.class_prefix + '\\S+', 'g');
$(this.target).attr('class', (i, c) => c ? c.replace(re, '') : ''); $(this.target).attr('class', (i, c) => c ? c.replace(re, '') : '');
}, },
add_class: function(status) { add_class: function (status) {
$(this.target).addClass(this.class_prefix + status); $(this.target).addClass(this.class_prefix + status);
} }
}; };