gMerge branch 'challenges' into develop

Conflicts:
	migrations/20131028_cleanup_deleted_tags.js
	src/controllers/groups.js
	views/options/groups/group.jade
	views/options/profile.jade
This commit is contained in:
Tyler Renelle
2013-10-29 15:38:47 -07:00
49 changed files with 1899 additions and 1424 deletions

View File

@@ -0,0 +1,122 @@
"use strict";
habitrpg.controller("ChallengesCtrl", ['$scope', 'User', 'Challenges', 'Notification', '$compile', 'groups', 'challenges',
function($scope, User, Challenges, Notification, $compile, groups, challenges) {
// groups & challenges are loaded as `resolve` via ui-router (see app.js)
$scope.groups = groups;
$scope.challenges = challenges;
//------------------------------------------------------------
// Challenge
//------------------------------------------------------------
/**
* Create
*/
$scope.create = function() {
$scope.newChallenge = new Challenges.Challenge({
name: '',
description: '',
habits: [],
dailys: [],
todos: [],
rewards: [],
leader: User.user._id,
group: null,
timestamp: +(new Date),
members: []
});
};
/**
* Save
*/
$scope.save = function(challenge) {
if (!challenge.group) return alert('Please select group');
var isNew = !challenge._id;
challenge.$save(function(){
if (isNew) {
Notification.text('Challenge Created');
$scope.discard();
Challenges.Challenge.query();
} else {
// TODO figure out a more elegant way about this
//challenge._editing = false;
challenge._locked = true;
}
});
};
/**
* Discard
*/
$scope.discard = function() {
$scope.newChallenge = null;
};
/**
* Delete
*/
$scope["delete"] = function(challenge) {
if (confirm("Delete challenge, are you sure?") !== true) return;
challenge.$delete();
};
//------------------------------------------------------------
// Tasks
//------------------------------------------------------------
$scope.addTask = function(list) {
var task = window.habitrpgShared.helpers.taskDefaults({text: list.newTask, type: list.type}, User.user.filters);
list.tasks.unshift(task);
//User.log({op: "addTask", data: task}); //TODO persist
delete list.newTask;
};
$scope.removeTask = function(list, $index) {
if (confirm("Are you sure you want to delete this task?")) return;
//TODO persist
// User.log({
// op: "delTask",
// data: task
//});
list.splice($index, 1);
};
$scope.saveTask = function(task){
task._editing = false;
// TODO persist
}
/*
--------------------------
Unsubscribe functions
--------------------------
*/
$scope.unsubscribe = function(keep) {
if (keep == 'cancel') {
$scope.selectedChal = undefined;
} else {
$scope.selectedChal.$leave({keep:keep});
}
$scope.popoverEl.popover('destroy');
}
$scope.clickUnsubscribe = function(chal, $event) {
$scope.selectedChal = chal;
$scope.popoverEl = $($event.target);
var html = $compile(
'<a ng-controller="ChallengesCtrl" ng-click="unsubscribe(\'remove-all\')">Remove Tasks</a><br/>\n<a ng-click="unsubscribe(\'keep-all\')">Keep Tasks</a><br/>\n<a ng-click="unsubscribe(\'cancel\')">Cancel</a><br/>'
)($scope);
$scope.popoverEl.popover('destroy').popover({
html: true,
placement: 'top',
trigger: 'manual',
title: 'Unsubscribe From Challenge And:',
content: html
}).popover('show');
}
}]);

View File

