[REF] web: Settings, labels search

This commit, removes from the compiler all the code made to collect all
the labels of the setting for the search. Now, the labels are collected
at mounted time, by the setting itself.

This commit prepares and is part of the task that aim to refactor the
relational model and migrate it to owl.

part-of task-id 3179751

closes odoo/odoo#125332

Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
This commit is contained in:
Jorge Pinna Puissant
2023-06-19 11:58:41 +02:00
parent fbeee26f15
commit 00cedbad65
8 changed files with 22 additions and 83 deletions
@@ -34,12 +34,7 @@ export class FormRenderer extends Component {
const { arch, xmlDoc } = archInfo;
const templates = { FormRenderer: xmlDoc };
this.state = useState({}); // Used by Form Compiler
this.templates = useViewCompiler(
Compiler || FormCompiler,
arch,
templates,
this.compileParams
);
this.templates = useViewCompiler(Compiler || FormCompiler, arch, templates);
useSubEnv({ model: record.model });
useBounceButton(useRef("compiled_view_root"), (target) => {
return !record.isInEdition && !!target.closest(".oe_title, .o_inner_group");
@@ -88,10 +83,6 @@ export class FormRenderer extends Component {
evalDomainFromRecord(record, expr) {
return evalDomain(expr, record.evalContext);
}
get compileParams() {
return {};
}
}
FormRenderer.template = xml`<t t-call="{{ templates.FormRenderer }}" t-call-context="{ __comp__: Object.assign(Object.create(this), { this: this }) }" />`;
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="web.HighlightText" owl="1">
<t t-foreach="splitText" t-as="name" t-key="name_index">
<b t-if="name_index % 2" t-out="name" t-att-class="constructor.highlightClass"/>
<t t-else="" t-out="name"/>
</t>
<span t-att-searchableText="props.originalText">
<t t-foreach="splitText" t-as="name" t-key="name_index">
<b t-if="name_index % 2" t-out="name" t-att-class="constructor.highlightClass"/>
<t t-else="" t-out="name"/>
</t>
</span>
</t>
</templates>
@@ -12,10 +12,6 @@ export class SettingsRadioField extends RadioField {
export const settingsRadioField = {
...radioField,
component: SettingsRadioField,
extractStringExpr(fieldName, record) {
const radioItems = SettingsRadioField.getItems(fieldName, record);
return radioItems.map((r) => r[1]);
},
};
registry.category("fields").add("base_settings.radio", settingsRadioField);
@@ -2,19 +2,28 @@
import { escapeRegExp } from "@web/core/utils/strings";
import { Setting } from "@web/views/form/setting/setting";
import { useState } from "@odoo/owl";
import { onMounted, useRef, useState } from "@odoo/owl";
import { FormLabelHighlightText } from "../highlight_text/form_label_highlight_text";
import { HighlightText } from "../highlight_text/highlight_text";
export class SearchableSetting extends Setting {
setup() {
this.settingRef = useRef("setting");
this.state = useState({
search: this.env.searchState,
showAllContainer: this.env.showAllContainer,
});
this.labels = this.props.labels || [];
this.labels = [];
this.labels.push(this.labelString, this.props.help);
super.setup();
onMounted(() => {
if (this.settingRef.el) {
const searchableTexts = this.settingRef.el.querySelectorAll("span[searchableText]");
searchableTexts.forEach((st) => {
this.labels.push(st.getAttribute("searchableText"));
});
}
});
}
get classNames() {
@@ -43,7 +52,3 @@ SearchableSetting.components = {
FormLabel: FormLabelHighlightText,
HighlightText,
};
SearchableSetting.props = {
...Setting.props,
labels: { type: Array, optional: 1 },
};
@@ -2,6 +2,7 @@
<templates xml:space="preserve">
<t t-name="web.SearchableSetting" t-inherit="web.Setting" t-inherit-mode="primary" owl="1">
<div t-att-class="classNames" position="attributes">
<attribute name="t-ref">setting</attribute>
<attribute name="t-if">visible()</attribute>
</div>
<span class="o_form_label" position="replace">
@@ -89,30 +89,13 @@ export class SettingsFormCompiler extends FormCompiler {
compileSetting(el, params) {
params.componentName =
el.getAttribute("type") === "header" ? "SettingHeader" : "SearchableSetting";
params.labels = [];
const res = super.compileSetting(el, params);
if (params.componentName === "SearchableSetting") {
res.setAttribute("labels", JSON.stringify(params.labels));
}
delete params.labels;
return res;
}
compileField(el, params) {
const res = super.compileField(el, params);
if (params.labels && el.hasAttribute("widget")) {
const label = params.getFieldExpr(el.getAttribute("name"), el.getAttribute("widget"));
if (label) {
params.labels.push(label);
}
}
return res;
}
compileNode(node, params, evalInvisible) {
if (isTextNode(node)) {
if (params.labels && node.textContent.trim()) {
params.labels.push(node.textContent.trim());
if (node.textContent.trim()) {
return createElement("HighlightText", {
originalText: toStringExpression(node.textContent),
});
@@ -121,18 +104,9 @@ export class SettingsFormCompiler extends FormCompiler {
return super.compileNode(node, params, evalInvisible);
}
createLabelFromField(fieldId, fieldName, fieldString, label, params) {
const res = super.createLabelFromField(fieldId, fieldName, fieldString, label, params);
if (res.hasAttribute("string") && params.labels) {
params.labels.push(res.getAttribute("string"));
}
return res;
}
compileButton(el, params) {
const res = super.compileButton(el, params);
if (res.hasAttribute("string") && params.labels && res.children.length === 0) {
params.labels.push(res.getAttribute("string"));
if (res.hasAttribute("string") && res.children.length === 0) {
const contentSlot = createElement("t");
contentSlot.setAttribute("t-set-slot", "contents");
const content = createElement("HighlightText", {
@@ -1,6 +1,5 @@
/** @odoo-module **/
import { registry } from "@web/core/registry";
import { FormRenderer } from "@web/views/form/form_renderer";
import { FormLabelHighlightText } from "./highlight_text/form_label_highlight_text";
import { HighlightText } from "./highlight_text/highlight_text";
@@ -12,15 +11,8 @@ import { SettingsPage } from "./settings/settings_page";
import { useState } from "@odoo/owl";
const fieldRegistry = registry.category("fields");
const labels = Object.create(null);
export class SettingsFormRenderer extends FormRenderer {
setup() {
if (!labels[this.props.archInfo.arch]) {
labels[this.props.archInfo.arch] = [];
}
super.setup();
this.searchState = useState(this.env.searchState);
}
@@ -28,28 +20,6 @@ export class SettingsFormRenderer extends FormRenderer {
get shouldAutoFocus() {
return false;
}
get compileParams() {
return {
...super.compileParams,
labels: labels[this.props.archInfo.arch],
getFieldExpr: this.getFieldExpr,
record: this.props.record,
};
}
getFieldExpr(fieldName, fieldWidget) {
const name = `base_settings.${fieldWidget}`;
let fieldClass;
if (fieldRegistry.contains(name)) {
fieldClass = fieldRegistry.get(name);
}
if (fieldClass && fieldClass.extractStringExpr) {
return fieldClass.extractStringExpr(fieldName, this.record);
} else {
return "";
}
}
}
SettingsFormRenderer.components = {
...FormRenderer.components,
@@ -1528,7 +1528,7 @@ QUnit.module("SettingsFormView", (hooks) => {
const expectedCompiled = `
<SettingsPage slots="{NoContentHelper:__comp__.props.slots.NoContentHelper}" initialTab="__comp__.props.initialApp" t-slot-scope="settings" modules="[{&quot;key&quot;:&quot;crm&quot;,&quot;string&quot;:&quot;CRM&quot;,&quot;imgurl&quot;:&quot;/crm/static/description/icon.png&quot;}]">
<SettingsApp key="\`crm\`" string="\`CRM\`" imgurl="\`/crm/static/description/icon.png\`" selectedTab="settings.selectedTab">
<SearchableSetting title="\`\`" help="\`\`" companyDependent="false" documentation="\`\`" record="__comp__.props.record" string="\`\`" addLabel="true" labels="[&quot;\`My\\&quot; little ' Label\`&quot;]">
<SearchableSetting title="\`\`" help="\`\`" companyDependent="false" documentation="\`\`" record="__comp__.props.record" string="\`\`" addLabel="true">
<FormLabel id="'display_name_0'" fieldName="'display_name'" record="__comp__.props.record" fieldInfo="__comp__.props.archInfo.fieldNodes['display_name_0']" className="&quot;highhopes&quot;" string="\`My&quot; little ' Label\`"/>
<Field id="'display_name_0'" name="'display_name'" record="__comp__.props.record" fieldInfo="__comp__.props.archInfo.fieldNodes['display_name_0']" readonly="__comp__.props.archInfo.activeActions?.edit === false and !__comp__.props.record.isNew"/>
</SearchableSetting>