summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/ui/app.js
blob: b4e8d0806ef26239940a5462f0d651a0b4367b5e (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function () {
    'use strict';

    /** Main app module where application dependencies are listed. */
    angular
        .module('testapiApp', [
            'ui.router','ui.bootstrap', 'cgBusy',
            'ngResource', 'angular-confirm', 'angular-json-tree'
        ]);

    angular
        .module('testapiApp')
        .config(configureRoutes);

    angular
        .module('testapiApp')
        .service("keepState", function(){
            this.filter = {};
        });

    angular
        .module('testapiApp')
        .service('sortService', function(){

            this.sortFunction = function(data, field, ascending){
                if(ascending){
                    data.sort(function(a,b) {
                        if (a[field].toLowerCase() > b[field].toLowerCase()) {
                            return -1;
                        }
                        if (a[field].toLowerCase() < b[field].toLowerCase()) {
                            return 1;
                        }
                        return 0;
                    });
                }else{
                    data.sort(function(a,b) {
                        if (a[field].toLowerCase() < b[field].toLowerCase()) {
                            return -1;
                        }
                        if (a[field].toLowerCase() > b[field].toLowerCase()) {
                            return 1;
                        }
                            return 0;
                    });
                }
                return data
            }
        });

    angular
        .module('testapiApp')
        .directive('dynamicModel', ['$compile', '$parse', function ($compile, $parse) {
            return {
                restrict: 'A',
                terminal: true,
                priority: 100000,
                link: function (scope, elem) {
                    var name = $parse(elem.attr('dynamic-model'))(scope);
                    elem.removeAttr('dynamic-model');
                    elem.attr('ng-model', name);
                    $compile(elem)(scope);
                }
            };
        }]);

    angular
        .module('testapiApp')
        .directive('ngEnter', function () {
            return function (scope, element, attrs) {
                element.bind("keydown keypress", function (event) {
                    if(event.which === 13) {
                        scope.$apply(function (){
                            scope.$eval(attrs.ngEnter);
                        });
                        event.preventDefault();
                    }
                });
            };
        });

    configureRoutes.$inject = ['$stateProvider', '$urlRouterProvider'];

    /**
     * Handle application routing. Specific templates and controllers will be
     * used based on the URL route.
     */
    function configureRoutes($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise('/');
        $stateProvider.
            state('home', {
                url: '/',
                templateUrl: 'testapi-ui/components/home/home.html'
            }).
            state('about', {
                url: '/about',
                templateUrl: 'testapi-ui/components/about/about.html'
            }).
            state('pods', {
                url: '/pods',
                templateUrl: 'testapi-ui/components/pods/pods.html',
                controller: 'PodsController as ctrl'
            }).
            state('pod', {
                url: '/pods/:name',
                templateUrl: 'testapi-ui/components/pods/pod/pod.html',
                controller: 'PodController as ctrl'
            }).
            state('projects', {
                url: '/projects',
                templateUrl: 'testapi-ui/components/projects/projects.html',
                controller: 'ProjectsController as ctrl'
            }).
            state('project', {
                url: '/projects/:name',
                templateUrl: 'testapi-ui/components/projects/project/project.html',
                controller: 'ProjectController as ctrl'
            }).
            state('scenarios', {
                url: '/scenarios',
                templateUrl: 'testapi-ui/components/scenarios/scenarios.html',
                controller: 'ScenariosController as ctrl'
            }).
            state('scenario', {
                url: '/scenarios/:name',
                templateUrl: 'testapi-ui/components/scenarios/scenario/scenario.html',
                controller: 'ScenarioController as ctrl'
            }).
            state('testCase', {
                url: '/projects/:project_name/:name',
                templateUrl: 'testapi-ui/components/projects/project/testCases/testCase/testCase.html',
                controller: 'TestCaseController as ctrl'
            }).
            state('results', {
                url: '/results',
                templateUrl: 'testapi-ui/components/results/results.html',
                controller: 'ResultsController as ctrl'
            }).
            state('result', {
                url: '/result/:_id',
                templateUrl: 'testapi-ui/components/results/result/result.html',
                controller: 'ResultController as ctrl'
            }).
            state('deployresults', {
                url: '/deployresults',
                templateUrl: 'testapi-ui/components/deploy-results/deployResults.html',
                controller: 'DeployResultsController as ctrl'
            }).
            state('deployresult', {
                url: '/deployresults/:_id',
                templateUrl: 'testapi-ui/components/deploy-results/deploy-result/deployResult.html',
                controller: 'DeployResultController as ctrl'
            }).
            state('profile', {
                url: '/profile',
                templateUrl: 'testapi-ui/components/profile/profile.html',
                controller: 'ProfileController as ctrl'
            }).
            state('authFailure', {
                url: '/auth_failure',
                templateUrl: 'testapi-ui/components/home/home.html',
                controller: 'AuthFailureController as ctrl'
            }).
            state('logout', {
                url: '/logout',
                templateUrl: 'testapi-ui/components/logout/logout.html',
                controller: 'LogoutController as ctrl'
            });
    }

    angular
        .module('testapiApp')
        .config(disableHttpCache);

    disableHttpCache.$inject = ['$httpProvider'];

    /**
     * Disable caching in $http requests. This is primarily for IE, as it
     * tends to cache Angular IE requests.
     */
    function disableHttpCache($httpProvider) {
        if (!$httpProvider.defaults.headers.get) {
            $httpProvider.defaults.headers.get = {};
        }
        $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
        $httpProvider.defaults.headers.get.Pragma = 'no-cache';
    }

    angular
        .module('testapiApp')
        .run(setup);

    setup.$inject = [
        '$http', '$rootScope', '$window', '$state', 'testapiApiUrl', "authenticate"
    ];

    /**
     * Set up the app with injections into $rootscope. This is mainly for auth
     * functions.
     */
    function setup($http, $rootScope, $window, $state, testapiApiUrl, authenticate) {

        $rootScope.auth = {};
        $rootScope.authenticate = authenticate
        $rootScope.auth.doSignIn = doSignIn;
        $rootScope.auth.doSignOut = doSignOut;
        $rootScope.auth.doSignCheck = doSignCheck;
        $rootScope.auth.doSubmitterCheck = doSubmitterCheck;

        var sign_in_url = testapiApiUrl + '/auth/signin';
        var sign_out_url = testapiApiUrl + '/auth/signout';
        var profile_url = testapiApiUrl + '/profile';

        /** This function initiates a sign in. */
        function doSignIn() {
            $window.location.href = sign_in_url;
        }

        /** This function will initate a sign out. */
        function doSignOut() {
            if(authenticate){
                $rootScope.auth.currentUser = null;
                $rootScope.auth.isAuthenticated = false;
                $rootScope.auth.projectNames = [];
                $window.location.href = sign_out_url;
            }else{
                $state.go("home", {reload: true})
            }
        }

        /**
         * This function checks to see if a user is logged in and
         * authenticated.
         */
        function doSignCheck() {
            if(authenticate){
                return $http.get(profile_url, {withCredentials: true}).
                    success(function (data) {
                        $rootScope.auth.currentUser = data;
                        $rootScope.auth.isAuthenticated = true;
                        $rootScope.auth.projectNames = $rootScope.auth.doSubmitterCheck(data.groups);
                    }).
                    error(function () {
                        $rootScope.auth.currentUser = null;
                        $rootScope.auth.isAuthenticated = false;
                        $rootScope.auth.projectNames  = [];
                    });
            }else{
                $rootScope.auth.currentUser = null;
                $rootScope.auth.isAuthenticated = true;
                $rootScope.auth.projectNames = []
            }
        }

        function doSubmitterCheck(groups){
            var projectNames = []
            for(var index=0;index<groups.length; index++){
                if(groups[index].indexOf('-submitters')>=0){
                    projectNames.push(groups[index].split('-')[2])
                }
            }
            return projectNames;
        }

        $rootScope.auth.doSignCheck();
    }

    angular
        .element(document)
        .ready(loadConfig);

    /**
     * Load config and start up the angular application.
     */
    function loadConfig() {

        var $http = angular.injector(['ng']).get('$http');

        /**
         * Store config variables as constants, and start the app.
         */
        function startApp(config) {
            // Add config options as constants.
            angular.forEach(config, function(value, key) {
                angular.module('testapiApp').constant(key, value);
            });

            angular.bootstrap(document, ['testapiApp']);
        }

        $http.get('testapi-ui/config.json').success(function (data) {
            startApp(data);
        }).error(function () {
            startApp({});
        });
    }
})();