summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/ui/components/projects/projectsController.js
blob: 42100bdd842c8a989c72d2e202cad226978e5c63 (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
312
313
314
315
316
317
318
319
/*
 * 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';

    angular
        .module('testapiApp')
        .controller('ProjectsController', ProjectsController);

        ProjectsController.$inject = [
        '$scope', '$http', '$filter', '$state', '$window', '$uibModal', 'testapiApiUrl',
        'raiseAlert', 'confirmModal', 'authenticate', 'keepState', 'sortService', '$timeout'
    ];

    /**
     * TestAPI Project Controller
     * This controller is for the '/projects' page where a user can browse
     * through projects declared in TestAPI.
     */
    function ProjectsController($scope, $http, $filter, $state, $window, $uibModal, testapiApiUrl,
        raiseAlert, confirmModal, authenticate, keepState, sortService, $timeout) {
        var ctrl = this;
        ctrl.url = testapiApiUrl + '/projects';

        ctrl.create = create;
        ctrl.listProjects = listProjects;
        ctrl.openCreateModal = openCreateModal;
        ctrl.viewProject = viewProject;
        ctrl.openUpdateModal = openUpdateModal;
        ctrl.update = update;
        ctrl.openDeleteModal = openDeleteModal;
        ctrl.openBatchDeleteModal = openBatchDeleteModal;
        ctrl.projectDelete = projectDelete;
        ctrl.batchDelete = batchDelete;
        ctrl.sortByName = sortByName

        ctrl.checkBox = [];
        ctrl.checkBoxList = [];
        ctrl.name = '';
        ctrl.details = '';
        ctrl.ascending = false;
        ctrl.toastError = toastError
        ctrl.toastSuccess = toastSuccess

        function toastError() {
            ctrl.showError = true
            $timeout(function(){ ctrl.showError = false;}, 7000);
        }

        function toastSuccess() {
            ctrl.showSuccess = true
            $timeout(function(){ ctrl.showSuccess = false;}, 7000);
        }

        /**
         * This will contact the TestAPI to create a new project.
         */
        function create(project) {
            var projects_url = ctrl.url;
            var body = {
                name: project.name,
                description: project.description
            };
            ctrl.projectsRequest = $http.post(projects_url, body)
            ctrl.projectsRequest.success(function (data){
                    ctrl.success = "Project is successfully created.";
                    ctrl.listProjects();
                    ctrl.toastSuccess();
                    ctrl.request = true;
                }).catch(function (data) {
                    ctrl.error = data.statusText;
                    ctrl.toastError();
                    ctrl.request = false;
                });

            return ctrl.projectsRequest
        }

        function sortByName(){
            ctrl.data.projects = sortService.sortFunction(ctrl.data.projects, 'name', ctrl.ascending)
            ctrl.ascending = !ctrl.ascending
        }

        /**
         * This will open the modal that will show the create
         * project view
         */
        function openCreateModal(){
            $uibModal.open({
                templateUrl: 'testapi-ui/components/projects/modals/projectModal.html',
                controller: 'ProjectModalCtrl as ProjectModalCtrl',
                size: 'md',
                resolve: {
                    data: function () {
                        return {
                            text: "Create Project",
                            successHandler: ctrl.create
                        };
                    }
                }
            });
        }

        /**
         * This will open the modal that will show the update
         * project view
         */
        function openUpdateModal(name){
            var project;
            var index;
            for(index in ctrl.data.projects){
                if(ctrl.data.projects[index].name==name){
                    project = ctrl.data.projects[index]
                    continue
                }
            }
            $uibModal.open({
                templateUrl: 'testapi-ui/components/projects/modals/projectModal.html',
                controller: 'ProjectModalCtrl as ProjectModalCtrl',
                size: 'md',
                resolve: {
                    data: function () {
                        return {
                            text: "Update Project",
                            successHandler: ctrl.update,
                            project : project
                        };
                    }
                }
            });
        }

        /**
         * This will contact the TestAPI to update an existing test case.
         */
        function update(name, project) {
            ctrl.showError = false;
            ctrl.showSuccess = false;
            var projectUrl = ctrl.url + '/' + name;
            ctrl.projectRequest = $http.put(projectUrl, project)
            ctrl.projectRequest.success(function (data){
                    ctrl.success = "Project is successfully updated."
                    ctrl.listProjects();
                    ctrl.toastSuccess();
                })
                .catch(function (data) {
                    ctrl.error = data.statusText;
                    ctrl.toastError();
                });
            return ctrl.projectRequest
        }

        /**
         * This will contact the TestAPI to get a listing of declared projects.
         */
        function listProjects() {
            ctrl.showError = false;
            var content_url = ctrl.url + '?';
            var filterText  = ctrl.filterText;
            if(filterText != undefined){
                content_url = content_url + 'name=' +
                filterText;
            }
            else if(keepState.filter.projectFilter){
                for (var filter in keepState.filter.projectFilter){
                    content_url = content_url + filter + '=' + keepState.filter.projectFilter[filter]
                    ctrl.filterText = keepState.filter.projectFilter[filter]
                }
            }
            ctrl.resultsRequest =
                $http.get(content_url).success(function (data) {
                    ctrl.data = data;
                    if(ctrl.filterText != undefined){
                        keepState.filter.projectFilter = {
                            'name': ctrl.filterText
                        }
                    }
                }).catch(function (data)  {
                    ctrl.data = null;
                    ctrl.error = data.statusText;
                    ctrl.toastError();
                });
        }

        function viewProject(name){
            $state.go('project', {'name':name}, {reload: true});
        }

        /**
         * This will contact the TestAPI to delete a project for given
         * name.
         */
        function projectDelete(projectName){
            var projectUrl = ctrl.url + "/" + projectName
            $http.delete(projectUrl).success(function(){
                ctrl.success = "Projects is successfully deleted"
                ctrl.toastSuccess();
                ctrl.listProjects();
            }).catch(function (data)  {
                ctrl.error = data.statusText;
                ctrl.toastError();
            });
        }

        /**
         * This will  delete list of projects.
         */
        function batchDelete(){
            var index;
            var checkedBox = [];
            ctrl.checkBox.forEach(function(project, index){
                if(!ctrl.showError){
                    if(project){
                        projectDelete(ctrl.data.projects[index].name);
                    }
                }
              });
            ctrl.checkBox = []
        }

        /**
         * This will open the modal that will show the batch delete confirm
         * message
         */
        function openBatchDeleteModal() {
            var deleteObjects = []
            ctrl.checkBox.forEach(function(project, index){
                if(!ctrl.showError){
                    if(project){
                        deleteObjects.push(ctrl.data.projects[index].name)
                    }
                }
              });
            confirmModal("Delete", 'projects', ctrl.batchDelete, deleteObjects);
        }

        /**
         * This will open the modal that will show the delete confirm
         * message
         */
        function openDeleteModal(name) {
            confirmModal("Delete",'projects', ctrl.projectDelete, name);
        }

        ctrl.listProjects();
    }

    /**
     * TestAPI Project  Modal Controller
     * This controller is for the create modal where a user can create
     * the project information and for the edit modal where user can
     * edit the project's details
     */
    angular.module('testapiApp').controller('ProjectModalCtrl', ProjectModalCtrl);
    ProjectModalCtrl.$inject = ['$scope', '$uibModalInstance', 'data'];
    function ProjectModalCtrl($scope, $uibModalInstance, data) {
        var ctrl = this;
        ctrl.confirm = confirm;
        ctrl.cancel = cancel;
        ctrl.data = angular.copy(data);
        ctrl.createRequirements = [
            {label: 'name', type: 'text', required: true},
            {label: 'description', type: 'textarea', required: false}
        ];
        ctrl.project = {
            name : '',
            description : ''
        }
        if(ctrl.data.project){
            ctrl.projectName = ctrl.data.project.name
            ctrl.project = ctrl.data.project
            delete ctrl.project._id;
        }

        /**
         * Initiate confirmation and call the success handler with the
         * inputs.
         */
        function confirm() {
            if (angular.isDefined(ctrl.data.successHandler)) {
                if(ctrl.project.name != ""){
                    var success = false;
                    if(ctrl.data.project){
                       ctrl.data.successHandler(ctrl.projectName, ctrl.project).success(function (data){
                            $uibModalInstance.close();
                       })
                    }else{
                        ctrl.data.successHandler(ctrl.project).success(function (data){
                            $uibModalInstance.close();
                        })
                    }
                }else{
                    ctrl.showCreateError = true;
                    ctrl.error = 'Name is missing.'
                }
            }
        }

        /**
         * Close the confirm modal without initiating changes.
         */
        function cancel() {
            $uibModalInstance.dismiss('cancel');
        }
    }

})();