Skip to content

Commit 0df9683

Browse files
committed
[IMP] awesome_dashboard: add and remove dashboard items
1 parent 6e53f4b commit 0df9683

File tree

7 files changed

+101
-9
lines changed

7 files changed

+101
-9
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 { Dialog } from "@web/core/dialog/dialog";
3+
import { CheckBox } from "@web/core/checkbox/checkbox";
4+
5+
export class ConfigurationDialog extends Component {
6+
static template = "awesome_dashboard.ConfigurationDialog"
7+
static components = { Dialog, CheckBox }
8+
static props = {
9+
items: [Array],
10+
close: [{}],
11+
onApply: [Function]
12+
}
13+
14+
setup() {
15+
this.disabledElements = this.props.items.filter(item => !item.enabled)
16+
.map(item => item.element.id)
17+
}
18+
19+
onChange(id) {
20+
let index = this.disabledElements.indexOf(id)
21+
if(index === -1) {
22+
this.disabledElements.push(id)
23+
return;
24+
}
25+
this.disabledElements.splice(index, 1)
26+
}
27+
28+
apply() {
29+
localStorage.setItem('disabledElements', this.disabledElements)
30+
this.props.onApply();
31+
this.props.close()
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="perserve">
3+
<t t-name="awesome_dashboard.ConfigurationDialog">
4+
<Dialog title="'Dashboard configuration dialog'">
5+
<div class="d-flex flex-column">
6+
<t t-foreach="props.items" t-as="item" t-key="item.element.id">
7+
<div class="d-inline-flex gap-2">
8+
<p><t t-esc="item.element.description" /></p>
9+
<CheckBox
10+
value="item.enabled"
11+
t-on-change="(_) => this.onChange(item.element.id)" />
12+
</div>
13+
</t>
14+
</div>
15+
<t t-set-slot="footer">
16+
<button class="btn btn-primary" t-on-click="apply" >
17+
Apply
18+
</button>
19+
</t>
20+
</Dialog>
21+
</t>
22+
</templates>

awesome_dashboard/static/src/dashboard/dashboard.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { registry } from "@web/core/registry";
66
import { Layout } from "@web/search/layout";
77
import { useService } from "@web/core/utils/hooks";
88
import { DashboardItem } from "./dashboard_item";
9-
import { items } from "./dashboard_items";
109
import { PieChart } from "./pie_chart/pie_chart";
10+
import { ConfigurationDialog } from "./configuration_dialog";
1111

1212

1313
class AwesomeDashboard extends Component {
@@ -17,12 +17,22 @@ class AwesomeDashboard extends Component {
1717
setup() {
1818
this.action = useService("action");
1919
this.statistics = useService("awesome_dashboard.statistics");
20-
this.state = useState({stats: this.statistics});
20+
this.state = useState({items: [], stats: this.statistics});
21+
this.dialog = useService("dialog");
22+
this.updateDashboard()
23+
}
2124

22-
for(let item of items) {
23-
registry.category("awesome_dashboard").add(item.id, item);
25+
updateDashboard() {
26+
let disabledElements = localStorage.getItem('disabledElements') || '';
27+
if(disabledElements.includes(',')) {
28+
disabledElements = disabledElements.split(',')
29+
}
30+
else{
31+
disabledElements = [disabledElements]
2432
}
25-
this.items = registry.category("awesome_dashboard").getAll();
33+
this.state.items = registry.category("awesome_dashboard")
34+
.getAll()
35+
.filter(el => !disabledElements.some((e) => e == el.id ))
2636
}
2737

2838
openCustomers() {
@@ -37,6 +47,23 @@ class AwesomeDashboard extends Component {
3747
views: [[false, 'list'], [false, 'form']]
3848
})
3949
}
50+
51+
customizeDashboard() {
52+
let items = []
53+
let all = registry.category("awesome_dashboard").getAll();
54+
all.forEach(el => {
55+
const enabled = this.state.items.some(item => item.id == el.id)
56+
items.push({
57+
element: el,
58+
enabled: enabled
59+
})
60+
})
61+
62+
this.dialog.add(ConfigurationDialog, {
63+
items: items,
64+
onApply: this.updateDashboard.bind(this)
65+
})
66+
}
4067
}
4168

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

awesome_dashboard/static/src/dashboard/dashboard.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
<t t-set-slot='layout-buttons'>
77
<button class="btn btn-primary" t-on-click="openCustomers">Customers</button>
88
<button class="btn btn-primary" t-on-click="openLeads">Leads</button>
9+
<button class="btn" t-on-click="customizeDashboard">
10+
<span class="fa fa-gear" />
11+
</button>
912
</t>
1013
<div class="d-flex p-4 flex-wrap">
11-
<t t-foreach="items" t-as="item" t-key="item.id">
14+
<t t-foreach="state.items" t-as="item" t-key="item.id">
1215
<DashboardItem size="item.size || 1">
1316
<t t-set="itemProp" t-value="item.props
1417
? item.props(state.stats)

awesome_dashboard/static/src/dashboard/dashboard_items.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { NumberCard } from "./number_card";
2+
import { registry } from "@web/core/registry";
23
import { PieChartCard } from "./pie_chart_card";
34

4-
export const items = [
5+
const items = [
56
{
67
id: "average_quantity",
78
description: "Average amount of t-shirt",
@@ -63,3 +64,7 @@ export const items = [
6364
})
6465
},
6566
]
67+
68+
for(let item of items) {
69+
registry.category("awesome_dashboard").add(item.id, item);
70+
}

awesome_dashboard/static/src/dashboard/pie_chart/pie_chart.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { loadJS } from "@web/core/assets"
44
export class PieChart extends Component {
55
static template = "awesome_dashboard.PieChart";
66

7-
static props = {'data':{optional: false}, 'size': {type: Number, optional: true}}
7+
static props = {
8+
'data':{optional: false},
9+
'size': {type: Number, optional: true}
10+
}
811

912
setup() {
1013
onWillStart(() => loadJS(["/web/static/lib/Chart/Chart.js"]));

awesome_owl/static/src/playground.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export class Playground extends Component {
1414
}
1515

1616
incrementSum() {
17-
console.log('call')
1817
this.state.sum++
1918
}
2019

0 commit comments

Comments
 (0)