[MIG] web_widget_url_advanced: Migration to 17.0

This commit is contained in:
Nedas Žilinskas
2024-05-27 20:16:28 +03:00
committed by Maksym Yankin
parent c85c6ed415
commit 7a357dd55d
9 changed files with 113 additions and 82 deletions

View File

@@ -0,0 +1,56 @@
/** @odoo-module **/
/* Copyright 2018 Simone Orsi - Camptocamp SA
License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html). */
import {patch} from "@web/core/utils/patch";
import {UrlField, formUrlField, urlField} from "@web/views/fields/url/url_field";
patch(UrlField.props, {
text_field: {type: String, optional: true},
prefix_name: {type: String, optional: true},
});
patch(UrlField.prototype, {
_get_text_field() {
if (this.props.text_field) {
let field_value = this.props.record.data[this.props.text_field];
if (Array.isArray(field_value) && field_value.length == 2) {
field_value = field_value[1];
}
return field_value;
}
return false;
},
get title() {
return (
this._get_text_field() ||
this.props.text ||
this.props.record.data[this.props.name] ||
""
);
},
get formattedHrefWithPrefix() {
let value = this.formattedHref;
if (this.props.prefix_name) {
value = this.props.prefix_name + ":" + value;
}
return value;
},
});
const patchExtractProps = {
extractProps({attrs, options}) {
const props = super.extractProps(...arguments);
props.text_field = attrs.text_field || options.text_field;
props.prefix_name = attrs.prefix_name || options.prefix_name;
return props;
},
};
patch(urlField, patchExtractProps);
patch(formUrlField, patchExtractProps);

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates id="template" xml:space="preserve">
<t
t-name="web_widget_url_advanced.UrlField"
t-inherit="web.UrlField"
t-inherit-mode="extension"
>
<xpath expr="//a[hasclass('o_field_widget')]" position="attributes">
<attribute name="t-esc">title</attribute>
<attribute name="t-att-href">formattedHrefWithPrefix</attribute>
</xpath>
</t>
<t
t-name="web_widget_url_advanced.FormUrlField"
t-inherit="web.FormUrlField"
t-inherit-mode="extension"
>
<xpath expr="//a[hasclass('o_field_widget')]" position="attributes">
<attribute name="t-esc">title</attribute>
<attribute name="t-att-href">formattedHrefWithPrefix</attribute>
</xpath>
</t>
</templates>

View File

@@ -1,50 +0,0 @@
/* Copyright 2018 Simone Orsi - Camptocamp SA
License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html). */
odoo.define("web_widget_url_advanced", function (require) {
"use strict";
var basic_fields = require("web.basic_fields");
basic_fields.UrlWidget.include({
/**
* @override
*/
init: function () {
this._super.apply(this, arguments);
// Retrieve customized `<a />` text from a field
// via `text_field` attribute or `options.text_field`
this.text_field = this.attrs.text_field || this.attrs.options.text_field;
},
/**
* Retrieve anchor text based on options.
* @returns {String}
*/
_get_text: function () {
if (this.text_field) {
var field_value = this.recordData[this.text_field];
if (_.isObject(field_value) && _.has(field_value.data)) {
field_value = field_value.data.display_name;
}
return field_value;
}
return this.attrs.text;
},
/**
*
* @override
* @private
*/
_renderReadonly: function () {
// Base widget uses `this.attrs.text` instead of `this.value` when available.
// TODO: To check better way for update link
this.attrs.text = this._get_text();
this._super.apply(this, arguments);
var prefix = this.attrs.prefix_name || this.attrs.options.prefix_name;
if (prefix) {
this.$el.html(
$(this.$el.html()).attr("href", prefix + ":" + this.value)
);
}
},
});
});