-
Notifications
You must be signed in to change notification settings - Fork 2.6k
18.0 webframework lecan #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 18.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Component, useState } from "@odoo/owl"; | ||
import { registry } from "@web/core/registry"; | ||
import { Layout } from "@web/search/layout"; | ||
import { useService } from "@web/core/utils/hooks"; | ||
import { DashboardItem } from "./dashboard_item"; | ||
import { Dialog } from "@web/core/dialog/dialog" | ||
import { CheckBox } from "@web/core/checkbox/checkbox"; | ||
import { browser } from "@web/core/browser/browser"; | ||
import { _t } from "@web/core/l10n/translation"; | ||
|
||
class AwesomeDashboard extends Component { | ||
static template = "awesome_dashboard.AwesomeDashboard"; | ||
static components = { Layout, DashboardItem }; | ||
|
||
setup() { | ||
this.action = useService("action"); | ||
this.statistics = useState(useService("awesome_dashboard.statistics")); | ||
this.items = registry.category("awesome_dashboard.items").getAll(); | ||
this.dialog = useService("dialog"); | ||
this.state = useState({disabledItems: browser.localStorage.getItem("disabledDashboardItems")?.split(",") || []}); | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
openCustomerView() { | ||
this.action.doAction("base.action_partner_form"); | ||
} | ||
|
||
openLeads() { | ||
this.action.doAction({ | ||
type: "ir.actions.act_window", | ||
name: _t("All leads"), | ||
res_model: "crm.lead", | ||
views: [[false, "list"], [false, "form"]], | ||
}); | ||
} | ||
|
||
openDialog() { | ||
this.dialog.add(ConfigurationDialog, { | ||
items: this.items, | ||
disabledItems: this.state.disabledItems, | ||
onUpdateConfiguration: this.updateConfiguration.bind(this), | ||
}) | ||
} | ||
|
||
updateConfiguration(newDisabledItems) { | ||
this.state.disabledItems = newDisabledItems; | ||
} | ||
} | ||
|
||
|
||
class ConfigurationDialog extends Component { | ||
static template = "awesome_dashboard.ConfigurationDialog"; | ||
static components = { Dialog, CheckBox }; | ||
static props = ["close", "items", "disabledItems", "onUpdateConfiguration"]; | ||
|
||
setup() { | ||
this.items = useState(this.props.items.map((item) => { | ||
return { | ||
...item, | ||
enabled: !this.props.disabledItems.includes(item.id), | ||
} | ||
})); | ||
} | ||
|
||
done() { | ||
this.props.close(); | ||
} | ||
|
||
onChange(checked, changedItem) { | ||
changedItem.enabled = checked; | ||
const newDisabledItems = Object.values(this.items).filter( | ||
(item) => !item.enabled | ||
).map((item) => item.id) | ||
|
||
browser.localStorage.setItem( | ||
"disabledDashboardItems", | ||
newDisabledItems, | ||
); | ||
|
||
this.props.onUpdateConfiguration(newDisabledItems); | ||
} | ||
} | ||
|
||
registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.o_dashboard { | ||
background-color: gray; | ||
} | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="awesome_dashboard.AwesomeDashboard"> | ||
<Layout display="{ controlPanel: {} } " className="'o_dashboard h-100'"> | ||
<t t-set-slot="layout-buttons"> | ||
<button class="btn btn-primary m-3" t-on-click="openCustomerView">Customers</button> | ||
<button class="btn btn-primary m-3" t-on-click="openLeads">Leads</button> | ||
</t> | ||
<t t-set-slot="control-panel-additional-actions"> | ||
<button t-on-click="openDialog" class="btn p-0 ms-1 border-0"> | ||
<i class="fa fa-cog"></i> | ||
</button> | ||
</t> | ||
<div class="d-flex" t-if="statistics.debounce"> | ||
<t t-foreach="items" t-as="item" t-key="item.id"> | ||
<DashboardItem t-if="!state.disabledItems.includes(item.id)" size="item.size"> | ||
<t t-set="itemProp" t-value="item.props ? item.props(statistics) : {'data': statistics}"/> | ||
<t t-component="item.Component" t-props="itemProp"/> | ||
</DashboardItem> | ||
</t> | ||
</div> | ||
</Layout> | ||
</t> | ||
|
||
<t t-name="awesome_dashboard.ConfigurationDialog"> | ||
<Dialog title="'Configuration'"> | ||
<t t-foreach="items" t-as="item" t-key="item.id"> | ||
<CheckBox value="item.enabled" onChange="(ev) => this.onChange(ev, item)"> | ||
<t t-esc="item.description"/> | ||
</CheckBox> | ||
</t> | ||
<t t-set-slot="footer"> | ||
<button class="btn btn-primary" t-on-click="done"> | ||
OK | ||
</button> | ||
</t> | ||
</Dialog> | ||
</t> | ||
|
||
</templates> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Component } from "@odoo/owl"; | ||
|
||
export class DashboardItem extends Component { | ||
static template = "awesome_dashboard.DashboardItem" | ||
static props = { | ||
slots: { | ||
type: Object, | ||
}, | ||
size: { | ||
type: Number, | ||
default: 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't do anything from what i understand. Try removing it and see if anything happens There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To add a default value for size look for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what doesn't do anything ? the default parameter ? or the entire size prop ? because we need it for the item dimension There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default parameter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because it said this in the tutorial : It should take an optional size number props, that default to 1 ( point 3 of chapter 2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes but the |
||
optional: true, | ||
}, | ||
}; | ||
} | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="awesome_dashboard.DashboardItem"> | ||
<div class="card m-2 border-dark" t-attf-style="width: {{18*props.size}}rem;"> | ||
<div class="card-body"> | ||
<t t-slot="default"/> | ||
</div> | ||
</div> | ||
</t> | ||
|
||
</templates> | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { _t } from "@web/core/l10n/translation"; | ||
import { NumberCard } from "./number_card"; | ||
import { PieChartCard } from "./piechart_card"; | ||
import { registry } from "@web/core/registry"; | ||
|
||
const items = [ | ||
{ | ||
id: "average_quantity", | ||
description: _t("Average amount of t-shirt"), | ||
Component: NumberCard, | ||
props: (data) => ({ | ||
title: _t("Average amount of t-shirt by order this month"), | ||
value: data.average_quantity, | ||
}) | ||
}, | ||
{ | ||
id: "average_time", | ||
description: _t("Average time for an order"), | ||
Component: NumberCard, | ||
props: (data) => ({ | ||
title: _t("Average time for an order to go from 'new' to 'sent' or 'cancelled'"), | ||
value: data.average_time, | ||
}) | ||
}, | ||
{ | ||
id: "number_new_orders", | ||
description: _t("New orders this month"), | ||
Component: NumberCard, | ||
props: (data) => ({ | ||
title: "Number of new orders this month", | ||
value: data.nb_new_orders, | ||
}) | ||
}, | ||
{ | ||
id: "cancelled_orders", | ||
description: _t("Cancelled orders this month"), | ||
Component: NumberCard, | ||
props: (data) => ({ | ||
title: _t("Number of cancelled orders this month"), | ||
value: data.nb_cancelled_orders, | ||
}) | ||
}, | ||
{ | ||
id: "amount_new_orders", | ||
description: _t("amount orders this month"), | ||
Component: NumberCard, | ||
props: (data) => ({ | ||
title: _t("Total amount of new orders this month"), | ||
value: data.total_amount, | ||
}) | ||
}, | ||
{ | ||
id: "pie_chart", | ||
description: _t("Shirt orders by size"), | ||
Component: PieChartCard, | ||
size: 2, | ||
props: (data) => ({ | ||
title: _t("Shirt orders by size"), | ||
values: data.orders_by_size, | ||
}) | ||
} | ||
] | ||
|
||
items.forEach(item => registry.category("awesome_dashboard.items").add(item.id, item)); | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Component } from "@odoo/owl"; | ||
|
||
export class NumberCard extends Component { | ||
static template = "awesome_dashboard.NumberCard" | ||
static props = { | ||
title: String, | ||
value: Number, | ||
} | ||
} | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_dashboard.NumberCard" owl="1"> | ||
<t t-esc="props.title"/> | ||
<div class="fs-1 fw-bold text-success text-center"> | ||
<t t-esc="props.value"/> | ||
</div> | ||
</t> | ||
</templates> | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { loadJS } from "@web/core/assets"; | ||
import { Component, onWillStart, useRef, useEffect, onWillUnmount } from "@odoo/owl"; | ||
|
||
export class PieChart extends Component { | ||
static template = "awesome_dashboard.PieChart"; | ||
static props = { | ||
label: String, | ||
data: Object, | ||
}; | ||
|
||
setup() { | ||
this.canvasRef = useRef("canvas"); | ||
onWillStart(() => loadJS("/web/static/lib/Chart/Chart.js")); | ||
useEffect(() => { | ||
if (this.chart) { this.chart.destroy();} | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.displayChart(); | ||
}); | ||
onWillUnmount(() => { | ||
this.chart.destroy(); | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
|
||
displayChart() { | ||
const labels = Object.keys(this.props.data); | ||
const data = Object.values(this.props.data); | ||
this.chart = new Chart(this.canvasRef.el, { | ||
type: "pie", | ||
data: { | ||
labels: labels, | ||
datasets: [ | ||
{ | ||
label: this.props.label, | ||
data: data, | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
} | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_dashboard.PieChart"> | ||
<div t-att-class="'h-100 ' + props.class" t-ref="root"> | ||
<div class="h-100 position-relative" t-ref="container"> | ||
<canvas t-ref="canvas" /> | ||
</div> | ||
</div> | ||
</t> | ||
</templates> | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component } from "@odoo/owl"; | ||
import { PieChart} from "./piechart"; | ||
|
||
export class PieChartCard extends Component { | ||
static template = "awesome_dashboard.PieChartCard" | ||
static components = { PieChart }; | ||
static props = { | ||
title: String, | ||
value: Object, | ||
} | ||
} | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_dashboard.PieChartCard" owl="1"> | ||
<t t-esc="props.title"/> | ||
<PieChart data="props.values" /> | ||
</t> | ||
</templates> | ||
lcantraine marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Component, xml } from "@odoo/owl"; | ||
import { LazyComponent } from "@web/core/assets"; | ||
import { registry } from "@web/core/registry"; | ||
|
||
class DashboardLoader extends Component { | ||
static components = { LazyComponent }; | ||
static template = xml` | ||
<LazyComponent bundle="'awesome_dashboard.dashboard'" Component="'AwesomeDashboard'"/> | ||
`; | ||
|
||
} | ||
|
||
registry.category("actions").add("awesome_dashboard.dashboard", DashboardLoader); |
Uh oh!
There was an error while loading. Please reload this page.