diff --git a/addons/web/static/src/js/views/control_panel/search/add_new_favorite_menu.js b/addons/web/static/src/js/views/control_panel/search/add_new_favorite_menu.js index 0a2498c69df..421fec3e04b 100644 --- a/addons/web/static/src/js/views/control_panel/search/add_new_favorite_menu.js +++ b/addons/web/static/src/js/views/control_panel/search/add_new_favorite_menu.js @@ -12,6 +12,7 @@ var AddNewFavoriteMenu = Widget.extend({ events: _.extend({}, Widget.prototype.events, { 'click .o_save_favorite': '_onSaveFavoriteClick', 'click .o_add_favorite.o_menu_header': '_onMenuHeaderClick', + 'click input[type="checkbox"]': '_onCheckboxClick', 'keyup .o_save_name input': '_onKeyUp', }), /** @@ -107,6 +108,33 @@ var AddNewFavoriteMenu = Widget.extend({ // Handlers //-------------------------------------------------------------------------- + /** + * Additional behavior when a checkbox is clicked upon + * Namely, some checkbox are mutually exclusive + * this function allows this + * + * @private + * @param {jQueryEvent} ev + */ + _onCheckboxClick: function (ev) { + function mutuallyExclusive (elem) { + if (!elem.id) return false; + return ['use_by_default', 'share_all_users'].some(function (str) { + return elem.id.indexOf(str); + }); + } + var $checkboxes = this.$('input[type="checkbox"]'); + var currentCheckBox = ev.target; + + if (mutuallyExclusive(currentCheckBox)) { + for (var i=0; i < $checkboxes.length; i++) { + var checkbox = $checkboxes[i]; + if (checkbox !== currentCheckBox && mutuallyExclusive(checkbox)) { + checkbox.checked = false; + } + } + } + }, /** * @private * @param {jQueryEvent} ev diff --git a/addons/web/static/tests/views/control_panel_tests.js b/addons/web/static/tests/views/control_panel_tests.js index f2654e55e1b..5c38c58ca45 100644 --- a/addons/web/static/tests/views/control_panel_tests.js +++ b/addons/web/static/tests/views/control_panel_tests.js @@ -416,6 +416,47 @@ QUnit.module('Views', { controlPanel.destroy(); }); + QUnit.test('Favorites Use by Default and Share are exclusive', async function (assert) { + assert.expect(11); + var controlPanel = await createControlPanel({ + model: 'partner', + arch: "", + data: this.data, + searchMenuTypes: ['favorite'], + }); + testUtils.dom.click(controlPanel.$('.o_favorites_menu_button')); + testUtils.dom.click(controlPanel.$('button.o_add_favorite')); + var $checkboxes = controlPanel.$('input[type="checkbox"]'); + + assert.strictEqual($checkboxes.length, 2, + '2 checkboxes are present') + + assert.notOk($checkboxes[0].checked, 'Start: None of the checkboxes are checked (1)'); + assert.notOk($checkboxes[1].checked, 'Start: None of the checkboxes are checked (2)'); + + testUtils.dom.click($checkboxes.eq(0)); + assert.ok($checkboxes[0].checked, 'The first checkbox is checked'); + assert.notOk($checkboxes[1].checked, 'The second checkbox is not checked'); + + testUtils.dom.click($checkboxes.eq(1)); + assert.notOk($checkboxes[0].checked, + 'Clicking on the second checkbox checks it, and unchecks the first (1)'); + assert.ok($checkboxes[1].checked, + 'Clicking on the second checkbox checks it, and unchecks the first (2)'); + + testUtils.dom.click($checkboxes.eq(0)); + assert.ok($checkboxes[0].checked, + 'Clicking on the first checkbox checks it, and unchecks the second (1)'); + assert.notOk($checkboxes[1].checked, + 'Clicking on the first checkbox checks it, and unchecks the second (2)'); + + testUtils.dom.click($checkboxes.eq(0)); + assert.notOk($checkboxes[0].checked, 'End: None of the checkboxes are checked (1)'); + assert.notOk($checkboxes[1].checked, 'End: None of the checkboxes are checked (2)'); + + controlPanel.destroy(); + }); + QUnit.test('load filter', async function (assert) { assert.expect(1);