Merge "Fix error in qtip job template"
[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     angular
30         .module('testapiApp')
31         .directive('dynamicModel', ['$compile', '$parse', function ($compile, $parse) {
32             return {
33                 restrict: 'A',
34                 terminal: true,
35                 priority: 100000,
36                 link: function (scope, elem) {
37                     var name = $parse(elem.attr('dynamic-model'))(scope);
38                     elem.removeAttr('dynamic-model');
39                     elem.attr('ng-model', name);
40                     $compile(elem)(scope);
41                 }
42             };
43         }]);
44
45     configureRoutes.$inject = ['$stateProvider', '$urlRouterProvider'];
46
47     /**
48      * Handle application routing. Specific templates and controllers will be
49      * used based on the URL route.
50      */
51     function configureRoutes($stateProvider, $urlRouterProvider) {
52         $urlRouterProvider.otherwise('/');
53         $stateProvider.
54             state('home', {
55                 url: '/',
56                 templateUrl: 'testapi-ui/components/home/home.html'
57             }).
58             state('about', {
59                 url: '/about',
60                 templateUrl: 'testapi-ui/components/about/about.html'
61             }).
62             state('pods', {
63                 url: '/pods',
64                 templateUrl: 'testapi-ui/components/pods/pods.html',
65                 controller: 'PodsController as ctrl'
66             }).
67             state('communityResults', {
68                 url: '/community_results',
69                 templateUrl: 'testapi-ui/components/results/results.html',
70                 controller: 'ResultsController as ctrl'
71             }).
72             state('userResults', {
73                 url: '/user_results',
74                 templateUrl: 'testapi-ui/components/results/results.html',
75                 controller: 'ResultsController as ctrl'
76             }).
77             state('resultsDetail', {
78                 url: '/results/:testID',
79                 templateUrl: 'testapi-ui/components/results-report' +
80                              '/resultsReport.html',
81                 controller: 'ResultsReportController as ctrl'
82             }).
83             state('profile', {
84                 url: '/profile',
85                 templateUrl: 'testapi-ui/components/profile/profile.html',
86                 controller: 'ProfileController as ctrl'
87             }).
88             state('authFailure', {
89                 url: '/auth_failure',
90                 templateUrl: 'testapi-ui/components/home/home.html',
91                 controller: 'AuthFailureController as ctrl'
92             }).
93             state('logout', {
94                 url: '/logout',
95                 templateUrl: 'testapi-ui/components/logout/logout.html',
96                 controller: 'LogoutController as ctrl'
97             }).
98             state('userVendors', {
99                 url: '/user_vendors',
100                 templateUrl: '/testapi-ui/components/vendors/vendors.html',
101                 controller: 'VendorsController as ctrl'
102             }).
103             state('publicVendors', {
104                 url: '/public_vendors',
105                 templateUrl: '/testapi-ui/components/vendors/vendors.html',
106                 controller: 'VendorsController as ctrl'
107             }).
108             state('vendor', {
109                 url: '/vendor/:vendorID',
110                 templateUrl: '/swagger/testapi-ui/components/vendors/vendor.html',
111                 controller: 'VendorController as ctrl'
112             }).
113             state('userProducts', {
114                 url: '/user_products',
115                 templateUrl: '/testapi-ui/components/products/products.html',
116                 controller: 'ProductsController as ctrl'
117             }).
118             state('publicProducts', {
119                 url: '/public_products',
120                 templateUrl: '/testapi-ui/components/products/products.html',
121                 controller: 'ProductsController as ctrl'
122             }).
123             state('cloud', {
124                 url: '/cloud/:id',
125                 templateUrl: '/testapi-ui/components/products/cloud.html',
126                 controller: 'ProductController as ctrl'
127             }).
128             state('distro', {
129                 url: '/distro/:id',
130                 templateUrl: '/testapi-ui/components/products/distro.html',
131                 controller: 'ProductController as ctrl'
132             });
133     }
134
135     angular
136         .module('testapiApp')
137         .config(disableHttpCache);
138
139     disableHttpCache.$inject = ['$httpProvider'];
140
141     /**
142      * Disable caching in $http requests. This is primarily for IE, as it
143      * tends to cache Angular IE requests.
144      */
145     function disableHttpCache($httpProvider) {
146         if (!$httpProvider.defaults.headers.get) {
147             $httpProvider.defaults.headers.get = {};
148         }
149         $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
150         $httpProvider.defaults.headers.get.Pragma = 'no-cache';
151     }
152
153     angular
154         .module('testapiApp')
155         .run(setup);
156
157     setup.$inject = [
158         '$http', '$rootScope', '$window', '$state', 'testapiApiUrl'
159     ];
160
161     /**
162      * Set up the app with injections into $rootscope. This is mainly for auth
163      * functions.
164      */
165     function setup($http, $rootScope, $window, $state, testapiApiUrl) {
166
167         $rootScope.auth = {};
168         $rootScope.auth.doSignIn = doSignIn;
169         $rootScope.auth.doSignOut = doSignOut;
170         $rootScope.auth.doSignCheck = doSignCheck;
171
172         var sign_in_url = testapiApiUrl + '/auth/signin';
173         var sign_out_url = testapiApiUrl + '/auth/signout';
174         var profile_url = testapiApiUrl + '/profile';
175
176         /** This function initiates a sign in. */
177         function doSignIn() {
178             $window.location.href = sign_in_url;
179         }
180
181         /** This function will initate a sign out. */
182         function doSignOut() {
183             $rootScope.auth.currentUser = null;
184             $rootScope.auth.isAuthenticated = false;
185             $window.location.href = sign_out_url;
186         }
187
188         /**
189          * This function checks to see if a user is logged in and
190          * authenticated.
191          */
192         function doSignCheck() {
193             return $http.get(profile_url, {withCredentials: true}).
194                 success(function (data) {
195                     $rootScope.auth.currentUser = data;
196                     $rootScope.auth.isAuthenticated = true;
197                 }).
198                 error(function () {
199                     $rootScope.auth.currentUser = null;
200                     $rootScope.auth.isAuthenticated = false;
201                 });
202         }
203
204         $rootScope.auth.doSignCheck();
205     }
206
207     angular
208         .element(document)
209         .ready(loadConfig);
210
211     /**
212      * Load config and start up the angular application.
213      */
214     function loadConfig() {
215
216         var $http = angular.injector(['ng']).get('$http');
217
218         /**
219          * Store config variables as constants, and start the app.
220          */
221         function startApp(config) {
222             // Add config options as constants.
223             angular.forEach(config, function(value, key) {
224                 angular.module('testapiApp').constant(key, value);
225             });
226
227             angular.bootstrap(document, ['testapiApp']);
228         }
229
230         $http.get('testapi-ui/config.json').success(function (data) {
231             startApp(data);
232         }).error(function () {
233             startApp({});
234         });
235     }
236 })();