blob: 7575e346cea65739f6a02e2e33f5ccf68e81d58c [file] [log] [blame]
Ed Tanous904063f2017-03-02 16:48:24 -08001'use strict';
2angular.module('Authentication', []);
3var app = angular.module('bmcApp', [
Ed Tanousc4771fb2017-03-13 13:39:49 -07004 'Authentication',
5 'ngCookies',
6 'ui.bootstrap',
7 'ui.router',
8 'ngSanitize',
9 'ngWebSocket',
10 'ngResource'
Ed Tanous904063f2017-03-02 16:48:24 -080011]);
12
Ed Tanous9140a672017-04-24 17:01:32 -070013
14
Ed Tanous1ccd57c2017-03-21 13:15:58 -070015app.controller('MainCtrl', ['$scope', function($scope) {
Ed Tanous904063f2017-03-02 16:48:24 -080016
Ed Tanous1ccd57c2017-03-21 13:15:58 -070017}]);
Ed Tanous904063f2017-03-02 16:48:24 -080018
Ed Tanousc4771fb2017-03-13 13:39:49 -070019app.service('loginInterceptor', ["$injector",
20 function($injector) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080021 var service = this;
22
23 service.responseError = function(response) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070024 var $state = $injector.get('$state');
25 var AuthenticationService = $injector.get('AuthenticationService');
Ed Tanous9b65f1f2017-03-07 15:17:13 -080026 if (response.status == 401){
27 console.log("Login required... ");
28
29 var invalidate_reason = "Your user was logged out.";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080030
31 // if we're attempting to log in, we need to
32 // continue the promise chain to make sure the user is informed
33 if ($state.current.name === "login") {
34 invalidate_reason = "Your username and password was incorrect";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080035 } else {
36 $state.after_login_state = $state.current.name;
37 $state.go('login');
38 }
39 AuthenticationService.ClearCredentials(invalidate_reason);
40 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070041 return response;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080042 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070043}])
Ed Tanous9b65f1f2017-03-07 15:17:13 -080044
45app.config(['$httpProvider', function ($httpProvider) {
46 $httpProvider.interceptors.push('loginInterceptor');
47}]);
48
Ed Tanous1ccd57c2017-03-21 13:15:58 -070049app.directive('windowSize', ['$window', function ($window) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070050 return function (scope, element) {
51 var w = angular.element($window);
52 scope.getWindowDimensions = function () {
53 return {
54 'h': w.height(),
55 'w': w.width()
56 };
57 };
58 scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
59 scope.windowHeight = newValue.h;
60 scope.windowWidth = newValue.w;
61 scope.style = function () {
62 return {
63 'height': (newValue.h - 100) + 'px',
64 'width': (newValue.w - 100) + 'px'
65 };
66 };
67 }, true);
68
69 w.bind('resize', function () {
70 scope.$apply();
71 });
72 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070073}]);
Ed Tanousc4771fb2017-03-13 13:39:49 -070074
Ed Tanousb4d29f42017-03-24 16:39:25 -070075app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', '$http', '$templateCache',
76 function($rootScope, $cookieStore, $state, $resource, AuthenticationService, $http, $templateCache) {
Ed Tanous9140a672017-04-24 17:01:32 -070077
78 $http.get('static/partial-login.html', {cache:$templateCache});
79 $http.get('static/partial-kvm.html', {cache: $templateCache});
80
Ed Tanous904063f2017-03-02 16:48:24 -080081 if ($rootScope.globals == undefined){
82 $rootScope.globals = {};
83 }
Ed Tanous9140a672017-04-24 17:01:32 -070084
Ed Tanous904063f2017-03-02 16:48:24 -080085
86 // keep user logged in after page refresh
87 AuthenticationService.RestoreCredientials();
88
89 $rootScope.$on(
90 '$stateChangeStart',
91 function(event, toState, toParams, fromState, fromParams, options) {
92 // redirect to login page if not logged in
Ed Tanousc4771fb2017-03-13 13:39:49 -070093 // unless we're already trying to go to the login page (prevent a loop)
94 if (!$rootScope.globals.currentUser && toState.name !== 'login') {
Ed Tanous904063f2017-03-02 16:48:24 -080095 // If logged out and transitioning to a logged in page:
96 event.preventDefault();
97 $state.go('login');
98 }
99 });
Ed Tanous9140a672017-04-24 17:01:32 -0700100
Ed Tanous904063f2017-03-02 16:48:24 -0800101 }
102]);
103
Ed Tanousc4771fb2017-03-13 13:39:49 -0700104app.config(['$stateProvider', '$urlRouterProvider',
105 function($stateProvider, $urlRouterProvider) {
Ed Tanous904063f2017-03-02 16:48:24 -0800106
107 $urlRouterProvider.otherwise('/systeminfo');
108
109 $stateProvider
110 // nested list with just some random string data
111 .state('login', {
112 url: '/login',
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700113 templateUrl: 'static/partial-login.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800114 controller: 'LoginController',
115 })
116 // systeminfo view ========================================
117 .state(
118 'systeminfo',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800119 {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800120
121
122 // HOME STATES AND NESTED VIEWS ========================================
123 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800124 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800125
126
127 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800128 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800129
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700130 .state(
131 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'})
132
Ed Tanouscc5a37f2017-05-11 10:27:23 -0700133 .state(
134 'sensor', {url: '/sensor', templateUrl: 'static/partial-sensor.html'})
135
Ed Tanous904063f2017-03-02 16:48:24 -0800136 // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800137 .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800138
139 // nested list with custom controller
140 .state('about.list', {
141 url: '/list',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800142 templateUrl: 'static/partial-home-list.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800143 controller: function($scope) {
144 $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
145 }
146 })
147
148
Ed Tanousc4771fb2017-03-13 13:39:49 -0700149}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800150
Ed Tanousc4771fb2017-03-13 13:39:49 -0700151app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) {
Ed Tanous904063f2017-03-02 16:48:24 -0800152 $scope.totalItems = 64;
153 $scope.currentPage = 4;
154
155 $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; };
156
157 $scope.pageChanged = function() {
158 $log.log('Page changed to: ' + $scope.currentPage);
159 };
160
161 $scope.maxSize = 5;
162 $scope.bigTotalItems = 175;
163 $scope.bigCurrentPage = 1;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700164}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800165
Ed Tanousc4771fb2017-03-13 13:39:49 -0700166angular.module('Authentication').factory(
Ed Tanous904063f2017-03-02 16:48:24 -0800167 'AuthenticationService',
Ed Tanousc4771fb2017-03-13 13:39:49 -0700168 ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http',
169 function($cookieStore, $rootScope, $timeout, $resource, $log, $http) {
Ed Tanous904063f2017-03-02 16:48:24 -0800170 var service = {};
171
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800172 service.Login = function(username, password, success_callback, fail_callback) {
Ed Tanous904063f2017-03-02 16:48:24 -0800173
174 var user = {"username": username, "password": password};
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800175 var UserLogin = $resource("/login");
176 var this_login = new UserLogin();
177 this_login.data = {"username": username, "password": password};
178 UserLogin.save(user, success_callback, fail_callback);
179
Ed Tanous904063f2017-03-02 16:48:24 -0800180 };
181
182 service.SetCredentials = function(username, token) {
183 $rootScope.globals["currentUser"] = {username: username, authdata: token};
Ed Tanousc4771fb2017-03-13 13:39:49 -0700184 $http.defaults.headers.common['Authorization'] = 'Token ' + token;
Ed Tanous904063f2017-03-02 16:48:24 -0800185 $cookieStore.put('globals', $rootScope.globals);
186 };
187
188 service.ClearCredentials = function(reason) {
189 $rootScope.globals["currentUser"] = null;
190 if (reason !== null) {
191 service.logoutreason = reason;
192 }
193 $cookieStore.remove('globals');
Ed Tanousc4771fb2017-03-13 13:39:49 -0700194 $http.defaults.headers.common['Authorization'] = '';
Ed Tanous904063f2017-03-02 16:48:24 -0800195 };
196
197 service.RestoreCredientials = function() {
198 var globals = $cookieStore.get('globals') || {};
199 if (globals.currentUser) {
200 service.SetCredentials(
201 globals.currentUser.username, globals.currentUser.authdata);
202 }
203 };
204
205 service.logoutreason = "";
206 return service;
207 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700208 ]);