-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainController.js
executable file
·96 lines (87 loc) · 4.21 KB
/
mainController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
// We initialize our module with our loaded dependences (Angular Route, Angular Material, and Angular Resource)
// Angular Route - helps us with redirection/responding to URL paths
// Angular Material - is a nice style aid that lets us use Google's Angular Material Library to make this app look fancy
// Angular Material - is a nice style aid that lets us use Google's Angular Material Library to make this app look fancy
var cs50App = angular.module('cs50App', ['ngRoute', 'ngMaterial', 'ngResource']);
/* First thing we do is configure our angular app with routes. For each route, we specify which HTML to show in the md-content and what Controller takes over */
cs50App.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'components/home/homeTemplate.html',
controller: 'HomeController'
}).
when('/login', {
templateUrl: 'components/login/loginTemplate.html',
controller: 'LoginController'
}).
when('/projects/new', {
templateUrl: 'components/submit/submit.html',
controller: 'SubmitController'
}).
when('/signup', {
templateUrl: 'components/signup/signupTemplate.html',
controller: 'signupController'
}).
when('/project/:projectId', {
templateUrl: 'components/project/projectTemplate.html',
controller: 'ProjectController'
}).
when('/review', {
templateUrl: 'components/underReview/underReviewTemplate.html',
controller: 'UnderReviewController'
}).
when('/profile/private', {
templateUrl: 'components/editableProfile/editableTemplate.html',
controller: 'privateProfileController'
}).
when('/projects/:projectId/edit', {
templateUrl: 'components/submit/submit.html',
controller: 'EditController'
}).
when('/editUser', {
templateUrl: 'components/editUser/editUser.html',
controller: 'EditUserController'
}).
otherwise({
redirectTo: '/home'
});
}]);
/* Here we initialize our first controller, titled "MainController"
* We inject into it a bunch of services, as specified by the $ signs
*/
cs50App.controller('MainController', ['$scope', '$rootScope', '$location', '$http', '$routeParams', '$resource', '$mdDialog', '$mdMedia',
function ($scope, $rootScope, $location, $http, $routeParams, $resource, $mdDialog, $mdMedia) {
$scope.loggedIn; // Boolean to keep track if user is logged in.
// the $rootScope injection helps us determine if a certain event has occurred that altered the route
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
if (!$scope.loggedIn) {
// no logged user, redirect to /login unless already there
if (next.templateUrl !== "components/login/loginTemplate.html" && next.templateUrl !== "components/signup/signupTemplate.html" ) {
$location.path("/login");
}
}
});
// $scope variables are accessible in any other Angular controller with the $scope injection. Also accessible in the HTML controlled by the Controller
$scope.main = {};
$scope.main.loggedInUser = "User Name"
$scope.logout = document.querySelector('.loggedInToolBar')
// Upon logout, communicate with server to logout/destroy session and manage view
$scope.logoutClicked = function(){
$scope.userReq = $resource('/admin/logout');
$scope.userReq.save({}, function () {
$scope.loggedIn = false;
window.location = "#/login";
$scope.logout.style.display = "None";
$rootScope.$broadcast('LoggedOut');
}, function errorHandling(err) {
console.log("INVALID LOGIN");
});
};
// When logging in, we set the loggedIn bool to true and signal to all other apps.
$scope.loggingIn = function() {
$scope.loggedIn = true;
$rootScope.$broadcast('LoggedIn');
};
}]);