Skip to content

Commit

Permalink
[ADD] add plm_many2one_image widget
Browse files Browse the repository at this point in the history
  • Loading branch information
jayraj-omnia committed Jan 24, 2025
1 parent 2cefa55 commit c5e90eb
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plm/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"plm/static/src/css/color_fields_tree.css",
"plm/static/src/js/mrp_bom_overview_line.js",
"plm/static/src/xml/mrp_bom_overview_table_inherit.xml",

# many2one Widget
"plm/static/src/js/many2one_widget/many2one_widget.js",
"plm/static/src/js/many2one_widget/many2one_widget.xml",
],
'web.report_assets_common': [
"plm/static/src/scss/document_bom.scss",
Expand Down
26 changes: 26 additions & 0 deletions plm/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,32 @@ def fillUpSrvPath(p_p_id):
fillUpSrvPath(product_id)
return list(set(out_src))

def action_open_linked_field(self, related_field):
view_id = self.env["ir.model.data"]._xmlid_to_res_id("plm.document_kanban_view")
related_field_value = getattr(self, related_field, False)
if related_field_value:
related_ids = related_field_value.ids
else:
related_ids = []
ctx = self.env.context.copy()
domain = [('is_plm', '=', True), ('id', 'in', related_ids)]
ctx.update({
"create": False,
"delete": False,
'default_res_ids': related_ids,
'readonly': True
})

return {
"type": "ir.actions.act_window",
"view_mode": "kanban",
'domain': domain,
"res_model": "ir.attachment",
"target": "new",
"views": [[view_id, "kanban"]],
"context": ctx,
}


class PlmTemporayMessage(models.TransientModel):
_name = "plm.temporary.message"
Expand Down
77 changes: 77 additions & 0 deletions plm/static/src/js/many2one_widget/many2one_widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/** @odoo-module */
import {registry} from "@web/core/registry";
import {Many2OneField, many2OneField} from "@web/views/fields/many2one/many2one_field";
import {onWillUpdateProps} from "@odoo/owl";

export class PlmMany2oneWidget extends Many2OneField {

static template = "plm.PlmMany2oneWidget";
static props = {
...Many2OneField.props,
options: {type: Object, optional: true},
};
static defaultProps = {
...Many2OneField.defaultProps,
options: false,
};

async setup() {
super.setup();
console.log("..........1");
this.imageData = false;
this.relatedField = false;
this.imageToolTipData = false;
onWillUpdateProps(async (nextProps) => {
this.imageData = false;
this.imageToolTipData = false;
let fieldName = nextProps.name;
if (nextProps && nextProps.record && nextProps.record.data && nextProps.record.data[fieldName] && nextProps.record.data[fieldName].length != 0) {
let imageData = await this.env.model.orm.call(this.relation, "search_read", [], {
domain: [["id", "=", nextProps.record.data[fieldName][0]]],
fields: [nextProps.options.image_field],
});
if (imageData && imageData.length != 0 && imageData[0][nextProps.options.image_field]) {
this.imageData = "data:image/png;base64, " + imageData[0][nextProps.options.image_field];
this.imageToolTipData = JSON.stringify({"url": this.imageData});
this.render();
}
}
});
if (this.props && this.props.record && this.props.record.data && this.props.record.data[this.props.name] && this.props.record.data[this.props.name].length != 0) {


let imageData = await this.env.model.orm.call(this.relation, "search_read", [], {
domain: [["id", "=", this.props.record.data[this.props.name][0]]],
fields: [this.props.options.image_field],
});
this.relatedField = await this.env.model.orm.call(this.relation, "search_read", [], {
domain: [["id", "=", this.props.record.data[this.props.name][0]]],
fields: [this.props.options.linked_field],
});
if (imageData && imageData.length != 0 && imageData[0][this.props.options.image_field]) {
this.imageData = "data:image/png;base64, " + imageData[0][this.props.options.image_field];
this.imageToolTipData = JSON.stringify({"url": this.imageData});
this.render();
}
}
}

onImageClicked() {

let selectedProductId = this.props.record.data.product_id[0];
let relatedFieldName = this.props.options.linked_field;
let model = this.props.record.model.root.model.config.fields[this.props.name].relation;
let action_open_linked_field = this.props.record.model.orm.call(model, "action_open_linked_field", [selectedProductId, relatedFieldName]);
return this.action.doAction(action_open_linked_field);
}
}

const plmMany2oneField = {
...many2OneField,
component: PlmMany2oneWidget,
extractProps: ({attrs, context, decorations, options, string}, dynamicInfo) => ({
...many2OneField.extractProps({attrs, context, decorations, options, string}, dynamicInfo),
options: options,
}),
};
registry.category("fields").add("plm_many2one_image", plmMany2oneField);
15 changes: 15 additions & 0 deletions plm/static/src/js/many2one_widget/many2one_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<templates xml:space="preserve">
<t t-name="plm.PlmMany2oneWidget" t-inherit="web.Many2OneField" t-inherit-mode="primary">
<xpath expr="//div[hasclass('o_field_many2one_selection')]/Many2XAutocomplete" position="before">
<span t-if="imageData">
<img data-tooltip-template="web.ImageZoomTooltip"
t-att-data-tooltip-info="imageToolTipData"
style="width:30px;height:30px" t-att-src="imageData"
t-on-click="onImageClicked"
/>
</span>
</xpath>
</t>
</templates>

0 comments on commit c5e90eb

Please sign in to comment.