User auth, mission list and active menu buttons

James Peret 9 years ago
parent
commit
1810684058

+ 2 - 0
www/index.html

@@ -22,6 +22,8 @@
22 22
     <script src="js/app.js"></script>
23 23
     <script src="js/controllers.js"></script>
24 24
     <script src="js/controllers/login-ctrl.js"></script>
25
+    <script src="js/controllers/mission-ctrl.js"></script>
26
+    <script src="js/controllers/briefing-ctrl.js"></script>
25 27
     <script src="js/services/data-service.js"></script>
26 28
     <script src="js/services/oauth-service.js"></script>
27 29
     <script src="js/services/realtime-service.js"></script>

+ 16 - 5
www/js/app.js

@@ -15,7 +15,11 @@ angular.module('avalanche3mobile', [
15 15
   'avalanche3mobile.controllers',
16 16
   'avalanche3mobile.missionData',
17 17
   'avalanche3mobile.LoginCtrl',
18
-  'avalancheServices.Oauth'
18
+  'avalanche3mobile.MissionCtrl',
19
+  'avalanche3mobile.BriefingCtrl',
20
+  'avalancheServices.Oauth',
21
+  'avalancheServices.Data',
22
+  'avalancheServices.Realtime'
19 23
 ])
20 24
 
21 25
 .run(function($ionicPlatform) {
@@ -43,6 +47,13 @@ angular.module('avalanche3mobile', [
43 47
     controller: 'AppCtrl'
44 48
   })
45 49
 
50
+  .state('mission', {
51
+    url: "/missions",
52
+    abstract: true,
53
+    templateUrl: "templates/menu.html",
54
+    controller: 'MissionCtrl'
55
+  })
56
+
46 57
   .state('simple', {
47 58
     url: "/simple",
48 59
     abstract: true,
@@ -89,7 +100,7 @@ angular.module('avalanche3mobile', [
89 100
     }
90 101
   })
91 102
 
92
-  .state('app.briefing', {
103
+  .state('mission.briefing', {
93 104
     url: "/missions/:missionId/briefing",
94 105
     views: {
95 106
       'menuContent': {
@@ -103,7 +114,7 @@ angular.module('avalanche3mobile', [
103 114
       }
104 115
     }
105 116
   })
106
-  .state('app.activity', {
117
+  .state('mission.activity', {
107 118
     url: "/missions/:missionId/activity",
108 119
     views: {
109 120
       'menuContent': {
@@ -118,7 +129,7 @@ angular.module('avalanche3mobile', [
118 129
     }
119 130
   })
120 131
 
121
-  .state('app.tasks', {
132
+  .state('mission.tasks', {
122 133
     url: "/missions/:missionId/tasks",
123 134
     views: {
124 135
       'menuContent': {
@@ -133,7 +144,7 @@ angular.module('avalanche3mobile', [
133 144
     }
134 145
   })
135 146
 
136
-  .state('app.agents', {
147
+  .state('mission.agents', {
137 148
     url: "/missions/:missionId/agents",
138 149
     views: {
139 150
       'menuContent': {

+ 4 - 7
www/js/controllers.js

@@ -1,6 +1,6 @@
1 1
 angular.module('avalanche3mobile.controllers', [])
2 2
 
3
-.controller('AppCtrl', function($scope, $rootScope, $location, $ionicModal, $timeout, missionData, $ionicSideMenuDelegate, $cordovaStatusbar) {
3
+.controller('AppCtrl', function($scope, $rootScope, $location, $ionicModal, $timeout, missionData, $ionicSideMenuDelegate, $cordovaStatusbar, $state) {
4 4
 
5 5
 
6 6
 
@@ -25,7 +25,9 @@ angular.module('avalanche3mobile.controllers', [])
25 25
 
26 26
   $scope.isActive = function (viewLocation) {
27 27
     //console.log(viewLocation + " | " + $location.path())
28
-    return $location.path().includes(viewLocation);
28
+    console.log($state.params.missionId)
29
+    if($state.params.missionId == viewLocation) { return true; }
30
+    else { return false; }
29 31
   };
30 32
 
31 33
   // Create the login modal that we will use later
@@ -80,11 +82,6 @@ angular.module('avalanche3mobile.controllers', [])
80 82
   ];
81 83
 })
82 84
 
83
-.controller('MissionBriefingCtrl', function($scope, $stateParams, mission) {
84
-  $scope.mission = mission;
85
-  console.log(mission)
86
-})
87
-
88 85
 .controller('MissionActivityCtrl', function($scope, $stateParams, mission) {
89 86
   $scope.mission = mission;
90 87
   console.log(mission)

+ 6 - 0
www/js/controllers/briefing-ctrl.js

@@ -0,0 +1,6 @@
1
+angular.module('avalanche3mobile.BriefingCtrl', [])
2
+
3
+  .controller('MissionBriefingCtrl', function($scope, $stateParams, mission, OAuthService, $state) {
4
+    $scope.mission = OAuthService.getMission($state.params.missionId);
5
+    console.log(mission)
6
+  })

+ 13 - 5
www/js/controllers/login-ctrl.js

@@ -1,4 +1,4 @@
1
-angular.module('avalanche3mobile.LoginCtrl', ['avalancheServices'])
1
+angular.module('avalanche3mobile.LoginCtrl', ['avalancheServices.Oauth'])
2 2
 
3 3
   .controller('LoginCtrl', ['$scope', '$rootScope', '$location', '$ionicModal', '$timeout', '$ionicSideMenuDelegate', '$cordovaStatusbar', 'OAuthService', '$state', function($scope, $rootScope, $location, $ionicModal, $timeout, $ionicSideMenuDelegate, $cordovaStatusbar, OAuthService, $state) {
4 4
 
@@ -38,13 +38,21 @@ angular.module('avalanche3mobile.LoginCtrl', ['avalancheServices'])
38 38
     $rootScope.$on('auth:success', function() {
39 39
       if(!$scope.$$phase) {
40 40
         $scope.$apply(function(){
41
-          $scope.modal.hide();
42
-          $state.go("app.search");
41
+          $scope.redirectLoggedInUser();
43 42
         });
44 43
       } else {
45
-        $scope.modal.hide();
46
-        $state.go("app.search");
44
+        $scope.redirectLoggedInUser();
47 45
       }
48 46
     });
49 47
 
48
+    $scope.redirectLoggedInUser = function() {
49
+      $scope.modal.hide();
50
+      var missions = OAuthService.getMissions();
51
+      if(missions.length > 0) {
52
+        $state.go("mission.briefing", { missionId: missions[0].slug});
53
+      } else {
54
+        $state.go("app.search");
55
+      }
56
+    }
57
+
50 58
   }]);

+ 42 - 0
www/js/controllers/mission-ctrl.js

@@ -0,0 +1,42 @@
1
+angular.module('avalanche3mobile.MissionCtrl', [])
2
+
3
+.controller('MissionCtrl', function($scope, $rootScope, $location, $ionicModal, $timeout, missionData, $ionicSideMenuDelegate, $cordovaStatusbar, $state, DataService, OAuthService) {
4
+
5
+
6
+
7
+  // With the new view caching in Ionic, Controllers are only called
8
+  // when they are recreated or on app start, instead of every page change.
9
+  // To listen for when this page is active (for example, to refresh data),
10
+  // listen for the $ionicView.enter event:
11
+  //$scope.$on('$ionicView.enter', function(e) {
12
+  //});
13
+
14
+  $scope.currentMissions = OAuthService.getMissions();
15
+  $scope.mission = OAuthService.getMission($state.params.missionId);
16
+
17
+  $rootScope.$on('$stateChangeSuccess',
18
+    function(event, toState, toParams, fromState, fromParams){
19
+      $scope.mission = OAuthService.getMission($state.params.missionId);
20
+  });
21
+
22
+  $scope.isActive = function (viewLocation) {
23
+    //console.log(viewLocation + " | " + $location.path())
24
+    console.log($state.params.missionId)
25
+    if($state.params.missionId == viewLocation) { return true; }
26
+    else { return false; }
27
+  };
28
+
29
+
30
+  $scope.toggleLeft = function() {
31
+    console.log("Opening left menu")
32
+    $ionicSideMenuDelegate.toggleLeft();
33
+    // if($cordovaStatusbar.isVisible()){
34
+    //   //$cordovaStatusbar.hide();
35
+    // } else {
36
+    //   //$cordovaStatusbar.show();
37
+    // }
38
+
39
+  };
40
+
41
+
42
+})

+ 1 - 1
www/js/services/data-service.js

@@ -1,4 +1,4 @@
1
-angular.module('avalancheServices', [])
1
+angular.module('avalancheServices.Data', [])
2 2
 .service('DataService', [ '$rootScope', '$http',  function($rootScope, $http) {
3 3
   var response = {};
4 4
 

+ 21 - 0
www/js/services/oauth-service.js

@@ -3,6 +3,8 @@ angular.module('avalancheServices.Oauth', [])
3 3
   var code = {};
4 4
   //var token = $cookies.get('avalanche_docs_token');
5 5
   var token = "";
6
+  var user = {};
7
+  var missions = [];
6 8
 
7 9
   this.authorizeApplication = function(code){
8 10
     $http({
@@ -33,6 +35,8 @@ angular.module('avalancheServices.Oauth', [])
33 35
       }
34 36
     }).then(function(data) {
35 37
       token = data.data.access_token;
38
+      user = data.data.user;
39
+      missions = data.data.missions;
36 40
       //$cookies.put('avalanche_docs_token', token);
37 41
       console.log("Resource Owner Password Credentials flow SUCCESSFULL")
38 42
       console.log(data.data);
@@ -49,4 +53,21 @@ angular.module('avalancheServices.Oauth', [])
49 53
     return token;
50 54
   }
51 55
 
56
+  this.getUser = function() {
57
+    return user;
58
+  }
59
+
60
+  this.getMissions = function() {
61
+    return missions;
62
+  }
63
+
64
+  this.getMission = function(slug) {
65
+    for (var i = 0; i < missions.length; i++) {
66
+      if(missions[i].slug == slug) {
67
+        return missions[i];
68
+      }
69
+    }
70
+    return false;
71
+  }
72
+
52 73
 }])

+ 1 - 1
www/js/services/realtime-service.js

@@ -1,4 +1,4 @@
1
-angular.module('avalancheServices', [])
1
+angular.module('avalancheServices.Realtime', [])
2 2
 .factory('RealtimeService', [ '$rootScope', '$http',  function($rootScope, $http) {
3 3
   var FayeServerURL = 'https://avalanche-realtime.herokuapp.com/faye'
4 4
 

+ 5 - 5
www/templates/menu.html

@@ -20,7 +20,7 @@
20 20
     <ion-content class="left-nav-bg">
21 21
       <h2>Missions</h2>
22 22
       <ion-list>
23
-        <ion-item ng-repeat="item in currentMissions" menu-close href="#/app/missions/{{item.slug}}/briefing" ng-class="{ current: isActive('/{{item.slug}}')}">
23
+        <ion-item ng-repeat="item in currentMissions" menu-close ui-sref="mission.briefing({ missionId: item.slug})" ng-class="{ current: isActive(item.slug) }">
24 24
           {{item.title}}
25 25
         </ion-item>
26 26
 
@@ -38,16 +38,16 @@
38 38
     <ion-content class="right-nav-bg">
39 39
       <h3>{{mission.title}}</h3>
40 40
       <ion-list>
41
-        <ion-item menu-close href="#/app/missions/{{mission.slug}}/briefing" ng-class="{ current: isActive('/briefing')}">
41
+        <ion-item menu-close ui-sref="mission.briefing({ missionId: mission.slug})" ui-sref-active="current">
42 42
           <i class="icon-mission icon-large"></i><span class="icon-large-text">Mission Briefing</span>
43 43
         </ion-item>
44
-        <ion-item menu-close href="#/app/missions/{{mission.slug}}/activity" ng-class="{ current: isActive('/activity')}">
44
+        <ion-item menu-close ui-sref="mission.activity({ missionId: mission.slug})" ui-sref-active="current">
45 45
           <i class="icon-radar icon-large"></i><span class="icon-large-text">Activity Feed</span>
46 46
         </ion-item>
47
-        <ion-item menu-close href="#/app/missions/{{mission.slug}}/tasks" ng-class="{ current: isActive('/tasks')}">
47
+        <ion-item menu-close ui-sref="mission.tasks({ missionId: mission.slug})" ui-sref-active="current">
48 48
           <i class="icon-task icon-large"></i><span class="icon-large-text">My Tasks</span>
49 49
         </ion-item>
50
-        <ion-item menu-close href="#/app/missions/{{mission.slug}}/agents" ng-class="{ current: isActive('/agents')}">
50
+        <ion-item menu-close ui-sref="mission.agents({ missionId: mission.slug})" ui-sref-active="current">
51 51
           <i class="icon-agent icon-large"></i><span class="icon-large-text">Agents</span>
52 52
         </ion-item>
53 53
       </ion-list>