From 9f80bb9b4fa82146e53ff9e933b8ee5924acd275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 27 Mar 2018 10:49:21 +0200 Subject: [PATCH] Disable buttons when clicked and enable them after the callback is complete. issue7148 review37131002 --- src/screen.js | 10 +++++----- src/tab.js | 43 ++++++++++++++++++++++++++----------------- src/view/form.js | 6 ++---- src/view/tree.js | 6 ++---- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/screen.js b/src/screen.js index e28ad63f05..43b5f409b3 100644 --- a/src/screen.js +++ b/src/screen.js @@ -1614,7 +1614,7 @@ (attributes.states || {})).pre_validate || []; prms.push(record.validate(fields, false, domain)); } - jQuery.when.apply(jQuery, prms).then(function() { + return jQuery.when.apply(jQuery, prms).then(function() { var record; for (var i = 0; i < selected_records.length; i++) { record = selected_records[i]; @@ -1632,12 +1632,12 @@ if (attributes.confirm) { prm = Sao.common.sur.run(attributes.confirm); } - prm.then(function() { + return prm.then(function() { var record = this.current_record; if (attributes.type === 'instance') { var args = record.expr_eval(attributes.change || []); var values = record._get_on_change_args(args); - record.model.execute(attributes.name, [values], + return record.model.execute(attributes.name, [values], this.context()).then(function(changes) { record.set_on_change(changes); record.group.root_group().screens.forEach( @@ -1646,7 +1646,7 @@ }); }); } else { - record.save(false).done(function() { + return record.save(false).then(function() { var context = this.context(); context._timestamp = {}; ids = []; @@ -1656,7 +1656,7 @@ record.get_timestamp()); ids.push(record.id); } - record.model.execute(attributes.name, + return record.model.execute(attributes.name, [ids], context).then(process_action.bind(this)); }.bind(this)); } diff --git a/src/tab.js b/src/tab.js index a865e47cb5..c2276033be 100644 --- a/src/tab.js +++ b/src/tab.js @@ -218,7 +218,15 @@ 'aria-hidden': 'true' })) .appendTo(group); - this.buttons[item.id].click(this[item.id].bind(this)); + this.buttons[item.id].click(item, function(event) { + var item = event.data; + var button = this.buttons[item.id]; + button.prop('disabled', true); + (this[item.id](this) || jQuery.when()) + .always(function() { + button.prop('disabled', false); + }); + }.bind(this)); }; this.menu_def().forEach(add_button.bind(this)); this.status_label = jQuery('', { @@ -588,10 +596,10 @@ }, new_: function() { if (!Sao.common.MODELACCESS.get(this.screen.model_name).create) { - return; + return jQuery.when(); } - this.modified_save().done(function() { - this.screen.new_().then(function() { + return this.modified_save().then(function() { + return this.screen.new_().then(function() { this.info_bar.message(); }.bind(this)); // TODO activate_save @@ -614,7 +622,7 @@ }.bind(this)); }, switch_: function() { - this.modified_save().done(function() { + return this.modified_save().done(function() { this.screen.switch_view(); }.bind(this)); }, @@ -645,10 +653,10 @@ }, copy: function() { if (!Sao.common.MODELACCESS.get(this.screen.model_name).create) { - return; + return jQuery.when(); } - this.modified_save().done(function() { - this.screen.copy().then(function() { + return this.modified_save().then(function() { + return this.screen.copy().then(function() { this.info_bar.message( Sao.i18n.gettext( 'Working now on the duplicated record(s).'), @@ -659,7 +667,7 @@ }, delete_: function() { if (!Sao.common.MODELACCESS.get(this.screen.model_name)['delete']) { - return; + return jQuery.when(); } var msg; if (this.screen.current_view.view_type == 'form') { @@ -667,8 +675,8 @@ } else { msg = Sao.i18n.gettext('Are you sure to remove those records?'); } - Sao.common.sur.run(msg).done(function() { - this.screen.remove(true, false, true).then( + return Sao.common.sur.run(msg).then(function() { + return this.screen.remove(true, false, true).then( function() { this.info_bar.message( Sao.i18n.gettext('Records removed.'), @@ -682,14 +690,14 @@ }.bind(this)); }, previous: function() { - this.modified_save().done(function() { + return this.modified_save().then(function() { this.screen.display_previous(); this.info_bar.message(); // TODO activate_save }.bind(this)); }, next: function() { - this.modified_save().done(function() { + return this.modified_save().then(function() { this.screen.display_next(); this.info_bar.message(); // TODO activate_save @@ -702,6 +710,7 @@ search_entry.focus(); }, 0); } + return jQuery.when(); }, logs: function() { var record = this.screen.current_record; @@ -709,7 +718,7 @@ this.info_bar.message( Sao.i18n.gettext('You have to select one record.'), 'info'); - return; + return jQuery.when(); } var fields = [ ['id', Sao.i18n.gettext('ID:')], @@ -721,7 +730,7 @@ ['write_date', Sao.i18n.gettext('Latest Modification Date:')] ]; - this.screen.model.execute('read', [[record.id], + return this.screen.model.execute('read', [[record.id], fields.map(function(field) { return field[0]; })], this.screen.context()) @@ -778,12 +787,12 @@ } }.bind(this); }.bind(this); - this.modified_save().done(function() { + return this.modified_save().then(function() { var ids = this.screen.current_view.selected_records().map( function(record) { return record.id; }); - this.screen.model.execute('history_revisions', + return this.screen.model.execute('history_revisions', [ids], this.screen.context()) .then(function(revisions) { new Sao.Window.Revision(revisions, set_revision(revisions)); diff --git a/src/view/form.js b/src/view/form.js index cbb29d0886..15d04f9299 100644 --- a/src/view/form.js +++ b/src/view/form.js @@ -382,11 +382,9 @@ function eval_pyson(value){ button_clicked: function(event) { var button = event.data; button.el.prop('disabled', true); - try { - this.screen.button(button.attributes); - } finally { + this.screen.button(button.attributes).always(function() { button.el.prop('disabled', false); - } + }); }, selected_records: function() { if (this.screen.current_record) { diff --git a/src/view/tree.js b/src/view/tree.js index 0101515f25..7903daa3ea 100644 --- a/src/view/tree.js +++ b/src/view/tree.js @@ -1952,11 +1952,9 @@ return; } button.el.prop('disabled', true); - try { - this.screen.button(this.attributes); - } finally { + this.screen.button(this.attributes).always(function() { button.el.prop('disabled', false); - } + }); } });