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
|
odoo.define('web.KanbanModel', function (require) {
"use strict";
/**
* The KanbanModel extends the BasicModel to add Kanban specific features like
* moving a record from a group to another, resequencing records...
*/
var BasicModel = require('web.BasicModel');
var viewUtils = require('web.viewUtils');
var KanbanModel = BasicModel.extend({
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Adds a record to a group in the localData, and fetch the record.
*
* @param {string} groupID localID of the group
* @param {integer} resId id of the record
* @returns {Promise<string>} resolves to the local id of the new record
*/
addRecordToGroup: function (groupID, resId) {
var self = this;
var group = this.localData[groupID];
var new_record = this._makeDataPoint({
res_id: resId,
modelName: group.model,
fields: group.fields,
fieldsInfo: group.fieldsInfo,
viewType: group.viewType,
parentID: groupID,
});
var def = this._fetchRecord(new_record).then(function (result) {
group.data.unshift(new_record.id);
group.res_ids.unshift(resId);
group.count++;
// update the res_ids and count of the parent
self.localData[group.parentID].count++;
self._updateParentResIDs(group);
return result.id;
});
return this._reloadProgressBarGroupFromRecord(new_record.id, def);
},
/**
* Creates a new group from a name (performs a name_create).
*
* @param {string} name
* @param {string} parentID localID of the parent of the group
* @returns {Promise<string>} resolves to the local id of the new group
*/
createGroup: function (name, parentID) {
var self = this;
var parent = this.localData[parentID];
var groupBy = parent.groupedBy[0];
var groupByField = parent.fields[groupBy];
if (!groupByField || groupByField.type !== 'many2one') {
return Promise.reject(); // only supported when grouped on m2o
}
return this._rpc({
model: groupByField.relation,
method: 'name_create',
args: [name],
context: parent.context, // todo: combine with view context
})
.then(function (result) {
const createGroupDataPoint = (model, parent) => {
const newGroup = model._makeDataPoint({
modelName: parent.model,
context: parent.context,
domain: parent.domain.concat([[groupBy, "=", result[0]]]),
fields: parent.fields,
fieldsInfo: parent.fieldsInfo,
isOpen: true,
limit: parent.limit,
parentID: parent.id,
openGroupByDefault: true,
orderedBy: parent.orderedBy,
value: result,
viewType: parent.viewType,
});
if (parent.progressBar) {
newGroup.progressBarValues = _.extend({
counts: {},
}, parent.progressBar);
}
return newGroup;
};
const newGroup = createGroupDataPoint(self, parent);
parent.data.push(newGroup.id);
if (self.isInSampleMode()) {
// in sample mode, create the new group in both models (main + sample)
const sampleParent = self.sampleModel.localData[parentID];
const newSampleGroup = createGroupDataPoint(self.sampleModel, sampleParent);
sampleParent.data.push(newSampleGroup.id);
}
return newGroup.id;
});
},
/**
* Creates a new record from the given value, and add it to the given group.
*
* @param {string} groupID
* @param {Object} values
* @returns {Promise} resolved with the local id of the created record
*/
createRecordInGroup: function (groupID, values) {
var self = this;
var group = this.localData[groupID];
var context = this._getContext(group);
var parent = this.localData[group.parentID];
var groupedBy = parent.groupedBy;
context['default_' + groupedBy] = viewUtils.getGroupValue(group, groupedBy);
var def;
if (Object.keys(values).length === 1 && 'display_name' in values) {
// only 'display_name is given, perform a 'name_create'
def = this._rpc({
model: parent.model,
method: 'name_create',
args: [values.display_name],
context: context,
}).then(function (records) {
return records[0];
});
} else {
// other fields are specified, perform a classical 'create'
def = this._rpc({
model: parent.model,
method: 'create',
args: [values],
context: context,
});
}
return def.then(function (resID) {
return self.addRecordToGroup(group.id, resID);
});
},
/**
* Add the following (kanban specific) keys when performing a `get`:
*
* - tooltipData
* - progressBarValues
* - isGroupedByM2ONoColumn
*
* @override
* @see _readTooltipFields
* @returns {Object}
*/
__get: function () {
var result = this._super.apply(this, arguments);
var dp = result && this.localData[result.id];
if (dp) {
if (dp.tooltipData) {
result.tooltipData = $.extend(true, {}, dp.tooltipData);
}
if (dp.progressBarValues) {
result.progressBarValues = $.extend(true, {}, dp.progressBarValues);
}
if (dp.fields[dp.groupedBy[0]]) {
var groupedByM2O = dp.fields[dp.groupedBy[0]].type === 'many2one';
result.isGroupedByM2ONoColumn = !dp.data.length && groupedByM2O;
} else {
result.isGroupedByM2ONoColumn = false;
}
}
return result;
},
/**
* Same as @see get but getting the parent element whose ID is given.
*
* @param {string} id
* @returns {Object}
*/
getColumn: function (id) {
var element = this.localData[id];
if (element) {
return this.get(element.parentID);
}
return null;
},
/**
* @override
*/
__load: function (params) {
this.defaultGroupedBy = params.groupBy || [];
params.groupedBy = (params.groupedBy && params.groupedBy.length) ? params.groupedBy : this.defaultGroupedBy;
return this._super(params);
},
/**
* Load more records in a group.
*
* @param {string} groupID localID of the group
* @returns {Promise<string>} resolves to the localID of the group
*/
loadMore: function (groupID) {
var group = this.localData[groupID];
var offset = group.loadMoreOffset + group.limit;
return this.reload(group.id, {
loadMoreOffset: offset,
});
},
/**
* Moves a record from a group to another.
*
* @param {string} recordID localID of the record
* @param {string} groupID localID of the new group of the record
* @param {string} parentID localID of the parent
* @returns {Promise<string[]>} resolves to a pair [oldGroupID, newGroupID]
*/
moveRecord: function (recordID, groupID, parentID) {
var self = this;
var parent = this.localData[parentID];
var new_group = this.localData[groupID];
var changes = {};
var groupedFieldName = parent.groupedBy[0];
var groupedField = parent.fields[groupedFieldName];
if (groupedField.type === 'many2one') {
changes[groupedFieldName] = {
id: new_group.res_id,
display_name: new_group.value,
};
} else if (groupedField.type === 'selection') {
var value = _.findWhere(groupedField.selection, {1: new_group.value});
changes[groupedFieldName] = value && value[0] || false;
} else {
changes[groupedFieldName] = new_group.value;
}
// Manually updates groups data. Note: this is done before the actual
// save as it might need to perform a read group in some cases so those
// updated data might be overridden again.
var record = self.localData[recordID];
var resID = record.res_id;
// Remove record from its current group
var old_group;
for (var i = 0; i < parent.data.length; i++) {
old_group = self.localData[parent.data[i]];
var index = _.indexOf(old_group.data, recordID);
if (index >= 0) {
old_group.data.splice(index, 1);
old_group.count--;
old_group.res_ids = _.without(old_group.res_ids, resID);
self._updateParentResIDs(old_group);
break;
}
}
// Add record to its new group
new_group.data.push(recordID);
new_group.res_ids.push(resID);
new_group.count++;
return this.notifyChanges(recordID, changes).then(function () {
return self.save(recordID);
}).then(function () {
record.parentID = new_group.id;
return [old_group.id, new_group.id];
});
},
/**
* @override
*/
reload: function (id, options) {
// if the groupBy is given in the options and if it is an empty array,
// fallback on the default groupBy
if (options && options.groupBy && !options.groupBy.length) {
options.groupBy = this.defaultGroupedBy;
}
return this._super(id, options);
},
/**
* @override
*/
__reload: function (id, options) {
var def = this._super(id, options);
if (options && options.loadMoreOffset) {
return def;
}
return this._reloadProgressBarGroupFromRecord(id, def);
},
/**
* @override
*/
save: function (recordID) {
var def = this._super.apply(this, arguments);
return this._reloadProgressBarGroupFromRecord(recordID, def);
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
*/
_makeDataPoint: function (params) {
var dataPoint = this._super.apply(this, arguments);
if (params.progressBar) {
dataPoint.progressBar = params.progressBar;
}
return dataPoint;
},
/**
* @override
*/
_load: function (dataPoint, options) {
if (dataPoint.groupedBy.length && dataPoint.progressBar) {
return this._readProgressBarGroup(dataPoint, options);
}
return this._super.apply(this, arguments);
},
/**
* Ensures that there is no nested groups in Kanban (only the first grouping
* level is taken into account).
*
* @override
* @private
* @param {Object} list valid resource object
*/
_readGroup: function (list) {
var self = this;
if (list.groupedBy.length > 1) {
list.groupedBy = [list.groupedBy[0]];
}
return this._super.apply(this, arguments).then(function (result) {
return self._readTooltipFields(list).then(_.constant(result));
});
},
/**
* @private
* @param {Object} dataPoint
* @returns {Promise<Object>}
*/
_readProgressBarGroup: function (list, options) {
var self = this;
var groupsDef = this._readGroup(list, options);
var progressBarDef = this._rpc({
model: list.model,
method: 'read_progress_bar',
kwargs: {
domain: list.domain,
group_by: list.groupedBy[0],
progress_bar: list.progressBar,
context: list.context,
},
});
return Promise.all([groupsDef, progressBarDef]).then(function (results) {
var data = results[1];
_.each(list.data, function (groupID) {
var group = self.localData[groupID];
group.progressBarValues = _.extend({
counts: data[group.value] || {},
}, list.progressBar);
});
return list;
});
},
/**
* Fetches tooltip specific fields on the group by relation and stores it in
* the column datapoint in a special key `tooltipData`.
* Data for the tooltips (group_by_tooltip) are fetched in batch for all
* groups, to avoid doing multiple calls.
* Data are stored in a special key `tooltipData` on the datapoint.
* Note that the option `group_by_tooltip` is only for m2o fields.
*
* @private
* @param {Object} list a list of groups
* @returns {Promise}
*/
_readTooltipFields: function (list) {
var self = this;
var groupedByField = list.fields[list.groupedBy[0].split(':')[0]];
if (groupedByField.type !== 'many2one') {
return Promise.resolve();
}
var groupIds = _.reduce(list.data, function (groupIds, id) {
var res_id = self.get(id, {raw: true}).res_id;
// The field on which we are grouping might not be set on all records
if (res_id) {
groupIds.push(res_id);
}
return groupIds;
}, []);
var tooltipFields = [];
var groupedByFieldInfo = list.fieldsInfo.kanban[list.groupedBy[0]];
if (groupedByFieldInfo && groupedByFieldInfo.options) {
tooltipFields = Object.keys(groupedByFieldInfo.options.group_by_tooltip || {});
}
if (groupIds.length && tooltipFields.length) {
var fieldNames = _.union(['display_name'], tooltipFields);
return this._rpc({
model: groupedByField.relation,
method: 'read',
args: [groupIds, fieldNames],
context: list.context,
}).then(function (result) {
_.each(list.data, function (id) {
var dp = self.localData[id];
dp.tooltipData = _.findWhere(result, {id: dp.res_id});
});
});
}
return Promise.resolve();
},
/**
* Reloads all progressbar data. This is done after given promise and
* insures that the given promise's result is not lost.
*
* @private
* @param {string} recordID
* @param {Promise} def
* @returns {Promise}
*/
_reloadProgressBarGroupFromRecord: function (recordID, def) {
var element = this.localData[recordID];
if (element.type === 'list' && !element.parentID) {
// we are reloading the whole view, so there is no need to manually
// reload the progressbars
return def;
}
// If we updated a record, then we must potentially update columns'
// progressbars, so we need to load groups info again
var self = this;
while (element) {
if (element.progressBar) {
return def.then(function (data) {
return self._load(element, {
keepEmptyGroups: true,
onlyGroups: true,
}).then(function () {
return data;
});
});
}
element = this.localData[element.parentID];
}
return def;
},
});
return KanbanModel;
});
|