Remove unused files with company keywords
[portal.git] / ecomp-portal-FE-common / client / app / views / user-notifications-admin / user.notifications.modal.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
22 (function() {
23
24     class userNotificationsModalCtrl {
25         constructor($scope, $log, functionalMenuService, confirmBoxService, notificationService, $modal, ngDialog, $state, $filter, items) {
26             this.debug = false;
27             $scope.newNotifModel = {
28                 'isOnlineUsersOnly': null,
29                 'isForAllRolesOptions': null,
30                 'selectedPriority': null,
31                 'isActive': null,
32                 'startTime': '',
33                 'endTime': '',
34                 'msgHeader': null,
35                 'msgDescription': null,
36                 'roleIds': null,
37                 'anyTreeItemSelected': false,
38                 'roleObj': {
39                     notificationRoleIds: null
40                 }
41             };
42
43             $scope.notificationId = null;
44             $scope.selectedCat = null;
45             $scope.selectedEcompFunc = null;
46             this.YN_index_mapping = {
47                 "Y": 0,
48                 "N": 1
49             }
50
51             $scope.onlineAllUsersOptions = [{
52                     "index": 0,
53                     "value": "Y",
54                     "title": "Online Users Only"
55                 },
56                 {
57                     "index": 1,
58                     "value": "N",
59                     "title": "Online & Offline Users"
60                 }
61
62             ];
63
64             $scope.isForAllRolesOptions = [{
65                     "index": 0,
66                     "value": "Y",
67                     "title": "Yes"
68                 },
69                 {
70                     "index": 1,
71                     "value": "N",
72                     "title": "No"
73                 }
74             ];
75
76             $scope.priorityOptions = [{
77                     "index": 0,
78                     "value": 1,
79                     "title": "Normal"
80                 },
81                 {
82                     "index": 1,
83                     "value": 2,
84                     "title": "Important"
85                 }
86             ];
87
88             $scope.isActiveOptions = [{
89                     "index": 0,
90                     "value": "Y",
91                     "title": "Yes"
92                 },
93                 {
94                     "index": 1,
95                     "value": "N",
96                     "title": "No"
97                 }
98             ];
99             $scope.newNotifModel.isActive = $scope.isActiveOptions[0];
100             $scope.newNotifModel.selectPriority = $scope.priorityOptions[0];
101             $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[1];
102             $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[0].value;
103             $scope.newNotifModel.isFunctionalMenu = "Y";
104
105             $scope.newNotifModel.selectedPriority = $scope.priorityOptions[0].value;
106             $scope.newNotifModel.msgHeader = '';
107             $scope.newNotifModel.msgDescription = '';
108             $scope.newNotifModel.treeTitle = "Functional Menu";
109             $scope.newNotifModel.notifObj = {
110                 isCategoriesFunctionalMenu: true
111             };
112
113             let init = () => {
114                 if (this.debug)
115                     $log.debug('userNotificationsModalCtrl::init');
116                 this.isSaving = false;
117                 var today = new Date();
118                 $scope.minDate = today.toISOString().substring(0, 10);
119                 var threeMonthsFromNow = new Date();
120                 threeMonthsFromNow.setMonth(threeMonthsFromNow.getMonth() + 3);
121                 $scope.maxDate = threeMonthsFromNow.toISOString().substring(0, 10);
122                 if (items && items.notif) {
123                     if (this.debug)
124                         $log.debug('userNotificationsModalCtrl:init:: Edit notification mode for', items.notif);
125                     $scope.isEditMode = true;
126                     $scope.editModeObj = {
127                         isEditMode: true
128                     };
129                     this.notif = _.clone(items.notif);
130                     $scope.modalPgTitle = 'View Notification'
131                     $scope.newNotifModel.isOnlineUsersOnly = $scope.onlineAllUsersOptions[this.YN_index_mapping[this.notif.isForOnlineUsers]];
132                     $scope.newNotifModel.isForAllRoles = $scope.isForAllRolesOptions[this.YN_index_mapping[this.notif.isForAllRoles]].value;
133                     $scope.newNotifModel.isActive = $scope.isActiveOptions[this.YN_index_mapping[this.notif.activeYn]];
134                     $scope.newNotifModel.selectedPriority = $scope.priorityOptions[this.notif.priority - 1].value;
135                     $scope.newNotifModel.startTime = new Date(this.notif.startTime);
136                     $scope.newNotifModel.endTime = new Date(this.notif.endTime);
137                     $scope.newNotifModel.msgHeader = this.notif.msgHeader;
138                     $scope.newNotifModel.msgDescription = this.notif.msgDescription;
139                     $scope.notificationId = this.notif.notificationId;
140                     $scope.newNotifModel.notificationRoleIds = this.notif.roleIds;
141                     $scope.roleObj = {
142                         notificationRoleIds: this.notif.roleIds
143                     };
144                 } else {
145                     if (this.debug)
146                         $log.debug('AppDetailsModalCtrl:init:: New app mode');
147                     $scope.isEditMode = false;
148                     $scope.editModeObj = {
149                         isEditMode: false
150                     };
151                     $scope.modalPgTitle = 'Add a New Notification'
152                     this.notif = _.clone($scope.newNotifModel);
153                     $scope.roleObj = {
154                         notificationRoleIds: null
155                     };
156                 }
157             };
158             this.conflictMessages = {};
159             this.scrollApi = {};
160             let handleConflictErrors = err => {
161                 if (!err.data) {
162                     return;
163                 }
164                 if (!err.data.length) { // support objects
165                     err.data = [err.data]
166                 }
167                 _.forEach(err.data, item => {
168                     _.forEach(item.fields, field => {
169                         // set conflict message
170                         this.conflictMessages[field.name] = errorMessageByCode[item.errorCode];
171                         // set field as invalid
172                         $scope.appForm[field.name].$setValidity('conflict', false);
173                         // set watch once to clear error after user correction
174                         watchOnce[field.name]();
175                     });
176                 });
177                 this.scrollApi.scrollTop();
178             };
179
180             let resetConflict = fieldName => {
181                 delete this.conflictMessages[fieldName];
182                 if ($scope.appForm[fieldName]) {
183                     $scope.appForm[fieldName].$setValidity('conflict', true);
184                 }
185             };
186             $scope.addUserNotificationValidation = function() {
187                 // // pre-processing
188                 if (!($scope.isEditMode)) {
189                     var validation = false;
190                     if ($scope.isStartDateValidFromToday($scope.newNotifModel.startTime)&&$scope.isStartDateValidFromToday($scope.newNotifModel.endTime)&&$scope.isDateValid($scope.newNotifModel.startTime) && $scope.isDateValid($scope.newNotifModel.endTime) && $scope.newNotifModel.msgHeader != '' && $scope.newNotifModel.msgDescription != '' && ($scope.newNotifModel.startTime < $scope.newNotifModel.endTime)) {
191                         validation = true;
192                         if ($scope.newNotifModel.isForAllRoles == 'N') {
193                             validation = $scope.checkTreeSelect();
194                         }
195                     } else {
196                         validation = false;
197                     }
198                     return !validation;
199                 }
200             }
201
202             /* format the value for viewing a notification */
203             $scope.formatStartDate = function() {
204                 if ($scope.newNotifModel.startTime) {
205                     $scope.newNotifModel.startTime = $filter('date')($scope.startTime, 'medium');
206                 }
207             }
208
209             /* format the value for viewing a notification */
210             $scope.formatEndDate = function() {
211                 if ($scope.newNotifModel.endTime) {
212                     $scope.newNotifModel.endTime = $filter('date')($scope.endTime, 'medium');
213                 }
214             }
215             
216             /*To validate the manual entry of date in MM/DD/YYYY Format*/
217
218             $scope.isDateValid = function(time) {
219                 if (time == undefined) {
220                     return false;
221                 }
222                 if (typeof time == 'object') {
223                     return true;
224                 }
225                 var startDateformat = time.split('/');
226                 if (startDateformat.length != 3) return false;
227                 var day = startDateformat[1];
228                 var month = startDateformat[0];
229                 month = parseInt(month) - 1;
230                 var year = startDateformat[2];
231                 if (year.length != 4) return false;
232                 var composedDate = new Date(year, month, day);
233                  return composedDate.getDate() == day &&
234                          composedDate.getMonth() == month &&
235                          composedDate.getFullYear() == year;
236             };
237             
238          /*The manual and drop down calendar should be consistent.
239          Start date must be greater than or equal to current date.The end dates are not allowed after the 3 months from current dates*/
240             
241             $scope.isStartDateValidFromToday = function (time) {
242                 if(time == undefined){
243                     return false;
244                 }
245                 if(typeof time == 'object'){
246                         return true;
247                 }
248                 var startDateformat     =time.split('/');
249                 if (startDateformat.length != 3) return true;
250                 var day = startDateformat[1];
251                 var month = startDateformat[0];
252                  month= parseInt(month)-1;
253                 var year = startDateformat[2];
254                 if(year.length!=4) return true;
255                 var composedDate = new Date(year, month, day);
256               /* As end dates are not allowed after the 3 months from current dates*/
257                 var x = 3; //or whatever offset
258                 var CurrentDate = new Date();
259                 /*If composed date is less than the current date,error message should display*/
260                 if(composedDate<CurrentDate)
261                         return false;
262                 CurrentDate.setMonth(CurrentDate.getMonth() + x);
263                 if(composedDate>CurrentDate)
264                         return false;
265                  return true;
266             };
267
268
269             $scope.addUserNotification = function() {
270                 $scope.notificationRoleIds = [];
271                 // pre-processing
272
273                 for (var fi = 0; fi < $scope.treedata.length; fi++) {
274                     var fLevel = $scope.treedata[fi];
275                     if (fLevel) {
276                         var fLevelChild = fLevel.child;
277                         if (fLevelChild) {
278                             for (var si = 0; si < fLevelChild.length; si++) {
279                                 var sLevel = fLevelChild[si];
280                                 if (sLevel) {
281                                     var sLevelChild = sLevel.child;
282                                     if (sLevelChild) {
283                                         for (var ti = 0; ti < sLevelChild.length; ti++) {
284                                             var tLevel = sLevelChild[ti];
285                                             if (tLevel.isSelected && tLevel.roleId) {
286                                                 $scope.newNotifModel.anyTreeItemSelected = true;
287                                                 for (var i in tLevel.roleId)
288                                                     $scope.notificationRoleIds.push(tLevel.roleId[i]);
289                                             }
290                                         }
291                                     }
292                                 }
293                                 if (sLevel.isSelected && sLevel.roleId) {
294                                     for (var i in sLevel.roleId)
295                                         $scope.notificationRoleIds.push(sLevel.roleId[i]);
296                                 }
297                             }
298                         }
299                     }
300                 }
301
302                 $scope.notificationRoleIds.sort();
303                 if (($scope.newNotifModel.isOnlineUsersOnly) && ($scope.newNotifModel.isForAllRoles) && ($scope.newNotifModel.selectedPriority) && ($scope.newNotifModel.isActive) &&
304                     ($scope.newNotifModel.startTime) && ($scope.newNotifModel.endTime) && ($scope.newNotifModel.msgHeader != '') && ($scope.newNotifModel.msgDescription != '')) {
305                     this.newUserNotification = {
306                         'notificationId': $scope.notificationId,
307                         'isForOnlineUsers': $scope.newNotifModel.isOnlineUsersOnly.value,
308                         'isForAllRoles': $scope.newNotifModel.isForAllRoles,
309                         'priority': $scope.newNotifModel.selectedPriority,
310                         'activeYn': $scope.newNotifModel.isActive.value,
311                         'startTime': $scope.newNotifModel.startTime,
312                         'endTime': $scope.newNotifModel.endTime,
313                         'msgHeader': $scope.newNotifModel.msgHeader,
314                         'msgDescription': $scope.newNotifModel.msgDescription,
315                         'roleIds': $scope.notificationRoleIds,
316                         'createdDate': new Date()
317                     };
318
319                     // POST ajax call here;
320                     if ($scope.isEditMode) {
321                         notificationService.updateAdminNotification(this.newUserNotification)
322                             .then(() => {
323                                 if (this.debug)
324                                     $log.debug('NotificationService:updateAdminNotification:: Admin notification update succeeded!');
325                                 $scope.closeThisDialog(true);
326                             }).catch(err => {
327                                 $log.error('notificationService.updateAdminNotfication failed: ' + JSON.stringify(err));
328                                 switch (err.status) {
329                                     case '409': // Conflict
330                                         // handleConflictErrors(err);
331                                         break;
332                                     case '500': // Internal Server Error
333                                         confirmBoxService.showInformation('There was a problem updating the notification. ' +
334                                             'Please try again later. Error: ' + err.status).then(isConfirmed => {});
335                                         break;
336                                     case '403': // Forbidden... possible
337                                         // webjunction error to
338                                         // try again
339                                         confirmBoxService.showInformation('There was a problem updating the notification. ' +
340                                             'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
341                                         break;
342                                     default:
343                                         confirmBoxService.showInformation('There was a problem updating the notification. ' +
344                                             'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
345                                 }
346                             }).finally(() => {
347                                 // for bug in IE 11
348                                 var objOffsetVersion = objAgent.indexOf("MSIE");
349                                 if (objOffsetVersion != -1) {
350                                     if (this.debug)
351                                         $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
352                                     $window.location.reload();
353                                 }
354                                 // for bug in IE 11
355                             });
356
357                     } else {
358                         notificationService.addAdminNotification(this.newUserNotification)
359                             .then((res) => {
360                                 if (this.debug)
361                                     $log.debug('notificationService:addAdminNotification:: Admin notification creation succeeded!,', res);
362                                 if (res.status == 'ERROR') {
363                                     confirmBoxService.showInformation('There was a problem adding the notification. ' +
364                                         ' Error: ' + res.response).then(isConfirmed => {});
365
366
367                                 } else {
368                                     // $scope.closeThisDialog(true);
369                                     $scope.$dismiss('cancel');
370                                 }
371
372                                 // emptyCookies();
373                             }).catch(err => {
374                                 switch (err.status) {
375                                     case '409': // Conflict
376                                         // handleConflictErrors(err);
377                                         break;
378                                     case '500': // Internal Server Error
379                                         confirmBoxService.showInformation('There was a problem adding the notification. ' +
380                                             'Please try again later. Error: ' + err.status).then(isConfirmed => {});
381                                         break;
382                                     default:
383                                         confirmBoxService.showInformation('There was a problem adding the notification. ' +
384                                             'Please try again. If the problem persists, then try again later. Error: ' +
385                                             err.status).then(isConfirmed => {});
386                                 }
387                                 $log.error('notificationService:addAdminNotification error:: ' + JSON.stringify(err));
388                             })
389                     }
390
391
392                 } else {
393                     $log.warn('please fill in all required fields');
394                     confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
395                 }
396             }
397             $scope.functionalMenuRes = {};
398             $scope.checkTreeSelect = function() {
399                 if ($scope.treedata) {
400                     for (var fi = 0; fi < $scope.treedata.length; fi++) {
401                         var fLevel = $scope.treedata[fi];
402                         if (fLevel.isSelected) {
403                             return true;
404                         }
405                         var sLevel = fLevel.child;
406                         if (sLevel) {
407                             for (var si = 0; si < sLevel.length; si++) {
408                                 if (sLevel[si].isSelected) {
409                                     return true;
410                                 }
411                                 var tLevel = sLevel[si].child;
412                                 if (tLevel) {
413                                     for (var ti = 0; ti < tLevel.length; ti++) {
414                                         if (tLevel[ti].isSelected) {
415                                             return true;
416                                         }
417                                     }
418                                 }
419                             }
420                         }
421                     }
422                 }
423                 return false;
424             }
425             // Populate the category list for category dropdown list
426             let getFunctionalMenu = () => {
427                 this.isLoadingTable = true;
428                 if (this.debug)
429                     $log.debug('getFunctionalMenu:init');
430
431                 functionalMenuService.getFunctionalMenuRole().then(role_res => {
432                     var menu_role_dict = {};
433                     if (this.debug)
434                         $log.debug('functionalMenuService:getFunctionalMenuRole:: getting result', role_res);
435
436                     for (var i in role_res) {
437                         // if first time appear in menu_role_dict
438                         if (!(role_res[i].menuId in menu_role_dict)) {
439                             menu_role_dict[role_res[i].menuId] = [role_res[i].roleId];
440                         } else {
441                             menu_role_dict[role_res[i].menuId].push(role_res[i].roleId);
442                         }
443                     }
444
445                     functionalMenuService.getManagedFunctionalMenuForNotificationTree().then(res => {
446                         if (this.debug)
447                             $log.debug('functionalMenuService:getManagedFunctionalMenuForNotificationTree:: getting result', res);
448                         var exclude_list = ['Favorites'];
449                         let actualData = [];
450                         $scope.functionalMenuRes = res;
451
452                         //Adding children and label attribute to all objects in res
453                         for (let i = 0; i < res.length; i++) {
454                             res[i].child = [];
455                             res[i].name = res[i].text;
456                             res[i].id = res[i].text;
457                             res[i].displayCheckbox = true;
458                             $scope.checkBoxObj = {
459                                 isAnyRoleSelected: false
460                             };
461                             res[i].roleId = menu_role_dict[res[i].menuId];
462                             res[i].onSelect = function() {
463                                 $scope.$apply(function() {
464                                     $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
465                                 })
466                             };
467
468                             if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
469                                 res[i].isSelected = true;
470                                 res[i].selected = true;
471                                 res[i].indeterminate = false;
472                             } else {
473                                 /*default*/
474                                 res[i].isSelected = false;
475                                 res[i].selected = false;
476                                 res[i].indeterminate = false;
477                             }
478                         }
479
480                         // Adding actual child items to children array in res
481                         // objects
482                         $scope.parentChildDict = {};
483                         $scope.parentChildRoleIdDict = {};
484                         for (let i = 0; i < res.length; i++) {
485                             let parentId = res[i].menuId;
486                             $scope.parentChildDict[parentId] = [];
487                             $scope.parentChildRoleIdDict[parentId] = [];
488                             for (let j = 0; j < res.length; j++) {
489                                 let childId = res[j].parentMenuId;
490                                 if (parentId === childId) {
491                                     res[i].child.push(res[j]);
492                                     $scope.parentChildDict[parentId].push(res[j].menuId);
493                                     //if res[j].roleId is defined
494                                     if (res[j].roleId) {
495                                         for (let k in res[j].roleId) {
496                                             $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
497                                         }
498
499                                     }
500                                 }
501                             }
502                         }
503                         //check if grand children exist
504                         for (var key in $scope.parentChildDict) {
505                             var child = $scope.parentChildDict[key];
506                             var isGrandParent = false;
507                             if (child.length > 0) {
508                                 for (var i in child) {
509                                     if ($scope.parentChildDict[child[i]].length > 0) {
510                                         isGrandParent = true;
511                                         break;
512                                     }
513                                 }
514                             }
515                             if (isGrandParent) {
516                                 for (var i in child) {
517                                     // if the child has children
518                                     if ($scope.parentChildDict[child[i]].length > 0) {
519                                         for (var j in $scope.parentChildRoleIdDict[child[i]]) {
520                                             if ($scope.parentChildRoleIdDict[key].indexOf($scope.parentChildRoleIdDict[child[i]][j]) === -1) {
521                                                 $scope.parentChildRoleIdDict[key].push($scope.parentChildRoleIdDict[child[i]][j]);
522                                             }
523                                         }
524                                     }
525                                 }
526                             }
527
528                         };
529
530                         // Sort the top-level menu items in order based on the column
531                         res.sort(function(a, b) {
532                             return a.column - b.column;
533                         });
534
535                         // Sort all the child in order based on the column
536                         for (let i = 0; i < res.length; i++) {
537                             res[i].child.sort(function(a, b) {
538                                 return a.column - b.column;
539                             });
540                         }
541
542                         //Forming actual parent items
543                         for (let i = 0; i < res.length; i++) {
544                             let parentId = res[i].parentMenuId;
545                             if (parentId === null) {
546                                 actualData.push(res[i]);
547                             }
548                         }
549                         var treedata = actualData[0].child;
550                         $scope.treedata = [];
551
552                         /*Remove favorite from the list */
553                         for (var i in treedata) {
554                             if (!(treedata[i].name.indexOf(exclude_list) > -1)) {
555                                 $scope.treedata.push(treedata[i])
556                             }
557                         }
558                         //setting b2b tree parameter
559                         $scope.settingTreeParam();
560
561                     }).catch(err => {
562                         $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
563                     }).finally(() => {
564                         this.isLoadingTable = false;
565                     })
566
567                 }).catch(err => {
568                     $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
569                 });
570             }
571
572
573             let getAppRoleIds = () => {
574                 $scope.notifObj = {
575                     isCategoriesFunctionalMenu: false
576                 };
577                 notificationService.getAppRoleIds().then(res => {
578                     if (this.debug)
579                         $log.debug('notificationService:getAppRoleIds:: getting result', res);
580                     res = res.data;
581                     let actualData = [];
582                     var app_id_name_list = {};
583                     $scope.checkBoxObj = {
584                         isAnyRoleSelected: false
585                     };
586                     for (let i = 0; i < res.length; i++) {
587                         if (!(res[i].appId in app_id_name_list)) {
588                             app_id_name_list[res[i].appId] = res[i].appName;
589                         }
590                         res[i].child = [];
591                         res[i].name = res[i].roleName;
592                         res[i].displayCheckbox = true;
593                         res[i].id = res[i].roleId;
594                         res[i].menuId = res[i].roleId;
595                         res[i].parentMenuId = res[i].appId;
596                         res[i].can_check = true;
597                         res[i].roleId = [res[i].roleId];
598                         res[i].onSelect = function() {
599                             $scope.$apply(function() {
600                                 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
601                             })
602                         };
603                         /*assigning selected value*/
604                         if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
605                             res[i].isSelected = true;
606                             res[i].selected = true;
607                             res[i].indeterminate = false;
608                         } else {
609                             /*default*/
610                             res[i].isSelected = false;
611                             res[i].selected = false;
612                             res[i].indeterminate = false;
613                         }
614                     }
615
616                     for (var app_id in app_id_name_list) {
617                         var new_res = {};
618                         new_res.child = [];
619                         new_res.name = app_id_name_list[app_id];
620                         new_res.id = app_id;
621                         new_res.displayCheckbox = true;
622                         new_res.menuId = app_id;
623                         new_res.parentMenuId = null;
624                         new_res.appId = null;
625                         new_res.can_check = true;
626                         new_res.roleId = null;
627                         new_res.onSelect = function() {
628                             $scope.$apply(function() {
629                                 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
630                             })
631                         };
632                         res.push(new_res);
633                     }
634                     $scope.parentChildRoleIdDict = {};
635                     //Adding actual child items to child array in res objects
636                     for (let i = 0; i < res.length; i++) {
637                         let parentId = res[i].menuId;
638                         $scope.parentChildRoleIdDict[parentId] = [];
639                         for (let j = 0; j < res.length; j++) {
640                             let childId = res[j].parentMenuId;
641                             if (parentId == childId) {
642                                 res[i].child.push(res[j]);
643                                 if (res[j].roleId) {
644                                     for (let k in res[j].roleId) {
645                                         $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
646                                     }
647
648                                 }
649                             }
650                         }
651                     }
652                     //Forming actual parent items
653                     for (let i = 0; i < res.length; i++) {
654                         let parentId = res[i].parentMenuId;
655                         if (parentId === null) {
656                             actualData.push(res[i]);
657                         }
658                     }
659
660                     $scope.treedata = actualData;
661                     //setting correct parameters for b2b tree
662                     $scope.settingTreeParam();
663                 }).catch(err => {
664                     $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
665                 }).finally(() => {
666                     this.isLoadingTable = false;
667                 })
668             }
669             $scope.getFunctionalMenu = function() {
670                 $scope.treeTitle = "Functional Menu";
671                 getFunctionalMenu();
672             }
673             $scope.getAppRoleIds = function() {
674                 $scope.treeTitle = "Applications/Roles";
675                 getAppRoleIds();
676             }
677
678
679
680             $scope.settingTreeParam = function() {
681                 /**************first level****************/
682                 for (var fi = 0; fi < $scope.treedata.length; fi++) {
683                     var fLevel = $scope.treedata[fi];
684                     var sLevel = $scope.treedata[fi].child;
685                     var sLevelSelectedCount = 0;
686                     var sLevelChildNumber = 0
687                     if (fLevel.child.length == 0 && fLevel.roleId == null) {
688                         delete fLevel.child;
689                     } else if (sLevel) {
690                         /**************Second level****************/
691                         var sLevelDelArray = [];
692                         for (var si = 0; si < sLevel.length; si++) {
693                             var deletThisSLev = false;
694                             if (sLevel[si].child.length == 0 && sLevel[si].roleId == null) {
695                                 sLevel[si].displayCheckbox = false;
696                                 sLevelDelArray.push(sLevel[si].name);
697                                 sLevel[si].name = '';
698                                 sLevel[si].active = false;
699                                 delete sLevel[si].child;
700                             } else if (sLevel[si].child.length == 0) {
701                                 delete sLevel[si].child;
702                             } else {
703                                 /**************Third level****************/
704                                 var tLevel = sLevel[si].child;
705                                 var tLevelSelectedCount = 0;
706                                 var tLevelChildNumber = 0;
707                                 if (tLevel) {
708                                     var tLevelDelArray = [];
709                                     var tLevelLen = tLevel.length;
710                                     var tLevelRoleIdUndefined = 0;
711                                     for (var ti = 0; ti < tLevel.length; ti++) {
712                                         delete tLevel[ti].child;
713                                         if (tLevel[ti].roleId == null) {
714                                             tLevel[ti].displayCheckbox = false;
715                                             tLevelDelArray.push(tLevel[ti].name);
716                                             tLevel[ti].name = '';
717                                             tLevel[ti].active = false;
718                                             tLevelRoleIdUndefined++
719                                         } else {
720                                             if (tLevel[ti].isSelected)
721                                                 tLevelSelectedCount++;
722
723                                             if (tLevel[ti].displayCheckbox)
724                                                 tLevelChildNumber++;
725                                         }
726                                     }
727                                     if (tLevelRoleIdUndefined == tLevelLen)
728                                         deletThisSLev = true;
729                                     if (tLevelSelectedCount == tLevelChildNumber) {
730                                         sLevel[si].isSelected = true;
731                                         sLevel[si].indeterminate = false;
732                                         sLevel[si].active = true;
733                                     } else if (tLevelSelectedCount > 0) {
734                                         sLevel[si].indeterminate = true;
735                                         sLevel[si].active = true;
736                                     }
737
738                                     /*Cleanup unused third level items*/
739                                     for (var i = 0; i < tLevelDelArray.length; i++) {
740                                         var name = tLevelDelArray[i];
741                                         for (var ti = 0; ti < tLevel.length; ti++) {
742                                             if (name == tLevel[ti].text) {
743                                                 tLevel.splice(ti, 1);
744                                                 break;
745                                             }
746                                         }
747                                     }
748                                 }
749                             }
750                             if (deletThisSLev) { //remove the whole second level item if all it's child has no roleId
751                                 sLevel[si].displayCheckbox = false;
752                                 sLevelDelArray.push(sLevel[si].name);
753                                 sLevel[si].name = '';
754                                 sLevel[si].active = false;
755                             } else {
756                                 if (sLevel[si].isSelected)
757                                     sLevelSelectedCount++;
758                                 if (sLevel[si].displayCheckbox)
759                                     sLevelChildNumber++;
760                             }
761                         }
762                         if (sLevelSelectedCount == sLevelChildNumber && sLevelChildNumber != 0) {
763                             fLevel.isSelected = true;
764                             fLevel.indeterminate = false;
765                             fLevel.active = true;
766                         } else if (sLevelSelectedCount > 0) {
767                             fLevel.indeterminate = true;
768                             fLevel.active = true;
769                         } else {
770                             //fLevel.active=false;
771                             fLevel.indeterminate = false;
772                         }
773                         /*Cleanup unused second level items*/
774                         for (var i = 0; i < sLevelDelArray.length; i++) {
775                             var name = sLevelDelArray[i];
776                             for (var si = 0; si < sLevel.length; si++) {
777                                 if (name == sLevel[si].text) {
778                                     sLevel.splice(si, 1);
779                                     break;
780                                 }
781                             }
782                         }
783                     }
784                 }
785             }
786             init();
787             getFunctionalMenu();
788
789         }
790
791     }
792
793     userNotificationsModalCtrl.$inject = ['$scope', '$log', 'functionalMenuService', 'confirmBoxService', 'notificationService', '$modal', 'ngDialog', '$state', '$filter', 'items'];
794     angular.module('ecompApp').controller('userNotificationsModalCtrl', userNotificationsModalCtrl);
795
796     angular.module('ecompApp').directive('attDatepickerCustom', ['$log', function($log) {
797         return {
798             restrict: 'A',
799             require: 'ngModel',
800             scope: {},
801
802             controller: ['$scope', '$element', '$attrs', '$compile', 'datepickerConfig', 'datepickerService', function($scope, $element, $attrs, $compile, datepickerConfig, datepickerService) {
803                 var dateFormatString = angular.isDefined($attrs.dateFormat) ? $scope.$parent.$eval($attrs.dateFormat) : datepickerConfig.dateFormat;
804                 var selectedDateMessage = '<div class="sr-focus hidden-spoken" tabindex="-1">the date you selected is {{$parent.current | date : \'' + dateFormatString + '\'}}</div>';
805                 $element.removeAttr('att-datepicker-custom');
806                 $element.removeAttr('ng-model');
807                 $element.attr('ng-value', '$parent.current |  date:"EEEE, MMMM d, y"');
808                 $element.attr('aria-describedby', 'datepicker');
809
810                 $element.attr('maxlength', 10);
811
812                 var wrapperElement = angular.element('<div></div>');
813                 wrapperElement.attr('datepicker-popup', '');
814                 wrapperElement.attr('current', 'current');
815
816                 datepickerService.setAttributes($attrs, wrapperElement);
817                 datepickerService.bindScope($attrs, $scope);
818
819                 wrapperElement.html('');
820                 wrapperElement.append($element.prop('outerHTML'));
821                 if (navigator.userAgent.match(/MSIE 8/) === null) {
822                     wrapperElement.append(selectedDateMessage);
823                 }
824                 var elm = wrapperElement.prop('outerHTML');
825                 elm = $compile(elm)($scope);
826                 $element.replaceWith(elm);
827             }],
828             link: function(scope, elem, attr, ctrl) {
829                 if (!ctrl) {
830                     // do nothing if no ng-model
831                     $log.error("ng-model is required.");
832                     return;
833                 }
834
835                 scope.$watch('current', function(value) {
836                     ctrl.$setViewValue(value);
837                 });
838                 ctrl.$render = function() {
839                     scope.current = ctrl.$viewValue;
840                 };
841
842             }
843         };
844     }]);
845
846
847 })();