WIP: Aureplop/kpsul js refactor #501

Draft
delobell wants to merge 215 commits from aureplop/kpsul_js_refactor into master
2 changed files with 20 additions and 17 deletions
Showing only changes of commit 8279bddf4e - Show all commits

View file

@ -38,7 +38,7 @@ function amountDisplay(amount, is_cof=false, tri='') {
} }
function amountToUKF(amount, is_cof=false) { function amountToUKF(amount, is_cof=false) {
var coef_cof = is_cof ? 1 + settings['subvention_cof'] / 100 : 1; var coef_cof = is_cof ? 1 + window.settings['subvention_cof'] / 100 : 1;
return Math.round(amount * coef_cof * 10); return Math.round(amount * coef_cof * 10);
} }

View file

@ -170,16 +170,17 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
'use strict';
// ----- // -----
// General // General
// ----- // -----
// Lock to avoid multiple requests // Lock to avoid multiple requests
lock = 0; var lock = 0;
// Retrieve settings // Retrieve settings
settings = {} window.settings = {}
function resetSettings() { function resetSettings() {
$.ajax({ $.ajax({
@ -188,9 +189,9 @@ $(document).ready(function() {
method : "POST", method : "POST",
}) })
.done(function(data) { .done(function(data) {
settings['addcost_for'] = data['addcost_for']; window.settings['addcost_for'] = data['addcost_for'];
settings['addcost_amount'] = parseFloat(data['addcost_amount']); window.settings['addcost_amount'] = parseFloat(data['addcost_amount']);
settings['subvention_cof'] = parseFloat(data['subvention_cof']); window.settings['subvention_cof'] = parseFloat(data['subvention_cof']);
displayAddcost(); displayAddcost();
}); });
} }
@ -813,10 +814,7 @@ $(document).ready(function() {
var basket_container = $('#basket table'); var basket_container = $('#basket table');
var arrowKeys = /^(37|38|39|40)$/; var arrowKeys = /^(37|38|39|40)$/;
function amountEuroPurchase(id,nb) { function amountEuroPurchase(article_data, nb) {
var i = 0;
while (i<articlesList.length && id != articlesList[i][1]) i++;
article_data = articlesList[i];
var amount_euro = - article_data[3] * nb ; var amount_euro = - article_data[3] * nb ;
if (settings['addcost_for'] && settings['addcost_amount'] && account_data['trigramme'] != settings['addcost_for']) if (settings['addcost_for'] && settings['addcost_amount'] && account_data['trigramme'] != settings['addcost_for'])
amount_euro -= settings['addcost_amount'] * nb; amount_euro -= settings['addcost_amount'] * nb;
@ -827,9 +825,14 @@ $(document).ready(function() {
} }
function addPurchase(id, nb) { function addPurchase(id, nb) {
var amount_euro = amountEuroPurchase(id, nb).toFixed(2);
var i = 0;
while (i<articlesList.length && id != articlesList[i][1]) i++;
var article_data = articlesList[i];
var amount_euro = amountEuroPurchase(article_data, nb).toFixed(2);
var index = addPurchaseToFormset(article_data[1], nb, amount_euro); var index = addPurchaseToFormset(article_data[1], nb, amount_euro);
article_basket_html = $(item_basket_default_html); var article_basket_html = $(item_basket_default_html);
article_basket_html article_basket_html
.attr('data-opeindex', index) .attr('data-opeindex', index)
.find('.number').text(nb).end() .find('.number').text(nb).end()
@ -1079,7 +1082,7 @@ $(document).ready(function() {
// History // History
// ----- // -----
khistory = new KHistory(); var khistory = new KHistory();
function getHistory() { function getHistory() {
var data = { var data = {
@ -1222,14 +1225,14 @@ $(document).ready(function() {
// Synchronization // Synchronization
// ----- // -----
websocket_msg_default = {'opegroups':[],'opes':[],'checkouts':[],'articles':[]} var websocket_msg_default = {'opegroups':[],'opes':[],'checkouts':[],'articles':[]}
var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws'; var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws';
var location_host = window.location.host; var location_host = window.location.host;
var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host;
socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/"); var socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/");
socket.onmessage = function(e) { socket.onmessage = function(e) {
data = $.extend({}, websocket_msg_default, JSON.parse(e.data)); var data = $.extend({}, websocket_msg_default, JSON.parse(e.data));
for (var i=0; i<data['opegroups'].length; i++) { for (var i=0; i<data['opegroups'].length; i++) {
if (data['opegroups'][i]['add']) { if (data['opegroups'][i]['add']) {
@ -1250,7 +1253,7 @@ $(document).ready(function() {
} }
} }
for (var i=0; i<data['articles'].length; i++) { for (var i=0; i<data['articles'].length; i++) {
article = data['articles'][i]; var article = data['articles'][i];
articles_container.find('#data-article-'+article['id']+' .stock') articles_container.find('#data-article-'+article['id']+' .stock')
.text(article['stock']); .text(article['stock']);
} }