Skip to content

Commit 5d78b62

Browse files
committed
[IMP] awesome_dashboard: Allow selecting items in dashboard
1 parent eadf4c3 commit 5d78b62

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Component } from "@odoo/owl";
2+
import { CheckBox } from "@web/core/checkbox/checkbox";
3+
import { Dialog } from "@web/core/dialog/dialog";
4+
import { registry } from "@web/core/registry";
5+
import dashboard_items from "../dashboard_items";
6+
7+
export class ConfigurationDialog extends Component {
8+
static template = "awesome_dashboard.ConfigurationDialog";
9+
static components = { CheckBox, Dialog };
10+
static props = {
11+
items: Array,
12+
close: Function,
13+
enabled_items: Array,
14+
setEnabledItems: Function,
15+
};
16+
17+
setup() {
18+
this.all_items = this.props.enabled_items;
19+
}
20+
21+
toggleValue(item) {
22+
if (this.all_items.includes(item)) {
23+
this.all_items = this.all_items.filter(name => name !== item);
24+
} else {
25+
this.all_items = [...this.all_items, item];
26+
}
27+
}
28+
29+
onApply() {
30+
this.props.setEnabledItems(this.all_items);
31+
this.props.close();
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_dashboard.ConfigurationDialog">
5+
<Dialog
6+
size="'md'"
7+
title="'Dashboard items configuration'"
8+
>
9+
<p>Which cards do you wish to see?</p>
10+
<t t-foreach="props.items" t-as="item" t-key="item.id">
11+
<CheckBox
12+
name="item.id"
13+
value="props.enabled_items.includes(item.id)"
14+
onChange="(cheked) => this.toggleValue(item.id)"
15+
>
16+
<t t-esc="item.description"/>
17+
</CheckBox>
18+
</t>
19+
<t t-set-slot="footer">
20+
<button class="btn btn-primary" t-on-click="onApply">
21+
Apply
22+
</button>
23+
</t>
24+
</Dialog>
25+
</t>
26+
27+
</templates>

awesome_dashboard/static/src/dashboard/dashboard.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Component, onWillStart, reactive } from "@odoo/owl";
1+
import { Component, onWillStart, reactive, useState } from "@odoo/owl";
22
import { registry } from "@web/core/registry";
33
import { useService } from "@web/core/utils/hooks";
44
import { Layout } from "@web/search/layout";
55
import { DashboardItem } from "./dashboard_item/dashboard_item";
66
import { PieChart } from "./pie_chart/pie_chart";
7+
import { ConfigurationDialog } from "./configuration_dialog/configuration_dialog";
78
import dashboard_items from "./dashboard_items";
89

910
class AwesomeDashboard extends Component {
@@ -14,6 +15,26 @@ class AwesomeDashboard extends Component {
1415
this.action = useService("action");
1516
this.state = reactive({ statistics: useService("statistics") });
1617
this.items = registry.category("awesome_dashboard").getAll();
18+
this.dialog = useService("dialog");
19+
this.context = useState({ enabled_items: this.getEnabledItems() });
20+
}
21+
22+
getEnabledItems() {
23+
// Open all items by default
24+
const stored_items = JSON.parse(
25+
localStorage.getItem("enabled_items")
26+
);
27+
if (!stored_items) {
28+
const all_items = this.items.map((item) => item.id);
29+
this.setEnabledItems(all_items);
30+
return all_items;
31+
}
32+
return stored_items || [];
33+
}
34+
35+
setEnabledItems(values) {
36+
localStorage.setItem("enabled_items", JSON.stringify(values));
37+
this.context.enabled_items = values;
1738
}
1839

1940
openLeads() {
@@ -31,6 +52,17 @@ class AwesomeDashboard extends Component {
3152
openCustomers() {
3253
this.action.doAction("base.action_partner_form");
3354
}
55+
56+
openConfiguration() {
57+
this.dialog.add(
58+
ConfigurationDialog,
59+
{
60+
items: this.items,
61+
enabled_items: this.context.enabled_items,
62+
setEnabledItems: this.setEnabledItems.bind(this),
63+
},
64+
);
65+
}
3466
}
3567

3668
registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard);

awesome_dashboard/static/src/dashboard/dashboard.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
Leads
1515
</button>
1616
</t>
17+
<t t-set-slot="control-panel-additional-actions">
18+
<button t-on-click="openConfiguration" class="btn">
19+
<i class="fa fa-cog"/>
20+
</button>
21+
</t>
1722
<div class="d-flex align-items-start flex-wrap">
1823
<t t-foreach="items" t-as="item" t-key="item.id">
19-
<DashboardItem size="item.size || 1">
24+
<DashboardItem
25+
size="item.size || 1"
26+
t-if="context.enabled_items.includes(item.id)"
27+
>
2028
<t t-set="itemProp" t-value="item.props ? item.props(state.statistics) : {'data': state.statistics}"/>
2129
<t t-component="item.Component" t-props="itemProp" />
2230
</DashboardItem>

0 commit comments

Comments
 (0)