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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
odoo.define('board.BoardView', function (require) {
"use strict";
var Context = require('web.Context');
var config = require('web.config');
var core = require('web.core');
var dataManager = require('web.data_manager');
var Dialog = require('web.Dialog');
var Domain = require('web.Domain');
var FormController = require('web.FormController');
var FormRenderer = require('web.FormRenderer');
var FormView = require('web.FormView');
var pyUtils = require('web.py_utils');
var session = require('web.session');
var viewRegistry = require('web.view_registry');
var _t = core._t;
var _lt = core._lt;
var QWeb = core.qweb;
var BoardController = FormController.extend({
custom_events: _.extend({}, FormController.prototype.custom_events, {
change_layout: '_onChangeLayout',
enable_dashboard: '_onEnableDashboard',
save_dashboard: '_saveDashboard',
switch_view: '_onSwitchView',
}),
/**
* @override
*/
init: function (parent, model, renderer, params) {
this._super.apply(this, arguments);
this.customViewID = params.customViewID;
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @override
*/
getTitle: function () {
return _t("My Dashboard");
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Actually save a dashboard
*
* @returns {Promise}
*/
_saveDashboard: function () {
var board = this.renderer.getBoard();
var arch = QWeb.render('DashBoard.xml', _.extend({}, board));
return this._rpc({
route: '/web/view/edit_custom',
params: {
custom_id: this.customViewID,
arch: arch,
}
}).then(dataManager.invalidate.bind(dataManager));
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param {OdooEvent} event
*/
_onChangeLayout: function (event) {
var self = this;
var dialog = new Dialog(this, {
title: _t("Edit Layout"),
$content: QWeb.render('DashBoard.layouts', _.clone(event.data))
});
dialog.opened().then(function () {
dialog.$('li').click(function () {
var layout = $(this).attr('data-layout');
self.renderer.changeLayout(layout);
self._saveDashboard();
dialog.close();
});
});
dialog.open();
},
/**
* We need to intercept switch_view event coming from sub views, because we
* don't actually want to switch view in dashboard, we want to do a
* do_action (which will open the record in a different breadcrumb).
*
* @private
* @param {OdooEvent} event
*/
_onSwitchView: function (event) {
event.stopPropagation();
this.do_action({
type: 'ir.actions.act_window',
res_model: event.data.model,
views: [[event.data.formViewID || false, 'form']],
res_id: event.data.res_id,
});
},
});
var BoardRenderer = FormRenderer.extend({
custom_events: _.extend({}, FormRenderer.prototype.custom_events, {
update_filters: '_onUpdateFilters',
switch_view: '_onSwitchView',
}),
events: _.extend({}, FormRenderer.prototype.events, {
'click .oe_dashboard_column .oe_fold': '_onFoldClick',
'click .oe_dashboard_link_change_layout': '_onChangeLayout',
'click .oe_dashboard_column .oe_close': '_onCloseAction',
}),
/**
* @override
*/
init: function (parent, state, params) {
this._super.apply(this, arguments);
this.noContentHelp = params.noContentHelp;
this.actionsDescr = {};
this._boardSubcontrollers = []; // for board: controllers of subviews
this._boardFormViewIDs = {}; // for board: mapping subview controller to form view id
},
/**
* Call `on_attach_callback` for each subview
*
* @override
*/
on_attach_callback: function () {
_.each(this._boardSubcontrollers, function (controller) {
if ('on_attach_callback' in controller) {
controller.on_attach_callback();
}
});
},
/**
* Call `on_detach_callback` for each subview
*
* @override
*/
on_detach_callback: function () {
_.each(this._boardSubcontrollers, function (controller) {
if ('on_detach_callback' in controller) {
controller.on_detach_callback();
}
});
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @param {string} layout
*/
changeLayout: function (layout) {
var $dashboard = this.$('.oe_dashboard');
var current_layout = $dashboard.attr('data-layout');
if (current_layout !== layout) {
var clayout = current_layout.split('-').length,
nlayout = layout.split('-').length,
column_diff = clayout - nlayout;
if (column_diff > 0) {
var $last_column = $();
$dashboard.find('.oe_dashboard_column').each(function (k, v) {
if (k >= nlayout) {
$(v).find('.oe_action').appendTo($last_column);
} else {
$last_column = $(v);
}
});
}
$dashboard.toggleClass('oe_dashboard_layout_' + current_layout + ' oe_dashboard_layout_' + layout);
$dashboard.attr('data-layout', layout);
}
},
/**
* Returns a representation of the current dashboard
*
* @returns {Object}
*/
getBoard: function () {
var self = this;
var board = {
form_title : this.arch.attrs.string,
style : this.$('.oe_dashboard').attr('data-layout'),
columns : [],
};
this.$('.oe_dashboard_column').each(function () {
var actions = [];
$(this).find('.oe_action').each(function () {
var actionID = $(this).attr('data-id');
var newAttrs = _.clone(self.actionsDescr[actionID]);
/* prepare attributes as they should be saved */
if (newAttrs.modifiers) {
newAttrs.modifiers = JSON.stringify(newAttrs.modifiers);
}
actions.push(newAttrs);
});
board.columns.push(actions);
});
return board;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
* @param {Object} params
* @param {jQueryElement} params.$node
* @param {integer} params.actionID
* @param {Object} params.context
* @param {any[]} params.domain
* @param {string} params.viewType
* @returns {Promise}
*/
_createController: function (params) {
var self = this;
return this._rpc({
route: '/web/action/load',
params: {action_id: params.actionID}
})
.then(function (action) {
if (!action) {
// the action does not exist anymore
return Promise.resolve();
}
var evalContext = new Context(params.context).eval();
if (evalContext.group_by && evalContext.group_by.length === 0) {
delete evalContext.group_by;
}
// tz and lang are saved in the custom view
// override the language to take the current one
var rawContext = new Context(action.context, evalContext, {lang: session.user_context.lang});
var context = pyUtils.eval('context', rawContext, evalContext);
var domain = params.domain || pyUtils.eval('domain', action.domain || '[]', action.context);
action.context = context;
action.domain = domain;
// When creating a view, `action.views` is expected to be an array of dicts, while
// '/web/action/load' returns an array of arrays.
action._views = action.views;
action.views = $.map(action.views, function (view) { return {viewID: view[0], type: view[1]}});
var viewType = params.viewType || action._views[0][1];
var view = _.find(action._views, function (descr) {
return descr[1] === viewType;
}) || [false, viewType];
return self.loadViews(action.res_model, context, [view])
.then(function (viewsInfo) {
var viewInfo = viewsInfo[viewType];
var View = viewRegistry.get(viewType);
const searchQuery = {
context: context,
domain: domain,
groupBy: typeof context.group_by === 'string' && context.group_by ?
[context.group_by] :
context.group_by || [],
orderedBy: context.orderedBy || [],
};
if (View.prototype.searchMenuTypes.includes('comparison')) {
searchQuery.timeRanges = context.comparison || {};
}
var view = new View(viewInfo, {
action: action,
hasSelectors: false,
modelName: action.res_model,
searchQuery,
withControlPanel: false,
withSearchPanel: false,
});
return view.getController(self).then(function (controller) {
self._boardFormViewIDs[controller.handle] = _.first(
_.find(action._views, function (descr) {
return descr[1] === 'form';
})
);
self._boardSubcontrollers.push(controller);
return controller.appendTo(params.$node);
});
});
});
},
/**
* @private
* @param {Object} node
* @returns {jQueryElement}
*/
_renderTagBoard: function (node) {
var self = this;
// we add the o_dashboard class to the renderer's $el. This means that
// this function has a side effect. This is ok because we assume that
// once we have a '<board>' tag, we are in a special dashboard mode.
this.$el.addClass('o_dashboard');
var hasAction = _.detect(node.children, function (column) {
return _.detect(column.children,function (element){
return element.tag === "action"? element: false;
});
});
if (!hasAction) {
return $(QWeb.render('DashBoard.NoContent'));
}
// We should start with three columns available
node = $.extend(true, {}, node);
// no idea why master works without this, but whatever
if (!('layout' in node.attrs)) {
node.attrs.layout = node.attrs.style;
}
for (var i = node.children.length; i < 3; i++) {
node.children.push({
tag: 'column',
attrs: {},
children: []
});
}
// register actions, alongside a generated unique ID
_.each(node.children, function (column, column_index) {
_.each(column.children, function (action, action_index) {
action.attrs.id = 'action_' + column_index + '_' + action_index;
self.actionsDescr[action.attrs.id] = action.attrs;
});
});
var $html = $('<div>').append($(QWeb.render('DashBoard', {node: node, isMobile: config.device.isMobile})));
this._boardSubcontrollers = []; // dashboard controllers are reset on re-render
// render each view
_.each(this.actionsDescr, function (action) {
self.defs.push(self._createController({
$node: $html.find('.oe_action[data-id=' + action.id + '] .oe_content'),
actionID: _.str.toNumber(action.name),
context: action.context,
domain: Domain.prototype.stringToArray(action.domain, {}),
viewType: action.view_mode,
}));
});
$html.find('.oe_dashboard_column').sortable({
connectWith: '.oe_dashboard_column',
handle: '.oe_header',
scroll: false
}).bind('sortstop', function () {
self.trigger_up('save_dashboard');
});
return $html;
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
*/
_onChangeLayout: function () {
var currentLayout = this.$('.oe_dashboard').attr('data-layout');
this.trigger_up('change_layout', {currentLayout: currentLayout});
},
/**
* @private
* @param {MouseEvent} event
*/
_onCloseAction: function (event) {
var self = this;
var $container = $(event.currentTarget).parents('.oe_action:first');
Dialog.confirm(this, (_t("Are you sure you want to remove this item?")), {
confirm_callback: function () {
$container.remove();
self.trigger_up('save_dashboard');
},
});
},
/**
* @private
* @param {MouseEvent} event
*/
_onFoldClick: function (event) {
var $e = $(event.currentTarget);
var $action = $e.closest('.oe_action');
var id = $action.data('id');
var actionAttrs = this.actionsDescr[id];
if ($e.is('.oe_minimize')) {
actionAttrs.fold = '1';
} else {
delete(actionAttrs.fold);
}
$e.toggleClass('oe_minimize oe_maximize');
$action.find('.oe_content').toggle();
this.trigger_up('save_dashboard');
},
/**
* Let FormController know which form view it should display based on the
* window action of the sub controller that is switching view
*
* @private
* @param {OdooEvent} event
*/
_onSwitchView: function (event) {
event.data.formViewID = this._boardFormViewIDs[event.target.handle];
},
/**
* Stops the propagation of 'update_filters' events triggered by the
* controllers instantiated by the dashboard to prevent them from
* interfering with the ActionManager.
*
* @private
* @param {OdooEvent} event
*/
_onUpdateFilters: function (event) {
event.stopPropagation();
},
});
var BoardView = FormView.extend({
config: _.extend({}, FormView.prototype.config, {
Controller: BoardController,
Renderer: BoardRenderer,
}),
display_name: _lt('Board'),
/**
* @override
*/
init: function (viewInfo) {
this._super.apply(this, arguments);
this.controllerParams.customViewID = viewInfo.custom_view_id;
},
});
return BoardView;
});
odoo.define('board.viewRegistry', function (require) {
"use strict";
var BoardView = require('board.BoardView');
var viewRegistry = require('web.view_registry');
viewRegistry.add('board', BoardView);
});
|