gestioCOF/kfet/open/static/kfetopen/kfet-open.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

var OpenWS = new KfetWebsocket({
relative_url: "open/"
});
2020-09-15 16:33:41 +02:00
var OpenKfet = function (force_close_url, admin) {
this.force_close_url = force_close_url;
this.admin = admin;
this.status = this.UNKNOWN;
this.dom = {
status_text: $('.kfetopen .status-text'),
force_close_btn: $('.kfetopen .force-close-btn'),
warning: $('.kfetopen .warning')
2020-09-15 16:37:41 +02:00
}
2020-09-15 16:37:41 +02:00
this.dom.force_close_btn.click(() => this.toggle_force_close());
2020-09-15 16:33:41 +02:00
setInterval(() => this.refresh(), this.refresh_interval * 1000);
OpenWS.add_handler(data => this.refresh(data));
};
OpenKfet.prototype = {
// Status is unknown after . minutes without update.
2017-06-22 05:44:05 +02:00
time_unknown: 15,
// Maximum interval (seconds) between two UI refresh.
2017-06-22 05:44:05 +02:00
refresh_interval: 20,
// Prefix for classes describing place status.
class_prefix: 'kfetopen-st-',
// Set status-classes on this dom element.
target: 'body',
// Status
OPENED: "opened",
CLOSED: "closed",
UNKNOWN: "unknown",
// Admin status
FAKE_CLOSED: "fake_closed",
// Display values
status_text: {
opened: "ouverte",
closed: "fermée",
unknown: "_____"
},
force_text: {
activate: "Fermer manuellement",
deactivate: "Réouvrir la K-Fêt"
},
get is_recent() {
return this.last_update && moment().diff(this.last_update, 'minute') <= this.time_unknown;
},
2020-09-15 16:33:41 +02:00
refresh: function (data) {
if (data) {
$.extend(this, data);
this.last_update = moment();
}
if (!this.is_recent)
this.status = this.UNKNOWN;
this.refresh_dom();
},
2020-09-15 16:33:41 +02:00
refresh_dom: function () {
let status = this.status;
this.clear_class();
this.add_class(status);
this.dom.status_text.html(this.status_text[status]);
// admin specific
if (this.admin) {
this.add_class(this.admin_status);
if (this.force_close) {
2017-09-03 14:42:38 +02:00
this.dom.warning.show().addClass('in');
this.dom.force_close_btn.html(this.force_text['deactivate']);
} else {
2017-09-03 14:42:38 +02:00
this.dom.warning.removeClass('in').hide();
this.dom.force_close_btn.html(this.force_text['activate']);
}
}
},
2020-09-15 16:33:41 +02:00
toggle_force_close: function (password) {
$.post({
2020-09-15 16:33:41 +02:00
context: this,
url: this.force_close_url,
2020-09-15 16:33:41 +02:00
data: { force_close: !this.force_close },
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password !== undefined)
$xhr.setRequestHeader("KFetPassword", password);
}
2020-09-15 16:37:41 +02:00
}).fail(function ($xhr) {
switch ($xhr.status) {
case 403:
requestAuth({ 'errors': {} }, () => this.toggle_force_close);
2020-09-15 16:37:41 +02:00
break;
}
});
},
2020-09-15 16:33:41 +02:00
clear_class: function () {
let re = new RegExp('(^|\\s)' + this.class_prefix + '\\S+', 'g');
$(this.target).attr('class', (i, c) => c ? c.replace(re, '') : '');
},
2020-09-15 16:33:41 +02:00
add_class: function (status) {
$(this.target).addClass(this.class_prefix + status);
}
};