Skip to content

Commit 697f47a

Browse files
robinlejdetrouxdev
authored andcommitted
[REF] web_editor, website_blog: convert many2one SnippetOption to OWL
task-3850413 # Conflicts: # addons/website_blog/static/src/js/options.js
1 parent e8dd21d commit 697f47a

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

addons/web_editor/static/src/js/editor/snippets.options.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9332,25 +9332,30 @@ registerOption("container_width", {
93329332
* Allows to replace a text value with the name of a database record.
93339333
* @todo replace this mechanism with real backend m2o field ?
93349334
*/
9335-
legacyRegistry.many2one = SnippetOptionWidget.extend({
9336-
init() {
9337-
this._super(...arguments);
9338-
this.orm = this.bindService("orm");
9339-
},
9340-
9335+
export class Many2oneOption extends SnippetOption {
9336+
constructor() {
9337+
super(...arguments);
9338+
this.orm = this.env.services.orm;
9339+
}
93419340
/**
93429341
* @override
93439342
*/
93449343
async willStart() {
9344+
await super.willStart(...arguments);
93459345
const {oeMany2oneModel, oeMany2oneId} = this.$target[0].dataset;
93469346
this.fields = ['name', 'display_name'];
9347-
return Promise.all([
9348-
this._super(...arguments),
9347+
9348+
const [{name: modelName}] = await this.orm.searchRead("ir.model", [['model', '=', oeMany2oneModel]], ['name']);
9349+
this.renderContext.model = oeMany2oneModel;
9350+
this.renderContext.fields = JSON.stringify(this.fields);
9351+
this.renderContext.modelName = modelName;
9352+
9353+
return Promise.resolve([
93499354
this.orm.read(oeMany2oneModel, [parseInt(oeMany2oneId)], this.fields).then(([initialRecord]) => {
93509355
this.initialRecord = initialRecord;
93519356
}),
93529357
]);
9353-
},
9358+
}
93549359

93559360
//--------------------------------------------------------------------------
93569361
// Options
@@ -9390,7 +9395,7 @@ legacyRegistry.many2one = SnippetOptionWidget.extend({
93909395
this.$prevContents = this.$target.contents();
93919396
this.prevRecordName = record.name;
93929397
}
9393-
},
9398+
}
93949399

93959400
//--------------------------------------------------------------------------
93969401
// Private
@@ -9403,22 +9408,8 @@ legacyRegistry.many2one = SnippetOptionWidget.extend({
94039408
if (methodName === 'changeRecord') {
94049409
return this.$target[0].dataset.oeMany2oneId;
94059410
}
9406-
return this._super(...arguments);
9407-
},
9408-
/**
9409-
* @override
9410-
*/
9411-
async _renderCustomXML(uiFragment) {
9412-
const many2oneWidget = document.createElement('we-many2one');
9413-
many2oneWidget.dataset.changeRecord = '';
9414-
9415-
const model = this.$target[0].dataset.oeMany2oneModel;
9416-
const [{name: modelName}] = await this.orm.searchRead("ir.model", [['model', '=', model]], ['name']);
9417-
many2oneWidget.setAttribute('String', modelName);
9418-
many2oneWidget.dataset.model = model;
9419-
many2oneWidget.dataset.fields = JSON.stringify(this.fields);
9420-
uiFragment.appendChild(many2oneWidget);
9421-
},
9411+
return super._computeWidgetState(...arguments);
9412+
}
94229413
/**
94239414
* @private
94249415
*/
@@ -9453,8 +9444,15 @@ legacyRegistry.many2one = SnippetOptionWidget.extend({
94539444
node.textContent = defaultText;
94549445
}
94559446
}));
9456-
},
9447+
}
9448+
}
9449+
registerOption("Many2oneOption", {
9450+
Class: Many2oneOption,
9451+
template: "web_editor.many2one",
9452+
selector: "[data-oe-many2one-model]:not([data-oe-readonly])",
9453+
noCheck: true,
94579454
});
9455+
94589456
/**
94599457
* Allows to display a warning message on outdated snippets.
94609458
*/

addons/web_editor/static/src/js/editor/snippets.options.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,4 +1172,9 @@
11721172
<WeInput selectStyle="''" unit="'px'" cssProperty="'padding'" title.translate="Padding"/>
11731173
</t>
11741174

1175+
<t t-name="web_editor.many2one">
1176+
<WeMany2one changeRecord="''" model="renderContext.model" fields="renderContext.fields"
1177+
title="renderContext.modelName"/>
1178+
</t>
1179+
11751180
</templates>

addons/web_editor/views/snippets.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,6 @@
390390
<!-- options (data-selector, data-drop-in, data-drop-near, data-js to link js object ) -->
391391
<template id="snippet_options">
392392
<!-- t-field -->
393-
<div data-js='many2one'
394-
data-selector="[data-oe-many2one-model]:not([data-oe-readonly])"
395-
data-no-check="true"/>
396-
397393
<div data-selector=".s_hr"
398394
data-drop-near="p, h1, h2, h3, blockquote, .s_hr"/>
399395

addons/website_blog/static/src/js/options.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
/** @odoo-module **/
22

33
import { _t } from "@web/core/l10n/translation";
4-
import options from "@web_editor/js/editor/snippets.options.legacy";
5-
import { SnippetOption } from "@web_editor/js/editor/snippets.options";
4+
import { SnippetOption, Many2oneOption } from "@web_editor/js/editor/snippets.options";
65
import {
76
CoverProperties,
87
} from "@website/js/editor/snippets.options";
98
import {
109
registerWebsiteOption,
1110
} from "@website/js/editor/snippets.registry";
1211
import { updateOption } from "@web_editor/js/editor/snippets.registry";
13-
import "@website/js/editor/snippets.options";
1412
import { uniqueId } from "@web/core/utils/functions";
1513
import { patch } from "@web/core/utils/patch";
1614

1715
const NEW_TAG_PREFIX = 'new-blog-tag-';
1816

19-
options.registry.many2one.include({
20-
17+
patch(Many2oneOption.prototype, {
2118
//--------------------------------------------------------------------------
2219
// Private
2320
//--------------------------------------------------------------------------
2421

2522
/**
2623
* @override
24+
* TODO: check why it's there and possibly remove the patch entirely. This
25+
* "overrides" a method that does not exist on the parent.
2726
*/
28-
_selectRecord: function ($opt) {
27+
_selectRecord($opt) {
2928
var self = this;
30-
this._super.apply(this, arguments);
29+
super._selectRecord.apply(this, arguments);
3130
if (this.$target.data('oe-field') === 'author_id') {
3231
var $nodes = $('[data-oe-model="blog.post"][data-oe-id="' + this.$target.data('oe-id') + '"][data-oe-field="author_avatar"]');
3332
$nodes.each(function () {

0 commit comments

Comments
 (0)