[FIX] web: correctly compile invisible when t-if already exists

Have a section of a settings view that can be search in.
Have a dynamic invisble modifier (governed by a domain) on it.

Before this commit, the condition were just juxtaposed, which will cause problem
if one of them contains a "OR" operator. So, sections that should have been invisible were visible
because the if looked like: search() or search_other() and isVisible().

After this commit, the t-ifs are correctly combined so that they look like:
(search() or search_other()) and isVisible().

closes odoo/odoo#97811

Signed-off-by: Samuel Degueldre <sad@odoo.com>
This commit is contained in:
Lucas Perais
2022-08-10 14:43:55 +02:00
parent 6d9ff04a18
commit 312b9b1502
2 changed files with 30 additions and 7 deletions
+6 -6
View File
@@ -254,12 +254,12 @@ export class ViewCompiler {
return;
}
if (!params.enableInvisible) {
combineAttributes(
compiled,
"t-if",
`!evalDomainFromRecord(props.record,${JSON.stringify(invisible)})`,
" and "
);
let isVisileExpr = `!evalDomainFromRecord(props.record,${JSON.stringify(invisible)})`;
if (compiled.hasAttribute("t-if")) {
const formerTif = compiled.getAttribute("t-if");
isVisileExpr = `( ${formerTif} ) and ${isVisileExpr}`;
}
compiled.setAttribute("t-if", isVisileExpr);
} else {
appendAttr(compiled, "class", `o_invisible_modifier:${invisible}`);
}
@@ -3,7 +3,8 @@ import { makeView } from "../helpers";
import { setupViewRegistries } from "@web/../tests/views/helpers";
import { FormCompiler } from "@web/views/form/form_compiler";
import { registry } from "@web/core/registry";
import { click, getFixture } from "../../helpers/utils";
import { click, getFixture, patchWithCleanup } from "../../helpers/utils";
import { createElement } from "@web/core/utils/xml";
function compileTemplate(arch) {
const parser = new DOMParser();
@@ -371,4 +372,26 @@ QUnit.module("Form Renderer", (hooks) => {
assert.containsOnce(target, "button[id=action_button]");
});
QUnit.test("invisible is correctly computed with another t-if", (assert) => {
patchWithCleanup(FormCompiler.prototype, {
setup() {
this._super();
this.compilers.push({
selector: "myNode",
fn: () => {
const div = createElement("div");
div.className = "myNode";
div.setAttribute("t-if", "myCondition or myOtherCondition");
return div;
},
});
},
});
const arch = `<myNode modifiers="{&quot;invisible&quot;: [[&quot;field&quot;, &quot;=&quot;, &quot;value&quot;]]}" />`;
const expected = `<t><div class="myNode" t-if="( myCondition or myOtherCondition ) and !evalDomainFromRecord(props.record,[[&quot;field&quot;,&quot;=&quot;,&quot;value&quot;]])" t-ref="compiled_view_root"/></t>`;
assert.areEquivalent(compileTemplate(arch), expected);
});
});