kpsul/kfet/static/kfet/js/kfet.js

187 lines
6.5 KiB
JavaScript
Raw Normal View History

$(document).ready(function() {
2016-08-26 15:30:40 +02:00
if (typeof Cookies !== 'undefined') {
// Retrieving csrf token
csrftoken = Cookies.get('csrftoken');
// Appending csrf token to ajax post requests
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
2016-08-26 15:30:40 +02:00
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
}
});
2017-03-31 17:23:27 +02:00
/*
* Generic Websocket class and k-psul ws instanciation
*/
2017-03-31 20:57:15 +02:00
class KfetWebsocket {
2017-03-31 17:23:27 +02:00
2017-03-31 22:37:39 +02:00
static get defaults() {
return {"relative_url": "", "default_msg": {}, "handlers": []};
2017-03-31 17:23:27 +02:00
}
constructor(data) {
2017-03-31 22:37:39 +02:00
$.extend(this, this.constructor.defaults, data);
2017-03-31 17:23:27 +02:00
}
2017-03-31 20:10:41 +02:00
get url() {
2017-03-31 17:23:27 +02:00
var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws';
var location_host = window.location.host;
var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host;
2017-03-31 20:10:41 +02:00
return websocket_protocol+"://" + location_url + this.relative_url ;
}
2017-03-31 22:37:39 +02:00
add_handler(handler) {
if (!this.socket)
this.listen();
this.handlers.push(handler);
}
listen() {
2017-03-31 20:10:41 +02:00
var that = this;
2017-03-31 20:45:15 +02:00
this.socket = new ReconnectingWebSocket(this.url);
2017-03-31 17:23:27 +02:00
2017-03-31 20:45:15 +02:00
this.socket.onmessage = function(e) {
2017-03-31 17:23:27 +02:00
var data = $.extend({}, that.default_msg, JSON.parse(e.data));
2017-03-31 22:37:39 +02:00
for (let handler of that.handlers) {
handler(data);
}
2017-03-31 17:23:27 +02:00
}
}
}
2017-03-31 20:57:15 +02:00
var OperationWebSocket = new KfetWebsocket({
2017-03-31 17:23:27 +02:00
'relative_url': '/ws/k-fet/k-psul/',
'default_msg': {'opegroups':[],'opes':[],'checkouts':[],'articles':[]},
});
function dateUTCToParis(date) {
return moment.tz(date, 'UTC').tz('Europe/Paris');
}
function amountDisplay(amount, is_cof=false, tri='') {
if (tri == 'LIQ')
return (- amount).toFixed(2) +'€';
return amountToUKF(amount, is_cof);
}
2017-02-12 22:05:41 +01:00
function amountToUKF(amount, is_cof=false, account=false) {
var rounding = account ? Math.floor : Math.round ;
var coef_cof = is_cof ? 1 + settings['subvention_cof'] / 100 : 1;
2017-02-12 22:05:41 +01:00
return rounding(amount * coef_cof * 10);
}
2016-08-26 15:30:40 +02:00
function isValidTrigramme(trigramme) {
var pattern = /^[^a-z]{3}$/;
return trigramme.match(pattern);
}
function getErrorsHtml(data) {
var content = '';
if (!data)
return "L'utilisateur n'est pas dans l'équipe";
2016-08-26 15:30:40 +02:00
if ('operation_group' in data['errors']) {
content += 'Général';
content += '<ul>';
if (data['errors']['operation_group'].indexOf('on_acc') != -1)
content += '<li>Pas de compte sélectionné</li>';
if (data['errors']['operation_group'].indexOf('checkout') != -1)
content += '<li>Pas de caisse sélectionnée</li>';
content += '</ul>';
}
if ('missing_perms' in data['errors']) {
content += 'Permissions manquantes';
content += '<ul>';
for (var i=0; i<data['errors']['missing_perms'].length; i++)
content += '<li>'+data['errors']['missing_perms'][i]+'</li>';
content += '</ul>';
}
if ('negative' in data['errors']) {
2016-09-06 18:50:57 +02:00
if (window.location.pathname.startsWith('/gestion/')) {
var url_base = '/gestion/k-fet/accounts/';
} else {
var url_base = '/k-fet/accounts/';
}
for (var i=0; i<data['errors']['negative'].length; i++) {
2016-09-05 19:50:16 +02:00
content += '<a class="btn btn-primary" href="'+url_base+data['errors']['negative'][i]+'/edit" target="_blank" style="width:100%">Autorisation de négatif requise pour '+data['errors']['negative'][i]+'</a>';
}
}
2016-08-26 15:30:40 +02:00
if ('addcost' in data['errors']) {
content += '<ul>';
if (data['errors']['addcost'].indexOf('__all__') != -1)
content += '<li>Compte invalide</li>';
if (data['errors']['addcost'].indexOf('amount') != -1)
content += '<li>Montant invalide</li>';
content += '</ul>';
}
2017-03-25 14:39:53 +01:00
if ('account' in data['errors']) {
content += 'Général';
content += '<ul>';
content += '<li>Opération invalide sur le compte '+data['errors']['account']+'</li>';
content += '</ul>';
}
2016-08-26 15:30:40 +02:00
return content;
}
function requestAuth(data, callback, focus_next = null) {
var content = getErrorsHtml(data);
2016-11-18 01:35:33 +01:00
content += '<div class="capslock"><span class="glyphicon glyphicon-lock"></span><input type="password" name="password" autofocus><div>',
2016-08-26 15:30:40 +02:00
$.confirm({
title: 'Authentification requise',
content: content,
backgroundDismiss: true,
animation:'top',
closeAnimation:'bottom',
keyboardEnabled: true,
confirm: function() {
var password = this.$content.find('input').val();
callback(password);
},
onOpen: function() {
var that = this;
2016-11-18 01:35:33 +01:00
var capslock = -1 ; // 1 -> caps on ; 0 -> caps off ; -1 or 2 -> unknown
2016-08-26 15:30:40 +02:00
this.$content.find('input').on('keypress', function(e) {
if (e.keyCode == 13)
that.$confirmButton.click();
2016-11-18 01:35:33 +01:00
var s = String.fromCharCode(e.which);
if ((s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey)|| //caps on, shift off
(s.toUpperCase() !== s && s.toLowerCase() === s && e.shiftKey)) { //caps on, shift on
capslock = 1 ;
} else if ((s.toLowerCase() === s && s.toUpperCase() !== s && !e.shiftKey)|| //caps off, shift off
(s.toLowerCase() !== s && s.toUpperCase() === s && e.shiftKey)) { //caps off, shift on
capslock = 0 ;
}
if (capslock == 1)
$('.capslock .glyphicon').show() ;
else if (capslock == 0)
$('.capslock .glyphicon').hide() ;
});
// Capslock key is not detected by keypress
this.$content.find('input').on('keydown', function(e) {
if (e.which == 20) {
capslock = 1-capslock ;
}
if (capslock == 1)
$('.capslock .glyphicon').show() ;
else if (capslock == 0)
$('.capslock .glyphicon').hide() ;
2016-08-26 15:30:40 +02:00
});
},
onClose: function() {
if (focus_next)
this._lastFocused = focus_next;
}
2016-11-18 01:35:33 +01:00
2016-08-26 15:30:40 +02:00
});
}