Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][CHG] fs_base_multi_image: drag and drop fixes #332

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions fs_base_multi_image/models/fs_image_relation_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ def _inverse_image(self):

@api.model
def _cleanup_vals(self, vals):
if (
"link_existing" in vals
and vals["link_existing"]
and "specific_image" in vals
):
vals["specific_image"] = False
link_existing = vals.get("link_existing")
if link_existing:
if "specific_image" in vals:
vals.pop("specific_image")
if "image" in vals:
# image is set when using the kanban renderer so it
# prevents the name field to be computed well
vals.pop("image")
return vals

@api.model_create_multi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ import {onWillRender, useRef, useState} from "@odoo/owl";
import {X2ManyField} from "@web/views/fields/x2many/x2many_field";
import {registry} from "@web/core/registry";

import {useX2ManyCrud} from "@web/views/fields/relational_utils";

export class FsImageRelationDndUploadField extends X2ManyField {
/**
* When using this widget, displayed image relation views must contains
* following fields:
* - sequence
* - image_id
* - specific_image
* - link_existing
*/
setup() {
super.setup();
this.options = this.activeField.options;
this.relationField = this.field.relation_field;
this.defaultTarget = this.options.target || "specific";
this.state = useState({
dragging: false,
Expand All @@ -19,6 +28,8 @@ export class FsImageRelationDndUploadField extends X2ManyField {
this.fileInput = useRef("fileInput");
this.defaultSequence = 0;

this.operations = useX2ManyCrud(() => this.list, this.isMany2Many);

onWillRender(() => {
this.initDefaultSequence();
});
Expand All @@ -28,6 +39,10 @@ export class FsImageRelationDndUploadField extends X2ManyField {
return this.state.target;
}

get relationRecordId() {
return this.props.record.data.id;
}

get displayDndZone() {
const activeActions = this.activeActions;
return (
Expand Down Expand Up @@ -92,18 +107,15 @@ export class FsImageRelationDndUploadField extends X2ManyField {

async uploadFsImage(imagesDesc) {
const self = this;
const createValues = [];
self.env.model.orm
.call("fs.image", "create", [imagesDesc])
.then((fsImageIds) => {
let values = {};
_.each(fsImageIds, (fsImageId) => {
values = self.getFsImageRelationValues(fsImageId);
createValues.push(values);
self.createFieldRelationRecords(values);
});
})
.then(() => {
self.createFieldRelationRecords(createValues);
unblockUI();
})
.catch(() => {
self.displayUploadError();
Expand All @@ -123,49 +135,43 @@ export class FsImageRelationDndUploadField extends X2ManyField {

getFsImageRelationValues(fsImageId) {
let values = {
image_id: fsImageId,
link_existing: true,
default_image_id: fsImageId,
default_link_existing: true,
};
values = {...values, ...this.getRelationCommonValues()};
return values;
}

async uploadSpecificImage(imagesDesc) {
const self = this;
const createValues = [];
_.each(imagesDesc, (imageDesc) => {
createValues.push(self.getSpecificImageRelationValues(imageDesc));
self.createFieldRelationRecords(
self.getSpecificImageRelationValues(imageDesc)
);
});
self.createFieldRelationRecords(createValues);
unblockUI();
}

getSpecificImageRelationValues(imageDesc) {
return {...imageDesc, ...this.getRelationCommonValues()};
return {
...this.getRelationCommonValues(),
default_specific_image: imageDesc.image,
};
}

getRelationCommonValues() {
const values = {
sequence: this.getNewSequence(),
return {
default_sequence: this.getNewSequence(),
};
values[this.relationField] = this.props.record.data.id;
return values;
}

async createFieldRelationRecords(createValues) {
const self = this;
const model = self.env.model;
model.orm
.call(self.activeField.relation, "create", [createValues])
.then(() => {
model.root.load();
model.root.save();
})
.then(() => {
unblockUI();
})
.catch(() => {
self.displayUploadError();
});
await this.list.addNew({
position: "bottom",
context: createValues,
mode: "readonly",
allowWarning: true,
});
}

async uploadImages(files) {
Expand Down
4 changes: 3 additions & 1 deletion fs_base_multi_image/views/fs_image_relation_mixin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
<field name="arch" type="xml">
<kanban>
<field name="image" />
<field name="name" />
<field name="sequence" />
<field name="image_id" />
<field name="specific_image" />
<field name="link_existing" />
<templates>
<t t-name="kanban-box">
<div class="oe_kanban_card oe_kanban_global_click">
Expand Down
2 changes: 1 addition & 1 deletion fs_product_multi_image/views/fs_product_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/>
<field name="mode">primary</field>
<field name="arch" type="xml">
<xpath expr="//kanban/field[@name='name']">
<xpath expr="//kanban/field[@name='image']">
<field name="tag_id" />
</xpath>
<xpath expr="//div[hasclass('o_kanban_image')]" position="inside">
Expand Down
15 changes: 9 additions & 6 deletions fs_product_multi_image/views/product_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
</field>
<page name="sales" position="after">
<page name="images" string="Images">
<field name="image_ids" widget="fs_image_relation_dnd_upload">
<field
name="image_ids"
widget="fs_image_relation_dnd_upload"
mode="kanban"
>
<tree>
<field name="sequence" widget="handle" />
<field name="name" />
<field name="link_existing" invisible="1" />
<field name="image_id" invisible="1" />
<field name="image" invisible="1" />
<field name="specific_image" invisible="1" />
</tree>
Comment on lines +27 to 35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is-it the right way to define the kanban?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a specific kanban view for the image relation model so we don't need to define an inline kanban view here (if this is what you asked).

</field>
<!-- field
name="image_ids"
mode="kanban"
widget="storage_image_handle"
/ -->
</page>
</page>
</field>
Expand Down
Loading