Bläddra i källkod

Feature: add account switch

febbweiss 10 år sedan
förälder
incheckning
69570d8702

+ 1 - 1
karma.conf.js

@@ -37,7 +37,7 @@ module.exports = function(config) {
     // preprocess matching files before serving them to the browser
     // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
     preprocessors: {
-      'public/js/**/*.js': ['coverage'],
+      'public/js/**/!(global.controller).js': ['coverage'],
       'public/**/*.controller.js': ['coverage'],
     },
 

+ 18 - 7
public/account/account.controller.js

@@ -12,7 +12,7 @@
               if( !input ) {
                   return '';
               }
-            var category = categories.filter(function(elt, idx) {
+            var category = categories.filter(function(elt) {
                     return elt._id === input;
                 });
             if( category.length > 0 ) {
@@ -28,7 +28,7 @@
                 return '';
             }
             
-            var category = categories.filter(function(elt, idx) {
+            var category = categories.filter(function(elt) {
                 return elt._id === category_id;
             })[0];
             
@@ -36,7 +36,7 @@
                 return ''; 
             }
 
-            var res = category.sub_categories.filter( function(elt, idx) {
+            var res = category.sub_categories.filter( function(elt) {
                             return elt._id === input;
                         });        
             if( res.length === 1 ) {
@@ -47,9 +47,9 @@
           };
     }
         
-    AccountController.$inject = ['$scope', '$location', '$routeParams', 'FlashService', 'AccountService'];
+    AccountController.$inject = ['$scope', '$rootScope', '$routeParams', 'FlashService', 'AccountsService', 'AccountService'];
     
-    function AccountController($scope, $location, $routeParams, FlashService, AccountService) {
+    function AccountController($scope, $rootScope, $routeParams, FlashService, AccountsService, AccountService) {
         var vm = this;
         
         $scope.calendar = {
@@ -62,13 +62,14 @@
                 $scope.calendar.opened[which] = true;
             } 
         };
-
+        
         vm.dataLoading = false;
+        vm.accounts = undefined;
+        vm.account = undefined;
         vm.entries = [];
         vm.balance = undefined;
         vm.categories = [];
         vm.sub_categories = [];
-        vm.account = undefined;
         vm.create = create;
         vm.drop = drop;
         vm.edit = edit;
@@ -77,6 +78,8 @@
         vm.disabledSubCategories = false;
         vm.edit_sub_categories = [];
         
+        $rootScope.current_account = $routeParams.account_id;
+        
         (function init() {
             vm.dataLoading = true;
             AccountService.details($routeParams.account_id)
@@ -99,6 +102,14 @@
                        FlashService.error(response.message);
                    }
                 });
+            AccountsService.list()
+                .then(function(response) {
+                    if( response.success ) {
+                        $rootScope.accounts = response.accounts
+                    } else {
+                        FlashService.error(response.message);
+                    }
+                });
         })();
         
         function create() {

+ 2 - 2
public/accounts/accounts.controller.js

@@ -5,9 +5,9 @@
         .module('cloudbudget')
         .controller('AccountsController', AccountsController);
         
-    AccountsController.$inject = ['$scope', '$location', '$rootScope', 'FlashService', 'AccountsService'];
+    AccountsController.$inject = ['$scope', '$location', 'FlashService', 'AccountsService'];
     
-    function AccountsController($scope, $location, $rootScope, FlashService, AccountsService) {
+    function AccountsController($scope, $location, FlashService, AccountsService) {
         var vm = this;
         
         vm.dataLoading = false;

+ 19 - 9
public/index.html

@@ -14,20 +14,29 @@
         
         <link rel="stylesheet" href="/css/cloudbudget.css" type="text/css" />
     </head>
-    <body>
-        <div class="container">
-            <nav class="navbar navbar-inverse">
+    <body ng-controller="GlobalController as globalController">
+        <nav class="navbar navbar-inverse">
+            <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="/">CloudBudget</a>
                 </div>
                 
-                <ul class="nav navbar-nav">
-                    <li><a href="/login" ng-hide="{{globals.user}}">Login</a></li>                    
-                    <li><a href="/logout" ng-show="{{globals.user}}">Logout</a></li>                    
+                <form class="nav navbar-left navbar-form">
+                    <label for=""current_account>Account</label>
+                    <select name="current_account" id="current_account" class="form-control input-sm" ng-model="globalController.current_account" ng-change="globalController.change_account($data)">
+                        <option value="{{account._id}}" ng-repeat="account in accounts" >{{account.name}}</option>
+                    </select>
+                </form>
+                <ul class="nav navbar-nav navbar-right">
+                    <li>
+                        <a href="/login" ng-hide="{{globals.user}}">Login</a>
+                        <a href="/logout" ng-show="{{globals.user}}">Logout</a>
+                    </li>                    
                 </ul>
-            </nav>
-        </div>
-         <div growl></div>
+            </div>
+        </nav>
+        
+        <div growl></div>
         <div ng-view></div>
         
         <div class="credits text-center">
@@ -55,6 +64,7 @@
         <script type="text/javascript" src="/js/services/accounts.service.js"></script>
         <script type="text/javascript" src="/js/services/account.service.js"></script>
         
+        <script type="text/javascript" src="/js/global.controller.js"></script>
         <script type="text/javascript" src="/home/home.controller.js"></script>
         <script type="text/javascript" src="/login/login.controller.js"></script>
         <script type="text/javascript" src="/register/register.controller.js"></script>

+ 26 - 0
public/js/global.controller.js

@@ -0,0 +1,26 @@
+(function() {
+    'use strict';
+    
+    angular
+        .module('cloudbudget')
+        .controller('GlobalController', GlobalController);
+        
+    GlobalController.$inject = ['$scope', '$rootScope', '$location'];
+    
+    function GlobalController($scope, $rootScope, $location) {
+        var vm = this;
+        
+        vm.change_account = change_account;
+        vm.current_account = undefined;
+        
+        $scope.$watch(function() {
+            return $rootScope.current_account;
+        }, function() {
+            vm.current_account = $rootScope.current_account;
+        }, true);
+        
+        function change_account() {
+            $location.path('/account/' + vm.current_account);
+        };
+    }
+})();

+ 63 - 7
test/account.controller.spec.js

@@ -48,14 +48,26 @@ describe('AccountController', function() {
             "type": "DEPOSIT",
             "category": "560a84058812ad8d0ff200f0",
             "sub_category": "560a84058812ad8d0ff200f3"
-        };
+        },
+        DEFAULT_ACCOUNTS = [
+            {
+                "_id": "560a84058812ad8d0ff200ee",
+                "name": "foo",
+                "reference": "baz",
+                "user_id": "55b78934d2a706265ea28e9c"
+            }, {
+                "_id": "560a7ad08812ad8d0ff20068",
+                "name": "bar",
+                "user_id": "55b78934d2a706265ea28e9c"
+            }
+        ];
         
     beforeEach(module('cloudbudget'));
     
-    beforeEach(inject(function ( _$rootScope_, _$httpBackend_,  $controller, _$location_, $routeParams, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) {
+    beforeEach(inject(function ( _$rootScope_, _$httpBackend_,  $controller, _$location_, _$timeout_, _$filter_, _AccountService_, _FlashService_, _apiRoutes_) {
         $location = _$location_;
-        $httpBackend = $httpBackend;
-        $rootScope = _$rootScope_.$new();
+        $httpBackend = _$httpBackend_;
+        $rootScope = _$rootScope_;
         $scope = _$rootScope_.$new();
         $scope.form = {
           $valid: true,
@@ -74,18 +86,21 @@ describe('AccountController', function() {
                 '$rootScope': $rootScope,
                 '$routeParams': {account_id: DEFAULT_ACCOUNT._id},
                 FlashService: _FlashService_,
-                AccountService: _AccountService_,
+                AccountService: _AccountService_
             });
         };
     }));
     
     describe('init()', function() {
-        it('should init successfully', inject(function($httpBackend) {
+        it('should init successfully', inject(function($httpBackend, $rootScope) {
             $httpBackend.expect('GET', apiRoutes.accounts +  DEFAULT_ACCOUNT._id)
                 .respond(DEFAULT_ACCOUNT);
     
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
+                
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
             
             var accountController = createController();
             $httpBackend.flush();
@@ -95,15 +110,19 @@ describe('AccountController', function() {
             accountController.account._id.should.be.equal(DEFAULT_ACCOUNT._id);
             accountController.entries.should.be.instanceof(Array).and.have.lengthOf(1);
             accountController.balance.should.be.equal(100);
+            $rootScope.accounts.should.be.instanceof(Array).and.have.lengthOf(2);
         }));
         
-        it('should fail to init', inject(function($httpBackend) {
+        it('should fail to init', inject(function($httpBackend, $rootScope) {
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id)
                 .respond(400);
     
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond(400);
                 
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(400);
+                
             var accountController = createController();
             $httpBackend.flush();
             $timeout.flush();
@@ -111,6 +130,7 @@ describe('AccountController', function() {
             should.not.exist(accountController.account);
             accountController.entries.should.be.instanceof(Array).and.have.lengthOf(0);
             should.not.exist(accountController.balance);
+            should.not.exist($rootScope.accounts);
         }));
     });
     
@@ -122,6 +142,9 @@ describe('AccountController', function() {
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[], balance: 0});
                 
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
+                
             var accountController = createController();
             $httpBackend.flush();
             $timeout.flush();
@@ -150,6 +173,9 @@ describe('AccountController', function() {
     
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[], balance: 0});
+            
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
                 
             var accountController = createController();
             $httpBackend.flush();
@@ -179,6 +205,9 @@ describe('AccountController', function() {
     
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
+            
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
                 
             var accountController = createController();
             $httpBackend.flush();
@@ -203,6 +232,9 @@ describe('AccountController', function() {
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[DEFAULT_ACCOUNT], balance: 100});
                 
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
+                
             var accountController = createController();
             $httpBackend.flush();
             $timeout.flush();
@@ -228,6 +260,9 @@ describe('AccountController', function() {
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
                 
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
+                
             var accountController = createController();
             $httpBackend.flush();
             $timeout.flush();
@@ -264,6 +299,9 @@ describe('AccountController', function() {
             $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                 .respond({entry: null, entries:[DEFAULT_ENTRY], balance: 100});
                 
+            $httpBackend.expect('GET', apiRoutes.accounts )
+                .respond(DEFAULT_ACCOUNTS);
+                
             var accountController = createController();
             $httpBackend.flush();
             $timeout.flush();
@@ -361,6 +399,9 @@ describe('AccountController', function() {
                 $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                     .respond({entry: null, entries:[], balance: 0});
                     
+                $httpBackend.expect('GET', apiRoutes.accounts )
+                    .respond(DEFAULT_ACCOUNTS);
+                    
                 var accountController = createController();
                 $httpBackend.flush();
                 $timeout.flush();
@@ -378,6 +419,9 @@ describe('AccountController', function() {
                 $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                     .respond({entry: null, entries:[], balance: 0});
                     
+                $httpBackend.expect('GET', apiRoutes.accounts )
+                    .respond(DEFAULT_ACCOUNTS);
+                    
                 var accountController = createController();
                 $httpBackend.flush();
                 $timeout.flush();
@@ -395,6 +439,9 @@ describe('AccountController', function() {
                 $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                     .respond({entry: null, entries:[], balance: 0});
                     
+                $httpBackend.expect('GET', apiRoutes.accounts )
+                    .respond(DEFAULT_ACCOUNTS);
+                    
                 var accountController = createController();
                 $httpBackend.flush();
                 $timeout.flush();
@@ -414,6 +461,9 @@ describe('AccountController', function() {
                 $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                     .respond({entry: null, entries:[], balance: 0});
                     
+                $httpBackend.expect('GET', apiRoutes.accounts )
+                    .respond(DEFAULT_ACCOUNTS);
+                    
                 var accountController = createController();
                 $httpBackend.flush();
                 $timeout.flush();
@@ -431,6 +481,9 @@ describe('AccountController', function() {
                 $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                     .respond({entry: null, entries:[], balance: 0});
                     
+                $httpBackend.expect('GET', apiRoutes.accounts )
+                    .respond(DEFAULT_ACCOUNTS);
+                    
                 var accountController = createController();
                 $httpBackend.flush();
                 $timeout.flush();
@@ -448,6 +501,9 @@ describe('AccountController', function() {
                 $httpBackend.expect('GET', apiRoutes.accounts + DEFAULT_ACCOUNT._id + '/entries')
                     .respond({entry: null, entries:[], balance: 0});
                     
+                $httpBackend.expect('GET', apiRoutes.accounts )
+                    .respond(DEFAULT_ACCOUNTS);
+                    
                 var accountController = createController();
                 $httpBackend.flush();
                 $timeout.flush();

+ 43 - 0
test/global.controller.spec.js

@@ -0,0 +1,43 @@
+describe('GlobalController', function() {
+    
+    var $location,
+        $rootScope,
+        $scope,
+        createController,
+        DEFAULT_ACCOUNTS = [
+            {
+                "_id": "560a84058812ad8d0ff200ee",
+                "name": "foo",
+                "reference": "baz",
+                "user_id": "55b78934d2a706265ea28e9c"
+            }, {
+                "_id": "560a7ad08812ad8d0ff20068",
+                "name": "bar",
+                "user_id": "55b78934d2a706265ea28e9c"
+            }
+        ];
+        
+    beforeEach(module('cloudbudget'));
+    
+    beforeEach(inject(function ( _$rootScope_, $controller, _$location_) {
+        $location = _$location_;
+        $rootScope = _$rootScope_;
+        $scope = _$rootScope_.$new();
+        
+        createController = function() {
+            return $controller('GlobalController', {
+                '$scope': $scope,
+                '$location': $location,
+                '$rootScope': $rootScope
+            });
+        };
+    }));
+    
+    it('should init successfully', inject(function($location, $rootScope) {
+        var globalController = createController();
+        globalController.current_account = '560a84058812ad8d0ff200ee';
+        globalController.change_account();
+        $location.path().should.be.equal('/account/560a84058812ad8d0ff200ee')
+    }));
+
+});

+ 1 - 1
test/home.controller.spec.js

@@ -14,7 +14,7 @@ describe('HomeController', function() {
             var homeController = $controller('HomeController', {
                 '$rootScope': rootScope
             });
-            
+
             homeController.firstname = 'George';
             homeController.lastname = 'Harrison';
             homeController.getFullname().should.be.equal('George Harrison');