forked from DGNum/gestioCOF
kfet.open
kfet.open app - Base data (raw_open, last_update...) is stored and shared through cache system. - 2 websockets groups: one for team users, one for other users. - UI is initialized and kept up-to-date with WS. - raw_open and force_close can be updated with standard HTTP requests. At this time, there isn't any restriction on raw_open view. Common sense tell us to change this behavior. Misc - Clean channels routing. - 'PermConsumerMixin': user who sent the message is available as argument in connection_groups method, which returns groups to which the user should be appended on websocket connection (and discarded on disconnection). - New kfet.utils module: should be used for mixins, whatever is useful and not concerns the kfet app. - Clean JS dependencies.
This commit is contained in:
parent
2381af92e3
commit
b8110c11a4
37 changed files with 852 additions and 404 deletions
126
kfet/open/static/kfetopen/kfet-open.js
Normal file
126
kfet/open/static/kfetopen/kfet-open.js
Normal file
|
@ -0,0 +1,126 @@
|
|||
var OpenWS = new KfetWebsocket({
|
||||
relative_url: "open/"
|
||||
});
|
||||
|
||||
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')
|
||||
},
|
||||
|
||||
this.dom.force_close_btn.click( () => this.toggle_force_close() );
|
||||
setInterval( () => this.refresh(), this.refresh_interval * 1000);
|
||||
OpenWS.add_handler( data => this.refresh(data) );
|
||||
|
||||
};
|
||||
|
||||
OpenKfet.prototype = {
|
||||
// Status is unknown after . minutes without update.
|
||||
time_unknown: 2,
|
||||
// Maximum interval (seconds) between two UI refresh.
|
||||
refresh_interval: 10,
|
||||
|
||||
// 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;
|
||||
},
|
||||
|
||||
refresh: function(data) {
|
||||
if (data)
|
||||
$.extend(this, data);
|
||||
this.refresh_status();
|
||||
this.refresh_dom();
|
||||
},
|
||||
|
||||
refresh_status: function() {
|
||||
// find new status
|
||||
let status = this.UNKNOWN;
|
||||
if (this.is_recent)
|
||||
status = this.is_open ? this.OPENED : this.CLOSED;
|
||||
this.status = status;
|
||||
|
||||
// admin specific
|
||||
if (this.admin) {
|
||||
let admin_status = status;
|
||||
if (status == this.CLOSED && this.raw_open)
|
||||
admin_status = this.FAKE_CLOSED;
|
||||
this.admin_status = admin_status;
|
||||
}
|
||||
},
|
||||
|
||||
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) {
|
||||
this.dom.warning.addClass('in');
|
||||
this.dom.force_close_btn.html(this.force_text['deactivate']);
|
||||
} else {
|
||||
this.dom.warning.removeClass('in');
|
||||
this.dom.force_close_btn.html(this.force_text['activate']);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
toggle_force_close: function(new_value, password) {
|
||||
$.post({
|
||||
url: this.force_close_url,
|
||||
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;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
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) {
|
||||
$(this.target).addClass(this.class_prefix + status);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue