From ccf653da1d34337387d093599fc9ee5abc187ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 26 Sep 2023 23:45:07 +0200 Subject: [PATCH] Support create record from autocomplete with default values --- sao/CHANGELOG | 1 + sao/src/common.js | 24 +++++++++++++---- sao/src/view/form.js | 26 +++++++++++++++---- tryton/CHANGELOG | 1 + tryton/tryton/common/completion.py | 14 +++++++--- .../view_form/view/form_gtk/many2many.py | 17 +++++++----- .../view_form/view/form_gtk/many2one.py | 19 ++++++++------ .../view_form/view/form_gtk/one2many.py | 17 +++++++----- .../window/view_form/view/list_gtk/widget.py | 25 +++++++++++++----- trytond/CHANGELOG | 1 + trytond/doc/ref/models.rst | 6 ++++- trytond/trytond/model/modelview.py | 4 ++- trytond/trytond/tests/test_modelview.py | 13 +++++----- 13 files changed, 118 insertions(+), 50 deletions(-) diff --git a/sao/CHANGELOG b/sao/CHANGELOG index ed7da86998..e7117cac11 100644 --- a/sao/CHANGELOG +++ b/sao/CHANGELOG @@ -1,3 +1,4 @@ +* Support create record from autocomplete * Use grid layout for form container * Support scanning code * Reset search offset when domain has changed diff --git a/sao/src/common.js b/sao/src/common.js index 5b58816405..085e914d9a 100644 --- a/sao/src/common.js +++ b/sao/src/common.js @@ -3740,15 +3740,15 @@ Sao.common.set_overflow(this.input, 'show'); }); }, - set_actions: function(action_activated, search, create) { + set_actions: function(action_activated, search=true, create=true) { if (action_activated !== undefined) { this.action_activated = action_activated; } var actions = []; - if (search || search == undefined) { + if (search) { actions.push(['search', Sao.i18n.gettext('Search...')]); } - if (create || create == undefined) { + if (create) { actions.push(['create', Sao.i18n.gettext('Create...')]); } this.menu.find('li.action').remove(); @@ -3840,7 +3840,7 @@ }); Sao.common.get_completion = function(el, source, - match_selected, action_activated, search, create) { + match_selected, action_activated, search=true, create=true) { var format = function(content) { return content.name; }; @@ -3849,6 +3849,7 @@ if (action_activated) { completion.set_actions(action_activated, search, create); } + completion._allow_create = create; return completion; }; @@ -3867,7 +3868,20 @@ var sao_model = new Sao.Model(model); return sao_model.execute( 'autocomplete', - [search_text, domain, Sao.config.limit, order], context).fail(function() { + [search_text, domain, Sao.config.limit, order], context) + .then( + function(result) { + return result.filter((value) => { + return value.id || entry.completion._allow_create; + }).map((value) => { + if (value.id === null) { + value.name = Sao.i18n.gettext( + 'Create "%1"...', value.name); + } + return value; + }); + }, + function() { Sao.Logger.warning( "Unable to search for completion of %s", model); }); diff --git a/sao/src/view/form.js b/sao/src/view/form.js index 8e5c5e64d4..420fc00982 100644 --- a/sao/src/view/form.js +++ b/sao/src/view/form.js @@ -2515,6 +2515,7 @@ function eval_pyson(value){ group, this._update_completion.bind(this), this._completion_match_selected.bind(this)); + this.entry.completion = this.wid_completion; } this.el.change(this.focus_out.bind(this)); this._readonly = false; @@ -2908,9 +2909,13 @@ function eval_pyson(value){ this.entry, record, field, model); }, _completion_match_selected: function(value) { - this.record.field_set_client(this.field_name, + if (value.id !== null) { + this.record.field_set_client(this.field_name, this.value_from_id( value.id, value.rec_name), true); + } else { + this.new_(value.defaults); + } }, _completion_action_activated: function(action) { if (action == 'search') { @@ -3188,6 +3193,7 @@ function eval_pyson(value){ this._completion_match_selected.bind(this), this._completion_action_activated.bind(this), this.read_access, this.create_access); + this.wid_text.completion = this.wid_completion; } buttons = jQuery('
', { @@ -3749,8 +3755,12 @@ function eval_pyson(value){ this.wid_text, this.record, this.field, model, domain); }, _completion_match_selected: function(value) { - this.screen.group.load([value.id], true); - this.wid_text.val(''); + if (value.id !== null) { + this.screen.group.load([value.id], true); + this.wid_text.val(''); + } else { + this.new_(value.defaults); + } }, _completion_action_activated: function(action) { if (action == 'search') { @@ -3813,6 +3823,7 @@ function eval_pyson(value){ this._completion_match_selected.bind(this), this._completion_action_activated.bind(this), this.read_access, this.create_access); + this.entry.completion = this.wid_completion; } var buttons = jQuery('
', { @@ -4106,8 +4117,12 @@ function eval_pyson(value){ this.entry, this.record, this.field, model, domain); }, _completion_match_selected: function(value) { - this.screen.group.load([value.id], true); - this.entry.val(''); + if (value.id !== null) { + this.screen.group.load([value.id], true); + this.entry.val(''); + } else { + this.new_(value.defaults); + } }, _completion_action_activated: function(action) { if (action == 'search') { @@ -4754,6 +4769,7 @@ function eval_pyson(value){ group, this._update_completion.bind(this), this._completion_match_selected.bind(this)); + this.wid_text.completion = this.wid_completion; } this.but_add = jQuery('