Merge "bug fix: bad format for start/time in Tempest reporting"
[releng.git] / utils / test / testapi / 3rd_party / static / testapi-ui / app.js
1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14
15 (function () {
16     'use strict';
17
18     /** Main app module where application dependencies are listed. */
19     angular
20         .module('testapiApp', [
21             'ui.router','ui.bootstrap', 'cgBusy',
22             'ngResource', 'angular-confirm'
23         ]);
24
25     angular
26         .module('testapiApp')
27         .config(configureRoutes);
28
29     configureRoutes.$inject = ['$stateProvider', '$urlRouterProvider'];
30
31     /**
32      * Handle application routing. Specific templates and controllers will be
33      * used based on the URL route.
34      */
35     function configureRoutes($stateProvider, $urlRouterProvider) {
36         $urlRouterProvider.otherwise('/');
37         $stateProvider.
38             state('home', {
39                 url: '/',
40                 templateUrl: 'testapi-ui/components/home/home.html'
41             }).
42             state('about', {
43                 url: '/about',
44                 templateUrl: 'testapi-ui/components/about/about.html'
45             }).
46             state('guidelines', {
47                 url: '/guidelines',
48                 templateUrl: 'testapi-ui/components/guidelines/guidelines.html',
49                 controller: 'GuidelinesController as ctrl'
50             }).
51             state('communityResults', {
52                 url: '/community_results',
53                 templateUrl: 'testapi-ui/components/results/results.html',
54                 controller: 'ResultsController as ctrl'
55             }).
56             state('userResults', {
57                 url: 'user_results',
58                 templateUrl: '/testapi-ui/components/results/results.html',
59                 controller: 'ResultsController as ctrl'
60             }).
61             state('resultsDetail', {
62                 url: '/results/:testID',
63                 templateUrl: 'testapi-ui/components/results-report' +
64                              '/resultsReport.html',
65                 controller: 'ResultsReportController as ctrl'
66             }).
67             state('profile', {
68                 url: '/profile',
69                 templateUrl: '/testapi-ui/components/profile/profile.html',
70                 controller: 'ProfileController as ctrl'
71             }).
72             state('authFailure', {
73                 url: '/auth_failure',
74                 templateUrl: 'testapi-ui/components/home/home.html',
75                 controller: 'AuthFailureController as ctrl'
76             }).
77             state('logout', {
78                 url: '/logout',
79                 templateUrl: 'testapi-ui/components/logout/logout.html',
80                 controller: 'LogoutController as ctrl'
81             }).
82             state('userVendors', {
83                 url: '/user_vendors',
84                 templateUrl: '/testapi-ui/components/vendors/vendors.html',
85                 controller: 'VendorsController as ctrl'
86             }).
87             state('publicVendors', {
88                 url: '/public_vendors',
89                 templateUrl: '/testapi-ui/components/vendors/vendors.html',
90                 controller: 'VendorsController as ctrl'
91             }).
92             state('vendor', {
93                 url: '/vendor/:vendorID',
94                 templateUrl: '/swagger/testapi-ui/components/vendors/vendor.html',
95                 controller: 'VendorController as ctrl'
96             }).
97             state('userProducts', {
98                 url: '/user_products',
99                 templateUrl: '/testapi-ui/components/products/products.html',
100                 controller: 'ProductsController as ctrl'
101             }).
102             state('publicProducts', {
103                 url: '/public_products',
104                 templateUrl: '/testapi-ui/components/products/products.html',
105                 controller: 'ProductsController as ctrl'
106             }).
107             state('cloud', {
108                 url: '/cloud/:id',
109                 templateUrl: '/testapi-ui/components/products/cloud.html',
110                 controller: 'ProductController as ctrl'
111             }).
112             state('distro', {
113                 url: '/distro/:id',
114                 templateUrl: '/testapi-ui/components/products/distro.html',
115                 controller: 'ProductController as ctrl'
116             });
117     }
118
119     angular
120         .module('testapiApp')
121         .config(disableHttpCache);
122
123     disableHttpCache.$inject = ['$httpProvider'];
124
125     /**
126      * Disable caching in $http requests. This is primarily for IE, as it
127      * tends to cache Angular IE requests.
128      */
129     function disableHttpCache($httpProvider) {
130         if (!$httpProvider.defaults.headers.get) {
131             $httpProvider.defaults.headers.get = {};
132         }
133         $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
134         $httpProvider.defaults.headers.get.Pragma = 'no-cache';
135     }
136
137     angular
138         .module('testapiApp')
139         .run(setup);
140
141     setup.$inject = [
142         '$http', '$rootScope', '$window', '$state', 'testapiApiUrl'
143     ];
144
145     /**
146      * Set up the app with injections into $rootscope. This is mainly for auth
147      * functions.
148      */
149     function setup($http, $rootScope, $window, $state, testapiApiUrl) {
150
151         $rootScope.auth = {};
152         $rootScope.auth.doSignIn = doSignIn;
153         $rootScope.auth.doSignOut = doSignOut;
154         $rootScope.auth.doSignCheck = doSignCheck;
155
156         var sign_in_url = testapiApiUrl + '/auth/signin';
157         var sign_out_url = testapiApiUrl + '/auth/signout';
158         var profile_url = testapiApiUrl + '/profile';
159
160         /** This function initiates a sign in. */
161         function doSignIn() {
162             $window.location.href = sign_in_url;
163         }
164
165         /** This function will initate a sign out. */
166         function doSignOut() {
167             $rootScope.auth.currentUser = null;
168             $rootScope.auth.isAuthenticated = false;
169             $window.location.href = sign_out_url;
170         }
171
172         /**
173          * This function checks to see if a user is logged in and
174          * authenticated.
175          */
176         function doSignCheck() {
177             return $http.get(profile_url, {withCredentials: true}).
178                 success(function (data) {
179                     $rootScope.auth.currentUser = data;
180                     $rootScope.auth.isAuthenticated = true;
181                 }).
182                 error(function () {
183                     $rootScope.auth.currentUser = null;
184                     $rootScope.auth.isAuthenticated = false;
185                 });
186         }
187
188         $rootScope.auth.doSignCheck();
189     }
190
191     angular
192         .element(document)
193         .ready(loadConfig);
194
195     /**
196      * Load config and start up the angular application.
197      */
198     function loadConfig() {
199
200         var $http = angular.injector(['ng']).get('$http');
201
202         /**
203          * Store config variables as constants, and start the app.
204          */
205         function startApp(config) {
206             // Add config options as constants.
207             angular.forEach(config, function(value, key) {
208                 angular.module('testapiApp').constant(key, value);
209             });
210
211             angular.bootstrap(document, ['testapiApp']);
212         }
213
214         $http.get('testapi-ui/config.json').success(function (data) {
215             startApp(data);
216         }).error(function () {
217             startApp({});
218         });
219     }
220 })();