Skip to content

Commit 8c572c0

Browse files
committed
[ADD] awesome_owl,*: add dashboard using owl framework
*= awesome_dashboard In this tutorial, understand owl framework and complete task and implement to-do list using owl framework. Also create dashboard. in dashboard component implement piechart , learn to display real life update and implement logic for add and remove items from the dashboard.
1 parent c3d9946 commit 8c572c0

28 files changed

+633
-26
lines changed

awesome_dashboard/__manifest__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
'web.assets_backend': [
2626
'awesome_dashboard/static/src/**/*',
2727
],
28+
'awesome_dashboard.dashboard': [
29+
'awesome_dashboard/static/src/dashboard/**/*'
30+
]
2831
},
2932
'license': 'AGPL-3'
3033
}

awesome_dashboard/static/src/dashboard.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

awesome_dashboard/static/src/dashboard.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/** @odoo-module **/
2+
3+
import { Component, onWillStart, useState } from "@odoo/owl";
4+
import { registry } from "@web/core/registry";
5+
import { Layout } from "@web/search/layout";
6+
import { useService } from "@web/core/utils/hooks";
7+
import { DashboardItem } from "./dashboard_item/dashboard_item";
8+
import { Dialog } from "@web/core/dialog/dialog";
9+
import { CheckBox } from "@web/core/checkbox/checkbox";
10+
import { browser } from "@web/core/browser/browser";
11+
12+
class AwesomeDashboard extends Component {
13+
static template = "awesome_dashboard.AwesomeDashboard";
14+
static components = { Layout, DashboardItem };
15+
16+
setup() {
17+
this.action = useService("action");
18+
this.display = {
19+
controlPanel: {},
20+
};
21+
this.items = registry.category("awesome_dashboard").getAll();
22+
this.statistics = useState(useService("awesome_dashboard.statistics"));
23+
this.dialog = useService("dialog");
24+
this.state = useState({
25+
disabledItems:
26+
browser.localStorage.getItem("disabledDashboardItems")?.split(",") ||
27+
[],
28+
});
29+
}
30+
31+
openConfiguration() {
32+
this.dialog.add(ConfigurationDialog, {
33+
items: this.items,
34+
disabledItems: this.state.disabledItems,
35+
onUpdateConfiguration: this.updateConfiguration.bind(this),
36+
});
37+
}
38+
openCustomers() {
39+
this.action.doAction("base.action_partner_form");
40+
}
41+
42+
updateConfiguration(newDisabledItems) {
43+
this.state.disabledItems = newDisabledItems;
44+
}
45+
46+
openLeads() {
47+
this.action.doAction({
48+
type: "ir.actions.act_window",
49+
name: "Leads",
50+
res_model: "crm.lead",
51+
views: [
52+
[false, "list"],
53+
[false, "form"],
54+
],
55+
target: "current",
56+
});
57+
}
58+
}
59+
60+
class ConfigurationDialog extends Component {
61+
static template = "awesome_dashboard.ConfigurationDialog";
62+
static components = { Dialog, CheckBox };
63+
static props = ["close", "items", "disabledItems", "onUpdateConfiguration"];
64+
65+
setup() {
66+
this.items = useState(
67+
this.props.items.map((item) => {
68+
return {
69+
...item,
70+
enabled: !this.props.disabledItems.includes(item.id),
71+
};
72+
})
73+
);
74+
}
75+
76+
done() {
77+
this.props.close();
78+
}
79+
80+
onChange(checked, changedItem) {
81+
changedItem.enabled = checked;
82+
const newDisabledItems = Object.values(this.items)
83+
.filter((item) => !item.enabled)
84+
.map((item) => item.id);
85+
86+
browser.localStorage.setItem("disabledDashboardItems", newDisabledItems);
87+
88+
this.props.onUpdateConfiguration(newDisabledItems);
89+
}
90+
}
91+
92+
registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.o_dashboard {
2+
background-color: gray;
3+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_dashboard.AwesomeDashboard">
5+
<Layout display="display" className="'o_dashboard h-100'">
6+
<t t-set-slot="layout-buttons">
7+
<button class="btn btn-primary" t-on-click="openCustomers">Customers</button>
8+
<button class="btn btn-primary" t-on-click="openLeads">Leads</button>
9+
<t t-set-slot="control-panel-additional-actions">
10+
<button t-on-click="openConfiguration" class="btn p-0 ms-1 border-0">
11+
<i class="fa fa-cog"></i>
12+
</button>
13+
</t>
14+
</t>
15+
<t t-slot="content">
16+
some content
17+
</t>
18+
<div class="d-flex flex-wrap" t-if="statistics.isReady">
19+
<t t-foreach="items" t-as="item" t-key="item.id">
20+
<DashboardItem t-if="!state.disabledItems.includes(item.id)" size="item.size || 1">
21+
<t t-set="itemProp" t-value="item.props ? item.props(statistics) : {'data': statistics}"/>
22+
<t t-component="item.Component" t-props="itemProp" />
23+
</DashboardItem>
24+
</t>
25+
</div>
26+
</Layout>
27+
</t>
28+
<t t-name="awesome_dashboard.ConfigurationDialog">
29+
<Dialog title="'Dashboard items configuration'">
30+
Which cards do you whish to see ?
31+
<t t-foreach="items" t-as="item" t-key="item.id">
32+
<CheckBox value="item.enabled" onChange="(ev) => this.onChange(ev, item)">
33+
<t t-esc="item.description"/>
34+
</CheckBox>
35+
</t>
36+
<t t-set-slot="footer">
37+
<button class="btn btn-primary" t-on-click="done">
38+
Done
39+
</button>
40+
</t>
41+
</Dialog>
42+
</t>
43+
44+
</templates>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** @odoo-module */
2+
3+
import { Component } from "@odoo/owl";
4+
5+
export class DashboardItem extends Component {
6+
static template = "awesome_dashboard.DashboardItem";
7+
static props = {
8+
slots: {
9+
type: Object,
10+
shape: {
11+
default: Object,
12+
},
13+
},
14+
size: {
15+
type: Number,
16+
default: 1,
17+
optional: true,
18+
},
19+
};
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<templates xml:space="preserve">
3+
<t t-name="awesome_dashboard.DashboardItem">
4+
<div class="card m-2 border-dark" t-attf-style="width: {{18*props.size}}rem;">
5+
<div class="card-body">
6+
<t t-slot="default"/>
7+
</div>
8+
</div>
9+
</t>
10+
</templates>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/** @odoo-module */
2+
3+
import { NumberCard } from "./number_card/number_card";
4+
import { PieChartCard } from "./pie_chart_card/pie_chart_card";
5+
import { registry } from "@web/core/registry";
6+
7+
const items = [
8+
{
9+
id: "average_quantity",
10+
description: "Average amount of t-shirt",
11+
Component: NumberCard,
12+
props: (data) => ({
13+
title: "Average amount of t-shirt by order this month",
14+
value: data.average_quantity,
15+
}),
16+
},
17+
{
18+
id: "average_time",
19+
description: "Average time for an order",
20+
Component: NumberCard,
21+
props: (data) => ({
22+
title:
23+
"Average time for an order to go from 'new' to 'sent' or 'cancelled'",
24+
value: data.average_time,
25+
}),
26+
},
27+
{
28+
id: "number_new_orders",
29+
description: "New orders this month",
30+
Component: NumberCard,
31+
props: (data) => ({
32+
title: "Number of new orders this month",
33+
value: data.nb_new_orders,
34+
}),
35+
},
36+
{
37+
id: "cancelled_orders",
38+
description: "Cancelled orders this month",
39+
Component: NumberCard,
40+
props: (data) => ({
41+
title: "Number of cancelled orders this month",
42+
value: data.nb_cancelled_orders,
43+
}),
44+
},
45+
{
46+
id: "amount_new_orders",
47+
description: "amount orders this month",
48+
Component: NumberCard,
49+
props: (data) => ({
50+
title: "Total amount of new orders this month",
51+
value: data.total_amount,
52+
}),
53+
},
54+
{
55+
id: "pie_chart",
56+
description: "Shirt orders by size",
57+
Component: PieChartCard,
58+
size: 2,
59+
props: (data) => ({
60+
title: "Shirt orders by size",
61+
values: data.orders_by_size,
62+
}),
63+
},
64+
];
65+
66+
items.forEach((item) => {
67+
registry.category("awesome_dashboard").add(item.id, item);
68+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** @odoo-module */
2+
3+
import { Component } from "@odoo/owl";
4+
5+
export class NumberCard extends Component {
6+
static template = "awesome_dashboard.NumberCard";
7+
static props = {
8+
title: {
9+
type: String,
10+
},
11+
value: {
12+
type: Number,
13+
},
14+
};
15+
}

0 commit comments

Comments
 (0)