From b67331fc18109d013624e6c11ba52d0c61c17975 Mon Sep 17 00:00:00 2001 From: Dharmraj Jhala Date: Wed, 4 Apr 2018 23:01:47 +0530 Subject: [PATCH] [IMP] web: Allow user to set custom 'create_text' for x2m fields For x2many relational fields(of type kanban), we shoud be able to set custom label for 'Create / Add' button by adding 'create_text' node option on particual field from xml. Right now, 'create_text' is only handled for many2many fields, preventing users to set custom label for one2many fields. This commit allows users to set Create Button label for both m2m and o2m fields. --- .../static/src/js/fields/relational_fields.js | 13 +--- .../tests/fields/relational_fields_tests.js | 70 ++++++++++++++++++- doc/reference/javascript_reference.rst | 8 +++ 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 8f5ffd65fac..fedaf5c07f6 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -699,6 +699,9 @@ var FieldX2Many = AbstractField.extend({ */ init: function (parent, name, record, options) { this._super.apply(this, arguments); + this.nodeOptions = _.defaults(this.nodeOptions, { + create_text: _t('Add'), + }); this.operations = []; this.isReadonly = this.mode === 'readonly'; this.view = this.attrs.views[this.attrs.mode]; @@ -1310,16 +1313,6 @@ var FieldMany2Many = FieldX2Many.extend({ className: 'o_field_many2many', supportedFieldTypes: ['many2many'], - /** - * @override - */ - init: function () { - this._super.apply(this, arguments); - this.nodeOptions = _.defaults(this.nodeOptions, { - create_text: _t('Add'), - }); - }, - //-------------------------------------------------------------------------- // Handlers //-------------------------------------------------------------------------- diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index 9d87c942459..072dd965d52 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -4017,7 +4017,7 @@ QUnit.module('relational_fields', { }); QUnit.test('one2many kanban: edition', function (assert) { - assert.expect(14); + assert.expect(15); this.data.partner.records[0].p = [2]; var form = createView({ @@ -4067,6 +4067,8 @@ QUnit.module('relational_fields', { 'delete icon should be visible in edit'); assert.ok(form.$('.o_field_one2many .o-kanban-button-new').length, '"Create" button should be visible in edit'); + assert.strictEqual(form.$('.o_field_one2many .o-kanban-button-new').text().trim(), "Add", + 'Create button should have "Add" label'); // edit existing subrecord form.$('.oe_kanban_global_click').click(); @@ -4107,6 +4109,36 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('one2many kabnan(editable): properly handle create_text node option', function (assert) { + assert.expect(1); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
', + res_id: 1, + }); + + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.o_field_one2many[name="turtles"] .o-kanban-button-new').text().trim(), + "Add turtle", "In O2M Kanban, Add button should have 'Add turtle' label"); + + form.destroy(); + }); + QUnit.test('one2many kanban: create action disabled', function (assert) { assert.expect(3); @@ -8713,7 +8745,7 @@ QUnit.module('relational_fields', { QUnit.module('FieldMany2Many'); QUnit.test('many2many kanban: edition', function (assert) { - assert.expect(28); + assert.expect(29); this.data.partner.records[0].timmy = [12, 14]; this.data.partner_type.records.push({id: 15, display_name: "red", color: 6}); @@ -8794,6 +8826,8 @@ QUnit.module('relational_fields', { 'delete icon should be visible in edit'); assert.ok(form.$('.o_field_many2many .o-kanban-button-new').length, '"Add" button should be visible in edit'); + assert.strictEqual(form.$('.o_field_many2many .o-kanban-button-new').text().trim(), "Add", + 'Create button should have "Add" label'); // edit existing subrecord form.$('.oe_kanban_global_click:first()').click(); @@ -8852,6 +8886,38 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('many2many kanban(editable): properly handle create_text node option', function (assert) { + assert.expect(1); + + this.data.partner.records[0].timmy = [12]; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '' + + '' + + '' + + '
' + + '' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
', + res_id: 1, + }); + + form.$buttons.find('.o_form_button_edit').click(); + assert.strictEqual(form.$('.o_field_many2many[name="timmy"] .o-kanban-button-new').text().trim(), + "Add timmy", "In M2M Kanban, Add button should have 'Add timmy' label"); + + form.destroy(); + }); + QUnit.test('many2many kanban: create action disabled', function (assert) { assert.expect(4); diff --git a/doc/reference/javascript_reference.rst b/doc/reference/javascript_reference.rst index b77aa9f5612..d13b2b86412 100644 --- a/doc/reference/javascript_reference.rst +++ b/doc/reference/javascript_reference.rst @@ -1782,6 +1782,14 @@ Relational fields - Supported field types: *one2many* + Options: + + - create_text: a string that is used to customize the 'Add' label/text. + + .. code-block:: xml + + + - statusbar (FieldStatus) This is a really specialized widget for the form views. It is the bar on top of many forms which represent a flow, and allow selecting a specific state.