@@ -34,7 +34,7 @@ habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User', 'API_URL', '
delete user.filters[tag.id];
user.tags.splice($index,1);
// remove tag from all tasks
_.each(user.tasks, function(task) {
_.each(user.habits.concat(user.dailys).concat(user.todos).concat(user.rewards), function(task) {
delete task.tags[tag.id];
});
User.log({op:'delTag',data:{'tag':tag.id}})

View File

@@ -1,7 +1,7 @@
"use strict";
habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'API_URL', '$q', 'User', 'Members', '$location',
function($scope, $rootScope, Groups, $http, API_URL, $q, User, Members, $location) {
habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'API_URL', '$q', 'User', 'Members', '$location', '$state',
function($scope, $rootScope, Groups, $http, API_URL, $q, User, Members, $location, $state) {
$scope.isMember = function(user, group){
return ~(group.members.indexOf(user._id));
@@ -35,10 +35,10 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
$scope.clickMember = function(uid, forceShow) {
if (User.user._id == uid && !forceShow) {
if ($location.path() == '/tasks') {
$location.path('/options');
if ($state.is('tasks')) {
$state.go('options');
} else {
$location.path('/tasks');
$state.go('tasks');
}
} else {
// We need the member information up top here, but then we pass it down to the modal controller
@@ -128,6 +128,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
.controller("GuildsCtrl", ['$scope', 'Groups', 'User', '$rootScope',
function($scope, Groups, User, $rootScope) {
Groups.fetchGuilds();
$scope.type = 'guild';
$scope.text = 'Guild';
$scope.newGroup = new Groups.Group({type:'guild', privacy:'private', leader: User.user._id, members: [User.user._id]});
@@ -212,8 +213,8 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
.controller("TavernCtrl", ['$scope', 'Groups', 'User',
function($scope, Groups, User) {
Groups.fetchTavern();
$scope.group = Groups.groups.tavern;
$scope.rest = function(){
User.user.flags.rest = !User.user.flags.rest;
User.log({op:'set',data:{'flags.rest':User.user.flags.rest}});

View File

@@ -3,8 +3,8 @@
/* Make user and settings available for everyone through root scope.
*/
habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http',
function($scope, $rootScope, $location, User, $http) {
habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http', '$state', '$stateParams',
function($scope, $rootScope, $location, User, $http, $state, $stateParams) {
$rootScope.modals = {};
$rootScope.modals.achievements = {};
$rootScope.User = User;
@@ -12,6 +12,15 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
$rootScope.settings = User.settings;
$rootScope.flash = {errors: [], warnings: []};
// Angular UI Router
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
// indexOf helper
$scope.indexOf = function(haystack, needle){
return haystack && ~haystack.indexOf(needle);
}
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {

View File

@@ -1,53 +0,0 @@
"use strict";
habitrpg.controller("TaskDetailsCtrl", ['$scope', '$rootScope', '$location', 'User',
function($scope, $rootScope, $location, User) {
$scope.save = function(task) {
var log, setVal;
setVal = function(k, v) {
var op;
if (typeof v !== "undefined") {
op = {
op: "set",
data: {}
};
op.data["tasks." + task.id + "." + k] = v;
return log.push(op);
}
};
log = [];
setVal("text", task.text);
setVal("notes", task.notes);
setVal("priority", task.priority);
setVal("tags", task.tags);
if (task.type === "habit") {
setVal("up", task.up);
setVal("down", task.down);
} else if (task.type === "daily") {
setVal("repeat", task.repeat);
// TODO we'll remove this once rewrite's running for a while. This was a patch for derby issues
setVal("streak", task.streak);
} else if (task.type === "todo") {
setVal("date", task.date);
} else {
if (task.type === "reward") {
setVal("value", task.value);
}
}
User.log(log);
task._editing = false;
};
$scope.cancel = function() {
/* reset $scope.task to $scope.originalTask
*/
var key;
for (key in $scope.task) {
$scope.task[key] = $scope.originalTask[key];
}
$scope.originalTask = null;
$scope.editedTask = null;
$scope.editing = false;
};
}]);

View File

@@ -1,36 +1,7 @@
"use strict";
habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', 'Algos', 'Helpers', 'Notification',
function($scope, $rootScope, $location, User, Algos, Helpers, Notification) {
/*FIXME
*/
$scope.taskLists = [
{
header: 'Habits',
type: 'habit',
placeHolder: 'New Habit',
main: true,
editable: true
}, {
header: 'Dailies',
type: 'daily',
placeHolder: 'New Daily',
main: true,
editable: true
}, {
header: 'Todos',
type: 'todo',
placeHolder: 'New Todo',
main: true,
editable: true
}, {
header: 'Rewards',
type: 'reward',
placeHolder: 'New Reward',
main: true,
editable: true
}
];
habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', 'Algos', 'Helpers', 'Notification', '$http', 'API_URL',
function($scope, $rootScope, $location, User, Algos, Helpers, Notification, $http, API_URL) {
$scope.score = function(task, direction) {
if (task.type === "reward" && User.user.stats.gp < task.value){
return Notification.text('Not enough GP.');
@@ -42,18 +13,20 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', '
$scope.addTask = function(list) {
var task = window.habitrpgShared.helpers.taskDefaults({text: list.newTask, type: list.type}, User.user.filters);
User.user[list.type + "s"].unshift(task);
// $scope.showedTasks.unshift newTask # FIXME what's thiss?
list.tasks.unshift(task);
User.log({op: "addTask", data: task});
delete list.newTask;
};
/*Add the new task to the actions log
*/
/**
* Add the new task to the actions log
*/
$scope.clearDoneTodos = function() {};
/**
* This is calculated post-change, so task.completed=true if they just checked it
*/
$scope.changeCheck = function(task) {
/* This is calculated post-change, so task.completed=true if they just checked it
*/
if (task.completed) {
$scope.score(task, "up");
} else {
@@ -66,27 +39,67 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', '
// uhoh! our first name conflict with habitrpg-shared/helpers, we gotta resovle that soon.
$rootScope.clickRevive = function() {
window.habitrpgShared.algos.revive(User.user);
User.log({
op: "revive"
});
User.log({ op: "revive" });
};
$scope.toggleEdit = function(task){
task._editing = !task._editing;
if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false;
$scope.removeTask = function(list, $index) {
if (!confirm("Are you sure you want to delete this task?")) return;
User.log({ op: "delTask", data: list[$index] });
list.splice($index, 1);
};
$scope.remove = function(task) {
var tasks;
if (confirm("Are you sure you want to delete this task?") !== true) {
return;
$scope.saveTask = function(task) {
var setVal = function(k, v) {
var op;
if (typeof v !== "undefined") {
op = { op: "set", data: {} };
op.data["tasks." + task.id + "." + k] = v;
return log.push(op);
}
};
var log = [];
setVal("text", task.text);
setVal("notes", task.notes);
setVal("priority", task.priority);
setVal("tags", task.tags);
if (task.type === "habit") {
setVal("up", task.up);
setVal("down", task.down);
} else if (task.type === "daily") {
setVal("repeat", task.repeat);
// TODO we'll remove this once rewrite's running for a while. This was a patch for derby issues
setVal("streak", task.streak);
} else if (task.type === "todo") {
setVal("date", task.date);
} else {
if (task.type === "reward") {
setVal("value", task.value);
}
}
tasks = User.user[task.type + "s"];
User.log({
op: "delTask",
data: task
});
tasks.splice(tasks.indexOf(task), 1);
User.log(log);
task._editing = false;
};
/**
* Reset $scope.task to $scope.originalTask
*/
$scope.cancel = function() {
var key;
for (key in $scope.task) {
$scope.task[key] = $scope.originalTask[key];
}
$scope.originalTask = null;
$scope.editedTask = null;
$scope.editing = false;
};
$scope.unlink = function(task, keep) {
// TODO move this to userServices, turn userSerivces.user into ng-resource
$http.post(API_URL + '/api/v1/user/task/' + task.id + '/unlink?keep=' + keep)
.success(function(){
User.log({});
});
};
/*
@@ -123,4 +136,15 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User', '
User.log({op: 'clear-completed'});
}
/**
* See conversation on http://productforums.google.com/forum/#!topic/adsense/WYkC_VzKwbA,
* Adsense is very sensitive. It must be called once-and-only-once for every <ins>, else things break.
* Additionally, angular won't run javascript embedded into a script template, so we can't copy/paste
* the html provided by adsense - we need to run this function post-link
*/
$scope.initAds = function(){
$.getScript('//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js');
(window.adsbygoogle = window.adsbygoogle || []).push({});
}
}]);