Skip to content

Commit

Permalink
[IMP] web_timeline - support multi group by level
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongnmtm committed Dec 2, 2024
1 parent 271ad52 commit e34aeec
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 117 deletions.
6 changes: 4 additions & 2 deletions web_timeline/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ Known issues / Roadmap

* Implement a more efficient way of refreshing timeline after a record update;
* Make ``attrs`` attribute work;
* When grouping by m2m and more than one record is set, the timeline item appears only
on one group. Allow showing in both groups.
* When grouping by m2m and dragging for changing the time or the group, the changes on
the group will not be set, because it could make disappear the records not related
with the changes that we want to make. When the item is showed in all groups change
Expand Down Expand Up @@ -238,6 +236,10 @@ Contributors

* Houzéfa Abbasbhay

* `Komit <https://komit-consulting.com>`_:

* Cuong Nguyen Mtm <[email protected]>

Maintainers
~~~~~~~~~~~

Expand Down
4 changes: 4 additions & 0 deletions web_timeline/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
* `XCG Consulting <https://xcg-consulting.fr>`_:

* Houzéfa Abbasbhay

* `Komit <https://komit-consulting.com>`_:

* Cuong Nguyen Mtm <[email protected]>
2 changes: 0 additions & 2 deletions web_timeline/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
* Implement a more efficient way of refreshing timeline after a record update;
* Make ``attrs`` attribute work;
* When grouping by m2m and more than one record is set, the timeline item appears only
on one group. Allow showing in both groups.
* When grouping by m2m and dragging for changing the time or the group, the changes on
the group will not be set, because it could make disappear the records not related
with the changes that we want to make. When the item is showed in all groups change
Expand Down
6 changes: 4 additions & 2 deletions web_timeline/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,6 @@ <h1><a class="toc-backref" href="#toc-entry-3">Known issues / Roadmap</a></h1>
<ul class="simple">
<li>Implement a more efficient way of refreshing timeline after a record update;</li>
<li>Make <tt class="docutils literal">attrs</tt> attribute work;</li>
<li>When grouping by m2m and more than one record is set, the timeline item appears only
on one group. Allow showing in both groups.</li>
<li>When grouping by m2m and dragging for changing the time or the group, the changes on
the group will not be set, because it could make disappear the records not related
with the changes that we want to make. When the item is showed in all groups change
Expand Down Expand Up @@ -609,6 +607,10 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
<li>Houzéfa Abbasbhay</li>
</ul>
</li>
<li><a class="reference external" href="https://komit-consulting.com">Komit</a>:<ul>
<li>Cuong Nguyen Mtm &lt;<a class="reference external" href="mailto:cuong.nmtm&#64;komit-consulting.com">cuong.nmtm&#64;komit-consulting.com</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
96 changes: 57 additions & 39 deletions web_timeline/static/src/js/timeline_controller.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/* Copyright 2023 Onestein - Anjeel Haria
* License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */
import AbstractController from "web.AbstractController";
import {Component} from "@odoo/owl";
import Dialog from "web.Dialog";
import {FormViewDialog} from "@web/views/view_dialogs/form_view_dialog";
import time from "web.time";
import core from "web.core";
import Dialog from "web.Dialog";
import time from "web.time";
var _t = core._t;
import {Component} from "@odoo/owl";

