From f241441433046c41e4f3edd05d5d69028da1f8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Tue, 10 Jul 2018 13:52:28 +0200 Subject: [IMP] web: add control + create tags to customize "add a line" The default "add a line" button creates a new line with the default values from the model. Sometimes we want to create a new line with different default values (or more generally a customized context), which is possible with this commit. Moreover it is possible to create multiple buttons for multiple default values/contexts. This works both for in-line creation ("editable=") and dialog creation. The new tags are: defines custom controls for the current view. Does not support any attribute, but can have children: adds a button to create a new element on the current list. This makes sense when the parent tree view is inside a One2many field. If any create is defined, it will overwrite the default "add a line" button. For more information and example, have a look at the Views documentation. Technically, the commit will: - parse the new XML tags (+ add appropriate validation) - display the buttons based on the XML - on button click/keyboard: pull up the additional context information from the button event up to the default_get - add unit tests - add documentation PR #25209 --- addons/web/static/src/js/fields/relational_fields.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'addons/web/static/src/js/fields') diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index b48c0dfbe69..104d8f3bed8 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -1266,11 +1266,18 @@ var FieldOne2Many = FieldX2Many.extend({ /** * @private * @param {Object} params + * @param {Object} [params.context] We allow additional context, this is + * used for example to define default values when adding new lines to + * a one2many with control/create tags. */ _openFormDialog: function (params) { + var context = this.record.getContext(_.extend({}, + this.recordParams, + { additionalContext: params.context } + )); this.trigger_up('open_one2many_record', _.extend(params, { domain: this.record.getDomain(this.recordParams), - context: this.record.getContext(this.recordParams), + context: context, field: this.field, fields_view: this.attrs.views && this.attrs.views.form, parentID: this.value.id, @@ -1297,6 +1304,7 @@ var FieldOne2Many = FieldX2Many.extend({ // we don't want interference with the components upstream. ev.stopPropagation(); + if (this.editable) { if (!this.activeActions.create) { if (data.onFail) { @@ -1308,12 +1316,14 @@ var FieldOne2Many = FieldX2Many.extend({ this._setValue({ operation: 'CREATE', position: this.editable, + context: data.context, }).always(function () { self.creatingRecord = false; }); } } else { this._openFormDialog({ + context: data.context, on_saved: function (record) { self._setValue({ operation: 'ADD', id: record.id }); }, -- cgit v1.2.3