Files
habitica/website/client-old/js/controllers/tasksCtrl.js
Keith Holliday ea24eeb019 Thehollidayinn/group plans part 2 (#8262)
* Added all ui components back

* Added group ui items back and initial group approval directive

* Added approval list view with approving functionality

* Added notification display for group approvals

* Fixed linting issues

* Removed expectation from beforeEach

* Moved string to locale

* Added per use group plan for stripe

* Added tests for stripe group plan upgrade

* Removed paypal option

* Abstract sub blocks. Hit group sub block from user settings page. Added group subscriptin beneifts display

* Fixed lint issue

* Added pricing and adjusted styles

* Moved text to translations

* Added group email types

* Fixed typo

* Fixed group plan abstraction and other style issues

* Fixed email unit test

* Added type to group plan to filter our group plans

* Removed dev protection from routes

* Removed hard coding and fixed upgrade plan

* Added error when group has subscription and tries to remove

* Fixed payment unit tests

* Added custom string and moved subscription check up in the logic

* Added ability for old leader to delete subscription the created

* Allowed old guild leader to edit their group subscription

* Fixed linting and tests

* Added group sub page to user sub settings

* Added approval and group tasks requests back. Hid user group sub on profile

* Added group tasks sync after adding to allow for editing

* Fixed promise chain when resolving group

* Added approvals to group promise chain

* Ensured compelted group todos are not delted at cron

* Updated copy and other minor styles

* Added group field to tags and recolored group tag.

* Added chat message when task is claimed

* Preventing task scoring when approval is needed

* Added approval requested indicator

* Updated column with for tasks on group page

* Added checklist sync on assign

* Added sync for checklist items

* Added checkilist sync when task is updated

* Added checklist sync remove

* Sanatized group tasks when updated

* Fixed lint issues

* Added instant scoring of approved task

* Added task modal

* Fixed editing of challenge and group tasks

* Added cancel button

* Added add new checklist option to update sync

* Added remove for checklist

* Added checklist update

* Added difference check and sync for checklist if there is a diff

* Fixed task syncing

* Fixed linting issues

* Fixed styles and karma tests

* Fixed minor style issues

* Fixed obj transfer on scope

* Fixed broken tests

* Added new benefits page

* Updated group page styles

* Updated benefits page style

* Added translations

* Prevented sync with empty trask list

* Added task title to edit modal

* Added new group plans page and upgrade redirect

* Added group plans redirect to upgrade

* Fixed party home page being hidden and home button click

* Fixed dynamic changing of task status and grey popup

* Fixed tag editing

* Hid benifites information if group has subscription

* Added quotes to task name

* Fixed issue with assigning multiple users

* Added new group plans ctrl

* Hid menu from public guilds

* Fixed task sync issue

* Updated placeholder for assign field

* Added correct cost to subscribe details

* Hid create, edit, delete task options from non group leaders

* Prevented some front end modifications to group tasks

* Hid tags option from group original task

* Added refresh for approvals and group tasks

* Prepend new group tasks

* Fix last checklist item sync

* Fixed casing issue with tags

* Added claimed by message on hover

* Prevent user from deleting assigned task

* Added single route for group plan sign up and payments

* Abstracted stripe payments and added initial tests

* Abstracted amazon and added initial tests

* Fixed create group message

* Update group id check and return group

* Updated to use the new returned group

* Fixed linting and promise issues

* Fixed broken leave test after merge issue

* Fixed undefined approval error and editing/deleting challenge tasks

* Add pricing to group plans, removed confirmation, and fixed redirect after payment

* Updated group plan cost text
2016-12-21 13:45:45 -06:00

344 lines
9.5 KiB
JavaScript

"use strict";
habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'ApiUrl', '$timeout', 'Content', 'Shared', 'Guide', 'Tasks', 'Analytics',
function($scope, $rootScope, $location, User, Notification, $http, ApiUrl, $timeout, Content, Shared, Guide, Tasks, Analytics) {
$scope.obj = User.user; // used for task-lists
$scope.user = User.user;
var CTRL_KEYS = [17, 224, 91];
$scope.armoireCount = function(gear) {
return Shared.count.remainingGearInSet(gear, 'armoire');
};
$scope.score = function(task, direction) {
switch (task.type) {
case 'reward':
playRewardSound(task);
break;
case 'daily':
$rootScope.playSound('Daily');
break;
case 'todo':
$rootScope.playSound('ToDo');
break;
default:
if (direction === 'down') $rootScope.playSound('Minus_Habit');
else if (direction === 'up') $rootScope.playSound('Plus_Habit');
}
User.score({params:{task: task, direction:direction}});
Analytics.updateUser();
};
function addUserTasks(listDef, tasks) {
tasks = tasks.map(function (task) {
return {
text: task,
type: listDef.type,
tags: _.keys(User.user.filters),
_advanced: !User.user.preferences.advancedCollapsed,
};
});
User.addTask({
body: tasks,
});
}
$scope.addTask = function(listDef) {
Tasks.addTasks(listDef, addUserTasks);
if (listDef.type=='daily') Guide.goto('intro', 2);
};
$scope.toggleBulk = Tasks.toggleBulk;
$scope.editTask = function (task, user, taskStatus) {
Tasks.editTask(task, user, taskStatus, $scope);
};
$scope.canEdit = function(task) {
// can't edit challenge tasks
return !task.challenge.id && (!task.group || !task.group.id);
}
$scope.checkGroupAccess = function (group) {
return true;
}
$scope.doubleClickTask = function (obj, task) {
if (obj._locked) {
return false;
}
if (task._editing) {
$scope.saveTask(task);
} else {
$scope.editTask(task, User.user);
}
}
/**
* Add the new task to the actions log
*/
$scope.clearDoneTodos = function() {
if (!confirm(window.env.t('sureDeleteCompletedTodos'))) {
return;
}
Tasks.clearCompletedTodos();
User.user.todos = _.reject(User.user.todos, 'completed');
};
/**
* Pushes task to top or bottom of list
*/
$scope.pushTask = function(task, index, location) {
var to = (location === 'bottom' || $scope.ctrlPressed) ? -1 : 0;
User.sortTask({params:{id: task._id, taskType: task.type}, query:{from:index, to:to}})
};
/**
* This is calculated post-change, so task.completed=true if they just checked it
*/
$scope.changeCheck = function(task) {
if (task.completed) {
$scope.score(task, "up");
} else {
$scope.score(task, "down");
}
};
$scope.saveTask = function(task, stayOpen, isSaveAndClose) {
Tasks.saveTask (task, stayOpen, isSaveAndClose);
User.updateTask(task, {body: task});
if (task.type == 'habit') Guide.goto('intro', 3);
};
$scope.completeChecklistItem = function completeChecklistItem(task) {
User.updateTask(task, {body: task});
};
/**
* Reset $scope.task to $scope.originalTask
*/
$scope.cancelTaskEdit = Tasks.cancelTaskEdit;
$scope.removeTask = function(task) {
if (!Tasks.removeTask(task)) return;
User.deleteTask({params:{id: task._id, taskType: task.type}})
};
$scope.unlink = function(task, keep) {
if (keep.search('-all') !== -1) { // unlink all tasks
Tasks.unlinkAllTasks(task.challenge.id, keep)
.success(function () {
User.sync({});
});
} else { // unlink a task
Tasks.unlinkOneTask(task._id, keep)
.success(function () {
User.sync({});
});
}
};
/*
------------------------
To-Dos
------------------------
*/
$scope._today = moment().add({days: 1});
$scope.loadedCompletedTodos = function () {
if (Tasks.loadedCompletedTodos === true) {
return;
}
User.user.todos = _.reject(User.user.todos, 'completed')
Tasks.getUserTasks(true)
.then(function (response) {
User.user.todos = User.user.todos.concat(response.data.data);
Tasks.loadedCompletedTodos = true;
});
}
/*
------------------------
Dailies
------------------------
*/
$scope.openDatePicker = function($event, task) {
$event.preventDefault();
$event.stopPropagation();
task._isDatePickerOpen = !task._isDatePickerOpen;
}
/*
------------------------
Checklists
------------------------
*/
/*
------------------------
Checklists
------------------------
*/
$scope.addChecklist = Tasks.addChecklist;
$scope.addChecklistItem = Tasks.addChecklistItemToUI;
$scope.removeChecklistItem = Tasks.removeChecklistItemFromUI;
$scope.swapChecklistItems = Tasks.swapChecklistItems;
$scope.navigateChecklist = Tasks.navigateChecklist;
$scope.checklistCompletion = Tasks.checklistCompletion;
$scope.collapseChecklist = function (task) {
Tasks.collapseChecklist(task);
//@TODO: Currently the api save of the task is separate, so whenever we need to save the task we need to call the respective api
Tasks.updateTask(task._id, task);
};
/*
------------------------
Items
------------------------
*/
$scope.$watch('user.items.gear.owned', function(){
$scope.itemStore = Shared.updateStore(User.user);
},true);
$scope.healthPotion = Content.potion;
$scope.armoire = Content.armoire;
$scope.buy = function(item) {
playRewardSound(item);
User.buy({params:{key:item.key}});
};
$scope.buyArmoire = function () {
playRewardSound($scope.armoire);
User.buyArmoire();
}
/*
------------------------
Hiding Tasks
------------------------
*/
$scope.shouldShow = function(task, list, prefs) {
if (task._editing) // never hide a task while being edited
return true;
var shouldDo = task.type == 'daily' ? habitrpgShared.shouldDo(new Date, task, prefs) : true;
switch (list.view) {
case "yellowred": // Habits
return task.value < 1;
case "greenblue": // Habits
return task.value >= 1;
case "remaining": // Dailies and To-Dos
return !task.completed && shouldDo;
case "complete": // Dailies and To-Dos
return task.completed || !shouldDo;
case "dated": // To-Dos
return !task.completed && task.date;
case "ingamerewards": // All skills/rewards except the user's own
return false; // Because "rewards" list includes only the user's own
case "all":
return true;
}
}
function playRewardSound (task) {
if (task.value <= User.user.stats.gp){
$rootScope.playSound('Reward');
}
}
var isCtrlPressed = function (keyEvent) {
if (CTRL_KEYS.indexOf(keyEvent.keyCode) > -1) {
$scope.ctrlPressed = true;
$scope.$apply();
}
}
var isCtrlLetGo = function (keyEvent) {
if (CTRL_KEYS.indexOf(keyEvent.keyCode) > -1) {
$scope.ctrlPressed = false;
$scope.$apply();
}
}
document.addEventListener('keydown', isCtrlPressed);
document.addEventListener('keyup', isCtrlLetGo);
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams, options){
if (toState.name.indexOf('tasks') < 0) {
document.removeEventListener('keydown', isCtrlPressed);
document.removeEventListener('keyup', isCtrlLetGo);
}
});
/*
------------------------
Tags
------------------------
*/
$scope.updateTaskTags = function (tagId, task) {
var tagIndex = task._edit.tags.indexOf(tagId);
if (tagIndex === -1) {
Tasks.addTagToTask(task._id, tagId);
task.tags.push(tagId);
} else {
Tasks.removeTagFromTask(task._id, tagId);
task.tags.splice(tagIndex, 1);
}
angular.copy(task.tags, task._edit.tags);
}
/*
------------------------
Disabling Spells
------------------------
*/
$scope.spellDisabled = function (skill) {
if (skill === 'frost' && $scope.user.stats.buffs.streaks) {
return true;
} else if (skill === 'stealth' && $scope.user.stats.buffs.stealth >= $scope.user.dailys.length) {
return true;
}
return false;
};
$scope.skillNotes = function (skill) {
var notes = skill.notes();
if (skill.key === 'frost' && $scope.spellDisabled(skill.key)) {
notes = window.env.t('spellWizardFrostAlreadyCast');
} else if (skill.key === 'stealth' && $scope.spellDisabled(skill.key)) {
notes = window.env.t('spellRogueStealthMaxedOut');
} else if (skill.key === 'stealth') {
notes = window.env.t('spellRogueStealthDaliesAvoided', { originalText: notes, number: $scope.user.stats.buffs.stealth });
}
return notes;
};
/*
* Task Details
*/
$scope.taskPopover = function (task) {
if (task.popoverOpen) return '';
var content = task.notes;
return content;
};
}]);