diff --git a/public/js/controllers/challengesCtrl.js b/public/js/controllers/challengesCtrl.js index 636ef0cedb..8c1cd05a57 100644 --- a/public/js/controllers/challengesCtrl.js +++ b/public/js/controllers/challengesCtrl.js @@ -1,7 +1,7 @@ "use strict"; -habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', - function($rootScope, $scope, User, Challenges, Notification, $compile, Groups, $state) { +habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state', + function($rootScope, $scope, Shared, User, Challenges, Notification, $compile, Groups, $state) { // FIXME $scope.challenges needs to be resolved first (see app.js) $scope.groups = Groups.Group.query({type:'party,guilds,tavern'}); @@ -133,7 +133,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenge //------------------------------------------------------------ $scope.addTask = function(addTo, listDef) { - var task = $rootScope.Shared.taskDefaults({text: listDef.newTask, type: listDef.type}); + var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type}); addTo.unshift(task); //User.log({op: "addTask", data: task}); //TODO persist delete listDef.newTask; @@ -229,4 +229,4 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenge $scope.search.group = _.transform($scope.groups, function(m,g){m[g._id] = false}); } -}]); \ No newline at end of file +}]); diff --git a/public/js/controllers/groupsCtrl.js b/public/js/controllers/groupsCtrl.js index 23a08bb0dd..d17beab766 100644 --- a/public/js/controllers/groupsCtrl.js +++ b/public/js/controllers/groupsCtrl.js @@ -1,7 +1,7 @@ "use strict"; -habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'API_URL', '$q', 'User', 'Members', '$state', - function($scope, $rootScope, Groups, $http, API_URL, $q, User, Members, $state) { +habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '$http', 'API_URL', '$q', 'User', 'Members', '$state', + function($scope, $rootScope, Shared, Groups, $http, API_URL, $q, User, Members, $state) { $scope.isMember = function(user, group){ return ~(group.members.indexOf(user._id)); @@ -66,7 +66,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A // We watch Members.selectedMember because it's asynchronously set, so would be a hassle to handle updates here $scope.$watch( function() { return Members.selectedMember; }, function (member) { if(member) - member.petCount = $rootScope.Shared.countPets(null, member.items.pets); + member.petCount = Shared.countPets(null, member.items.pets); $scope.profile = member; }); } diff --git a/public/js/controllers/inventoryCtrl.js b/public/js/controllers/inventoryCtrl.js index 212cf3513b..cc4ed11448 100644 --- a/public/js/controllers/inventoryCtrl.js +++ b/public/js/controllers/inventoryCtrl.js @@ -1,8 +1,7 @@ -habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User', - function($rootScope, $scope, $window, User) { +habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User', 'Content', + function($rootScope, $scope, $window, User, Content) { var user = User.user; - var Content = $rootScope.Content; // convenience vars since these are accessed frequently @@ -135,4 +134,4 @@ habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User', $scope.closeQuest(); } } -]); \ No newline at end of file +]); diff --git a/public/js/controllers/notificationCtrl.js b/public/js/controllers/notificationCtrl.js index 2ba427ab11..a93deb9b1a 100644 --- a/public/js/controllers/notificationCtrl.js +++ b/public/js/controllers/notificationCtrl.js @@ -1,7 +1,7 @@ 'use strict'; habitrpg.controller('NotificationCtrl', - ['$scope', '$rootScope', 'User', 'Guide', 'Notification', function ($scope, $rootScope, User, Guide, Notification) { + ['$scope', '$rootScope', 'Shared', 'User', 'Guide', 'Notification', function ($scope, $rootScope, Shared, User, Guide, Notification) { $rootScope.$watch('user.stats.hp', function(after, before) { if (after == before) return; @@ -71,7 +71,7 @@ habitrpg.controller('NotificationCtrl', $rootScope.$watch('user.items.pets', function(after, before){ if(_.size(after) === _.size(before) || - $rootScope.Shared.countPets(null, after) < 90) return; + Shared.countPets(null, after) < 90) return; User.user.achievements.beastMaster = true; $rootScope.modals.achievements.beastMaster = true; }, true); diff --git a/public/js/controllers/rootCtrl.js b/public/js/controllers/rootCtrl.js index dd652eee74..7b08e0aab8 100644 --- a/public/js/controllers/rootCtrl.js +++ b/public/js/controllers/rootCtrl.js @@ -3,8 +3,8 @@ /* Make user and settings available for everyone through root scope. */ -habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http', '$state', '$stateParams', 'Notification', 'Groups', - function($scope, $rootScope, $location, User, $http, $state, $stateParams, Notification, Groups) { +habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http', '$state', '$stateParams', 'Notification', 'Groups', 'Shared', 'Content', + function($scope, $rootScope, $location, User, $http, $state, $stateParams, Notification, Groups, Shared, Content) { var user = User.user; var initSticky = _.once(function(){ @@ -20,8 +20,8 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ $rootScope.moment = window.moment; $rootScope._ = window._; $rootScope.settings = User.settings; - $rootScope.Shared = window.habitrpgShared; - $rootScope.Content = window.habitrpgShared.content; + $rootScope.Shared = Shared; + $rootScope.Content = Content; // Angular UI Router $rootScope.$state = $state; @@ -45,10 +45,10 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ // count pets, mounts collected totals, etc $rootScope.countExists = function(items) {return _.reduce(items,function(m,v){return m+(v?1:0)},0)} - $rootScope.petCount = $rootScope.Shared.countPets(null, User.user.items.pets); + $rootScope.petCount = Shared.countPets(null, User.user.items.pets); $rootScope.$watch('user.items.pets', function(pets){ - $rootScope.petCount = $rootScope.Shared.countPets($rootScope.countExists(pets), User.user.items.pets); + $rootScope.petCount = Shared.countPets($rootScope.countExists(pets), User.user.items.pets); }, true); $scope.safeApply = function(fn) { diff --git a/public/js/controllers/tasksCtrl.js b/public/js/controllers/tasksCtrl.js index cf234fbb3c..5dd8fc9369 100644 --- a/public/js/controllers/tasksCtrl.js +++ b/public/js/controllers/tasksCtrl.js @@ -1,7 +1,7 @@ "use strict"; -habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'API_URL', '$timeout', - function($scope, $rootScope, $location, User, Notification, $http, API_URL, $timeout) { +habitrpg.controller("TasksCtrl", ['$scope', '$location', 'User','Notification', '$http', 'API_URL', '$timeout', 'Shared', + function($scope, $location, User, Notification, $http, API_URL, $timeout, Shared) { $scope.obj = User.user; // used for task-lists $scope.user = User.user; @@ -138,7 +138,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N */ $scope.$watch('user.items.gear.equipped', function(){ - $scope.itemStore = $rootScope.Shared.updateStore(User.user); + $scope.itemStore = Shared.updateStore(User.user); },true); $scope.buy = function(item) { diff --git a/public/js/controllers/userCtrl.js b/public/js/controllers/userCtrl.js index 0f117ffaee..ce3c629d82 100644 --- a/public/js/controllers/userCtrl.js +++ b/public/js/controllers/userCtrl.js @@ -1,9 +1,9 @@ "use strict"; -habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$http', '$state', 'Guide', - function($rootScope, $scope, $location, User, $http, $state, Guide) { +habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$http', '$state', 'Guide', 'Shared', + function($rootScope, $scope, $location, User, $http, $state, Guide, Shared) { $scope.profile = User.user; - $scope.profile.petCount = $rootScope.Shared.countPets(null, $scope.profile.items.pets); + $scope.profile.petCount = Shared.countPets(null, $scope.profile.items.pets); $scope.hideUserAvatar = function() { $(".userAvatar").hide(); }; @@ -25,7 +25,7 @@ habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$ User.user.ops.changeClass({query:{class:klass}}); $scope.selectedClass = undefined; - $rootScope.Shared.updateStore(User.user); + Shared.updateStore(User.user); $state.go('options.profile.stats'); window.setTimeout(Guide.classesTour, 10); } diff --git a/public/js/services/memberServices.js b/public/js/services/memberServices.js index 73a9c6f663..29384f4cab 100644 --- a/public/js/services/memberServices.js +++ b/public/js/services/memberServices.js @@ -4,9 +4,9 @@ * Services that persists and retrieves user from localStorage. */ -angular.module('memberServices', ['ngResource']). - factory('Members', ['$rootScope', 'API_URL', '$resource', - function($rootScope, API_URL, $resource) { +angular.module('memberServices', ['ngResource', 'sharedServices']). + factory('Members', ['$rootScope', 'Shared', 'API_URL', '$resource', + function($rootScope, Shared, API_URL, $resource) { var members = {}; var Member = $resource(API_URL + '/api/v2/members/:uid', {uid:'@_id'}); var memberServices = { @@ -65,12 +65,12 @@ angular.module('memberServices', ['ngResource']). // and then for guild) // and if not, fetch them if (members[uid] && members[uid].items && members[uid].items.weapon) { - $rootScope.Shared.wrap(members[uid],false); + Shared.wrap(members[uid],false); self.selectedMember = members[uid]; } else { Member.get({uid: uid}, function(member){ self.populate(member); // lazy load for later - $rootScope.Shared.wrap(member,false); + Shared.wrap(member,false); self.selectedMember = members[member._id]; }); } diff --git a/public/js/services/sharedServices.js b/public/js/services/sharedServices.js new file mode 100644 index 0000000000..31ef0fc120 --- /dev/null +++ b/public/js/services/sharedServices.js @@ -0,0 +1,15 @@ +'use strict'; + +/** + * Services that expose habitrpg-shared + */ + +angular.module('sharedServices', []). + factory('Shared', [function () { + return window.habitrpgShared; + } +]). + factory('Content', ['Shared', function (Shared) { + return Shared.content; + } +]); diff --git a/public/manifest.json b/public/manifest.json index 52f6a5951f..3a4025e4d0 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -32,6 +32,7 @@ "bower_components/habitrpg-shared/dist/habitrpg-shared.js", "js/app.js", + "js/services/sharedServices.js", "js/services/authServices.js", "js/services/notificationServices.js", "bower_components/habitrpg-shared/script/userServices.js", @@ -87,4 +88,4 @@ "static.css" ] } -} \ No newline at end of file +} diff --git a/test/spec/filtersCtrlSpec.js b/test/spec/filtersCtrlSpec.js index c6a7243e1a..d31855abc2 100644 --- a/test/spec/filtersCtrlSpec.js +++ b/test/spec/filtersCtrlSpec.js @@ -4,9 +4,9 @@ describe('Filters Controller', function() { var scope, user; beforeEach(module('habitrpg')); - beforeEach(inject(function($rootScope, $controller) { + beforeEach(inject(function($rootScope, $controller, Shared) { user = {filters: {}}; - window.habitrpgShared.wrap(user); + Shared.wrap(user); scope = $rootScope.$new(); $controller('FiltersCtrl', {$scope: scope, User: {user: user}}); })); @@ -18,11 +18,11 @@ describe('Filters Controller', function() { expect(user.tags[0]).to.have.property('id'); }); - it('toggles tag filtering', function(){ - var tag = {id: window.habitrpgShared.uuid(), name: 'myTag'}; + it('toggles tag filtering', inject(function(Shared){ + var tag = {id: Shared.uuid(), name: 'myTag'}; scope.toggleFilter(tag); expect(user.filters[tag.id]).to.eql(true); scope.toggleFilter(tag); expect(user.filters[tag.id]).to.eql(false); - }) -}); \ No newline at end of file + })) +}); diff --git a/test/spec/inventoryCtrlSpec.js b/test/spec/inventoryCtrlSpec.js index 9cbf9334ff..b4f2df12f6 100644 --- a/test/spec/inventoryCtrlSpec.js +++ b/test/spec/inventoryCtrlSpec.js @@ -4,20 +4,19 @@ describe('Inventory Controller', function() { var scope, ctrl, user, $rootScope; beforeEach(module('habitrpg')); - beforeEach(inject(function($rootScope, $controller){ + beforeEach(inject(function($rootScope, $controller, Shared){ user = { balance: 4, stats: {gp: 0}, items: {eggs: {Cactus: 1}, hatchingPotions: {Base: 1}, food: {Meat: 1}, pets: {}}, }; - window.habitrpgShared.wrap(user); + Shared.wrap(user); var mockWindow = { confirm: function(msg){ return true; } }; scope = $rootScope.$new(); - $rootScope.Content = window.habitrpgShared.content; ctrl = $controller('InventoryCtrl', {$scope: scope, User: {user: user}, $window: mockWindow}); })); @@ -74,9 +73,9 @@ describe('Inventory Controller', function() { expect(user.items.currentPet).to.eql('Cactus-Base'); }); - it('purchases an egg', function(){ - scope.purchase('eggs', window.habitrpgShared.content.eggs['Wolf']); + it('purchases an egg', inject(function(Content){ + scope.purchase('eggs', Content.eggs['Wolf']); expect(user.balance).to.eql(3.25); expect(user.items.eggs).to.eql({Cactus: 1, Wolf: 1}) - }); + })); }); diff --git a/test/spec/memberServicesSpec.js b/test/spec/memberServicesSpec.js index 394b0809b4..09db1ce698 100644 --- a/test/spec/memberServicesSpec.js +++ b/test/spec/memberServicesSpec.js @@ -6,16 +6,10 @@ describe('memberServices', function() { beforeEach(module('memberServices')); beforeEach(module('habitrpg')); - beforeEach(function(){ - inject(function(_$httpBackend_, $rootScope){ - $httpBackend = _$httpBackend_; - $rootScope.Shared = window.habitrpgShared; - }); - // $rootScope.Shared is set, so now we can inject Members - inject(function(Members){ - members = Members; - }); - }); + beforeEach(inject(function (_$httpBackend_, Members) { + $httpBackend = _$httpBackend_; + members = Members; + })); it('has no members at the beginning', function() { expect(members.members).to.be.an('object'); @@ -43,4 +37,4 @@ describe('memberServices', function() { expect(members.members).to.have.property(uid); }); -}); \ No newline at end of file +}); diff --git a/test/spec/userServicesSpec.js b/test/spec/userServicesSpec.js index 9fea953a3b..9770befb26 100644 --- a/test/spec/userServicesSpec.js +++ b/test/spec/userServicesSpec.js @@ -8,8 +8,7 @@ describe('userServices', function() { beforeEach(function(){ module(function($provide){ - var habitrpgShared = {helpers: {newUser: sinon.spy()}}; - $window = {href: '', alert: sinon.spy(), location: {search: '', pathname: ''}, habitrpgShared: habitrpgShared}; + $window = {href: '', alert: sinon.spy(), location: {search: '', pathname: ''}}; $provide.value('$window', $window); }); @@ -48,4 +47,4 @@ describe('userServices', function() { //TODO where does that null comes from? expect(user.settings.sync.queue).to.eql([null, {}]); }); -}); \ No newline at end of file +});