Skip to content

Commit

Permalink
[IMP] spreadsheet_oca: Alloe to edit Pivot data
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Jun 10, 2023
1 parent 6c5dc3c commit 4765373
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions spreadsheet_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"spreadsheet.o_spreadsheet": [
"spreadsheet_oca/static/src/spreadsheet/bundle/spreadsheet.xml",
"spreadsheet_oca/static/src/spreadsheet/bundle/filter.esm.js",
"spreadsheet_oca/static/src/spreadsheet/bundle/filter_panel_datasources.esm.js",
"spreadsheet_oca/static/src/spreadsheet/bundle/spreadsheet_renderer.esm.js",
"spreadsheet_oca/static/src/spreadsheet/bundle/spreadsheet_controlpanel.esm.js",
"spreadsheet_oca/static/src/spreadsheet/bundle/spreadsheet_action.esm.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/** @odoo-module **/

import {Component, onWillStart, onWillUpdateProps} from "@odoo/owl";
import {Domain} from "@web/core/domain";
import {DomainSelector} from "@web/core/domain_selector/domain_selector";
import {DomainSelectorDialog} from "@web/core/domain_selector_dialog/domain_selector_dialog";
import {_t} from "web.core";
import spreadsheet from "@spreadsheet/o_spreadsheet/o_spreadsheet_extended";
import {time_to_str} from "web.time";
import {useService} from "@web/core/utils/hooks";

const {sidePanelRegistry, topbarMenuRegistry} = spreadsheet.registries;
const {createFullMenuItem} = spreadsheet.helpers;

topbarMenuRegistry.addChild("data_sources", ["data"], (env) => {
const children = env.model.getters.getPivotIds().map((pivotId, index) =>
createFullMenuItem(`data_source_pivot_ ${pivotId}`, {
name: env.model.getters.getPivotDisplayName(pivotId),
sequence: 100,
action: (child_env) => {
child_env.model.dispatch("SELECT_PIVOT", {
pivotId: pivotId,
});
child_env.openSidePanel("PivotPanel", {});
},
icon: "fa fa-table",
separator: index === env.model.getters.getPivotIds().length - 1,
})
);
return children.concat([
createFullMenuItem(`refresh_all_data`, {
name: _t("Refresh all data"),
sequence: 110,
action: (child_env) => {
child_env.model.dispatch("REFRESH_ALL_DATA_SOURCES");
},
separator: true,
}),
]);
});

export class PivotPanelDisplay extends Component {
setup() {
this.dialog = useService("dialog");
onWillStart(this.modelData.bind(this));
onWillUpdateProps(this.modelData.bind(this));
}
async modelData() {
this.PivotDataSource = await this.env.model.getters.getAsyncPivotDataSource(
this.props.pivotId
);
this.modelLabel = await this.PivotDataSource.getModelLabel();
}
get domain() {
return new Domain(this.props.pivotDefinition.domain).toString();
}
get pivotDimensions() {
return [
...this.props.pivotDefinition.rowGroupBys,
...this.props.pivotDefinition.colGroupBys,
].map((fieldName) => this.PivotDataSource.getFormattedGroupBy(fieldName));
}
get sortInformation() {
const sortedColumn = this.props.pivotDefinition.sortedColumn;
const orderTranslate =
sortedColumn.order === "asc" ? _t("ascending") : _t("descending");
const GroupByDisplayLabel = this.PivotDataSource.getGroupByDisplayLabel(
"measure",
sortedColumn.measure
);
return `${GroupByDisplayLabel} (${orderTranslate})`;
}
get lastUpdate() {
const lastUpdate = this.PivotDataSource.lastUpdate;
if (lastUpdate) {
return time_to_str(new Date(lastUpdate));
}
return _t("not updated");
}
editDomain() {
this.dialog.add(DomainSelectorDialog, {
resModel: this.props.pivotDefinition.model,
initialValue: this.domain,
readonly: false,
isDebugMode: Boolean(this.env.debug),
onSelected: this.onSelectDomain.bind(this),
});
}
onSelectDomain(domain) {
this.env.model.dispatch("UPDATE_ODOO_PIVOT_DOMAIN", {
pivotId: this.props.pivotId,
domain: new Domain(domain).toList(),
});
}
}

PivotPanelDisplay.template = "spreadsheet_oca.PivotPanelDisplay";
PivotPanelDisplay.components = {
DomainSelector,
};
PivotPanelDisplay.properties = {
pivotId: String,
pivotDefinition: Object,
};

export class PivotPanel extends Component {
get pivotId() {
return this.env.model.getters.getSelectedPivotId();
}
get pivotDefinition() {
return this.env.model.getters.getPivotDefinition(this.pivotId);
}
}

PivotPanel.template = "spreadsheet_oca.PivotPanel";
PivotPanel.components = {
PivotPanelDisplay,
};

sidePanelRegistry.add("PivotPanel", {
title: "Pivot table",
Body: PivotPanel,
});
55 changes: 55 additions & 0 deletions spreadsheet_oca/static/src/spreadsheet/bundle/spreadsheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,61 @@
/>
</div>
</t>
<t t-name="spreadsheet_oca.PivotPanel" owl="1">
<PivotPanelDisplay pivotId="pivotId" pivotDefinition="pivotDefinition" />
</t>
<t t-name="spreadsheet_oca.PivotPanelDisplay" owl="1">
<div class="o_spreadsheet_oca_pivot_panel">
<div class="o_spreadsheet_oca_pivot_panel_field">
<div class="o_spreadsheet_oca_pivot_panel_field_title">Pivot name</div>
<span t-esc="env.model.getters.getPivotDisplayName(props.pivotId)" />
</div>
<div class="o_spreadsheet_oca_pivot_panel_field">
<div class="o_spreadsheet_oca_pivot_panel_field_title">Model</div>
<div><t t-esc="modelLabel" /> (<t
t-esc="props.pivotDefinition.model"
/>)</div>
</div>
<div class="o_spreadsheet_oca_pivot_panel_field">
<div class="o_spreadsheet_oca_pivot_panel_field_title">Domain</div>
<DomainSelector
resModel="props.pivotDefinition.model"
value="domain"
t-key="'pivot_' + props.pivotId"
/>
<div class="btn btn-link" t-on-click="editDomain">
Edit domain
</div>
</div>
<div class="o_spreadsheet_oca_pivot_panel_field">
<div class="o_spreadsheet_oca_pivot_panel_field_title">Dimensions</div>
<t t-foreach="pivotDimensions" t-as="dimension" t-key="dimension">
<div t-esc="dimension" />
</t>
</div>
<div
class="o_spreadsheet_oca_pivot_panel_field"
t-if="props.pivotDefinition.sortedColumn"
>
<div class="o_spreadsheet_oca_pivot_panel_field_title">Sorting</div>
<div t-esc="sortInformation" />
</div>
<div class="o_spreadsheet_oca_pivot_panel_field">
<div class="o_spreadsheet_oca_pivot_panel_field_title">Measures</div>
<t
t-foreach="props.pivotDefinition.measures"
t-as="measure"
t-key="measure"
>
<div t-esc="measure" />
</t>
</div>
<div class="o_spreadsheet_oca_pivot_panel_info">
Last updated at <t t-esc="lastUpdate" />
</div>
</div>

</t>
<t t-name="spreadsheet_oca.FilterPanel" owl="1">
<div
t-foreach="env.model.getters.getGlobalFilters()"
Expand Down
13 changes: 13 additions & 0 deletions spreadsheet_oca/static/src/spreadsheet/spreadsheet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
.o_spreadsheet_oca_name_warning {
font-size: 0.8em;
}

.o_spreadsheet_oca_pivot_panel {
padding: 16px;
.o_spreadsheet_oca_pivot_panel_field {
.o_spreadsheet_oca_pivot_panel_field_title {
font-weight: bold;
}
}
}
.o_spreadsheet_oca_pivot_panel_info {
font-style: italic;
color: $o-gray-600;
}
.o_spreadsheet_oca_filter {
padding: 16px;
.spreadsheet_oca_filter_label {
Expand Down

0 comments on commit 4765373

Please sign in to comment.