diff --git a/kfet/static/kfet/js/account.js b/kfet/static/kfet/js/account.js index 3e216155..ac98e1dd 100644 --- a/kfet/static/kfet/js/account.js +++ b/kfet/static/kfet/js/account.js @@ -10,7 +10,6 @@ var Account = Backbone.Model.extend({ 'is_frozen': false, 'departement': '', 'nickname': '', - 'trigramme': '', }, url: function () { @@ -18,8 +17,8 @@ var Account = Backbone.Model.extend({ }, reset: function () { - // On ne veut pas trigger un `change` deux fois - this.clear({ silent: true }).set(this.defaults) + // On n'utilise pas .clear() car on ne veut pas clear le trigramme + this.set(this.defaults) }, parse: function (resp, options) { @@ -31,27 +30,37 @@ var Account = Backbone.Model.extend({ }, view: function () { - view_class = this.get("trigramme") == 'LIQ' ? LIQView : AccountView; + if (!this.is_valid()) { + view_class = EmptyAccountView + } else if (this.get("trigramme") == 'LIQ') { + view_class = LIQView + } else { + view_class = AccountView + } return new view_class({ model: this }) }, render: function () { this.view().render(); - } + }, + + is_valid: function () { + return (this.id != 0) + }, }) var AccountView = Backbone.View.extend({ el: '#account', - input: '#id_trigramme', buttons: '.buttons', + id_field: "#id_on_acc", props: _.keys(Account.prototype.defaults), get: function (property) { /* If the function this.get_ is defined, we call it ; else we call this.model.. */ - getter_name = 'get_' + property; + getter_name = `get_${property}`; if (_.functions(this).includes(getter_name)) return this[getter_name]() else @@ -67,9 +76,9 @@ var AccountView = Backbone.View.extend({ }, attr_data_balance: function () { - if (this.model.id == 0) { - return ''; - } else if (this.model.get("is_frozen")) { + // Cette fonction est utilisée uniquement sur un compte valide + + if (this.model.get("is_frozen")) { return "frozen"; } else if (this.model.get("balance") < 0) { return 'neg'; @@ -81,23 +90,9 @@ var AccountView = Backbone.View.extend({ }, get_buttons: function () { - var buttons = ''; - if (this.model.id != 0) { - var url = django_urls["kfet.account.read"](encodeURIComponent(this.model.get("trigramme"))) - buttons += ``; - } else { - var trigramme = this.$(this.input).val().toUpperCase(); - if (isValidTrigramme(trigramme)) { - trigramme = encodeURIComponent(trigramme); - var url_base = django_urls["kfet.account.create"](); - var url = `${url_base}?trigramme=${trigramme}`; - buttons += ``; - } else { - buttons += ''; - } - } + var url = django_urls["kfet.account.read"](this.model.get("trigramme")); - return buttons + return ``; }, render: function () { @@ -108,16 +103,7 @@ var AccountView = Backbone.View.extend({ this.$el.attr("data-balance", this.attr_data_balance()); this.$(this.buttons).html(this.get_buttons()); - }, - - reset: function () { - for (let prop of this.props) { - var selector = "#account-" + prop; - this.$(selector).text(''); - } - - this.$el.attr("data-balance", ''); - this.$(this.buttons).html(this.get_buttons()); + $(this.id_field).val(this.get("id")); }, }) @@ -131,3 +117,28 @@ var LIQView = AccountView.extend({ } }) +var EmptyAccountView = AccountView.extend({ + get: function () { + return ''; + }, + + attr_data_balance: function () { + return ''; + }, + + get_buttons: function () { + /* Léger changement de fonctionnement : + on affiche *toujours* le bouton de recherche si + le compte est invalide */ + buttons = ''; + trigramme = this.model.get("trigramme") + if (trigramme.is_valid_tri()) { + trigramme = encodeURIComponent(trigramme); + var url_base = django_urls["kfet.account.create"](); + var url = `${url_base}?trigramme=${trigramme}`; + buttons += ``; + } + + return buttons + } +}) \ No newline at end of file