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