diff --git a/addons/account/static/src/xml/bills_tree_upload_views.xml b/addons/account/static/src/xml/bills_tree_upload_views.xml index adb6fdab487..32505e1206c 100644 --- a/addons/account/static/src/xml/bills_tree_upload_views.xml +++ b/addons/account/static/src/xml/bills_tree_upload_views.xml @@ -15,11 +15,6 @@ - - if (window.odoo._modules.indexOf("base_import") >= 0) { - r.push(context.engine.tools.call(context, 'ImportView.import_button', dict)); - }; - diff --git a/addons/base_import/static/src/js/import_buttons.js b/addons/base_import/static/src/js/import_buttons.js deleted file mode 100644 index 89baedc8113..00000000000 --- a/addons/base_import/static/src/js/import_buttons.js +++ /dev/null @@ -1,123 +0,0 @@ -odoo.define('base_import.import_buttons', function (require) { -"use strict"; - -var config = require('web.config'); -var KanbanController = require('web.KanbanController'); -var KanbanView = require('web.KanbanView'); -var ListController = require('web.ListController'); -var ListView = require('web.ListView'); - - -// Mixins that enable the 'Import' feature -var ImportViewMixin = { - /** - * @override - * @param {Object} params - * @param {boolean} [params.import_enabled=true] set to false to disable - * the Import feature (no 'Import' button in the control panel). Can also - * be disabled with 'import' attrs set to '0' in the arch. - */ - init: function (viewInfo, params) { - var importEnabled = !!JSON.parse(this.arch.attrs.import || '1') && - ('import_enabled' in params ? params.import_enabled : true); - this.controllerParams.importEnabled = importEnabled && !config.device.isMobile; - }, -}; - -var ImportControllerMixin = { - /** - * @override - */ - init: function (parent, model, renderer, params) { - this.importEnabled = params.importEnabled; - }, - - //-------------------------------------------------------------------------- - // Private - //-------------------------------------------------------------------------- - - /** - * Adds an event listener on the import button. - * - * @private - */ - _bindImport: function () { - if (!this.$buttons) { - return; - } - var self = this; - this.$buttons.on('click', '.o_button_import', function () { - var state = self.model.get(self.handle, {raw: true}); - self.do_action({ - type: 'ir.actions.client', - tag: 'import', - params: { - model: self.modelName, - context: state.getContext(), - } - }); - }); - } -}; - -// Activate 'Import' feature on List views -ListView.include({ - init: function () { - this._super.apply(this, arguments); - ImportViewMixin.init.apply(this, arguments); - }, -}); - -ListController.include({ - init: function () { - this._super.apply(this, arguments); - ImportControllerMixin.init.apply(this, arguments); - }, - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - /** - * Extends the renderButtons function of ListView by adding an event listener - * on the import button. - * - * @override - */ - renderButtons: function () { - this._super.apply(this, arguments); // Sets this.$buttons - ImportControllerMixin._bindImport.call(this); - } -}); - -// Activate 'Import' feature on Kanban views -KanbanView.include({ - init: function () { - this._super.apply(this, arguments); - ImportViewMixin.init.apply(this, arguments); - }, -}); - -KanbanController.include({ - init: function () { - this._super.apply(this, arguments); - ImportControllerMixin.init.apply(this, arguments); - }, - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - /** - * Extends the renderButtons function of ListView by adding an event listener - * on the import button. - * - * @override - */ - renderButtons: function () { - this._super.apply(this, arguments); // Sets this.$buttons - ImportControllerMixin._bindImport.call(this); - } -}); - -}); diff --git a/addons/base_import/static/src/js/import_menu.js b/addons/base_import/static/src/js/import_menu.js new file mode 100644 index 00000000000..a5c5c5e1293 --- /dev/null +++ b/addons/base_import/static/src/js/import_menu.js @@ -0,0 +1,63 @@ +odoo.define('base_import.ImportMenu', function (require) { + "use strict"; + + const DropdownMenuItem = require('web.DropdownMenuItem'); + const FavoriteMenu = require('web.FavoriteMenu'); + const { useModel } = require('web.model'); + + /** + * Import Records menu + * + * This component is used to import the records for particular model. + * + * @extends DropdownMenuItem + */ + class ImportMenu extends DropdownMenuItem { + constructor() { + super(...arguments); + this.model = useModel('controlPanelModel'); + } + + //--------------------------------------------------------------------- + // Handlers + //--------------------------------------------------------------------- + + /** + * @private + */ + _onImportClick() { + const action = { + type: 'ir.actions.client', + tag: 'import', + params: { + model: this.model.modelName, + context: this.model.actionContext, + } + }; + this.trigger('do-action', {action: action}); + } + + //--------------------------------------------------------------------- + // Static + //--------------------------------------------------------------------- + + /** + * @param {Object} env + * @returns {boolean} + */ + static shouldBeDisplayed(env) { + return env.view && + ['kanban', 'list'].includes(env.view.type) && + !env.device.isMobile && + !!JSON.parse(env.view.arch.attrs.import || '1') && + !!JSON.parse(env.view.arch.attrs.create || '1'); + } + } + + ImportMenu.props = {}; + ImportMenu.template = 'base_import.ImportMenu'; + + FavoriteMenu.registry.add('import-menu', ImportMenu, 1); + + return ImportMenu; +}); diff --git a/addons/base_import/static/src/xml/base_import.xml b/addons/base_import/static/src/xml/base_import.xml index d0dd6657a79..827f95a337a 100644 --- a/addons/base_import/static/src/xml/base_import.xml +++ b/addons/base_import/static/src/xml/base_import.xml @@ -1,4 +1,12 @@ + + + +
@@ -217,19 +225,4 @@ - - - - - - - - - - - - - diff --git a/addons/base_import/static/tests/import_buttons_mobile_tests.js b/addons/base_import/static/tests/import_buttons_mobile_tests.js deleted file mode 100644 index 49c8960069c..00000000000 --- a/addons/base_import/static/tests/import_buttons_mobile_tests.js +++ /dev/null @@ -1,56 +0,0 @@ -odoo.define('web.base_import_mobile_tests', function (require) { -"use strict"; - -var KanbanView = require('web.KanbanView'); -var ListView = require('web.ListView'); -var testUtils = require('web.test_utils'); - -var createView = testUtils.createView; - -QUnit.module('Base Import Mobile Tests', { - beforeEach: function () { - this.data = { - foo: { - fields: { - foo: {string: "Foo", type: "char"}, - }, - }, - }; - } -}); - -QUnit.test('import button should be hidden in list on mobile screens', async function (assert) { - assert.expect(1); - - var list = await createView({ - View: ListView, - model: 'foo', - data: this.data, - arch: '', - }); - - assert.notOk(list.$buttons.find('.o_button_import').is(':visible'), - "import button should be hidden"); - list.destroy(); -}); - -QUnit.test('import button should be hidden in kanban on mobile screens', async function (assert) { - assert.expect(1); - - var kanban = await createView({ - View: KanbanView, - model: 'foo', - data: this.data, - arch: '' + - '
' + - '' + - '
' + - '
', - }); - - assert.notOk(kanban.$buttons.find('.o_button_import').is(':visible'), - "import button should be hidden"); - kanban.destroy(); -}); - -}); diff --git a/addons/base_import/static/tests/import_buttons_tests.js b/addons/base_import/static/tests/import_buttons_tests.js index fdb0e2a816e..e561d021d78 100644 --- a/addons/base_import/static/tests/import_buttons_tests.js +++ b/addons/base_import/static/tests/import_buttons_tests.js @@ -1,14 +1,15 @@ odoo.define('web.base_import_tests', function (require) { "use strict"; -var KanbanView = require('web.KanbanView'); -var ListView = require('web.ListView'); -var testUtils = require('web.test_utils'); +const KanbanView = require('web.KanbanView'); +const ListView = require('web.ListView'); +const PivotView = require('web.PivotView'); +const testUtils = require('web.test_utils'); -var createView = testUtils.createView; +const createView = testUtils.createView; QUnit.module('Base Import Tests', { - beforeEach: function() { + beforeEach: function () { this.data = { foo: { fields: { @@ -22,119 +23,148 @@ QUnit.module('Base Import Tests', { } }); -QUnit.test('add import button in list', async function(assert) { +QUnit.test('import in favorite dropdown in list', async function (assert) { assert.expect(2); - var list = await createView({ + const list = await createView({ View: ListView, model: 'foo', data: this.data, arch: '', }); - assert.isVisible(list.$buttons.find('.o_button_import:contains(Import)'), - "should have a visible Import button"); - - testUtils.mock.intercept(list, 'do_action', function() { + testUtils.mock.intercept(list, 'do_action', function () { assert.ok(true, "should have triggered a do_action"); }); - await testUtils.dom.click(list.$buttons.find('.o_button_import:contains(Import)')); + await testUtils.dom.click(list.$('.o_favorite_menu button')); + assert.containsOnce(list, '.o_import_menu'); + + await testUtils.dom.click(list.$('.o_import_menu button')); + list.destroy(); }); -QUnit.test('list without import button', async function(assert) { +QUnit.test('import favorite dropdown item should not in list with create="0"', async function (assert) { assert.expect(1); - var list = await createView({ + const list = await createView({ View: ListView, model: 'foo', data: this.data, - arch: '', - viewOptions: { - import_enabled: false, - } + arch: '', }); - assert.containsNone(list.$buttons, '.o_button_import', 'should not have an Import button'); + await testUtils.dom.click(list.$('.o_favorite_menu button')); + assert.containsNone(list, '.o_import_menu'); + list.destroy(); }); -QUnit.test('add import button in kanban', async function(assert) { - assert.expect(2); - - var kanban = await createView({ - View: KanbanView, - model: 'foo', - data: this.data, - arch: '' + - '
' + - '' + - '
' + - '
', - }); - - assert.isVisible(kanban.$buttons.find('.o_button_import:contains(Import)'), - "should have a visible Import button"); - - testUtils.mock.intercept(kanban, 'do_action', function() { - assert.ok(true, "should have triggered a do_action"); - }); - - await testUtils.dom.click(kanban.$buttons.find('.o_button_import:contains(Import)')); - kanban.destroy(); -}); - -QUnit.test('kanban without import button', async function(assert) { +QUnit.test('import favorite dropdown item should not in list with import="0"', async function (assert) { assert.expect(1); - var kanban = await createView({ - View: KanbanView, - model: 'foo', - data: this.data, - arch: '' + - '
' + - '' + - '
' + - '
', - viewOptions: { - import_enabled: false, - } - }); - - assert.containsNone(kanban.$buttons, '.o_button_import', "should not have an Import button"); - kanban.destroy(); -}); - -QUnit.test('import attrs in list views', async function (assert) { - assert.expect(1); - - var list = await createView({ + const list = await createView({ View: ListView, model: 'foo', data: this.data, arch: '', }); - assert.containsNone(list.$buttons, '.o_button_import'); + await testUtils.dom.click(list.$('.o_favorite_menu button')); + assert.containsNone(list, '.o_import_menu'); + list.destroy(); }); -QUnit.test('import attrs in kanban views', async function (assert) { - assert.expect(1); +QUnit.test('import in favorite dropdown in kanban', async function (assert) { + assert.expect(2); - var kanban = await createView({ + const kanban = await createView({ View: KanbanView, model: 'foo', data: this.data, - arch: '' + - '' + - '
' + - '
' + - '
', + arch: ` + + +
+
+
+
`, }); - assert.containsNone(kanban.$buttons, '.o_button_import'); + testUtils.mock.intercept(kanban, 'do_action', function () { + assert.ok(true, "should have triggered a do_action"); + }); + + await testUtils.dom.click(kanban.$('.o_favorite_menu button')); + assert.containsOnce(kanban, '.o_import_menu'); + + await testUtils.dom.click(kanban.$('.o_import_menu button')); + kanban.destroy(); }); + +QUnit.test('import favorite dropdown item should not in list with create="0"', async function (assert) { + assert.expect(1); + + const kanban = await createView({ + View: KanbanView, + model: 'foo', + data: this.data, + arch: ` + + +
+
+
+
`, + }); + + await testUtils.dom.click(kanban.$('.o_favorite_menu button')); + assert.containsNone(kanban, '.o_import_menu'); + + kanban.destroy(); +}); + +QUnit.test('import dropdown favorite should not in kanban with import="0"', async function (assert) { + assert.expect(1); + + const kanban = await createView({ + View: KanbanView, + model: 'foo', + data: this.data, + arch: ` + + +
+
+
+
`, + }); + + await testUtils.dom.click(kanban.$('.o_favorite_menu button')); + assert.containsNone(kanban, '.o_import_menu'); + + kanban.destroy(); +}); + +QUnit.test('import should not available in favorite dropdown in pivot (other than kanban or list)', async function (assert) { + assert.expect(1); + + this.data.foo.fields.foobar = { string: "Fubar", type: "integer", group_operator: 'sum' }; + + const pivot = await createView({ + View: PivotView, + model: 'foo', + data: this.data, + arch: '', + }); + + await testUtils.dom.click(pivot.$('.o_favorite_menu button')); + assert.containsNone(pivot, '.o_import_menu'); + + pivot.destroy(); +}); + }); diff --git a/addons/base_import/views/base_import_templates.xml b/addons/base_import/views/base_import_templates.xml index ce2af4e0c26..092cdc4ef15 100644 --- a/addons/base_import/views/base_import_templates.xml +++ b/addons/base_import/views/base_import_templates.xml @@ -6,7 +6,7 @@ - + @@ -15,11 +15,5 @@ - - diff --git a/addons/crm_iap_lead/static/src/xml/leads_tree_generate_leads_views.xml b/addons/crm_iap_lead/static/src/xml/leads_tree_generate_leads_views.xml index b431e02601c..8254fb7fc8f 100644 --- a/addons/crm_iap_lead/static/src/xml/leads_tree_generate_leads_views.xml +++ b/addons/crm_iap_lead/static/src/xml/leads_tree_generate_leads_views.xml @@ -10,22 +10,12 @@ - - if (window.odoo._modules.indexOf("base_import") >= 0) { - r.push(context.engine.tools.call(context, 'ImportView.import_button', dict)); - }; - - - if (window.odoo._modules.indexOf("base_import") >= 0) { - r.push(context.engine.tools.call(context, 'ImportView.import_button', dict)); - }; - diff --git a/addons/hr_expense/static/src/xml/documents_upload_views.xml b/addons/hr_expense/static/src/xml/documents_upload_views.xml index 8f2dc880d07..549ee9d0f96 100644 --- a/addons/hr_expense/static/src/xml/documents_upload_views.xml +++ b/addons/hr_expense/static/src/xml/documents_upload_views.xml @@ -15,11 +15,6 @@ - - if (window.odoo._modules.indexOf("base_import") >= 0) { - r.push(context.engine.tools.call(context, 'ImportView.import_button', dict)); - }; - @@ -28,11 +23,6 @@ - - if (window.odoo._modules.indexOf("base_import") >= 0) { - r.push(context.engine.tools.call(context, 'ImportView.import_button', dict)); - }; -