mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Remove references to $rootScope.Shared & .Content in non-view code.
Instead, have them request services that return them so tests don’t need to prepare special state.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
});
|
||||
}
|
||||
|
||||
15
public/js/services/sharedServices.js
Normal file
15
public/js/services/sharedServices.js
Normal file
@@ -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;
|
||||
}
|
||||
]);
|
||||
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
}))
|
||||
});
|
||||
@@ -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})
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -6,16 +6,10 @@ describe('memberServices', function() {
|
||||
beforeEach(module('memberServices'));
|
||||
beforeEach(module('habitrpg'));
|
||||
|
||||
beforeEach(function(){
|
||||
inject(function(_$httpBackend_, $rootScope){
|
||||
beforeEach(inject(function (_$httpBackend_, Members) {
|
||||
$httpBackend = _$httpBackend_;
|
||||
$rootScope.Shared = window.habitrpgShared;
|
||||
});
|
||||
// $rootScope.Shared is set, so now we can inject Members
|
||||
inject(function(Members){
|
||||
members = Members;
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('has no members at the beginning', function() {
|
||||
expect(members.members).to.be.an('object');
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user