From 0b5dd24b8043455258d650def227ffcaabd4c098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 3 Oct 2024 14:44:09 +0200 Subject: [PATCH] Update usage of synchronous Record.validate and _check_load Closes #13497 --- sao/src/model.js | 55 ++++------ sao/src/screen.js | 94 +++++++---------- sao/src/view/form.js | 24 ++--- sao/src/view/tree.js | 245 +++++++++++++++++++++---------------------- sao/src/window.js | 28 ++--- sao/src/wizard.js | 2 +- 6 files changed, 203 insertions(+), 245 deletions(-) diff --git a/sao/src/model.js b/sao/src/model.js index 3250f70d39..1f1a9ea3ad 100644 --- a/sao/src/model.js +++ b/sao/src/model.js @@ -1023,19 +1023,14 @@ return jQuery.when.apply(jQuery, promises).then(() => { this.on_change(fieldnames); this.on_change_with(fieldnames); - const callback = () => { - if (modified) { - this.set_modified(); - return jQuery.when.apply( - jQuery, this.group.root_group.screens - .map(screen => screen.display())); - } - }; if (validate) { - return this.validate(null, true) - .then(callback); - } else { - return callback(); + return this.validate(null, true); + } + if (modified) { + this.set_modified(); + return jQuery.when.apply( + jQuery, this.group.root_group.screens + .map(screen => screen.display())); } }); }, @@ -1400,31 +1395,23 @@ return path; }, children_group: function(field_name) { - var group_prm = jQuery.Deferred(); if (!field_name) { - group_prm.resolve([]); - return group_prm; + return []; } - var load_prm = this._check_load([field_name]); - load_prm.done(() => { - var group = this._values[field_name]; - if (group === undefined) { - group_prm.resolve(null); - return; - } - - if (group.model.fields !== this.group.model.fields) { - jQuery.extend(this.group.model.fields, group.model.fields); - group.model.fields = this.group.model.fields; - } - group.on_write = this.group.on_write; - group.readonly = this.group.readonly; - jQuery.extend(group._context, this.group._context); - - group_prm.resolve(group); + this._check_load([field_name]); + var group = this._values[field_name]; + if (group === undefined) { return; - }); - return group_prm; + } + + if (group.model.fields !== this.group.model.fields) { + jQuery.extend(this.group.model.fields, group.model.fields); + group.model.fields = this.group.model.fields; + } + group.on_write = this.group.on_write; + group.readonly = this.group.readonly; + jQuery.extend(group._context, this.group._context); + return group; }, get deleted() { return Boolean(~this.group.record_deleted.indexOf(this)); diff --git a/sao/src/screen.js b/sao/src/screen.js index a03ab810a5..85db45d234 100644 --- a/sao/src/screen.js +++ b/sao/src/screen.js @@ -955,8 +955,7 @@ } var fields = this.current_view.get_fields(); if (this.current_record && this.current_view.editable && - !this.current_record.validate( - fields, false, false, true)) { + !this.current_record.validate(fields, false, false)) { this.screen_container.set(this.current_view.el); return this.current_view.display().done(() => { this.set_cursor(); @@ -1039,7 +1038,7 @@ } var context_record = this.context_screen.current_record; if (context_record && - !context_record.validate(null, false, null, true)) { + !context_record.validate(null, false, null)) { this.new_group(); this.context_screen.display(true); return jQuery.when(); @@ -1630,7 +1629,7 @@ var prm = jQuery.Deferred(); if (this.current_view && (this.current_view.view_type == 'tree')) { prm = this.group.save().then(() => this.current_record); - } else if (current_record.validate(fields, null, null, true)) { + } else if (current_record.validate(fields, null, null)) { prm = current_record.save().then(() => current_record); } else if (this.current_view) { return this.current_view.display().then(() => { @@ -1998,65 +1997,52 @@ var fields = this.current_view.get_fields(); } - var prms = []; - const reset_state = record => { - return () => { - this.display(true); - // Reset valid state with normal domain - record.validate(fields); - }; - }; for (const record of selected_records) { const domain = record.expr_eval( (attributes.states || {})).pre_validate || []; - prms.push(record.validate(fields, false, domain)); - } - return jQuery.when.apply(jQuery, prms).then((...args) => { - var record; - for (var i = 0; i < selected_records.length; i++) { - record = selected_records[i]; - var result = args[i]; - if (result) { - continue; - } + if (!record.validate(fields, false, domain)) { Sao.common.warning.run( - this.invalid_message(record), - Sao.i18n.gettext('Pre-validation')) - .then(reset_state(record)); + this.invalid_message(record), + Sao.i18n.gettext('Pre-validation')) + .then(() => { + this.display(true); + // Reset valid state with normal domain + record.validate(fields); + }); return; } - var prm = jQuery.when(); - if (attributes.confirm) { - prm = Sao.common.sur.run(attributes.confirm); - } - return prm.then(() => { - var record = this.current_record; - if (attributes.type === 'instance') { - var args = record.expr_eval(attributes.change || []); - var values = record._get_on_change_args(args); - return record.model.execute(attributes.name, [values], - this.context).then(function(changes) { + } + var prm = jQuery.when(); + if (attributes.confirm) { + prm = Sao.common.sur.run(attributes.confirm); + } + return prm.then(() => { + var record = this.current_record; + if (attributes.type === 'instance') { + var args = record.expr_eval(attributes.change || []); + var values = record._get_on_change_args(args); + return record.model.execute(attributes.name, [values], + this.context).then(function(changes) { record.set_on_change(changes); record.set_modified(); }); - } else { - return record.save(false).then(() => { - var context = this.context; - context._timestamp = {}; - ids = []; - for (i = 0; i < selected_records.length; i++) { - record = selected_records[i]; - jQuery.extend(context._timestamp, - record.get_timestamp()); - ids.push(record.id); - } - return record.model.execute(attributes.name, - [ids], context) - .then(process_action) - .fail(() => this.reload(ids, true)); - }); - } - }); + } else { + return record.save(false).then(() => { + var context = this.context; + context._timestamp = {}; + ids = []; + for (let i = 0; i < selected_records.length; i++) { + record = selected_records[i]; + jQuery.extend(context._timestamp, + record.get_timestamp()); + ids.push(record.id); + } + return record.model.execute(attributes.name, + [ids], context) + .then(process_action) + .fail(() => this.reload(ids, true)); + }); + } }); }, client_action: function(action) { diff --git a/sao/src/view/form.js b/sao/src/view/form.js index 9b5d6ad663..df94f44809 100644 --- a/sao/src/view/form.js +++ b/sao/src/view/form.js @@ -3882,21 +3882,17 @@ function eval_pyson(value){ var record = this.screen.current_record; if (record) { var fields = this.screen.current_view.get_fields(); - record.validate(fields).then(validate => { - if (!validate) { - this.screen.display(true); - prm.reject(); - return; - } - if (this.screen.pre_validate) { - return record.pre_validate().then( - prm.resolve, prm.reject); - } - prm.resolve(); - }); - } else { - prm.resolve(); + if (!record.validate(fields)) { + this.screen.display(true); + prm.reject(); + return; + } + if (this.screen.pre_validate) { + return record.pre_validate().then( + prm.resolve, prm.reject); + } } + prm.resolve(); return prm; }, set_value: function() { diff --git a/sao/src/view/tree.js b/sao/src/view/tree.js index 00adc39962..eb8def9819 100644 --- a/sao/src/view/tree.js +++ b/sao/src/view/tree.js @@ -641,77 +641,75 @@ }; row.rows.forEach(move_child); - var dest_group_prm; + var dest_group; var origin_group, origin_position; origin_group = record.group; origin_position = row.group_position; if (parent_row) { - dest_group_prm = parent_row.record.children_group( + dest_group = parent_row.record.children_group( this.children_field); } else { - dest_group_prm = jQuery.Deferred().resolve(this.group); + dest_group = this.group; } - dest_group_prm.then(dest_group => { - var origin_rows, dest_rows; - if (row.parent_) { - origin_rows = row.parent_.rows; + var origin_rows, dest_rows; + if (row.parent_) { + origin_rows = row.parent_.rows; + } else { + origin_rows = this.rows; + } + if (parent_row) { + dest_rows = parent_row.rows; + } else { + dest_rows = this.rows; + } + + if (origin_group === dest_group) { + if (origin_position < dest_position) { + dest_position -= 1; + } + origin_group.splice(origin_position, 1); + origin_group.splice(dest_position, 0, record); + origin_group.record_modified(); + } else { + origin_group.remove(record, true, true); + // Don't remove record from previous group + // as the new parent will change the parent + // This prevents concurrency conflict + origin_group.record_removed.splice( + origin_group.record_removed.indexOf(record)); + dest_group.add(record, dest_position); + if (!record.parent_name) { + record.modified_fields[origin_group.parent_name] = true; + record._values[origin_group.parent_name] = null; } else { - origin_rows = this.rows; - } - if (parent_row) { - dest_rows = parent_row.rows; - } else { - dest_rows = this.rows; + record.modified_fields[origin_group.parent_name] = true; } + } + dest_rows.splice(dest_position, 0, row); + origin_rows.splice(origin_position, 1); - if (origin_group === dest_group) { - if (origin_position < dest_position) { - dest_position -= 1; - } - origin_group.splice(origin_position, 1); - origin_group.splice(dest_position, 0, record); - origin_group.record_modified(); - } else { - origin_group.remove(record, true, true); - // Don't remove record from previous group - // as the new parent will change the parent - // This prevents concurrency conflict - origin_group.record_removed.splice( - origin_group.record_removed.indexOf(record)); - dest_group.add(record, dest_position); - if (!record.parent_name) { - record.modified_fields[origin_group.parent_name] = true; - record._values[origin_group.parent_name] = null; - } else { - record.modified_fields[origin_group.parent_name] = true; - } - } - dest_rows.splice(dest_position, 0, row); - origin_rows.splice(origin_position, 1); + row.parent_ = parent_row; + row.record.group = dest_group; + for (const r of dest_rows.slice(dest_position)) { + r.reset_path(); + } + for (const r of origin_rows.slice(origin_position)) { + r.reset_path(); + } - row.parent_ = parent_row; - row.record.group = dest_group; - for (const r of dest_rows.slice(dest_position)) { - r.reset_path(); - } - for (const r of origin_rows.slice(origin_position)) { - r.reset_path(); - } + var selected = this.get_selected_paths(); + row.redraw(selected); + var child_redraw = function(child_row) { + child_row.redraw(selected); + child_row.rows.forEach(child_redraw); + }; + row.rows.forEach(child_redraw); - var selected = this.get_selected_paths(); - row.redraw(selected); - var child_redraw = function(child_row) { - child_row.redraw(selected); - child_row.rows.forEach(child_redraw); - }; - row.rows.forEach(child_redraw); - - if (this.attributes.sequence) { - row.record.group.set_sequence( - this.attributes.sequence, this.screen.new_position); - } - }); + if (this.attributes.sequence) { + row.record.group.set_sequence( + this.attributes.sequence, this.screen.new_position); + } }, get_fields: function() { return Object.keys(this.widgets); @@ -2148,74 +2146,71 @@ event_.which == Sao.common.DOWN_KEYCODE || event_.which == Sao.common.RETURN_KEYCODE) { next_column = this.edited_column; - this.record.validate(this.tree.get_fields()) - .then(validate => { - if (!validate) { - var invalid_fields = - this.record.invalid_fields(); - for (i = 0; i < this.tree.columns.length; i++) { - var col = this.tree.columns[i]; - if (col.attributes.name in invalid_fields) { - next_column = i; - break; - } - } - this._get_column_td(next_column) - .find(':input,[tabindex=0]').focus(); - } else { - var prm = jQuery.when(); - if (!this.tree.screen.group.parent) { - prm = this.record.save(); - } else if (this.tree.screen.attributes.pre_validate) { - prm = this.record.pre_validate(); - } - prm.fail(function() { - widget.focus(); - }); - var next_row; - if (event_.which == Sao.common.UP_KEYCODE) { - next_row = this.el.prev('tr'); - } else if (event_.which == Sao.common.DOWN_KEYCODE) { - next_row = this.el.next('tr'); - } else { - if (this.tree.screen.new_position == -1) { - next_row = this.el.next('tr'); - } else { - next_row = this.el.prev('tr'); - } - } - if (!next_row.length && - ((event_.which == Sao.common.RETURN_KEYCODE) || - ((event_.which == Sao.common.UP_KEYCODE) && - (this.tree.screen.new_position == 0)) || - ((event_.which == Sao.common.DOWN_KEYCODE) && - (this.tree.screen.new_position == -1)))) { - var model = this.tree.screen.group; - var access = Sao.common.MODELACCESS.get( - this.tree.screen.model_name); - var limit = ((this.tree.screen.size_limit !== null) && - (model.length >= this.tree.screen.size_limit)); - if (this.tree.creatable && access.create && !limit) { - prm.then(() => this.tree.screen.new_()) - .then(record => { - var sequence = this.tree.attributes.sequence; - if (sequence) { - record.group.set_sequence( - sequence, this.tree.screen.new_position); - } - }); - } - } else { - prm.then(() => { - this._get_column_td( - next_column, next_row) - .trigger('click') - .find(':input,[tabindex=0]') - .focus(); - }); - } + if (!this.record.validate(this.tree.get_fields())) { + var invalid_fields = + this.record.invalid_fields(); + for (i = 0; i < this.tree.columns.length; i++) { + var col = this.tree.columns[i]; + if (col.attributes.name in invalid_fields) { + next_column = i; + break; } + } + this._get_column_td(next_column) + .find(':input,[tabindex=0]').focus(); + } else { + var prm = jQuery.when(); + if (!this.tree.screen.group.parent) { + prm = this.record.save(); + } else if (this.tree.screen.attributes.pre_validate) { + prm = this.record.pre_validate(); + } + prm.fail(function() { + widget.focus(); }); + var next_row; + if (event_.which == Sao.common.UP_KEYCODE) { + next_row = this.el.prev('tr'); + } else if (event_.which == Sao.common.DOWN_KEYCODE) { + next_row = this.el.next('tr'); + } else { + if (this.tree.screen.new_position == -1) { + next_row = this.el.next('tr'); + } else { + next_row = this.el.prev('tr'); + } + } + if (!next_row.length && + ((event_.which == Sao.common.RETURN_KEYCODE) || + ((event_.which == Sao.common.UP_KEYCODE) && + (this.tree.screen.new_position == 0)) || + ((event_.which == Sao.common.DOWN_KEYCODE) && + (this.tree.screen.new_position == -1)))) { + var model = this.tree.screen.group; + var access = Sao.common.MODELACCESS.get( + this.tree.screen.model_name); + var limit = ((this.tree.screen.size_limit !== null) && + (model.length >= this.tree.screen.size_limit)); + if (this.tree.creatable && access.create && !limit) { + prm.then(() => this.tree.screen.new_()) + .then(record => { + var sequence = this.tree.attributes.sequence; + if (sequence) { + record.group.set_sequence( + sequence, this.tree.screen.new_position); + } + }); + } + } else { + prm.then(() => { + this._get_column_td( + next_column, next_row) + .trigger('click') + .find(':input,[tabindex=0]') + .focus(); + }); + } + } } else if (event_.which == Sao.common.ESC_KEYCODE) { this.tree.edit_row(null); this.get_static_el().show().find('[tabindex=0]').focus(); @@ -2537,7 +2532,7 @@ var current_record = this.tree.screen.current_record; var fields = this.tree.get_fields(); if (!current_record || current_record.validate( - fields, false, false, true)) { + fields, false, false)) { var value = cell.prop('checked'); this.field.set_client(record, value); } else { diff --git a/sao/src/window.js b/sao/src/window.js index f2bc212a91..b03bb8fe24 100644 --- a/sao/src/window.js +++ b/sao/src/window.js @@ -496,7 +496,7 @@ if (~['RESPONSE_OK', 'RESPONSE_ACCEPT'].indexOf(response_id) && !readonly && this.screen.current_record) { - this.screen.current_record.validate().then(validate => { + jQuery.when(this.screen.current_record.validate()).then(validate => { if (validate && this.screen.attributes.pre_validate) { return this.screen.current_record.pre_validate().then( () => true, () => false); @@ -1096,9 +1096,8 @@ this.screen.current_record.set(preferences); this.screen.current_record.id = this.screen.model.session.user_id; - this.screen.current_record.validate(null, true).then(() => { - this.screen.display(true); - }); + this.screen.current_record.validate(null, true); + this.screen.display(true); dialog.body.append(this.screen.screen_container.el); this.el.modal('show'); }; @@ -1111,21 +1110,16 @@ .then(set_view, this.destroy); }, response: function(response_id) { - const end = () => { - this.destroy(); - }; - var prm = jQuery.when(); if (response_id == 'RESPONSE_OK') { - prm = this.screen.current_record.validate() - .then(validate => { - if (validate) { - var values = jQuery.extend({}, this.screen.get()); - return this.screen.model.execute( - 'set_preferences', [values], {}); - } - }); + if (this.screen.current_record.validate()) { + var values = jQuery.extend({}, this.screen.get()); + return this.screen.model.execute( + 'set_preferences', [values], {}) + .then(() => this.destroy()); + } + return; } - prm.done(end); + this.destroy(); }, destroy: function() { this.el.modal('hide'); diff --git a/sao/src/wizard.js b/sao/src/wizard.js index 6c133e0cf6..998b65b53b 100644 --- a/sao/src/wizard.js +++ b/sao/src/wizard.js @@ -148,7 +148,7 @@ this.__waiting_response = false; this.screen.current_view.set_value(); if (definition.validate && !this.screen.current_record.validate( - null, null, null, true)) { + null, null, null)) { this.screen.display(true); this.info_bar.add(this.screen.invalid_message(), 'danger'); return;