Skip to content

Commit

Permalink
minimize changes to priority
Browse files Browse the repository at this point in the history
  • Loading branch information
piascikj committed Aug 22, 2019
1 parent c563e44 commit 3746f83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.5.31
- When moving a task, only change the order of the current task unless it's moving to the top of a list with more than one task, then we update the task that was previously first as well.

# 1.5.28
- Implement Repository.modifyTaskFromContent

Expand Down
47 changes: 32 additions & 15 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,23 +1027,40 @@ Repository.prototype.moveTasks = function(tasks, newList, newPos, noEmit, cb) {
return _task.equals(task);
});
}
// Modify the tasks in current list
var index = 0;
var taskMapper = function(_task, cb) {
self.setTaskPriority(_task, index, function() {
index++;
self.modifyTask(_task, cb);
});
};

async.eachSeries(toListTasks, taskMapper, function(err) {
if (err) return cb([new Error("moveTasks: Error while modifying tasks in the current list"), err]);
// Modify the task order
const tasksToModify = []
if (newPos === 0) { // is first
task.order = 0
if (toListTasks.length > 1) {
const nextTask = toListTasks[1]
if (toListTasks.length > 2) {
const nextOrder = toListTasks[2].order
nextTask.order = nextOrder ? nextOrder/2 : 10
} else {
nextTask.order = 10
}
tasksToModify.push(nextTask)
}
} else if (newPos === toListTasks.length-1) { // is last
const oldLastTask = toListTasks[toListTasks.length-2]
const oldLastTaskOrder = oldLastTask.order || (toListTasks.length-2)*10
task.order = oldLastTaskOrder + 10
} else {
const prevTask = toListTasks[newPos-1]
const nextTask = toListTasks[newPos+1]
const prevTaskOrder = prevTask.order || 0
const nextTaskOrder = nextTask.order || 100
task.order = prevTaskOrder + (nextTaskOrder - prevTaskOrder)/2
}
tasksToModify.push(task)

// Remove the task from the old list
if (!sameList) {
async.eachSeries(fromListTasks, taskMapper, cb);
} else cb(null, task);
});
async.eachSeries(tasksToModify, (task, cb) => {
self.modifyTask(task, cb);
}, err => {
if (err) return cb([new Error("moveTasks: Error while modifying tasks in the current list"), err])
cb(null, task)
})
};

async.series(_.map(tasks, function(task, i) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imdone-core",
"version": "1.5.30",
"version": "1.5.31",
"description": "imdone-core",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 3746f83

Please sign in to comment.