[FIX] web: click on href inside kanban record

Before this commit, when clicking on a link in a kanban
record having `href` that is set, it was opening the record
and accessing the `href`.

The intended behaviour is to not open a record if any children
component of the record contains either a click event or is
a link with `href`. The former was correctly handled, but not
the latter.

This commit fixes the issue by preventing opening the record
when clicking on a inner link with href from a kanban record.

Closes #24664

closes odoo/odoo#27529
This commit is contained in:
Alexandre Kühn
2018-10-09 08:39:50 +00:00
parent 62cbc5f240
commit 5c12a664d5
2 changed files with 70 additions and 2 deletions
@@ -446,10 +446,12 @@ var KanbanRecord = Widget.extend({
ischild = false;
}
var test_event = events && events.click && (events.click.length > 1 || events.click[0].namespace !== "tooltip");
var testLinkWithHref = elem.nodeName.toLowerCase() === 'a' && elem.href;
if (ischild) {
children.push(elem);
if (test_event) {
// do not trigger global click if one child has a click event registered
if (test_event || testLinkWithHref) {
// Do not trigger global click if one child has a click
// event registered (or it is a link with href)
trigger = false;
}
}
@@ -839,6 +839,72 @@ QUnit.module('Views', {
kanban.destroy();
});
QUnit.test('Do not open record when clicking on `a` with `href`', function (assert) {
assert.expect(5);
this.data.partner.records = [
{ id: 1, foo: 'yop' },
];
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban class="o_kanban_test">' +
'<templates>' +
'<t t-name="kanban-box">' +
'<div class="oe_kanban_global_click">' +
'<field name="foo"/>' +
'<div>' +
'<a class="o_test_link" href="#">test link</a>' +
'</div>' +
'</div>' +
'</t>' +
'</templates>' +
'</kanban>',
intercepts: {
// when clicking on a record in kanban view,
// it switches to form view.
switch_view: function () {
throw new Error("should not switch view");
},
},
});
var $record = kanban.$('.o_kanban_record:not(.o_kanban_ghost)');
assert.strictEqual($record.length, 1,
"should display a kanban record");
var $testLink = $record.find('a');
assert.strictEqual($testLink.length, 1,
"should contain a link in the kanban record");
assert.ok(!!$testLink[0].href,
"link inside kanban record should have non-empty href");
// Mocked views prevent accessing a link with href. This is intented
// most of the time, but not in this test which specifically needs to
// let the browser access a link with href.
kanban.$el.off('click', 'a');
// Prevent the browser default behaviour when clicking on anything.
// This includes clicking on a `<a>` with `href`, so that it does not
// change the URL in the address bar.
// Note that we should not specify a click listener on 'a', otherwise
// it may influence the kanban record global click handler to not open
// the record.
$(document.body).on('click.o_test', function (ev) {
assert.notOk(ev.isDefaultPrevented(),
"should not prevented browser default behaviour beforehand");
assert.strictEqual(ev.target, $testLink[0],
"should have clicked on the test link in the kanban record");
ev.preventDefault();
});
$testLink.click();
$(document.body).off('click.o_test');
kanban.destroy();
});
QUnit.test('can drag and drop a record from one column to the next', function (assert) {
assert.expect(9);