fix(client): Allow bulk task adding in challenges

closes #6781
fixes #6723
This commit is contained in:
Blade Barringer
2016-09-11 21:59:18 -05:00
parent 0474b5d2e6
commit 379b318202
4 changed files with 75 additions and 59 deletions

View File

@@ -257,28 +257,42 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
//------------------------------------------------------------
// Tasks
//------------------------------------------------------------
function addTask (addTo, listDef, challenge) {
var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type});
//If the challenge has not been created, we bulk add tasks on save
if (challenge._id) Tasks.createChallengeTasks(challenge._id, task);
if (!challenge[task.type + 's']) challenge[task.type + 's'] = [];
challenge[task.type + 's'].unshift(task);
delete listDef.newTask;
function addChallengeTasks (listDef, challenge, tasks) {
var type = listDef.type;
// If the challenge has not been created, we bulk add tasks on save
tasks = tasks.map(function (task) {
return Shared.taskDefaults({
text: task,
type: type,
});
});
type = type + 's';
if (challenge._id) {
Tasks.createChallengeTasks(challenge._id, tasks).then(function (res) {
addToList(challenge, type, res.data.data);
});
} else {
addToList(challenge, type, tasks);
}
};
$scope.addTask = function(addTo, listDef, challenge) {
if (listDef.bulk) {
var tasks = listDef.newTask.split(/[\n\r]+/);
//Reverse the order of tasks so the tasks will appear in the order the user entered them
tasks.reverse();
_.each(tasks, function(t) {
listDef.newTask = t;
addTask(addTo, listDef, challenge);
});
listDef.bulk = false;
} else {
addTask(addTo, listDef, challenge);
function addToList (challenge, type, tasks) {
if (!_.isArray(tasks)) {
tasks = [tasks];
}
if (!challenge[type]) {
challenge[type] = [];
}
challenge[type].unshift.apply(challenge[type], tasks);
}
$scope.addTask = function(listDef, challenge) {
Tasks.addTasks(listDef, function (listDef, tasks) {
addChallengeTasks(listDef, challenge, tasks);
});
}
$scope.removeTask = function(task, challenge) {
@@ -294,13 +308,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
task._editing = false;
}
$scope.toggleBulk = function(list) {
if (typeof list.bulk === 'undefined') {
list.bulk = false;
}
list.bulk = !list.bulk;
list.focus = true;
};
$scope.toggleBulk = Tasks.toggleBulk;
/*
--------------------------