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;
/*
--------------------------

View File

@@ -30,42 +30,25 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
Analytics.updateUser();
};
function addTask(addTo, listDef, tasks) {
tasks = _.isArray(tasks) ? tasks : [tasks];
function addUserTasks(listDef, tasks) {
tasks = tasks.map(function (task) {
return {
text: task,
type: listDef.type,
tags: _.keys(User.user.filters),
};
});
User.addTask({
body: tasks.map(function (task) {
return {
text: task,
type: listDef.type,
tags: _.keys(User.user.filters),
}
}),
body: tasks,
});
}
$scope.addTask = function(addTo, listDef) {
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();
addTask(addTo, listDef, tasks);
listDef.bulk = false;
} else {
addTask(addTo, listDef, listDef.newTask);
}
delete listDef.newTask;
delete listDef.focus;
$scope.addTask = function(listDef) {
Tasks.addTasks(listDef, addUserTasks);
if (listDef.type=='daily') Guide.goto('intro', 2);
};
$scope.toggleBulk = function(list) {
if (typeof list.bulk === 'undefined') {
list.bulk = false;
}
list.bulk = !list.bulk;
list.focus = true;
};
$scope.toggleBulk = Tasks.toggleBulk;
$scope.editTask = Tasks.editTask;

View File

@@ -5,6 +5,29 @@ var TASK_KEYS_TO_REMOVE = ['_id', 'completed', 'date', 'dateCompleted', 'history
angular.module('habitrpg')
.factory('Tasks', ['$rootScope', 'Shared', '$http',
function tasksFactory($rootScope, Shared, $http) {
function addTasks(listDef, addTaskFn) {
var tasks = listDef.newTask;
if (listDef.bulk) {
tasks = tasks.split(/[\n\r]+/);
// Reverse the order of tasks so the tasks
// will appear in the order the user entered them
tasks.reverse();
listDef.bulk = false;
} else {
tasks = [tasks];
}
addTaskFn(listDef, tasks);
delete listDef.newTask;
delete listDef.focus;
}
function toggleBulk (list) {
list.bulk = !list.bulk;
list.focus = true;
};
function getUserTasks (getCompletedTodos) {
var url = '/api/v3/tasks/user';
@@ -33,11 +56,11 @@ angular.module('habitrpg')
});
};
function createChallengeTasks (challengeId, taskDetails) {
function createChallengeTasks (challengeId, tasks) {
return $http({
method: 'POST',
url: '/api/v3/tasks/challenge/' + challengeId,
data: taskDetails,
data: tasks,
});
};
@@ -181,6 +204,8 @@ angular.module('habitrpg')
}
return {
addTasks: addTasks,
toggleBulk: toggleBulk,
getUserTasks: getUserTasks,
loadedCompletedTodos: false,
createUserTasks: createUserTasks,

View File

@@ -1,5 +1,5 @@
form.task-add(name='new{{list.type}}form', ng-hide='obj._locked', ng-submit='addTask(obj[list.type+"s"], list, obj)', novalidate)
textarea(rows='6', focus-element='list.bulk && list.focus', ng-model='list.newTask', placeholder='{{list.placeHolderBulk}}', ng-if='list.bulk', ui-keydown='{"meta-enter ctrl-enter":"addTask(obj[list.type+\'s\'],list)"}', required)
form.task-add(name='new{{list.type}}form', ng-hide='obj._locked', ng-submit='addTask(list, obj)', novalidate)
textarea(rows='6', focus-element='list.bulk && list.focus', ng-model='list.newTask', placeholder='{{list.placeHolderBulk}}', ng-if='list.bulk', ui-keydown='{"meta-enter ctrl-enter":"addTask(list, obj)"}', required)
input(type='text', focus-element='!list.bulk && list.focus', ng-model='list.newTask', placeholder='{{list.placeHolder}}', ng-if='!list.bulk', required)
button(type='submit', ng-disabled='new{{list.type}}form.$invalid')
div(ng-show='new{{list.type}}form.$invalid', tooltip=env.t("emptyTask"))