blob: 7d31cc2071007858970fd0f231e6b7eee27acb3e [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 Tanous1ccd57c2017-03-21 13:15:58 -070013app.controller('MainCtrl', ['$scope', function($scope) {
Ed Tanous904063f2017-03-02 16:48:24 -080014
Ed Tanous1ccd57c2017-03-21 13:15:58 -070015}]);
Ed Tanous904063f2017-03-02 16:48:24 -080016
Ed Tanousc4771fb2017-03-13 13:39:49 -070017app.service('loginInterceptor', ["$injector",
18 function($injector) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080019 var service = this;
20
21 service.responseError = function(response) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070022 var $state = $injector.get('$state');
23 var AuthenticationService = $injector.get('AuthenticationService');
Ed Tanous9b65f1f2017-03-07 15:17:13 -080024 if (response.status == 401){
25 console.log("Login required... ");
26
27 var invalidate_reason = "Your user was logged out.";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080028
29 // if we're attempting to log in, we need to
30 // continue the promise chain to make sure the user is informed
31 if ($state.current.name === "login") {
32 invalidate_reason = "Your username and password was incorrect";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080033 } else {
34 $state.after_login_state = $state.current.name;
35 $state.go('login');
36 }
37 AuthenticationService.ClearCredentials(invalidate_reason);
38 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070039 return response;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080040 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070041}])
Ed Tanous9b65f1f2017-03-07 15:17:13 -080042
43app.config(['$httpProvider', function ($httpProvider) {
44 $httpProvider.interceptors.push('loginInterceptor');
45}]);
46
Ed Tanous1ccd57c2017-03-21 13:15:58 -070047app.directive('windowSize', ['$window', function ($window) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070048 return function (scope, element) {
49 var w = angular.element($window);
50 scope.getWindowDimensions = function () {
51 return {
52 'h': w.height(),
53 'w': w.width()
54 };
55 };
56 scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
57 scope.windowHeight = newValue.h;
58 scope.windowWidth = newValue.w;
59 scope.style = function () {
60 return {
61 'height': (newValue.h - 100) + 'px',
62 'width': (newValue.w - 100) + 'px'
63 };
64 };
65 }, true);
66
67 w.bind('resize', function () {
68 scope.$apply();
69 });
70 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070071}]);
Ed Tanousc4771fb2017-03-13 13:39:49 -070072
Ed Tanousb4d29f42017-03-24 16:39:25 -070073app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', '$http', '$templateCache',
74 function($rootScope, $cookieStore, $state, $resource, AuthenticationService, $http, $templateCache) {
Ed Tanous904063f2017-03-02 16:48:24 -080075 if ($rootScope.globals == undefined){
76 $rootScope.globals = {};
77 }
78
79 // keep user logged in after page refresh
80 AuthenticationService.RestoreCredientials();
81
82 $rootScope.$on(
83 '$stateChangeStart',
84 function(event, toState, toParams, fromState, fromParams, options) {
85 // redirect to login page if not logged in
Ed Tanousc4771fb2017-03-13 13:39:49 -070086 // unless we're already trying to go to the login page (prevent a loop)
87 if (!$rootScope.globals.currentUser && toState.name !== 'login') {
Ed Tanous904063f2017-03-02 16:48:24 -080088 // If logged out and transitioning to a logged in page:
89 event.preventDefault();
90 $state.go('login');
91 }
92 });
Ed Tanousb4d29f42017-03-24 16:39:25 -070093
94 $http.get('static/partial-kvm.html', { cache: $templateCache });
Ed Tanous904063f2017-03-02 16:48:24 -080095 }
96]);
97
Ed Tanousc4771fb2017-03-13 13:39:49 -070098app.config(['$stateProvider', '$urlRouterProvider',
99 function($stateProvider, $urlRouterProvider) {
Ed Tanous904063f2017-03-02 16:48:24 -0800100
101 $urlRouterProvider.otherwise('/systeminfo');
102
103 $stateProvider
104 // nested list with just some random string data
105 .state('login', {
106 url: '/login',
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700107 templateUrl: 'static/partial-login.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800108 controller: 'LoginController',
109 })
110 // systeminfo view ========================================
111 .state(
112 'systeminfo',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800113 {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800114
115
116 // HOME STATES AND NESTED VIEWS ========================================
117 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800118 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800119
120
121 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800122 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800123
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700124 .state(
125 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'})
126
Ed Tanous904063f2017-03-02 16:48:24 -0800127 // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800128 .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800129
130 // nested list with custom controller
131 .state('about.list', {
132 url: '/list',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800133 templateUrl: 'static/partial-home-list.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800134 controller: function($scope) {
135 $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
136 }
137 })
138
139
Ed Tanousc4771fb2017-03-13 13:39:49 -0700140}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800141
Ed Tanousc4771fb2017-03-13 13:39:49 -0700142app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) {
Ed Tanous904063f2017-03-02 16:48:24 -0800143 $scope.totalItems = 64;
144 $scope.currentPage = 4;
145
146 $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; };
147
148 $scope.pageChanged = function() {
149 $log.log('Page changed to: ' + $scope.currentPage);
150 };
151
152 $scope.maxSize = 5;
153 $scope.bigTotalItems = 175;
154 $scope.bigCurrentPage = 1;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700155}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800156
Ed Tanousc4771fb2017-03-13 13:39:49 -0700157angular.module('Authentication').factory(
Ed Tanous904063f2017-03-02 16:48:24 -0800158 'AuthenticationService',
Ed Tanousc4771fb2017-03-13 13:39:49 -0700159 ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http',
160 function($cookieStore, $rootScope, $timeout, $resource, $log, $http) {
Ed Tanous904063f2017-03-02 16:48:24 -0800161 var service = {};
162
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800163 service.Login = function(username, password, success_callback, fail_callback) {
Ed Tanous904063f2017-03-02 16:48:24 -0800164
165 var user = {"username": username, "password": password};
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800166 var UserLogin = $resource("/login");
167 var this_login = new UserLogin();
168 this_login.data = {"username": username, "password": password};
169 UserLogin.save(user, success_callback, fail_callback);
170
Ed Tanous904063f2017-03-02 16:48:24 -0800171 };
172
173 service.SetCredentials = function(username, token) {
174 $rootScope.globals["currentUser"] = {username: username, authdata: token};
Ed Tanousc4771fb2017-03-13 13:39:49 -0700175 $http.defaults.headers.common['Authorization'] = 'Token ' + token;
Ed Tanous904063f2017-03-02 16:48:24 -0800176 $cookieStore.put('globals', $rootScope.globals);
177 };
178
179 service.ClearCredentials = function(reason) {
180 $rootScope.globals["currentUser"] = null;
181 if (reason !== null) {
182 service.logoutreason = reason;
183 }
184 $cookieStore.remove('globals');
Ed Tanousc4771fb2017-03-13 13:39:49 -0700185 $http.defaults.headers.common['Authorization'] = '';
Ed Tanous904063f2017-03-02 16:48:24 -0800186 };
187
188 service.RestoreCredientials = function() {
189 var globals = $cookieStore.get('globals') || {};
190 if (globals.currentUser) {
191 service.SetCredentials(
192 globals.currentUser.username, globals.currentUser.authdata);
193 }
194 };
195
196 service.logoutreason = "";
197 return service;
198 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700199 ]);