[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / directives / search-users / search-users.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 'use strict';
21 (function () {
22     class SearchUsersCtrl {
23         constructor($log, usersService, $scope) {
24             $scope.UserSearchsIsNull=false;
25             this.scrollApi = {};//scrollTop directive
26             $scope.txtResults = 'result';
27             /**
28              * Handle all active HTTP requests
29              * activeRequests @type {Array[requests with cancel option]}
30              */
31             let activeRequests = [];
32             let clearReq = (req) => {
33                 activeRequests.splice(activeRequests.indexOf(req), 1);
34             };
35
36             /**
37              * this function retrieves users info 
38              */
39             this.searchUsers = () => {
40                 this.isLoading = true;
41                 if(this.searchUsersInProgress){
42                     return;
43                 }
44                 this.selectedUser = null;
45                 this.searchUsersInProgress = true;
46                 this.searchUsersResults = null;
47
48                 let searchUsersReq = usersService.searchUsers(this.searchUserString);
49                 activeRequests.push(searchUsersReq);
50                 searchUsersReq.promise().then(usersList => {
51                     $log.debug('searchUsers found the following users: ', JSON.stringify(usersList));
52                     this.searchUsersResults = usersList;
53                     $log.debug('searchUsersResults length: ', usersList.length);
54                     if (usersList.length != 1) {
55                         $scope.txtResults = 'results'
56                     } else {
57                         $scope.txtResults = 'result'
58                     }
59                     $scope.UserSearchsIsNull=false;
60                 }).catch(err => {
61                     $log.error('SearchUsersCtrl.searchUsers: ' + err);
62                     $scope.UserSearchsIsNull=true;
63                 }).finally(() => {
64                     this.scrollApi.scrollTop();
65                     this.searchUsersInProgress = false;
66                     clearReq(searchUsersReq);
67                     this.isLoading = false;
68                 });
69             };
70
71             let init = () => {
72                 this.isLoading = false;
73                 this.searchUsersInProgress = false;
74             };
75
76             this.setSelectedUser = user => {
77                 this.selectedUser = user;
78             };
79
80             init();
81
82             $scope.$on('$destroy', () => {
83                 //cancel all active requests when closing the modal
84                 activeRequests.forEach(req => {
85                     req.cancel();
86                 });
87             });
88         }
89     }
90     SearchUsersCtrl.$inject = ['$log', 'usersService', '$scope'];
91     angular.module('ecompApp').controller('SearchUsersCtrl', SearchUsersCtrl);
92 })();