/
kanban_record.js
558 lines (533 loc) · 18.9 KB
/
kanban_record.js
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
odoo.define('web.KanbanRecord', function (require) {
"use strict";
/**
* This file defines the KanbanRecord widget, which corresponds to a card in
* a Kanban view.
*/
var config = require('web.config');
var core = require('web.core');
var Domain = require('web.Domain');
var field_utils = require('web.field_utils');
var utils = require('web.utils');
var Widget = require('web.Widget');
var widgetRegistry = require('web.widget_registry');
var _t = core._t;
var QWeb = core.qweb;
var NB_KANBAN_RECORD_COLORS = 12;
var KanbanRecord = Widget.extend({
events: {
'click .oe_kanban_action': '_onKanbanActionClicked',
'click .o_kanban_manage_toggle_button': '_onManageTogglerClicked',
},
/**
* @override
*/
init: function (parent, state, options) {
this._super(parent);
this.fields = state.fields;
this.fieldsInfo = state.fieldsInfo.kanban;
this.modelName = state.model;
this.options = options;
this.editable = options.editable;
this.deletable = options.deletable;
this.read_only_mode = options.read_only_mode;
this.qweb = options.qweb;
this.subWidgets = {};
this._setState(state);
// avoid quick multiple clicks
this._onKanbanActionClicked = _.debounce(this._onKanbanActionClicked, 300, true);
},
start: function () {
return this._super.apply(this, arguments).then(this._render.bind(this));
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Re-renders the record with a new state
*
* @param {Object} state
*/
update: function (state) {
// detach the widgets because the record will empty its $el, which will
// remove all event handlers on its descendants, and we want to keep
// those handlers alive as we will re-use these widgets
_.invoke(_.pluck(this.subWidgets, '$el'), 'detach');
this._setState(state);
this._render();
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
*/
_attachTooltip: function () {
var self = this;
this.$('[tooltip]').each(function () {
var $el = $(this);
var tooltip = $el.attr('tooltip');
if (tooltip) {
$el.tooltip({
'html': true,
'title': self.qweb.render(tooltip, self.qweb_context)
});
}
});
},
/**
* @private
* @param {string} d a stringified domain
* @returns {boolean} the domain evaluted with the current values
*/
_computeDomain: function (d) {
return new Domain(d).compute(this.state.evalContext);
},
/**
* Generates the color classname from a given variable
*
* @private
* @param {number | string} variable
* @return {string} the classname
*/
_getColorClassname: function (variable) {
var color = this._getColorID(variable);
return 'oe_kanban_color_' + color;
},
/**
* Computes a color id between 0 and 10 from a given value
*
* @private
* @param {number | string} variable
* @returns {integer} the color id
*/
_getColorID: function (variable) {
if (typeof variable === 'number') {
return Math.round(variable) % NB_KANBAN_RECORD_COLORS;
}
if (typeof variable === 'string') {
var index = 0;
for (var i = 0 ; i < variable.length ; i++) {
index += variable.charCodeAt(i);
}
return index % NB_KANBAN_RECORD_COLORS;
}
return 0;
},
/**
* @private
* @param {string} model the name of the model
* @param {string} field the name of the field
* @param {integer} id the id of the resource
* @param {integer} cache the cache duration, in seconds
* @param {Object} options
* @returns {string} the url of the image
*/
_getImageURL: function (model, field, id, cache, options) {
options = options || {};
var url;
if (this.record[field] && this.record[field].value && !utils.is_bin_size(this.record[field].value)) {
url = 'data:image/png;base64,' + this.record[field].value;
} else if (this.record[field] && ! this.record[field].value) {
url = "/web/static/src/img/placeholder.png";
} else {
if (_.isArray(id)) { id = id[0]; }
if (!id) { id = undefined; }
if (options.preview_image)
field = options.preview_image;
var unique = this.record.__last_update && this.record.__last_update.value.replace(/[^0-9]/g, '');
var session = this.getSession();
url = session.url('/web/image', {model: model, field: field, id: id, unique: unique});
if (cache !== undefined) {
// Set the cache duration in seconds.
url += '&cache=' + parseInt(cache, 10);
}
}
return url;
},
/**
* Triggers up an event to open the record
*
* @private
*/
_openRecord: function () {
var editMode = this.$el.hasClass('oe_kanban_global_click_edit');
this.trigger_up('open_record', {
id: this.db_id,
mode: editMode ? 'edit' : 'readonly',
});
},
/**
* Processes each 'field' tag and replaces it by the specified widget, if
* any, or directly by the formatted value
*
* @private
*/
_processFields: function () {
var self = this;
this.$("field").each(function () {
var $field = $(this);
var field_name = $field.attr("name");
var field_widget = $field.attr("widget");
// a widget is specified for that field or a field is a many2many ;
// in this latest case, we want to display the widget many2manytags
// even if it is not specified in the view.
if (field_widget || self.fields[field_name].type === 'many2many') {
var widget = self.subWidgets[field_name];
if (!widget) {
// the widget doesn't exist yet, so instanciate it
var Widget = self.fieldsInfo[field_name].Widget;
if (Widget) {
widget = self._processWidget($field, field_name, Widget);
self.subWidgets[field_name] = widget;
} else if (config.debug) {
// the widget is not implemented
$field.replaceWith($('<span>', {
text: _.str.sprintf(_t('[No widget %s]'), field_widget),
}));
}
} else {
// a widget already exists for that field, so reset it with the new state
widget.reset(self.state);
$field.replaceWith(widget.$el);
}
} else {
self._processField($field, field_name);
}
});
},
/**
* Replace a field by its formatted value.
*
* @private
* @param {JQuery} $field
* @param {String} field_name
* @returns {Jquery} the modified node
*/
_processField: function ($field, field_name) {
// no widget specified for that field, so simply use a formatter
// note: we could have used the widget corresponding to the field's type, but
// it is much more efficient to use a formatter
var field = this.fields[field_name];
var value = this.recordData[field_name];
var options = { data: this.recordData };
var formatted_value = field_utils.format[field.type](value, field, options);
var $result = $('<span>', {
text: formatted_value,
});
$field.replaceWith($result);
this._setFieldDisplay($result, field_name);
return $result;
},
/**
* Replace a field by its corresponding widget.
*
* @private
* @param {JQuery} $field
* @param {String} field_name
* @param {Class} Widget
* @returns {Widget} the widget instance
*/
_processWidget: function ($field, field_name, Widget) {
// some field's attrs might be record dependent (they start with
// 't-att-') and should thus be evaluated, which is done by qweb
// we here replace those attrs in the dict of attrs of the state
// by their evaluted value, to make it transparent from the
// field's widgets point of view
// that dict being shared between records, we don't modify it
// in place
var attrs = Object.create(null);
_.each(this.fieldsInfo[field_name], function (value, key) {
if (_.str.startsWith(key, 't-att-')) {
key = key.slice(6);
value = $field.attr(key);
}
attrs[key] = value;
});
var options = _.extend({}, this.options, {attrs: attrs});
var widget = new Widget(this, field_name, this.state, options);
widget.replace($field);
this._setFieldDisplay(widget.$el, field_name);
return widget;
},
_processWidgets: function () {
var self = this;
this.$("widget").each(function () {
var $field = $(this);
var Widget = widgetRegistry.get($field.attr('name'));
var widget = new Widget(self, self.state);
var def = widget._widgetRenderAndInsert(function () {});
if (def.state() === 'pending') {
self.defs.push(def);
}
widget.$el.addClass('o_widget');
$field.replaceWith(widget.$el);
});
},
/**
* Renders the record
*/
_render: function () {
this._replaceElement(this.qweb.render('kanban-box', this.qweb_context));
this.$el.addClass('o_kanban_record').attr("tabindex",0);
this.$el.data('record', this);
if (this.$el.hasClass('oe_kanban_global_click') ||
this.$el.hasClass('oe_kanban_global_click_edit')) {
this.$el.on('click', this._onGlobalClick.bind(this));
this.$el.on('keydown', this._onKeyDownCard.bind(this));
} else {
this.$el.on('keydown', this._onKeyDownOpenFirstLink.bind(this));
}
this._processFields();
this._processWidgets();
this._setupColor();
this._setupColorPicker();
this._attachTooltip();
// We use boostrap tooltips for better and faster display
this.$('span.o_tag').tooltip({delay: {'show': 50}});
},
/**
* Sets particular classnames on a field $el according to the
* field's attrs (display or bold attributes)
*
* @private
* @param {JQuery} $el
* @param {string} fieldName
*/
_setFieldDisplay: function ($el, fieldName) {
// attribute display
if (this.fieldsInfo[fieldName].display === 'right') {
$el.addClass('pull-right');
} else if (this.fieldsInfo[fieldName].display === 'full') {
$el.addClass('o_text_block');
}
// attribute bold
if (this.fieldsInfo[fieldName].bold) {
$el.addClass('o_text_bold');
}
},
/**
* Sets internal values of the kanban record according to the given state
*
* @private
* @param {Object} recordState
*/
_setState: function (recordState) {
this.state = recordState;
this.id = recordState.res_id;
this.db_id = recordState.id;
this.recordData = recordState.data;
this.record = this._transformRecord(recordState.data);
this.qweb_context = {
kanban_image: this._getImageURL.bind(this),
kanban_color: this._getColorClassname.bind(this),
kanban_getcolor: this._getColorID.bind(this),
kanban_compute_domain: this._computeDomain.bind(this),
read_only_mode: this.read_only_mode,
record: this.record,
user_context: this.getSession().user_context,
widget: this,
};
},
/**
* If an attribute `color` is set on the kanban record, adds the
* corresponding color classname.
*
* @private
*/
_setupColor: function () {
var color_field = this.$el.attr('color');
if (color_field && color_field in this.fields) {
var colorClass = this._getColorClassname(this.recordData[color_field]);
this.$el.addClass(colorClass);
}
},
/**
* Renders the color picker in the kanban record, and binds the event handler
*
* @private
*/
_setupColorPicker: function () {
var $colorpicker = this.$('ul.oe_kanban_colorpicker');
if (!$colorpicker.length) {
return;
}
$colorpicker.html(QWeb.render('KanbanColorPicker'));
$colorpicker.on('click', 'a', this._onColorChanged.bind(this));
},
/**
* Builds an object containing the formatted record data used in the
* template
*
* @private
* @param {Object} recordData
* @returns {Object} transformed record data
*/
_transformRecord: function (recordData) {
var self = this;
var new_record = {};
_.each(this.state.getFieldNames(), function (name) {
var value = recordData[name];
var r = _.clone(self.fields[name] || {});
if ((r.type === 'date' || r.type === 'datetime') && value) {
r.raw_value = value.toDate();
} else if (r.type === 'one2many' || r.type === 'many2many') {
r.raw_value = value.count ? value.res_ids : [];
} else if (r.type === 'many2one' ) {
r.raw_value = value && value.res_id || false;
} else {
r.raw_value = value;
}
if (r.type) {
var formatter = field_utils.format[r.type];
r.value = formatter(value, self.fields[name], recordData, self.state);
} else {
r.value = value;
}
new_record[name] = r;
});
return new_record;
},
/**
* Notifies the controller that the record has changed
*
* @private
* @param {Object} data the new values
*/
_updateRecord: function (data) {
this.trigger_up('kanban_record_update', data);
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param {MouseEvent} event
*/
_onColorChanged: function (event) {
event.preventDefault();
var data = {};
var color_field = $(event.delegateTarget).data('field') || 'color';
data[color_field] = $(event.currentTarget).data('color');
this.trigger_up('kanban_record_update', data);
},
/**
* @private
* @param {MouseEvent} event
*/
_onGlobalClick: function (event) {
if ($(event.target).parents('.o_dropdown_kanban').length) {
return;
}
var trigger = true;
var elem = event.target;
var ischild = true;
var children = [];
while (elem) {
var events = $._data(elem, 'events');
if (elem === event.currentTarget) {
ischild = false;
}
var test_event = events && events.click && (events.click.length > 1 || events.click[0].namespace !== "tooltip");
var testLinkWithHref = elem.nodeName.toLowerCase() === 'a' && elem.href;
if (ischild) {
children.push(elem);
if (test_event || testLinkWithHref) {
// Do not trigger global click if one child has a click
// event registered (or it is a link with href)
trigger = false;
}
}
if (trigger && test_event) {
_.each(events.click, function (click_event) {
if (click_event.selector) {
// For each parent of original target, check if a
// delegated click is bound to any previously found children
_.each(children, function (child) {
if ($(child).is(click_event.selector)) {
trigger = false;
}
});
}
});
}
elem = elem.parentElement;
}
if (trigger) {
this._openRecord();
}
},
/**
* @private
* @param {MouseEvent} event
*/
_onKanbanActionClicked: function (event) {
event.preventDefault();
var $action = $(event.currentTarget);
var type = $action.data('type') || 'button';
switch (type) {
case 'edit':
this.trigger_up('open_record', {id: this.db_id, mode: 'edit'});
break;
case 'open':
this.trigger_up('open_record', {id: this.db_id});
break;
case 'delete':
this.trigger_up('kanban_record_delete', {id: this.db_id, record: this});
break;
case 'action':
case 'object':
this.trigger_up('button_clicked', {
attrs: $action.data(),
record: this.state,
});
break;
default:
this.do_warn("Kanban: no action for type : " + type);
}
},
/**
* This event is linked to the kanban card when there is a global_click
* class on this card
*
* @private
* @param {KeyDownEvent} event
*/
_onKeyDownCard: function (event) {
switch (event.keyCode) {
case $.ui.keyCode.ENTER:
event.preventDefault();
this._onGlobalClick(event);
break;
}
},
/**
* This event is linked ot the kanban card when there is no global_click
* class on the card
*
* @private
* @param {KeyDownEvent} event
*/
_onKeyDownOpenFirstLink: function (event) {
switch (event.keyCode) {
case $.ui.keyCode.ENTER:
event.preventDefault();
$(event.target).find('a, button').first().click();
break;
}
},
/**
* Toggles the configuration panel of the record
*
* @private
* @param {MouseEvent} event
*/
_onManageTogglerClicked: function (event) {
event.preventDefault();
this.$el.toggleClass('o_dropdown_open');
var colorClass = this._getColorClassname(this.recordData.color || 0);
this.$('.o_kanban_manage_button_section').toggleClass(colorClass);
},
});
return KanbanRecord;
});