Debian Codegraph @

addons/board/static/src/add_to_board/add_to_board.js

raw symbols SCIP
1 import { _t } from "@web/core/l10n/translation";
2 import { Dropdown } from "@web/core/dropdown/dropdown";
3 import { rpc } from "@web/core/network/rpc";
4 import { registry } from "@web/core/registry";
5 import { useAutofocus, useService } from "@web/core/utils/hooks";
6 import { Component, useState } from "@odoo/owl";
7
8 const cogMenuRegistry = registry.category("cogMenu");
9
10 /**
11 * 'Add to board' menu
12 *
13 * Component consisiting of a toggle button, a text input and an 'Add' button.
14 * The first button is simply used to toggle the component and will determine
15 * whether the other elements should be rendered.
16 * The input will be given the name (or title) of the view that will be added.
17 * Finally, the last button will send the name as well as some of the action
18 * properties to the server to add the current view (and its context) to the
19 * user's dashboard.
20 * This component is only available in actions of type 'ir.actions.act_window'.
21 * @extends Component
22 */
23 export class AddToBoard extends Component {
24 static template = "board.AddToBoard";
25 static components = { Dropdown };
26 static props = {};
27
28 setup() {
29 this.notification = useService("notification");
30 this.state = useState({ name: this.env.config.getDisplayName() });
31
32 useAutofocus();
33 }
34
35 //---------------------------------------------------------------------
36 // Protected
37 //---------------------------------------------------------------------
38
39 async addToBoard() {
40 const { domain, globalContext } = this.env.searchModel;
41 const { context, groupBys, orderBy } = this.env.searchModel.getPreFavoriteValues();
42 const contextToSave = {
43 ...Object.fromEntries(
44 Object.entries(globalContext).filter(
45 (entry) => !entry[0].startsWith("search_default_")
46 )
47 ),
48 ...context,
49 orderedBy: orderBy,
50 group_by: groupBys,
51 dashboard_merge_domains_contexts: false,
52 };
53
54 const result = await rpc("/board/add_to_dashboard", {
55 action_id: this.env.config.actionId || false,
56 context_to_save: contextToSave,
57 domain,
58 name: this.state.name,
59 view_mode: this.env.config.viewType,
60 });
61
62 if (result) {
63 this.notification.add(
64 _t("Please refresh your browser for the changes to take effect."),
65 {
66 title: _t("ā€œ%sā€ added to dashboard", this.state.name),
67 type: "warning",
68 }
69 );
70 this.state.name = this.env.config.getDisplayName();
71 } else {
72 this.notification.add(_t("Could not add filter to dashboard"), {
73 type: "danger",
74 });
75 }
76 }
77
78 //---------------------------------------------------------------------
79 // Handlers
80 //---------------------------------------------------------------------
81
82 /**
83 * @param {KeyboardEvent} ev
84 */
85 onInputKeydown(ev) {
86 if (ev.key === "Enter") {
87 ev.preventDefault();
88 this.addToBoard();
89 }
90 }
91 }
92
93 export const addToBoardItem = {
94 Component: AddToBoard,
95 groupNumber: 20,
96 isDisplayed: ({ config }) => {
97 const { actionType, actionId, viewType } = config;
98 return actionType === "ir.actions.act_window" && actionId && viewType !== "form";
99 },
100 };
101
102 cogMenuRegistry.add("add-to-board", addToBoardItem, { sequence: 10 });