WIP(party-page): Refactor groups service

This commit is contained in:
Sabe Jones
2015-08-14 12:58:46 -05:00
parent 0f31873262
commit 572e64e3ed
14 changed files with 2843 additions and 2821 deletions
File diff suppressed because one or more lines are too long
+836 -836
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

+665 -653
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 109 KiB

+399 -393
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 550 KiB

After

Width:  |  Height:  |  Size: 554 KiB

+418 -418
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 235 KiB

After

Width:  |  Height:  |  Size: 235 KiB

+411 -405
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 KiB

After

Width:  |  Height:  |  Size: 202 KiB

+28 -51
View File
@@ -7,13 +7,13 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
if (!group.quest || !group.quest.members) return false;
if (group.quest.active) return false; // quest is started, not pending
return userid in group.quest.members && group.quest.members[userid] != false;
}
};
$scope.isMemberOfRunningQuest = function(userid, group) {
if (!group.quest || !group.quest.members) return false;
if (!group.quest.active) return false; // quest is pending, not started
return group.quest.members[userid];
}
};
$scope.isMemberOfGroup = function(userid, group){
@@ -32,11 +32,11 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
if (!group.members) return false;
var memberIds = _.map(group.members, function(x){return x._id});
return ~(memberIds.indexOf(userid));
}
};
$scope.isMember = function(user, group){
return ~(group.members.indexOf(user._id));
}
};
$scope.Members = Members;
$scope._editing = {group:false};
@@ -45,13 +45,13 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
if(group._newLeader && group._newLeader._id) group.leader = group._newLeader._id;
group.$save();
group._editing = false;
}
};
$scope.deleteAllMessages = function() {
if (confirm(window.env.t('confirmDeleteAllMessages'))) {
User.user.ops.clearPMs({});
}
}
};
// ------ Modals ------
@@ -69,7 +69,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
$rootScope.openModal('member', {controller:'MemberModalCtrl', windowClass:'profile-modal', size:'lg'});
});
}
}
};
$scope.removeMember = function(group, member, isMember){
@@ -80,7 +80,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
isMember: isMember
};
$rootScope.openModal('remove-member', {scope: $scope});
}
};
$scope.confirmRemoveMember = function(confirm){
if(confirm){
@@ -100,7 +100,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
}else{
$scope.removeMemberData = undefined;
}
}
};
$scope.openInviteModal = function(group){
$rootScope.openModal('invite-friends', {controller:'InviteToGroupCtrl', resolve:
@@ -109,23 +109,6 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
}}});
};
//var serializeQs = function(obj, prefix){
// var str = [];
// for(var p in obj) {
// if (obj.hasOwnProperty(p)) {
// var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
// str.push(typeof v == "object" ?
// serializeQs(v, k) :
// encodeURIComponent(k) + "=" + encodeURIComponent(v));
// }
// }
// return str.join("&");
//}
//
//$scope.inviteLink = function(obj){
// return window.env.BASE_URL + '?' + serializeQs({partyInvite: obj});
//}
$scope.quickReply = function(uid) {
Members.selectMember(uid, function(){
$rootScope.openModal('private-message',{controller:'MemberModalCtrl'});
@@ -141,45 +124,39 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '
$scope.emails = [{name:"",email:""},{name:"",email:""}];
$scope.invitees = {uuid:""};
$scope.inviteNewUsers = function(inviteMethod){
if (!$scope.group) {
Groups.group.create($scope.newGroup, function() {
inviteByMethod(inviteMethod);
$scope.inviteNewUsers = function(inviteMethod) {
if (!$scope.group._id) {
group.create($scope.newGroup, function() {
_inviteByMethod(inviteMethod);
});
} else {
inviteByMethod(inviteMethod);
_inviteByMethod(inviteMethod);
}
};
var inviteByMethod = function(inviteMethod) {
function _inviteByMethod(inviteMethod) {
if (inviteMethod === 'email') {
inviteEmails();
Groups.Group.invite({gid: $scope.group._id}, {inviter: $scope.inviter, emails: $scope.emails}, function(){
Notification.text(window.env.t('invitationsSent'));
$scope.emails = [{name:'',email:''},{name:'',email:''}];
}, function(){
$scope.emails = [{name:'',email:''},{name:'',email:''}];
});
}
else if (inviteMethod === 'uuid') {
invite();
Groups.Group.invite({gid: $scope.group._id}, {uuids: [$scope.invitees.uuid]}, function(){
Notification.text(window.env.t('invitationsSent'));
$scope.invitees = {uuid:""};
}, function(){
$scope.invitees = {uuid:""};
});
}
else {
return console.log('Invalid invite method.')
}
};
}
$scope.inviteEmails = function(){
Groups.Group.invite({gid: $scope.group._id}, {inviter: $scope.inviter, emails: $scope.emails}, function(){
Notification.text(window.env.t('invitationsSent'));
$scope.emails = [{name:'',email:''},{name:'',email:''}];
}, function(){
$scope.emails = [{name:'',email:''},{name:'',email:''}];
});
};
$scope.invite = function(){
Groups.Group.invite({gid: $scope.group._id}, {uuids: [$scope.invitees.uuid]}, function(){
Notification.text(window.env.t('invitationsSent'));
$scope.invitees = {uuid:""};
}, function(){
$scope.invitees = {uuid:""};
});
};
}])
.controller('AutocompleteCtrl', ['$scope', '$timeout', 'Groups', 'User', 'InputCaret', function ($scope,$timeout,Groups,User,InputCaret) {
+84 -63
View File
@@ -1,93 +1,104 @@
'use strict';
/**
* Services that persists and retrieves user from localStorage.
*/
(function() {
angular
.module('habitrpg')
.factory('Groups', groupsFactory);
angular.module('habitrpg').factory('Groups',
['$rootScope','ApiUrl', '$resource', '$q', '$http', 'User', 'Challenges', 'Analytics', '$location',
function($rootScope, ApiUrl, $resource, $q, $http, User, Challenges, Analytics, $location) {
var Group = $resource(ApiUrl.get() + '/api/v2/groups/:gid',
{gid:'@_id', messageId: '@_messageId'},
{
get: {
method: "GET",
isArray:false,
// Wrap challenges as ngResource so they have functions like $leave or $join
transformResponse: function(data, headers) {
data = angular.fromJson(data);
_.each(data && data.challenges, function(c) {
angular.extend(c, Challenges.Challenge.prototype);
});
return data;
}
},
groupsFactory.$inject = [
'$location',
'$resource',
'$rootScope',
'Analytics',
'ApiUrl',
'Challenges',
'User'
];
syncParty: {method: "GET", url: '/api/v2/groups/party'},
join: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/join'},
leave: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/leave'},
invite: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/invite'},
removeMember: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/removeMember'},
questAccept: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questAccept'},
questReject: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questReject'},
questCancel: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questCancel'},
questAbort: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questAbort'}
});
function groupsFactory($location, $resource, $rootScope, Analytics, ApiUrl, Challenges, User) {
// Defer loading everything until they're requested
var data = {party: undefined, myGuilds: undefined, publicGuilds: undefined, tavern: undefined};
var data = {party: undefined, myGuilds: undefined, publicGuilds: undefined, tavern: undefined};
var Group = $resource(ApiUrl.get() + '/api/v2/groups/:gid',
{gid:'@_id', messageId: '@_messageId'},
{
get: {
method: "GET",
isArray:false,
// Wrap challenges as ngResource so they have functions like $leave or $join
transformResponse: function(data) {
data = angular.fromJson(data);
_.each(data && data.challenges, function(c) {
angular.extend(c, Challenges.Challenge.prototype);
});
return data;
}
},
var syncUser = function(res) {
User.sync();
}
var logError = function(err) {
console.log(err);
}
syncParty: {method: "GET", url: '/api/v2/groups/party'},
join: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/join'},
leave: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/leave'},
invite: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/invite'},
removeMember: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/removeMember'},
questAccept: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questAccept'},
questReject: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questReject'},
questCancel: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questCancel'},
questAbort: {method: "POST", url: ApiUrl.get() + '/api/v2/groups/:gid/questAbort'}
});
return {
party: function(cb){
function syncUser() {
User.sync();
}
function logError(err) {
console.log(err);
}
function party(cb) {
if (!data.party) return (data.party = Group.get({gid: 'party'}, cb));
return (cb) ? cb(party) : data.party;
},
publicGuilds: function(){
}
function publicGuilds() {
//TODO combine these as {type:'guilds,public'} and create a $filter() to separate them
if (!data.publicGuilds) data.publicGuilds = Group.query({type:'public'});
return data.publicGuilds;
},
myGuilds: function(){
}
function myGuilds() {
if (!data.myGuilds) data.myGuilds = Group.query({type:'guilds'});
return data.myGuilds;
},
tavern: function(){
}
function tavern() {
if (!data.tavern) data.tavern = Group.get({gid:'habitrpg'});
return data.tavern;
},
}
questAccept: function(party){
function questAccept(party) {
Analytics.updateUser({'partyID':party.id,'partySize':party.memberCount});
party.$questAccept()
.then(syncUser, logError);
},
}
questReject: function(party){
function questReject(party) {
Analytics.updateUser({'partyID':party.id,'partySize':party.memberCount});
party.$questReject()
.then(syncUser, logError);
},
}
questCancel: function(party){
function questCancel(party) {
Analytics.updateUser({'partyID':party.id,'partySize':party.memberCount});
party.$questCancel()
.then(syncUser, logError);
},
}
questAbort: function(party){
function questAbort(party) {
Analytics.updateUser({'partyID':party.id,'partySize':party.memberCount});
party.$questAbort()
.then(syncUser, logError);
},
}
inviteOrStartParty: function(group) {
function inviteOrStartParty(group) {
if (group.type === "party" || $location.$$path === "/options/groups/party") {
$rootScope.openModal('invite-friends', {
controller:'InviteToGroupCtrl',
@@ -99,12 +110,22 @@ function($rootScope, ApiUrl, $resource, $q, $http, User, Challenges, Analytics,
Analytics.track({'hitType':'event','eventCategory':'button','eventAction':'click','eventLabel':'Invite Friends'});
$location.path("/options/groups/party");
}
},
}
// Pass reference to party, myGuilds, publicGuilds, tavern; inside data in order to
// be able to modify them directly (otherwise will be stick with cached version)
data: data,
return {
syncUser: syncUser,
logError: logError,
party: party,
publicGuilds: publicGuilds,
myGuilds: myGuilds,
tavern: tavern,
questAccept: questAccept,
questReject: questReject,
questCancel: questCancel,
inviteOrStartParty: inviteOrStartParty,
Group: Group
data: data,
Group: Group
}
}
}])
})();
@@ -50,7 +50,7 @@ script(type='text/ng-template', id='modals/invite-friends.html')
.quest_basilist.pull-left
p(style='margin:2em')=env.t('exclusiveQuestScroll')
button.btn.btn-default.pull-right(style='margin:auto 1em', ng-click='$close()')=env.t('close')
.modal-footer(style='padding-bottom:0',ng-if='!group || group.memberCount === 1')
.modal-footer(style='padding-bottom:0',ng-if='!group._id || group.memberCount < 2')
p!=env.t('wantExistingParty', {linkStart: "<a href='/#/options/groups/guilds/f2db2a7f-13c5-454d-b3ee-ea1f5089e601'>", linkEnd: "</a>"})
pre(style='margin:1em 0 0 0').
{{user.id}}