/
kanban_column.js
386 lines (363 loc) · 12.7 KB
/
kanban_column.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
odoo.define('web.KanbanColumn', function (require) {
"use strict";
var config = require('web.config');
var core = require('web.core');
var Dialog = require('web.Dialog');
var KanbanRecord = require('web.KanbanRecord');
var RecordQuickCreate = require('web.kanban_record_quick_create');
var view_dialogs = require('web.view_dialogs');
var Widget = require('web.Widget');
var KanbanColumnProgressBar = require('web.KanbanColumnProgressBar');
var _t = core._t;
var QWeb = core.qweb;
var KanbanColumn = Widget.extend({
template: 'KanbanView.Group',
custom_events: {
cancel_quick_create: '_onCancelQuickCreate',
quick_create_add_record: '_onQuickCreateAddRecord',
tweak_column: '_onTweakColumn',
tweak_column_records: '_onTweakColumnRecords',
},
events: {
'click .o_column_edit': '_onEditColumn',
'click .o_column_delete': '_onDeleteColumn',
'click .o_kanban_quick_add': '_onAddQuickCreate',
'click .o_kanban_load_more': '_onLoadMore',
'click .o_kanban_toggle_fold': '_onToggleFold',
'click .o_column_archive_records': '_onArchiveRecords',
'click .o_column_unarchive_records': '_onUnarchiveRecords'
},
/**
* @override
*/
init: function (parent, data, options, recordOptions) {
this._super(parent);
this.db_id = data.id;
this.data_records = data.data;
this.data = data;
var value = data.value;
this.id = data.res_id;
this.folded = !data.isOpen;
this.has_active_field = 'active' in data.fields;
this.fields = data.fields;
this.records = [];
this.modelName = data.model;
this.quick_create = options.quick_create;
this.quickCreateView = options.quickCreateView;
this.groupedBy = options.groupedBy;
this.grouped_by_m2o = options.grouped_by_m2o;
this.editable = options.editable;
this.deletable = options.deletable;
this.draggable = options.draggable;
this.records_editable = options.records_editable;
this.records_deletable = options.records_deletable;
this.relation = options.relation;
this.offset = 0;
this.remaining = data.count - this.data_records.length;
if (options.hasProgressBar) {
this.barOptions = {
columnID: this.db_id,
progressBarStates: options.progressBarStates,
};
}
this.record_options = _.clone(recordOptions);
if (options.grouped_by_m2o) {
// For many2one, a false value means that the field is not set.
this.title = value ? value : _t('Undefined');
} else {
// False and 0 might be valid values for these fields.
this.title = value === undefined ? _t('Undefined') : value;
}
if (options.group_by_tooltip) {
this.tooltipInfo = _.map(options.group_by_tooltip, function (help, field) {
return (data.tooltipData && data.tooltipData[field] && "<div>" + help + "<br>" + data.tooltipData[field] + "</div>") || '';
}).join('');
} else {
this.tooltipInfo = "";
}
},
/**
* @override
*/
start: function () {
var self = this;
var defs = [this._super.apply(this, arguments)];
this.$header = this.$('.o_kanban_header');
for (var i = 0; i < this.data_records.length; i++) {
this._addRecord(this.data_records[i]);
}
this.$header.find('.o_kanban_header_title').tooltip();
if (!config.device.isMobile) {
// deactivate sortable in mobile mode. It does not work anyway,
// and it breaks horizontal scrolling in kanban views. Someday, we
// should find a way to use the touch events to make sortable work.
this.$el.sortable({
connectWith: '.o_kanban_group',
containment: this.draggable ? false : 'parent',
revert: 0,
delay: 0,
items: '> .o_kanban_record:not(.o_updating)',
helper: 'clone',
cursor: 'move',
over: function () {
self.$el.addClass('o_kanban_hover');
},
out: function () {
self.$el.removeClass('o_kanban_hover');
},
update: function (event, ui) {
var record = ui.item.data('record');
var index = self.records.indexOf(record);
record.$el.removeAttr('style'); // jqueryui sortable add display:block inline
if (index >= 0) {
if ($.contains(self.$el[0], record.$el[0])) {
// resequencing records
self.trigger_up('kanban_column_resequence', {ids: self._getIDs()});
}
} else {
// adding record to this column
ui.item.addClass('o_updating');
self.trigger_up('kanban_column_add_record', {record: record, ids: self._getIDs()});
}
}
});
}
this.$el.click(function (event) {
if (self.folded) {
self._onToggleFold(event);
}
});
if (this.barOptions) {
this.$el.addClass('o_kanban_has_progressbar');
this.progressBar = new KanbanColumnProgressBar(this, this.barOptions, this.data);
defs.push(this.progressBar.appendTo(this.$header));
}
var title = this.folded ? this.title + ' (' + this.data.count + ')' : this.title;
this.$header.find('.o_column_title').text(title);
this.$el.toggleClass('o_column_folded', this.folded && !config.device.isMobile);
var tooltip = this.data.count + _t(' records');
tooltip = '<p>' + tooltip + '</p>' + this.tooltipInfo;
this.$header.find('.o_kanban_header_title').tooltip({html: true}).attr('data-original-title', tooltip);
if (!this.remaining) {
this.$('.o_kanban_load_more').remove();
} else {
this.$('.o_kanban_load_more').html(QWeb.render('KanbanView.LoadMore', {widget: this}));
}
return $.when.apply($, defs);
},
/**
* Called when a record has been quick created, as a new column is rendered
* and appended into a fragment, before replacing the old column in the DOM.
* When this happens, the quick create widget is inserted into the new
* column directly, and it should be focused. However, as it is rendered
* into a fragment, the focus has to be set manually once in the DOM.
*/
on_attach_callback: function () {
if (this.quickCreateWidget) {
this.quickCreateWidget.on_attach_callback();
}
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Adds the quick create record to the top of the column.
*
* @returns {Deferred}
*/
addQuickCreate: function () {
if (this.folded) {
// first open the column, and then add the quick create
this.trigger_up('column_toggle_fold', {
openQuickCreate: true,
});
return;
}
if (this.quickCreateWidget) {
return $.Deferred().reject();
}
this.trigger_up('close_quick_create'); // close other quick create widgets
this.trigger_up('start_quick_create');
var context = this.data.getContext();
context['default_' + this.groupedBy] = this.id;
this.quickCreateWidget = new RecordQuickCreate(this, {
context: context,
formViewRef: this.quickCreateView,
model: this.modelName,
});
return this.quickCreateWidget.insertAfter(this.$header);
},
/**
* Closes the quick create widget if it isn't dirty.
*/
cancelQuickCreate: function () {
if (this.quickCreateWidget) {
this.quickCreateWidget.cancel();
}
},
/**
* @returns {Boolean} true iff the column is empty
*/
isEmpty: function () {
return !this.records.length;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Adds a record in the column.
*
* @private
* @param {Object} recordState
* @param {Object} [options]
* @param {string} [options.position]
* 'before' to add at the top, add at the bottom by default
*/
_addRecord: function (recordState, options) {
var record = new KanbanRecord(this, recordState, this.record_options);
this.records.push(record);
if (options && options.position === 'before') {
record.insertAfter(this.quickCreateWidget ? this.quickCreateWidget.$el : this.$header);
} else {
var $load_more = this.$('.o_kanban_load_more');
if ($load_more.length) {
record.insertBefore($load_more);
} else {
record.appendTo(this.$el);
}
}
},
/**
* Destroys the QuickCreate widget.
*
* @private
*/
_cancelQuickCreate: function () {
this.quickCreateWidget.destroy();
this.quickCreateWidget = undefined;
},
/**
* @returns {integer[]} the res_ids of the records in the column
*/
_getIDs: function () {
var ids = [];
this.$('.o_kanban_record').each(function (index, r) {
ids.push($(r).data('record').id);
});
return ids;
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
*/
_onAddQuickCreate: function () {
this.addQuickCreate();
},
/**
* @private
*/
_onCancelQuickCreate: function () {
this._cancelQuickCreate();
},
/**
* @private
* @param {MouseEvent} event
*/
_onDeleteColumn: function (event) {
event.preventDefault();
var buttons = [
{
text: _t("Ok"),
classes: 'btn-primary',
close: true,
click: this.trigger_up.bind(this, 'kanban_column_delete'),
},
{text: _t("Cancel"), close: true}
];
new Dialog(this, {
size: 'medium',
buttons: buttons,
$content: $('<div>', {
text: _t("Are you sure that you want to remove this column ?")
}),
}).open();
},
/**
* @private
* @param {MouseEvent} event
*/
_onEditColumn: function (event) {
event.preventDefault();
new view_dialogs.FormViewDialog(this, {
res_model: this.relation,
res_id: this.id,
title: _t("Edit Column"),
on_saved: this.trigger_up.bind(this, 'reload'),
}).open();
},
/**
* @private
* @param {MouseEvent} event
*/
_onLoadMore: function (event) {
event.preventDefault();
this.trigger_up('kanban_load_more');
},
/**
* @private
* @param {OdooEvent} event
*/
_onQuickCreateAddRecord: function (event) {
this.trigger_up('quick_create_record', event.data);
},
/**
* @private
* @param {MouseEvent} event
*/
_onToggleFold: function (event) {
event.preventDefault();
this.trigger_up('column_toggle_fold');
},
/**
* @private
* @param {OdooEvent} ev
*/
_onTweakColumn: function (ev) {
ev.data.callback(this.$el);
},
/**
* @private
* @param {OdooEvent} ev
*/
_onTweakColumnRecords: function (ev) {
_.each(this.records, function (record) {
ev.data.callback(record.$el, record.state.data);
});
},
/**
* @private
* @param {MouseEvent} event
*/
_onArchiveRecords: function (event) {
event.preventDefault();
Dialog.confirm(this, _t("Are you sure that you want to archive all the records from this column?"), {
confirm_callback: this.trigger_up.bind(this, 'kanban_column_records_toggle_active', {
archive: true,
}),
});
},
/**
* @private
* @param {MouseEvent} event
*/
_onUnarchiveRecords: function (event) {
event.preventDefault();
this.trigger_up('kanban_column_records_toggle_active', {
archive: false,
});
}
});
return KanbanColumn;
});