"use strict"; habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', '$stateParams', function($rootScope, $scope, Shared, User, Challenges, Notification, $compile, Groups, $state, $stateParams) { // Use presence of cid to determine whether to show a list or a single // challenge $scope.cid = $state.params.cid; // Fetch single challenge if a cid is present; fetch multiple challenges // otherwise var getChallenges = function() { if ($scope.cid) { Challenges.Challenge.get({cid: $scope.cid}, function(challenge) { $scope.challenges = [challenge]; }); } else { Challenges.Challenge.query(function(challenges){ $scope.challenges = challenges; $scope.groupsFilter = _.uniq(_.pluck(challenges, 'group'), function(g){return g._id}); $scope.search = { group: _.transform($scope.groups, function(m,g){m[g._id]=true;}) }; }); } }; getChallenges(); // FIXME $scope.challenges needs to be resolved first (see app.js) $scope.groups = Groups.Group.query({type:'party,guilds,tavern'}); // we should fix this, that's pretty brittle // override score() for tasks listed in challenges-editing pages, so that nothing happens $scope.score = function(){} //------------------------------------------------------------ // Challenge //------------------------------------------------------------ // Use this to force the top view to change, not just the nested view. $scope.edit = function(challenge) { $state.transitionTo('options.social.challenges.edit', {cid: challenge._id}, { reload: true, inherit: false, notify: true }); }; /** * Create */ $scope.create = function() { //If the user has one filter selected, assume that the user wants to default to that group var defaultGroup; //Our filters contain all groups, but we only want groups that have atleast one challenge var groupsWithChallenges = _.uniq(_.pluck($scope.groupsFilter, '_id')); var len = groupsWithChallenges.length; var filterCount = 0; for ( var i = 0; i < len; i += 1 ) { if ( $scope.search.group[groupsWithChallenges[i]] == true ) { filterCount += 1; defaultGroup = groupsWithChallenges[i]; } if (filterCount > 1) { defaultGroup = $scope.groups[0]._id break; } } $scope.obj = $scope.newChallenge = new Challenges.Challenge({ name: '', description: '', habits: [], dailys: [], todos: [], rewards: [], leader: User.user._id, group: defaultGroup, timestamp: +(new Date), members: [], official: false }); }; /** * Save */ $scope.save = function(challenge) { if (!challenge.group) return alert(window.env.t('selectGroup')); var isNew = !challenge._id; challenge.$save(function(_challenge){ if (isNew) { Notification.text(window.env.t('challengeCreated')); $state.go('options.social.challenges.detail', {cid: _challenge._id}); $scope.discard(); $scope.challenges = Challenges.Challenge.query(); User.sync(); } else { // TODO figure out a more elegant way about this //challenge._editing = false; challenge._locked = true; getChallenges(); } }); }; /** * Discard */ $scope.discard = function() { $scope.newChallenge = null; }; /** * Close Challenge * ------------------ */ function backToChallenges(){ $scope.popoverEl.popover('destroy'); $scope.cid = null; $state.go('options.social.challenges'); $scope.challenges = Challenges.Challenge.query(); User.log({}); } $scope.cancelClosing = function(challenge) { $scope.popoverEl.popover('destroy'); $scope.popoverEl = undefined; $scope.closingChal = undefined; challenge.winner = undefined; } $scope["delete"] = function(challenge) { var warningMsg; if(challenge.group._id == 'habitrpg') { warningMsg = window.env.t('sureDelChaTavern'); } else { warningMsg = window.env.t('sureDelCha'); } if (!confirm(warningMsg)) return; challenge.$delete(function(){ $scope.popoverEl.popover('destroy'); backToChallenges(); }); }; $scope.selectWinner = function(challenge) { if (!challenge.winner) return; if (!confirm(window.env.t('youSure'))) return; challenge.$close({uid:challenge.winner}, function(){ $scope.popoverEl.popover('destroy'); backToChallenges(); }) } $scope.close = function(challenge, $event) { $scope.closingChal = challenge; $scope.popoverEl = $($event.target); var html = $compile('