[ADD] web_tree_column_width_remember: Remember the tree view columns' widths

Remember the tree columns' widths across sessions, and after filtering, grouping, or reordering.
This commit is contained in:
Francisco Javier Luna Vazquez
2022-08-17 13:41:17 -05:00
committed by Francisco Javier Luna Vázquez
parent 0ce1d1c98e
commit 5e48866283
11 changed files with 629 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
odoo.define("remember_tree_column_width.ListRenderer", function (require) {
"use strict";
const ListRenderer = require("web.ListRenderer");
ListRenderer.include({
events: Object.assign({}, ListRenderer.prototype.events, {
"pointerdown th .o_resize": "_onMouseDownResize",
mouseup: "_onMouseUpResize",
}),
_onMouseDownResize: function () {
this.resizeInProgress = true;
},
_getLocalStorageWidthColumnName: function (model, field) {
return "odoo.columnWidth." + model + "." + field;
},
_onMouseUpResize: function (ev) {
if (this.resizeInProgress) {
this.resizeInProgress = false;
const target = $(ev.target);
const $th = target.is("th") ? target : target.parent("th");
const fieldName = $th.length ? $th.data("name") : undefined;
if (
this.state &&
this.state.model &&
fieldName &&
window.localStorage
) {
window.localStorage.setItem(
this._getLocalStorageWidthColumnName(
this.state.model,
fieldName
),
parseInt(($th[0].style.width || "0").replace("px", "")) || 0
);
}
}
},
_squeezeTable: function () {
const columnWidths = this._super.apply(this, arguments);
const table = this.el.getElementsByTagName("table")[0];
const thead = table.getElementsByTagName("thead")[0];
const thElements = [...thead.getElementsByTagName("th")];
const self = this;
thElements.forEach(function (el, elIndex) {
const fieldName = $(el).data("name");
if (
self.state &&
self.state.model &&
fieldName &&
window.localStorage
) {
const storedWidth = window.localStorage.getItem(
self._getLocalStorageWidthColumnName(
self.state.model,
fieldName
)
);
if (storedWidth) {
columnWidths[elIndex] = parseInt(storedWidth);
}
}
});
return columnWidths;
},
});
});