1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
odoo.define('web.ActionMenus', function (require) {
"use strict";
const Context = require('web.Context');
const DropdownMenu = require('web.DropdownMenu');
const Registry = require('web.Registry');
const { Component } = owl;
let registryActionId = 1;
/**
* Action menus (or Action/Print bar, previously called 'Sidebar')
*
* The side bar is the group of dropdown menus located on the left side of the
* control panel. Its role is to display a list of items depending on the view
* type and selected records and to execute a set of actions on active records.
* It is made out of 2 dropdown menus: Print and Action.
*
* This component also provides a registry to use custom components in the ActionMenus's
* Action menu.
* @extends Component
*/
class ActionMenus extends Component {
async willStart() {
this.actionItems = await this._setActionItems(this.props);
this.printItems = await this._setPrintItems(this.props);
}
async willUpdateProps(nextProps) {
this.actionItems = await this._setActionItems(nextProps);
this.printItems = await this._setPrintItems(nextProps);
}
mounted() {
this._addTooltips();
}
patched() {
this._addTooltips();
}
//---------------------------------------------------------------------
// Private
//---------------------------------------------------------------------
/**
* Add the tooltips to the items
* @private
*/
_addTooltips() {
$(this.el.querySelectorAll('[title]')).tooltip({
delay: { show: 500, hide: 0 }
});
}
/**
* @private
* @param {Object} props
* @returns {Promise<Object[]>}
*/
async _setActionItems(props) {
// Callback based actions
const callbackActions = (props.items.other || []).map(
action => Object.assign({ key: `action-${action.description}` }, action)
);
// Action based actions
const actionActions = props.items.action || [];
const relateActions = props.items.relate || [];
const formattedActions = [...actionActions, ...relateActions].map(
action => ({ action, description: action.name, key: action.id })
);
// ActionMenus action registry components
const registryActions = [];
const rpc = this.rpc.bind(this);
for (const { Component, getProps } of this.constructor.registry.values()) {
const itemProps = await getProps(props, this.env, rpc);
if (itemProps) {
registryActions.push({
Component,
key: `registry-action-${registryActionId++}`,
props: itemProps,
});
}
}
return [...callbackActions, ...formattedActions, ...registryActions];
}
/**
* @private
* @param {Object} props
* @returns {Promise<Object[]>}
*/
async _setPrintItems(props) {
const printActions = props.items.print || [];
const printItems = printActions.map(
action => ({ action, description: action.name, key: action.id })
);
return printItems;
}
//---------------------------------------------------------------------
// Handlers
//---------------------------------------------------------------------
/**
* Perform the action for the item clicked after getting the data
* necessary with a trigger.
* @private
* @param {OwlEvent} ev
*/
async _executeAction(action) {
let activeIds = this.props.activeIds;
if (this.props.isDomainSelected) {
activeIds = await this.rpc({
model: this.env.action.res_model,
method: 'search',
args: [this.props.domain],
kwargs: {
limit: this.env.session.active_ids_limit,
},
});
}
const activeIdsContext = {
active_id: activeIds[0],
active_ids: activeIds,
active_model: this.env.action.res_model,
};
if (this.props.domain) {
// keep active_domain in context for backward compatibility
// reasons, and to allow actions to bypass the active_ids_limit
activeIdsContext.active_domain = this.props.domain;
}
const context = new Context(this.props.context, activeIdsContext).eval();
const result = await this.rpc({
route: '/web/action/load',
params: { action_id: action.id, context },
});
result.context = new Context(result.context || {}, activeIdsContext)
.set_eval_context(context);
result.flags = result.flags || {};
result.flags.new_window = true;
this.trigger('do-action', {
action: result,
options: {
on_close: () => this.trigger('reload'),
},
});
}
/**
* Handler used to determine which way must be used to execute a selected
* action: it will be either:
* - a callback (function given by the view controller);
* - an action ID (string);
* - an URL (string).
* @private
* @param {OwlEvent} ev
*/
_onItemSelected(ev) {
ev.stopPropagation();
const { item } = ev.detail;
if (item.callback) {
item.callback([item]);
} else if (item.action) {
this._executeAction(item.action);
} else if (item.url) {
// Event has been prevented at its source: we need to redirect manually.
this.env.services.navigate(item.url);
}
}
}
ActionMenus.registry = new Registry();
ActionMenus.components = { DropdownMenu };
ActionMenus.props = {
activeIds: { type: Array, element: [Number, String] }, // virtual IDs are strings.
context: Object,
domain: { type: Array, optional: 1 },
isDomainSelected: { type: Boolean, optional: 1 },
items: {
type: Object,
shape: {
action: { type: Array, optional: 1 },
print: { type: Array, optional: 1 },
other: { type: Array, optional: 1 },
},
},
};
ActionMenus.template = 'web.ActionMenus';
return ActionMenus;
});
|