export default AbstractController.extend({
custom_events: _.extend({}, AbstractController.prototype.custom_events, {
Expand Down Expand Up @@ -55,11 +55,29 @@ export default AbstractController.extend({
const group_bys = params.groupBy || this.renderer.last_group_bys || [];
this.last_domains = domains;
this.last_contexts = contexts;

const cleanGroupBys = (groupBys) => {
return groupBys.map((group) => {
if (group.includes(":")) {
return group.split(":")[0];
}
return group;
});
};

// Select the group by
let n_group_bys = group_bys;
if (!n_group_bys.length && this.renderer.arch.attrs.default_group_by) {
n_group_bys = this.renderer.arch.attrs.default_group_by.split(",");
let arch_attrs_default_group_by = this.renderer.arch.attrs.default_group_by
? this.renderer.arch.attrs.default_group_by.split(",")
: [];
arch_attrs_default_group_by = cleanGroupBys(arch_attrs_default_group_by);

if (!n_group_bys.length && arch_attrs_default_group_by.length) {
n_group_bys = arch_attrs_default_group_by;
} else {
n_group_bys = cleanGroupBys(n_group_bys);
}

this.renderer.last_group_bys = n_group_bys;
this.renderer.last_domains = domains;

Expand All @@ -73,7 +91,9 @@ export default AbstractController.extend({
kwargs: {
fields: fields,
domain: domains,
order: [{name: this.renderer.arch.attrs.default_group_by}],
order: arch_attrs_default_group_by.map((group) => {
return {name: group};
}),
},
context: this.getSession().user_context,
}).then((data) =>
Expand All @@ -92,11 +112,12 @@ export default AbstractController.extend({
* @returns {jQuery.Deferred}
*/
_onGroupClick: function (event) {
const groupField = this.renderer.last_group_bys[0];
const groups = event.data.item.group.split("/");
const [res_model, res_id] = groups[groups.length - 1].split("-");
return this.do_action({
type: "ir.actions.act_window",
res_model: this.renderer.fields[groupField].relation,
res_id: event.data.item.group,
res_model,
res_id: parseInt(res_id, 10),
target: "new",
views: [[false, "form"]],
});
Expand All @@ -119,14 +140,19 @@ export default AbstractController.extend({
*
* @private
* @param {EventObject} event
* @returns {jQuery.Deferred}
*/
_onUpdate: function (event) {
const item = event.data.item;
const item_id = Number(item.evt.id) || item.evt.id;
const item_id = Number(item.evt.record_id) || item.evt.record_id;
return this.openItem(item_id, true);
},

/** Open specified item, either through modal, or by navigating to form view. */
/** Open specified item, either through modal, or by navigating to form view.
* @param {Number} item_id
* @param {Boolean} is_editable
* @returns {void}
*/
openItem: function (item_id, is_editable) {
if (this.open_popup_action) {
const options = {
Expand Down Expand Up @@ -166,11 +192,7 @@ export default AbstractController.extend({
const fields = this.renderer.fields;
const event_start = item.start;
const event_end = item.end;
let group = false;
if (item.group !== -1) {
group = item.group;
}
const data = {};
let data = {};
// In case of a move event, the date_delay stay the same,
// only date_start and stop must be updated
data[this.date_start] = time.auto_date_to_str(
Expand All @@ -194,29 +216,25 @@ export default AbstractController.extend({
);
data[this.date_delay] = diff_seconds / 3600;
}
const grouped_field = this.renderer.last_group_bys[0];
this._rpc({
model: this.modelName,
method: "fields_get",
args: [grouped_field],
context: this.getSession().user_context,
}).then(async (fields_processed) => {
if (
this.renderer.last_group_bys &&
this.renderer.last_group_bys instanceof Array &&
fields_processed[grouped_field].type !== "many2many"
) {
data[this.renderer.last_group_bys[0]] = group;
}

this.moveQueue.push({
id: event.data.item.id,
data: data,
event: event,
});
const group_record_values = this.renderer.groups.reduce((acc, group) => {
if (group.id === item.group) {
return group.group_record_values;
}
return acc;
}, {});
data = {
...data,
...group_record_values,
};

this.debouncedInternalMove();
this.moveQueue.push({
id: event.data.item.id,
data: data,
event: event,
});

this.debouncedInternalMove();
},

/**
Expand All @@ -235,7 +253,7 @@ export default AbstractController.extend({
this._rpc({
model: this.model.modelName,
method: "write",
args: [[item.event.data.item.id], item.data],
args: [[item.event.data.item.record_id], item.data],
context: this.getSession().user_context,
}).then(() => {
item.event.data.callback(item.event.data.item);
Expand Down Expand Up @@ -360,12 +378,12 @@ export default AbstractController.extend({
return this._rpc({
model: this.modelName,
method: "unlink",
args: [[event.data.item.id]],
args: [[event.data.item.record_id]],
context: this.getSession().user_context,
}).then(() => {
let unlink_index = false;
for (var i = 0; i < this.model.data.data.length; i++) {
if (this.model.data.data[i].id === event.data.item.id) {
if (this.model.data.data[i].id === event.data.item.record_id) {
unlink_index = i;
}
}
Expand Down
7 changes: 6 additions & 1 deletion web_timeline/static/src/js/timeline_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ odoo.define("web_timeline.TimelineModel", function (require) {
* @returns {jQuery.Deferred}
*/
_loadTimeline: function () {
const order = this.default_group_by.split(",").map((group) => {
// Handle the case where the group by is a field with a group operator
// e.g. date:month
return {name: group.includes(":") ? group.split(":")[0] : group};
});
return this._rpc({
model: this.modelName,
method: "search_read",
kwargs: {
fields: this.fieldNames,
domain: this.data.domain,
order: [{name: this.default_group_by}],
order: order,
context: this.data.context,
},
}).then((events) => {
Expand Down
Loading

0 comments on commit e34aeec

Please sign in to comment.