Permalink
Browse files

⚡️ correct subtask sorting on the creating new line

  • Loading branch information...
KolushovAlexandr committed Dec 6, 2018
1 parent d9b8043 commit 5e2804dd971dfddac5884f6356a7843f3ae027c9
Showing with 98 additions and 4 deletions.
  1. +98 −4 project_task_subtask/static/src/js/one2many_renderer.js
@@ -1,14 +1,15 @@
odoo.define('project_task_subtask.one2many_renderer', function(require){

var FieldOne2Many = require('web.relational_fields').FieldOne2Many;
var BasicModel = require('web.BasicModel');

FieldOne2Many.include({

check_task_tree_mode: function(){
if (this.view &&
this.view.arch.tag === 'tree' &&
this.record && this.record.model === "project.task" &&
this.mode !== 'edit') {
this.record && this.record.model === "project.task"
) {
return true;
}
return false;
@@ -17,7 +18,11 @@ odoo.define('project_task_subtask.one2many_renderer', function(require){
sort_data: function() {
var user_id = this.record.context.uid;

var data = this.value.data;
var new_rows = _.filter(this.value.data, function(d){
return !d.res_id
});
var data = _.difference(this.value.data, new_rows);

_.each(data, function(d){
d.u_name = d.data.user_id.data.display_name;
});
@@ -53,7 +58,11 @@ odoo.define('project_task_subtask.one2many_renderer', function(require){
d.index += 700000;
}
});
this.value.data = _.sortBy(data, 'index');
data = _.sortBy(data, 'index');
_.each(new_rows, function(r){
data.push(r);
});
this.value.data = data;
},

_render: function () {
@@ -63,6 +72,91 @@ odoo.define('project_task_subtask.one2many_renderer', function(require){
return this._super(arguments);
},

reset: function (record, ev, fieldChanged) {
var self = this;
return this._super.apply(this, arguments).then(function(res){
if (self.check_task_tree_mode()) {
self._render();
}
});
},

});

BasicModel.include({

_sortList: function(list){
// taken from odoo
if (!list.static) {
// only sort x2many lists
return;
}
var self = this;
// -----

if (list.model === "project.task.subtask" && list.orderedResIDs) {
var rows = [];
var new_rows = [];
_.each(list.data, function(d){
var r = self.localData[d];
if (Number(r.res_id) === r.res_id) {
rows.push(r);
} else {
new_rows.push(r);
}
});
rows = this.sort_data(rows, list.context.uid, this);
_.each(new_rows, function(r){
rows.push(r);
});
list.orderedResIDs = _.pluck(rows, 'res_id');
return this._setDataInRange(list);
}

return this._super(list);

},

sort_data: function(data, user_id, parent) {
user_id = user_id || 1;

_.each(data, function(d){
d.u_name = parent.localData[d.data.user_id].data.display_name;
});

var name_index = _.sortBy(_.uniq(_.map(data, function(d){
return parent.localData[d.data.user_id].data.display_name;
})));


data = _.sortBy(data, 'u_name');
_.each(data, function(d){
d.deadline = d.data.deadline;
if (parent.localData[d.data.user_id].data.id === user_id) {
d.index = 0;
} else {
d.index = (_.indexOf(name_index, parent.localData[d.data.user_id].data.display_name) + 1) * 1000000;
}
});

data = _.sortBy(data, 'deadline');
_.each(data, function(d){
d.index += _.indexOf(data, d);
if (!d.deadline) {
d.index += 90000;
}
if (d.data.state === 'todo') {
// continue
} else if (d.data.state === 'waiting') {
d.index += 100000;
} else if (d.data.state === 'done') {
d.index += 400000;
} else {
d.index += 700000;
}
});
return _.sortBy(data, 'index');
},

});
});

0 comments on commit 5e2804d

Please sign in to comment.