[FIX] web: Favorites checkboxes are mutually exclusive

Before this commit, one could activate the two checkboxes "use by default", and "share for all users"

After this commit, only one out of the two can be checked

OPW 2010458

closes odoo/odoo#34167

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
Lucas Perais (lpe)
2019-06-17 07:49:06 +00:00
parent caaaa129f7
commit 800a55c06f
2 changed files with 69 additions and 0 deletions
@@ -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
@@ -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: "<search></search>",
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);