From 3ab27f32d26c53e94b3d29ebb3c17eeb32ba356d Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 16 Jun 2015 23:11:04 -0500 Subject: [PATCH] Refactored editTask into TaskService --- karma.conf.js | 1 + test/spec/controllers/challengesCtrlSpec.js | 16 ++++ test/spec/controllers/tasksCtrlSpec.js | 39 +++++++++ test/spec/services/taskServicesSpec.js | 82 +++++++++++++++++++ .../public/js/controllers/challengesCtrl.js | 8 +- website/public/js/controllers/tasksCtrl.js | 8 +- .../js/directives/habitrpg-tasks.directive.js | 8 -- website/public/js/services/taskServices.js | 24 ++++++ website/public/manifest.json | 1 + website/views/shared/tasks/edit/tags.jade | 2 +- 10 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 test/spec/controllers/tasksCtrlSpec.js create mode 100644 test/spec/services/taskServicesSpec.js create mode 100644 website/public/js/services/taskServices.js diff --git a/karma.conf.js b/karma.conf.js index a4ee7a1296..fcf3f9eb31 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -49,6 +49,7 @@ module.exports = function(config) { "website/public/js/services/memberServices.js", "website/public/js/services/guideServices.js", "website/public/js/services/challengeServices.js", + "website/public/js/services/taskServices.js", "website/public/js/services/paymentServices.js", "website/public/js/filters/money.js", diff --git a/test/spec/controllers/challengesCtrlSpec.js b/test/spec/controllers/challengesCtrlSpec.js index 417095c152..05d921ff7e 100644 --- a/test/spec/controllers/challengesCtrlSpec.js +++ b/test/spec/controllers/challengesCtrlSpec.js @@ -174,4 +174,20 @@ describe('Challenges Controller', function() { expect(scope.filterChallenges(notOwnNotMem)).to.eql(true); }); }); + + describe('editTask', function() { + it('calls Tasks.editTask', function() { + inject(function(Tasks) { + sinon.stub(Tasks, 'editTask'); + var task = { + id: 'task-id', + type: 'todo' + }; + + scope.editTask(task); + expect(Tasks.editTask).to.be.calledOnce; + expect(Tasks.editTask).to.be.calledWith(task); + }); + }); + }); }); diff --git a/test/spec/controllers/tasksCtrlSpec.js b/test/spec/controllers/tasksCtrlSpec.js new file mode 100644 index 0000000000..c535b232e7 --- /dev/null +++ b/test/spec/controllers/tasksCtrlSpec.js @@ -0,0 +1,39 @@ +'use strict'; + +describe('Tasks Controller', function() { + var $rootScope, scope, user, ctrl; + + beforeEach(function() { + user = specHelper.newUser(); + module(function($provide) { + $provide.value('User', {user: user}); + $provide.value('Guide', {}); + }); + + inject(function($rootScope, $controller){ + + scope = $rootScope.$new(); + + $controller('RootCtrl', {$scope: scope, User: {user: user}}); + + ctrl = $controller('TasksCtrl', {$scope: scope, User: {user: user}}); + + }); + }); + + describe('editTask', function() { + it('calls Tasks.editTask', function() { + inject(function(Tasks) { + sinon.stub(Tasks, 'editTask'); + var task = { + id: 'task-id', + type: 'todo' + }; + + scope.editTask(task); + expect(Tasks.editTask).to.be.calledOnce; + expect(Tasks.editTask).to.be.calledWith(task); + }); + }); + }); +}); diff --git a/test/spec/services/taskServicesSpec.js b/test/spec/services/taskServicesSpec.js new file mode 100644 index 0000000000..b2a43d848b --- /dev/null +++ b/test/spec/services/taskServicesSpec.js @@ -0,0 +1,82 @@ +'use strict'; + +describe('Tasks Service', function() { + var rootScope, tasks, user; + + beforeEach(function() { + + module(function($provide) { + user = specHelper.newUser(); + $provide.value('User', {user: user}); + }); + + inject(function(_$rootScope_, Tasks, User) { + rootScope = _$rootScope_; + rootScope.charts = {}; + tasks = Tasks; + }); + }); + + describe('editTask', function() { + + var task; + + beforeEach(function(){ + task = { id: 'task-id' }; // @TODO: replace with task factory + }); + + it('opens the edit menu if it is not already open', function() { + tasks.editTask(task); + + expect(task._editing).to.eql(true); + }); + + it('closes the edit menu if it is not already open', function() { + task._editing = true; + tasks.editTask(task); + + expect(task._editing).to.eql(false); + }); + + it('shows tags by default', function() { + tasks.editTask(task); + + expect(task._tags).to.eql(true); + }); + + it('does not show tags if tagsCollapsed preference is enabled', function() { + user.preferences.tagsCollapsed = true; + tasks.editTask(task); + + expect(task._tags).to.eql(false); + }); + + it('closes the edit menu if it is not already open', function() { + task._editing = true; + tasks.editTask(task); + + expect(task._editing).to.eql(false); + }); + + it('does not open open advanced options automatically if user preference is set to collapsed', function(){ + user.preferences.advancedCollapsed = true; + tasks.editTask(task); + + expect(task._advanced).to.eql(false); + }); + + it('does open open advanced options automatically if user preference is set to not collapsed', function(){ + user.preferences.advancedCollapsed = false; + tasks.editTask(task); + + expect(task._advanced).to.eql(true); + }); + + it('closes task chart if it exists', function() { + rootScope.charts[task.id] = true; + + tasks.editTask(task); + expect(rootScope.charts[task.id]).to.eql(false); + }); + }); +}); diff --git a/website/public/js/controllers/challengesCtrl.js b/website/public/js/controllers/challengesCtrl.js index 770d736803..2f9bc84c0e 100644 --- a/website/public/js/controllers/challengesCtrl.js +++ b/website/public/js/controllers/challengesCtrl.js @@ -1,7 +1,7 @@ "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) { +habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', '$stateParams', 'Tasks', + function($rootScope, $scope, Shared, User, Challenges, Notification, $compile, Groups, $state, $stateParams, Tasks) { // Use presence of cid to determine whether to show a list or a single // challenge @@ -49,6 +49,10 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', }); }; + $scope.editTask = function(task) { + Tasks.editTask(task); + }; + /** * Create */ diff --git a/website/public/js/controllers/tasksCtrl.js b/website/public/js/controllers/tasksCtrl.js index 634847ad1a..58e109507d 100644 --- a/website/public/js/controllers/tasksCtrl.js +++ b/website/public/js/controllers/tasksCtrl.js @@ -1,7 +1,7 @@ "use strict"; -habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'ApiUrl', '$timeout', 'Shared', 'Guide', - function($scope, $rootScope, $location, User, Notification, $http, ApiUrl, $timeout, Shared, Guide) { +habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'ApiUrl', '$timeout', 'Shared', 'Guide', 'Tasks', + function($scope, $rootScope, $location, User, Notification, $http, ApiUrl, $timeout, Shared, Guide, Tasks) { $scope.obj = User.user; // used for task-lists $scope.user = User.user; @@ -65,6 +65,10 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N list.focus = true; }; + $scope.editTask = function(task) { + Tasks.editTask(task); + }; + /** * Add the new task to the actions log */ diff --git a/website/public/js/directives/habitrpg-tasks.directive.js b/website/public/js/directives/habitrpg-tasks.directive.js index d14aa87c4a..10aa7f424c 100644 --- a/website/public/js/directives/habitrpg-tasks.directive.js +++ b/website/public/js/directives/habitrpg-tasks.directive.js @@ -18,14 +18,6 @@ function habitrpgTasks($rootScope, User) { // main: '@', // true if it's the user's main list // obj: '=' //}, - controller: ['$scope', '$rootScope', function($scope, $rootScope){ - $scope.editTask = function(task){ - task._editing = !task._editing; - task._tags = User.user.preferences.tagsCollapsed; - task._advanced = !User.user.preferences.advancedCollapsed; - if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false; - }; - }], link: function(scope, element, attrs) { // $scope.obj needs to come from controllers, so we can pass by ref scope.main = attrs.main; diff --git a/website/public/js/services/taskServices.js b/website/public/js/services/taskServices.js new file mode 100644 index 0000000000..ac13b70641 --- /dev/null +++ b/website/public/js/services/taskServices.js @@ -0,0 +1,24 @@ +'use strict'; + +angular + .module('habitrpg') + .factory('Tasks', tasksFactory); + +tasksFactory.$inject = [ + '$rootScope', + 'User' +]; + +function tasksFactory($rootScope, User) { + + function editTask(task) { + task._editing = !task._editing; + task._tags = !User.user.preferences.tagsCollapsed; + task._advanced = !User.user.preferences.advancedCollapsed; + if($rootScope.charts[task.id]) $rootScope.charts[task.id] = false; + } + + return { + editTask: editTask + }; +} diff --git a/website/public/manifest.json b/website/public/manifest.json index d7afa0e961..73b7299a82 100644 --- a/website/public/manifest.json +++ b/website/public/manifest.json @@ -46,6 +46,7 @@ "js/services/groupServices.js", "js/services/memberServices.js", "js/services/guideServices.js", + "js/services/taskServices.js", "js/services/challengeServices.js", "js/services/paymentServices.js", diff --git a/website/views/shared/tasks/edit/tags.jade b/website/views/shared/tasks/edit/tags.jade index ab512731fd..697eae67c0 100644 --- a/website/views/shared/tasks/edit/tags.jade +++ b/website/views/shared/tasks/edit/tags.jade @@ -1,5 +1,5 @@ fieldset.option-group(ng-if='!$state.includes("options.social.challenges")') p.option-title.mega(ng-click='task._tags = !task._tags', tooltip=env.t('expandCollapse'))=env.t('tags') - label.checkbox(ng-repeat='tag in user.tags', ng-class="{visuallyhidden: task._tags}") + label.checkbox(ng-repeat='tag in user.tags', ng-if='task._tags') input(type='checkbox', ng-model='task.tags[tag.id]') markdown(text='tag.name')