WIP: Aureplop/kpsul js refactor #501

Draft
delobell wants to merge 215 commits from aureplop/kpsul_js_refactor into master
2 changed files with 60 additions and 31 deletions
Showing only changes of commit 5c7a1d6874 - 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 + window.settings['subvention_cof'] / 100 : 1; var coef_cof = is_cof ? 1 + Config.getAll().subvention_cof / 100 : 1;
return Math.round(amount * coef_cof * 10); return Math.round(amount * coef_cof * 10);
} }

View file

@ -169,6 +169,50 @@
{% csrf_token %} {% csrf_token %}
<script type="text/javascript"> <script type="text/javascript">
function floatCheck(v) {
if (typeof v === 'number')
return v;
return Number.parseFloat(v);
}
class Config {
static getAll() {
if (typeof window.config === 'undefined')
window.config = {};
return window.config;
}
static reset(callback) {
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.get_settings' %}",
method : "POST",
})
.done(function(data) {
Config.addcost_for = data['addcost_for'];
Config.addcost_amount = data['addcost_amount'];
Config.subvention_cof = data['subvention_cof'];
})
.always(callback);
}
static set addcost_for(v) {
Config.getAll().addcost_for = v;
}
static set addcost_amount(v) {
Config.getAll().addcost_amount = floatCheck(v);
}
static set subvention_cof(v) {
Config.getAll().subvention_cof = floatCheck(v);
}
}
$(document).ready(function() { $(document).ready(function() {
'use strict'; 'use strict';
// ----- // -----
@ -178,25 +222,6 @@ $(document).ready(function() {
// Lock to avoid multiple requests // Lock to avoid multiple requests
var lock = 0; var lock = 0;
// Retrieve settings
window.settings = {}
function resetSettings() {
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.get_settings' %}",
method : "POST",
})
.done(function(data) {
window.settings['addcost_for'] = data['addcost_for'];
window.settings['addcost_amount'] = parseFloat(data['addcost_amount']);
window.settings['subvention_cof'] = parseFloat(data['subvention_cof']);
displayAddcost();
});
}
// ----- // -----
// Account data management // Account data management
// ----- // -----
@ -815,12 +840,13 @@ $(document).ready(function() {
var arrowKeys = /^(37|38|39|40)$/; var arrowKeys = /^(37|38|39|40)$/;
function amountEuroPurchase(article_data, nb) { function amountEuroPurchase(article_data, nb) {
var cfg = Config.getAll();
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 (cfg.addcost_for && cfg.addcost_amount && account_data['trigramme'] != cfg.addcost_for)
amount_euro -= settings['addcost_amount'] * nb; amount_euro -= cfg.addcost_amount * nb;
var reduc_divisor = 1; var reduc_divisor = 1;
if (account_data['is_cof']) if (account_data['is_cof'])
reduc_divisor = 1 + settings['subvention_cof'] / 100; reduc_divisor = 1 + cfg.subvention_cof / 100;
return amount_euro / reduc_divisor; return amount_euro / reduc_divisor;
} }
@ -1122,13 +1148,14 @@ $(document).ready(function() {
var addcost_default_html = '<div id="addcost">Majoration vers <span class="addcost_for"></span> de <span class="addcost_amount"></span></div>'; var addcost_default_html = '<div id="addcost">Majoration vers <span class="addcost_for"></span> de <span class="addcost_amount"></span></div>';
function displayAddcost() { function displayAddcost() {
var cfg = Config.getAll();
checkout_container.find('#addcost').remove(); checkout_container.find('#addcost').remove();
$('body').css('animation', 'none'); $('body').css('animation', 'none');
if (settings['addcost_for'] && settings['addcost_amount']) { if (cfg.addcost_for && cfg.addcost_amount) {
var addcost_html = $(addcost_default_html); var addcost_html = $(addcost_default_html);
addcost_html addcost_html
.find('.addcost_for').text(settings['addcost_for']).end() .find('.addcost_for').text(cfg.addcost_for).end()
.find('.addcost_amount').text(settings['addcost_amount'].toFixed(2)).end(); .find('.addcost_amount').text(cfg.addcost_amount.toFixed(2)).end();
checkout_container.prepend(addcost_html); checkout_container.prepend(addcost_html);
$('body').css('animation', 'colorchange 3s infinite'); $('body').css('animation', 'colorchange 3s infinite');
} }
@ -1258,8 +1285,8 @@ $(document).ready(function() {
.text(article['stock']); .text(article['stock']);
} }
if (data['addcost']) { if (data['addcost']) {
settings['addcost_for'] = data['addcost']['for']; Config.addcost_for = data['addcost']['for'];
settings['addcost_amount'] = parseFloat(data['addcost']['amount']); Config.addcost_amount = data['addcost']['amount'];
displayAddcost(); displayAddcost();
} }
} }
@ -1285,9 +1312,11 @@ $(document).ready(function() {
resetArticles(); resetArticles();
resetPreviousOp(); resetPreviousOp();
khistory.reset(); khistory.reset();
resetSettings(); Config.reset(function() {
getArticles(); displayAddcost();
getHistory(); getArticles();
getHistory();
});
} }
function resetSelectable() { function resetSelectable() {