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 // Tasks
//------------------------------------------------------------ //------------------------------------------------------------
function addTask (addTo, listDef, challenge) { function addChallengeTasks (listDef, challenge, tasks) {
var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type}); var 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 the challenge has not been created, we bulk add tasks on save
if (!challenge[task.type + 's']) challenge[task.type + 's'] = []; tasks = tasks.map(function (task) {
challenge[task.type + 's'].unshift(task); return Shared.taskDefaults({
delete listDef.newTask; 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) { function addToList (challenge, type, tasks) {
if (listDef.bulk) { if (!_.isArray(tasks)) {
var tasks = listDef.newTask.split(/[\n\r]+/); tasks = [tasks];
//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);
} }
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) { $scope.removeTask = function(task, challenge) {
@@ -294,13 +308,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
task._editing = false; task._editing = false;
} }
$scope.toggleBulk = function(list) { $scope.toggleBulk = Tasks.toggleBulk;
if (typeof list.bulk === 'undefined') {
list.bulk = false;
}
list.bulk = !list.bulk;
list.focus = true;
};
/* /*
-------------------------- --------------------------

View File

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

View File

@@ -5,6 +5,29 @@ var TASK_KEYS_TO_REMOVE = ['_id', 'completed', 'date', 'dateCompleted', 'history
angular.module('habitrpg') angular.module('habitrpg')
.factory('Tasks', ['$rootScope', 'Shared', '$http', .factory('Tasks', ['$rootScope', 'Shared', '$http',
function tasksFactory($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) { function getUserTasks (getCompletedTodos) {
var url = '/api/v3/tasks/user'; var url = '/api/v3/tasks/user';
@@ -33,11 +56,11 @@ angular.module('habitrpg')
}); });
}; };
function createChallengeTasks (challengeId, taskDetails) { function createChallengeTasks (challengeId, tasks) {
return $http({ return $http({
method: 'POST', method: 'POST',
url: '/api/v3/tasks/challenge/' + challengeId, url: '/api/v3/tasks/challenge/' + challengeId,
data: taskDetails, data: tasks,
}); });
}; };
@@ -181,6 +204,8 @@ angular.module('habitrpg')
} }
return { return {
addTasks: addTasks,
toggleBulk: toggleBulk,
getUserTasks: getUserTasks, getUserTasks: getUserTasks,
loadedCompletedTodos: false, loadedCompletedTodos: false,
createUserTasks: createUserTasks, 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) 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(obj[list.type+\'s\'],list)"}', required) 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) 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') button(type='submit', ng-disabled='new{{list.type}}form.$invalid')
div(ng-show='new{{list.type}}form.$invalid', tooltip=env.t("emptyTask")) div(ng-show='new{{list.type}}form.$invalid', tooltip=env.t("emptyTask"))