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/"
});
var OpenKfet = function(force_close_url, admin) {
var OpenKfet = function (force_close_url, admin) {
this.force_close_url = force_close_url;
this.admin = admin;
@ -13,9 +13,9 @@ var OpenKfet = function(force_close_url, admin) {
warning: $('.kfetopen .warning')
},
this.dom.force_close_btn.click( () => this.toggle_force_close() );
setInterval( () => this.refresh(), this.refresh_interval * 1000);
OpenWS.add_handler( data => this.refresh(data) );
this.dom.force_close_btn.click(() => this.toggle_force_close());
setInterval(() => this.refresh(), this.refresh_interval * 1000);
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;
},
refresh: function(data) {
refresh: function (data) {
if (data) {
$.extend(this, data);
this.last_update = moment();
@ -63,7 +63,7 @@ OpenKfet.prototype = {
this.refresh_dom();
},
refresh_dom: function() {
refresh_dom: function () {
let status = this.status;
this.clear_class();
@ -83,31 +83,32 @@ OpenKfet.prototype = {
}
},
toggle_force_close: function(password) {
toggle_force_close: function (password) {
$.post({
context: this,
url: this.force_close_url,
data: {force_close: !this.force_close},
data: { force_close: !this.force_close },
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password !== undefined)
$xhr.setRequestHeader("KFetPassword", password);
}
})
.fail(function($xhr) {
switch ($xhr.status) {
case 403:
requestAuth({'errors': {}}, this.toggle_force_close);
break;
}
});
.fail(function ($xhr) {
switch ($xhr.status) {
case 403:
requestAuth({ 'errors': {} }, this.toggle_force_close.bind(this));
break;
}
});
},
clear_class: function() {
clear_class: function () {
let re = new RegExp('(^|\\s)' + this.class_prefix + '\\S+', 'g');
$(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);
}
};