WIP: Aureplop/kpsul js refactor #501

Draft
delobell wants to merge 215 commits from aureplop/kpsul_js_refactor into master
3 changed files with 74 additions and 66 deletions
Showing only changes of commit 2774dbb5de - Show all commits

View file

@ -4,65 +4,6 @@
* @license MIT * @license MIT
*/ */
/**
* Get and store K-Psul config from API.
* <br><br>
*
* Config should be accessed statically only.
*/
class Config {
/**
* Get or create config object.
* @private
* @return {object} object - config keys/values
*/
static _get_or_create_config() {
if (window.config === undefined)
window.config = {};
return window.config;
}
/**
* Get config from API.
* @param {jQueryAjaxComplete} [callback] - A function to be called when
* the request finishes.
*/
static reset(callback) {
$.getJSON(Urls['kfet.kpsul.get_settings']())
.done(function(data) {
for (var key in data) {
Config.set(key, data[key]);
}
})
.always(callback);
}
/**
* Get value for key in config.
* @param {string} key
*/
static get(key) {
return this._get_or_create_config()[key];
}
/**
* Set value for key in config.
* @param {string} key
* @param {*} value
*/
static set(key, value) {
// API currently returns string for Decimal type
if (['addcost_amount', 'subvention_cof'].indexOf(key) > -1)
value = floatCheck(value);
this._get_or_create_config()[key] = value;
}
}
/* ---------- ---------- */
/** /**
* Virtual namespace for models. * Virtual namespace for models.
@ -154,14 +95,23 @@ class ModelObject {
*/ */
from(data) { from(data) {
// TODO: add restrict // TODO: add restrict
$.extend(this, this.constructor.default_data, data); this.clear();
this.update(data);
}
/**
* Update properties of this instance from data ones.
* @param {Object} data
*/
update(data) {
$.extend(this, data);
} }
/** /**
* Clear properties to {@link Models.ModelObject.default_data|default_data}. * Clear properties to {@link Models.ModelObject.default_data|default_data}.
*/ */
clear() { clear() {
this.from({}); $.extend(this, this.constructor.default_data);
} }
@ -172,10 +122,12 @@ class ModelObject {
* @param {Formatters.Formatter} * @param {Formatters.Formatter}
* [formatter={@link Models.ModelObject#formatter|this.formatter()}] * [formatter={@link Models.ModelObject#formatter|this.formatter()}]
* Formatter class to use. * Formatter class to use.
* @return {jQuery} The DOM element $container, allowing methods chaining.
*/ */
display($container, options, formatter) { display($container, options, formatter) {
formatter = formatter || this.formatter(); formatter = formatter || this.formatter();
return formatter.render(this, $container, options); formatter.render(this, $container, options);
return $container;
} }
/** /**

View file

@ -70,6 +70,63 @@ function booleanCheck(v) {
} }
/**
* Get and store K-Psul config from API.
* <br><br>
*
* Config should be accessed statically only.
*/
class Config {
/**
* Get or create config object.
* @private
* @return {object} object - config keys/values
*/
static _get_or_create_config() {
if (window.config === undefined)
window.config = {};
return window.config;
}
/**
* Get config from API.
* @param {jQueryAjaxComplete} [callback] - A function to be called when
* the request finishes.
*/
static reset(callback) {
$.getJSON(Urls['kfet.kpsul.get_settings']())
.done(function(data) {
for (var key in data) {
Config.set(key, data[key]);
}
})
.always(callback);
}
/**
* Get value for key in config.
* @param {string} key
*/
static get(key) {
return this._get_or_create_config()[key];
}
/**
* Set value for key in config.
* @param {string} key
* @param {*} value
*/
static set(key, value) {
// API currently returns string for Decimal type
if (['addcost_amount', 'subvention_cof'].indexOf(key) > -1)
value = floatCheck(value);
this._get_or_create_config()[key] = value;
}
}
$(document).ready(function() { $(document).ready(function() {
$(window).scroll(function() { $(window).scroll(function() {
if ($(window).width() >= 768 && $(this).scrollTop() > 72.6) { if ($(window).width() >= 768 && $(this).scrollTop() > 72.6) {

View file

@ -13,7 +13,6 @@
<script type="text/javascript" src="{% static 'kfet/js/moment-fr.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/moment-fr.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}"></script>
<script type="text/javascript" src="{% url 'js_reverse' %}"></script> <script type="text/javascript" src="{% url 'js_reverse' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet.api.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/kfet.api.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kpsul.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/kpsul.js' %}"></script>
@ -279,7 +278,7 @@ $(document).ready(function() {
function amountEuroPurchase(article, nb) { function amountEuroPurchase(article, nb) {
var amount_euro = - article.price * nb ; var amount_euro = - article.price * nb ;
if (Config.get('addcost_for') && Config.get('addcost_amount') && account_manager.account.trigramme != Config.get('addcost_for')) if (Config.get('addcost_for') && Config.get('addcost_amount') && kpsul.account_manager.account.trigramme != Config.get('addcost_for'))
amount_euro -= Config.get('addcost_amount') * nb; amount_euro -= Config.get('addcost_amount') * nb;
var reduc_divisor = 1; var reduc_divisor = 1;
if (kpsul.account_manager.account.is_cof) if (kpsul.account_manager.account.is_cof)
@ -565,7 +564,7 @@ $(document).ready(function() {
addcost_html addcost_html
.find('.addcost_for').text(Config.get('addcost_for')).end() .find('.addcost_for').text(Config.get('addcost_for')).end()
.find('.addcost_amount').text(Config.get('addcost_amount').toFixed(2)).end(); .find('.addcost_amount').text(Config.get('addcost_amount').toFixed(2)).end();
checkout_container.prepend(addcost_html); kpsul.checkout_manager._$container.prepend(addcost_html);
$('body').css('animation', 'colorchange 3s infinite'); $('body').css('animation', 'colorchange 3s infinite');
} }
} }
@ -622,7 +621,7 @@ $(document).ready(function() {
} }
}); });
}, },
onClose: function() { this._lastFocused = account_manager._$input_trigramme; } onClose: function() { this._lastFocused = kpsul.account_manager.selection._$input; }
}); });
} }