diff --git a/awesome_dashboard/static/src/dashboard.js b/awesome_dashboard/static/src/dashboard.js deleted file mode 100644 index c4fb245621b..00000000000 --- a/awesome_dashboard/static/src/dashboard.js +++ /dev/null @@ -1,8 +0,0 @@ -import { Component } from "@odoo/owl"; -import { registry } from "@web/core/registry"; - -class AwesomeDashboard extends Component { - static template = "awesome_dashboard.AwesomeDashboard"; -} - -registry.category("actions").add("awesome_dashboard.dashboard", AwesomeDashboard); diff --git a/awesome_dashboard/static/src/dashboard.xml b/awesome_dashboard/static/src/dashboard.xml deleted file mode 100644 index 1a2ac9a2fed..00000000000 --- a/awesome_dashboard/static/src/dashboard.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - hello dashboard - - - diff --git a/awesome_dashboard/static/src/dashboard/dashboard.js b/awesome_dashboard/static/src/dashboard/dashboard.js new file mode 100644 index 00000000000..2dc2a3bbb51 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.js @@ -0,0 +1,93 @@ +import { Component, onWillStart, useState } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { useService } from "@web/core/utils/hooks"; +import { Layout } from "@web/search/layout"; +import { Dialog } from "@web/core/dialog/dialog"; +import { browser } from "@web/core/browser/browser"; +import { CheckBox } from "@web/core/checkbox/checkbox"; + + +import { DashboardItem } from "./dashboard_item/dashboard_item"; +import { PieChart } from "./pie_chart"; + +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); + } + +} + +class AwesomeDashboard extends Component { + static template = "awesome_dashboard.AwesomeDashboard"; + static components = { Layout, DashboardItem, PieChart }; + + setup() { + this.action = useService("action"); + this.statService = useService("statistics"); + this.statistics = this.statService.onUpdate; + this.items = registry.category("awesome_dashboard").getAll(); + this.dialog = useService("dialog"); + this.state = useState({ + // NOTE; ConfigurationDialog performs saving into browser local storage + disabledItems: browser.localStorage.getItem("disabledDashboardItems")?.split(",") || [], + }) + + onWillStart(async () => { + const result = await this.statService.loadStatistics(); + // WARN; Perform immediate/synchronous update of state because sub-components + // logic isn't safeguarded against undefined values. + for (const [key, value] of Object.entries(result)) { + this.statistics[key] = value; + } + }); + } + + async openCustomers() { + this.action.doAction("base.action_partner_form", {}); + } + + async openLeads() { + this.action.doAction("crm.crm_lead_all_leads"); + } + + async openSettings() { + this.dialog.add(ConfigurationDialog, { + items: this.items, + disabledItems: this.state.disabledItems, + onUpdateConfiguration: this.updateConfiguration.bind(this), + }) + } + + async updateConfiguration(newDisabledItems) { + this.state.disabledItems = newDisabledItems; + } +} + +registry.category("lazy_components").add("AwesomeDashboard", AwesomeDashboard); diff --git a/awesome_dashboard/static/src/dashboard/dashboard.scss b/awesome_dashboard/static/src/dashboard/dashboard.scss new file mode 100644 index 00000000000..19074e8e411 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.scss @@ -0,0 +1,3 @@ +.o_dashboard { + background-color: aquamarine; +} \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard.xml b/awesome_dashboard/static/src/dashboard/dashboard.xml new file mode 100644 index 00000000000..56989c3471d --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard.xml @@ -0,0 +1,46 @@ + + + + +
+ + + +
+ + + + + + + + + +
+ + + + Which cards do you whish to see ? + + + + + + + + + + + +
diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js new file mode 100644 index 00000000000..75a3f50dd01 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.js @@ -0,0 +1,15 @@ +import { Component } from "@odoo/owl"; + +export class DashboardItem extends Component { + static template = "awesome_dashboard.DashboardItem"; + static props = { + size: { + type: Number, + default: 1, + optional: true, + }, + slots: { + type: Object, + } + } +} \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml new file mode 100644 index 00000000000..ddcc3fefdfe --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_item.xml @@ -0,0 +1,10 @@ + + + + +
+ +
+
+ +
\ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_items.js b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_items.js new file mode 100644 index 00000000000..4ba1c2d1e5c --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/dashboard_items.js @@ -0,0 +1,67 @@ +import { registry } from "@web/core/registry"; + +import { NumberCard } from "./number_card"; +import { PieChartCard } from "./pie_chart_card"; + +const items = [ + { + id: "average_quantity", + description: "Average amount of t-shirt", + Component: NumberCard, + props: (data) => ({ + title: "Average amount of t-shirt by order this month", + value: data.average_quantity, + }) + }, + { + id: "average_time", + description: "Average time for an order", + Component: NumberCard, + props: (data) => ({ + title: "Average time for an order to go from 'new' to 'sent' or 'cancelled'", + value: data.average_time, + }) + }, + { + id: "number_new_orders", + description: "New orders this month", + Component: NumberCard, + props: (data) => ({ + title: "Number of new orders this month", + value: data.nb_new_orders, + }) + }, + { + id: "cancelled_orders", + description: "Cancelled orders this month", + Component: NumberCard, + props: (data) => ({ + title: "Number of cancelled orders this month", + value: data.nb_cancelled_orders, + }) + }, + { + id: "amount_new_orders", + description: "amount orders this month", + Component: NumberCard, + props: (data) => ({ + title: "Total amount of new orders this month", + value: data.total_amount, + }) + }, + { + id: "pie_chart", + description: "Shirt orders by size", + Component: PieChartCard, + size: 2, + props: (data) => ({ + title: "Shirt orders by size", + values: data.orders_by_size, + }) + } +]; + +const dashboard_registry = registry.category("awesome_dashboard"); +items.forEach(element => { + dashboard_registry.add(element.description, element); +}); \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/number_card.js b/awesome_dashboard/static/src/dashboard/dashboard_item/number_card.js new file mode 100644 index 00000000000..3b1e614ad34 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/number_card.js @@ -0,0 +1,17 @@ +import { Component } from "@odoo/owl"; + +export class NumberCard extends Component { + static template = "awesome_dashboard.NumberCard"; + static props = { + size: { + type: Number, + default: 1, + optional: true, + }, + title: {type: String}, + value: {type: String}, + slots: { + type: Object, + } + } +} \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/number_card.xml b/awesome_dashboard/static/src/dashboard/dashboard_item/number_card.xml new file mode 100644 index 00000000000..f5ac696dcef --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/number_card.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/pie_chart_card.js b/awesome_dashboard/static/src/dashboard/dashboard_item/pie_chart_card.js new file mode 100644 index 00000000000..c8d866c0c45 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/pie_chart_card.js @@ -0,0 +1,19 @@ +import { Component } from "@odoo/owl"; +import { PieChart } from "../pie_chart"; + +export class PieChartCard extends Component { + static components = { PieChart }; + static template = "awesome_dashboard.PieChartCard"; + static props = { + size: { + type: Number, + default: 1, + optional: true, + }, + title: {type: String}, + values: {type: Object}, + slots: { + type: Object, + } + } +} \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/dashboard_item/pie_chart_card.xml b/awesome_dashboard/static/src/dashboard/dashboard_item/pie_chart_card.xml new file mode 100644 index 00000000000..73ddb049805 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/dashboard_item/pie_chart_card.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/pie_chart.js b/awesome_dashboard/static/src/dashboard/pie_chart.js new file mode 100644 index 00000000000..65ec6ad7472 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart.js @@ -0,0 +1,66 @@ +import { Component, onWillStart, useEffect, onWillUnmount, useRef } from "@odoo/owl"; +import { loadJS } from "@web/core/assets"; + +export class PieChart extends Component { + static template = "awesome_dashboard.PieChart"; + static props = { + // ERROR; Redirection through props/useState fails meta-function application! + // eg this.props.stats.keys() won't return what you think! + stats: { + type: Object, + shape: { + m: Number, + s: Number, + xl: Number, + } + }, + slots: { type: Object, optional: true }, + }; + + setup() { + this.chart = null; + this.canvasRef = useRef("canvas"); + + onWillStart(async () => { + // Injects the script directly into the frontend + await loadJS("/web/static/lib/Chart/Chart.js"); + }); + useEffect(() => this.renderChart()); + onWillUnmount(this.onWillUnmount); + } + + onWillUnmount() { + if (this.chart) { + this.chart.destroy(); + } + } + + getChartConfig() { + const stats = this.props.stats; + console.log(stats); + return { + type: 'doughnut', + data: { + datasets: [{ + data: [stats.m, stats.s, stats.xl], + }], + labels: ["m", "s", "xl"], + }, + options: {}, + }; + } + + // NOTE; Ripped from https://github.com/odoo/odoo/blob/1f4e583ba20a01f4c44b0a4ada42c4d3bb074273/addons/web/static/src/views/graph/graph_renderer.js#L618 + renderChart() { + if (this.chart) { + this.chart.destroy(); + } + const config = this.getChartConfig(); + this.chart = new Chart(this.canvasRef.el, config); + // To perform its animations, ChartJS will perform each animation + // step in the next animation frame. The initial rendering itself + // is delayed for consistency. We can avoid this by manually + // advancing the animation service. + // (?) Chart.animationService.advance(); + } +} \ No newline at end of file diff --git a/awesome_dashboard/static/src/dashboard/pie_chart.xml b/awesome_dashboard/static/src/dashboard/pie_chart.xml new file mode 100644 index 00000000000..c96665f48b0 --- /dev/null +++ b/awesome_dashboard/static/src/dashboard/pie_chart.xml @@ -0,0 +1,12 @@ + + + + +
+
+ +
+
+
+ +
\ No newline at end of file diff --git a/awesome_dashboard/static/src/lazy_dashboard_loader.js b/awesome_dashboard/static/src/lazy_dashboard_loader.js new file mode 100644 index 00000000000..a5d46931a3d --- /dev/null +++ b/awesome_dashboard/static/src/lazy_dashboard_loader.js @@ -0,0 +1,19 @@ +import { Component, xml } from "@odoo/owl"; +import { registry } from "@web/core/registry"; +import { LazyComponent } from "@web/core/assets"; + +class LazyDashboardLoader extends Component { + static components = {LazyComponent}; + static template = xml` + + `; +} + +class DashboardAction extends Component { + static components = {LazyDashboardLoader}; + static template = xml` + + `; +} + +registry.category("actions").add("awesome_dashboard.dashboard", DashboardAction); \ No newline at end of file diff --git a/awesome_dashboard/static/src/statistics.js b/awesome_dashboard/static/src/statistics.js new file mode 100644 index 00000000000..f66e37b2d42 --- /dev/null +++ b/awesome_dashboard/static/src/statistics.js @@ -0,0 +1,31 @@ +import { registry } from "@web/core/registry"; +import { rpc } from "@web/core/network/rpc"; +import { reactive } from "@odoo/owl"; + +const statisticsService = { + start() { + let reactivePayload = reactive({}); + let state = { timer: undefined}; + const service = { + onUpdate: () => reactivePayload, + loadStatistics: async () => { + const result = await rpc("/awesome_dashboard/statistics"); + console.log(result); + let data = {}; + for (const [key, value] of Object.entries(result)) { + data[key] = value; + } + reactivePayload = data; + if(state.timer === undefined) { + // state.timer = setInterval(() => service.loadStatistics(), 600000); // 10 minutes + state.timer = setInterval(() => service.loadStatistics(), 10000); // 10 seconds + } + return data; + }, + } + + return service; + } +}; + +registry.category("services").add("statistics", statisticsService); \ No newline at end of file diff --git a/awesome_owl/static/src/card/card.js b/awesome_owl/static/src/card/card.js new file mode 100644 index 00000000000..a7229af7f2d --- /dev/null +++ b/awesome_owl/static/src/card/card.js @@ -0,0 +1,18 @@ +import { Component, useState } from "@odoo/owl"; + + +export class Card extends Component { + static template = "awesome_owl.card"; + static props = { + title: {type: String}, + slots: {type: Object, optional: true}, + }; + + setup() { + this.opened = useState({r: true}); + } + + toggleOpened() { + this.opened.v = !this.opened.v; + } +} \ No newline at end of file diff --git a/awesome_owl/static/src/card/card.xml b/awesome_owl/static/src/card/card.xml new file mode 100644 index 00000000000..7ec53d243f2 --- /dev/null +++ b/awesome_owl/static/src/card/card.xml @@ -0,0 +1,16 @@ + + + + +
+
+
+ +
+ +
+
+
+
+ +
\ No newline at end of file diff --git a/awesome_owl/static/src/counter/counter.js b/awesome_owl/static/src/counter/counter.js new file mode 100644 index 00000000000..b1992a3c5c4 --- /dev/null +++ b/awesome_owl/static/src/counter/counter.js @@ -0,0 +1,23 @@ +import { Component, useState } from "@odoo/owl"; + + +export class Counter extends Component { + static template = "awesome_owl.counter"; + static props = { + onChange: { + type: Function, + optional: true, + }, + }; + + setup() { + this.state = useState({ value: 0 }); + } + + increment() { + this.state.value++; + if(this.props.onChange !== undefined) { + this.props.onChange(); + } + } +} \ No newline at end of file diff --git a/awesome_owl/static/src/counter/counter.xml b/awesome_owl/static/src/counter/counter.xml new file mode 100644 index 00000000000..5e6370933f4 --- /dev/null +++ b/awesome_owl/static/src/counter/counter.xml @@ -0,0 +1,9 @@ + + + + +

Counter:

+ +
+ +
\ No newline at end of file diff --git a/awesome_owl/static/src/playground.js b/awesome_owl/static/src/playground.js index 4ac769b0aa5..5465be086f7 100644 --- a/awesome_owl/static/src/playground.js +++ b/awesome_owl/static/src/playground.js @@ -1,5 +1,18 @@ -import { Component } from "@odoo/owl"; +import { Component, useState, markup } from "@odoo/owl"; + +import { Counter } from "./counter/counter"; +import { Card } from "./card/card"; +import { TodoList } from "./todo/todolist"; export class Playground extends Component { static template = "awesome_owl.playground"; -} + static components = { Counter, Card, TodoList }; + + setup() { + this.state = useState({ sum: 2 }); + } + + incrementSum() { + this.state.sum++; + } +} \ No newline at end of file diff --git a/awesome_owl/static/src/playground.xml b/awesome_owl/static/src/playground.xml index 4fb905d59f9..f84411b1b72 100644 --- a/awesome_owl/static/src/playground.xml +++ b/awesome_owl/static/src/playground.xml @@ -2,9 +2,24 @@ -
- hello world + Hello World +
+
+ +
+ + + +
+
The sum is:
+ + + + + +

Goedemorgen!

+
diff --git a/awesome_owl/static/src/todo/todoitem.js b/awesome_owl/static/src/todo/todoitem.js new file mode 100644 index 00000000000..06f4d36c078 --- /dev/null +++ b/awesome_owl/static/src/todo/todoitem.js @@ -0,0 +1,30 @@ +import { Component, useState } from "@odoo/owl"; + +export class TodoItem extends Component { + static template = "awesome_owl.todoitem"; + static props = { + todo: { + type: Object, + shape: { + id: Number, + description: String, + isCompleted: Boolean, + }, + }, + toggleState: { + type: Function, + }, + removeTodo: { + type: Function, + }, + }; + + change(event) { + let isCompleted = event.target.checked; + this.props.toggleState(this.props.todo.id, isCompleted); + } + + remove() { + this.props.removeTodo(this.props.todo.id); + } +} \ No newline at end of file diff --git a/awesome_owl/static/src/todo/todoitem.xml b/awesome_owl/static/src/todo/todoitem.xml new file mode 100644 index 00000000000..c88a1385a4c --- /dev/null +++ b/awesome_owl/static/src/todo/todoitem.xml @@ -0,0 +1,13 @@ + + + + +
+ + +
+
+
\ No newline at end of file diff --git a/awesome_owl/static/src/todo/todolist.js b/awesome_owl/static/src/todo/todolist.js new file mode 100644 index 00000000000..86f90070d42 --- /dev/null +++ b/awesome_owl/static/src/todo/todolist.js @@ -0,0 +1,52 @@ +import { Component, useState } from "@odoo/owl"; +import { TodoItem } from "./todoitem"; +import { useAutoFocus } from "../utils"; + +export class TodoList extends Component { + static template = "awesome_owl.todolist"; + static components = { TodoItem }; + + setup() { + // this.todos = useState([ + // { id: 3, description: "buy milk", isCompleted: false }, + // { id: 4, description: "FIX MACHINE PLX", isCompleted: false }, + // { id: 5, description: "WAKE UP", isCompleted: true }, + // ]); + this.todos = useState([]); + useAutoFocus("list_input"); + } + + addTodo(event) { + // console.warn("addTodo"); + // console.log(event); + if (event.keyCode === 13) { + let description = event.target.value; + if (description.length == 0) { + return; + } + let next_id = 1 + Math.max(...this.todos.map(item => item.id), 0); + this.todos.push({ id: next_id, description: description, isCompleted: false }); + // Reset input + event.target.value = ''; + } + } + + toggleState(id, newState) { + let todo = this.todos.find((el) => el.id == id); + if(todo === undefined) { + return; + } + + todo.isCompleted = newState; + } + + removeTodo(id) { + const index = this.todos.findIndex((el) => el.id ===id); + if(index < 0) { + return; + } + + // Inplace manipulation + this.todos.splice(index, 1); + } +} \ No newline at end of file diff --git a/awesome_owl/static/src/todo/todolist.xml b/awesome_owl/static/src/todo/todolist.xml new file mode 100644 index 00000000000..e12735cbb03 --- /dev/null +++ b/awesome_owl/static/src/todo/todolist.xml @@ -0,0 +1,18 @@ + + + + +
+ +
+
+ + + +
+
+ +
\ No newline at end of file diff --git a/awesome_owl/static/src/utils.js b/awesome_owl/static/src/utils.js new file mode 100644 index 00000000000..06a33c4cbb8 --- /dev/null +++ b/awesome_owl/static/src/utils.js @@ -0,0 +1,13 @@ +import { useRef } from "@odoo/owl"; +import { onMounted } from "@odoo/owl"; + +export function useAutoFocus(name) { + // NOTE; Object remains alive due to (weak)ref stored inside the closure + // of 'onMounted'. + let theElement = useRef(name); + + onMounted(() => { + theElement.el.focus(); + console.log("Executed autofocus"); + }); +} \ No newline at end of file