diff --git a/kfet/static/kfet/js/kfet.api.js b/kfet/static/kfet/js/kfet.api.js index 348fe57b..293ff6a2 100644 --- a/kfet/static/kfet/js/kfet.api.js +++ b/kfet/static/kfet/js/kfet.api.js @@ -4,65 +4,6 @@ * @license MIT */ -/** - * Get and store K-Psul config from API. - *

- * - * 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. @@ -143,14 +84,23 @@ class ModelObject { */ from(data) { // 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() { - this.from({}); + $.extend(this, this.constructor.default_data); } @@ -161,10 +111,12 @@ class ModelObject { * @param {Formatters.Formatter} * [formatter={@link Models.ModelObject#formatter|this.formatter()}] * Formatter class to use. + * @return {jQuery} The DOM element $container, allowing methods chaining. */ display($container, options, formatter) { formatter = formatter || this.formatter(); - return formatter.render(this, $container, options); + formatter.render(this, $container, options); + return $container; } /** diff --git a/kfet/static/kfet/js/kfet.js b/kfet/static/kfet/js/kfet.js index 0fb5c0e1..cf120fd4 100644 --- a/kfet/static/kfet/js/kfet.js +++ b/kfet/static/kfet/js/kfet.js @@ -40,6 +40,63 @@ function booleanCheck(v) { } +/** + * Get and store K-Psul config from API. + *

+ * + * 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() { $(window).scroll(function() { if ($(window).width() >= 768 && $(this).scrollTop() > 72.6) { diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 1657ba6d..af957944 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -13,7 +13,6 @@ - @@ -346,7 +345,7 @@ $(document).ready(function() { function amountEuroPurchase(article, 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; var reduc_divisor = 1; if (kpsul.account_manager.account.is_cof) @@ -655,7 +654,7 @@ $(document).ready(function() { addcost_html .find('.addcost_for').text(Config.get('addcost_for')).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'); } } @@ -712,7 +711,7 @@ $(document).ready(function() { } }); }, - onClose: function() { this._lastFocused = account_manager._$input_trigramme; } + onClose: function() { this._lastFocused = kpsul.account_manager.selection._$input; } }); }