81384ad022b6b7db704b45d5173c445cc58e40a8
[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.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             $scope.isDateValid = function(time) {
217                 if (time == undefined) {
218                     return false;
219                 }
220                 if (typeof time == 'object') {
221                     return true;
222                 }
223                 var startDateformat = time.split('/');
224                 if (startDateformat.length != 3) return false;
225                 var day = startDateformat[1];
226                 var month = startDateformat[0];
227                 month = parseInt(month) - 1;
228                 var year = startDateformat[2];
229                 if (year.length != 4) return false;
230                 var composedDate = new Date(year, month, day);
231                 return composedDate.getDate() == day &&
232                     composedDate.getMonth() == month &&
233                     composedDate.getFullYear() == year;
234
235             };
236
237
238             $scope.addUserNotification = function() {
239                 $scope.notificationRoleIds = [];
240                 // pre-processing
241
242                 for (var fi = 0; fi < $scope.treedata.length; fi++) {
243                     var fLevel = $scope.treedata[fi];
244                     if (fLevel) {
245                         var fLevelChild = fLevel.child;
246                         if (fLevelChild) {
247                             for (var si = 0; si < fLevelChild.length; si++) {
248                                 var sLevel = fLevelChild[si];
249                                 if (sLevel) {
250                                     var sLevelChild = sLevel.child;
251                                     if (sLevelChild) {
252                                         for (var ti = 0; ti < sLevelChild.length; ti++) {
253                                             var tLevel = sLevelChild[ti];
254                                             if (tLevel.isSelected && tLevel.roleId) {
255                                                 $scope.newNotifModel.anyTreeItemSelected = true;
256                                                 for (var i in tLevel.roleId)
257                                                     $scope.notificationRoleIds.push(tLevel.roleId[i]);
258                                             }
259                                         }
260                                     }
261                                 }
262                                 if (sLevel.isSelected && sLevel.roleId) {
263                                     for (var i in sLevel.roleId)
264                                         $scope.notificationRoleIds.push(sLevel.roleId[i]);
265                                 }
266                             }
267                         }
268                     }
269                 }
270
271                 $scope.notificationRoleIds.sort();
272                 if (($scope.newNotifModel.isOnlineUsersOnly) && ($scope.newNotifModel.isForAllRoles) && ($scope.newNotifModel.selectedPriority) && ($scope.newNotifModel.isActive) &&
273                     ($scope.newNotifModel.startTime) && ($scope.newNotifModel.endTime) && ($scope.newNotifModel.msgHeader != '') && ($scope.newNotifModel.msgDescription != '')) {
274                     this.newUserNotification = {
275                         'notificationId': $scope.notificationId,
276                         'isForOnlineUsers': $scope.newNotifModel.isOnlineUsersOnly.value,
277                         'isForAllRoles': $scope.newNotifModel.isForAllRoles,
278                         'priority': $scope.newNotifModel.selectedPriority,
279                         'activeYn': $scope.newNotifModel.isActive.value,
280                         'startTime': $scope.newNotifModel.startTime,
281                         'endTime': $scope.newNotifModel.endTime,
282                         'msgHeader': $scope.newNotifModel.msgHeader,
283                         'msgDescription': $scope.newNotifModel.msgDescription,
284                         'roleIds': $scope.notificationRoleIds,
285                         'createdDate': new Date()
286                     };
287
288                     // POST ajax call here;
289                     if ($scope.isEditMode) {
290                         notificationService.updateAdminNotification(this.newUserNotification)
291                             .then(() => {
292                                 if (this.debug)
293                                     $log.debug('NotificationService:updateAdminNotification:: Admin notification update succeeded!');
294                                 $scope.closeThisDialog(true);
295                             }).catch(err => {
296                                 $log.error('notificationService.updateAdminNotfication failed: ' + JSON.stringify(err));
297                                 switch (err.status) {
298                                     case '409': // Conflict
299                                         // handleConflictErrors(err);
300                                         break;
301                                     case '500': // Internal Server Error
302                                         confirmBoxService.showInformation('There was a problem updating the notification. ' +
303                                             'Please try again later. Error: ' + err.status).then(isConfirmed => {});
304                                         break;
305                                     case '403': // Forbidden... possible
306                                         // webjunction error to
307                                         // try again
308                                         confirmBoxService.showInformation('There was a problem updating the notification. ' +
309                                             'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
310                                         break;
311                                     default:
312                                         confirmBoxService.showInformation('There was a problem updating the notification. ' +
313                                             'Please try again. If the problem persists, then try again later. Error: ' + err.status).then(isConfirmed => {});
314                                 }
315                             }).finally(() => {
316                                 // for bug in IE 11
317                                 var objOffsetVersion = objAgent.indexOf("MSIE");
318                                 if (objOffsetVersion != -1) {
319                                     if (this.debug)
320                                         $log.debug('AppDetailsModalCtrl:updateOnboardingApp:: Browser is IE, forcing Refresh');
321                                     $window.location.reload();
322                                 }
323                                 // for bug in IE 11
324                             });
325
326                     } else {
327                         notificationService.addAdminNotification(this.newUserNotification)
328                             .then((res) => {
329                                 if (this.debug)
330                                     $log.debug('notificationService:addAdminNotification:: Admin notification creation succeeded!,', res);
331                                 if (res.status == 'ERROR') {
332                                     confirmBoxService.showInformation('There was a problem adding the notification. ' +
333                                         ' Error: ' + res.response).then(isConfirmed => {});
334
335
336                                 } else {
337                                     // $scope.closeThisDialog(true);
338                                     $scope.$dismiss('cancel');
339                                 }
340
341                                 // emptyCookies();
342                             }).catch(err => {
343                                 switch (err.status) {
344                                     case '409': // Conflict
345                                         // handleConflictErrors(err);
346                                         break;
347                                     case '500': // Internal Server Error
348                                         confirmBoxService.showInformation('There was a problem adding the notification. ' +
349                                             'Please try again later. Error: ' + err.status).then(isConfirmed => {});
350                                         break;
351                                     default:
352                                         confirmBoxService.showInformation('There was a problem adding the notification. ' +
353                                             'Please try again. If the problem persists, then try again later. Error: ' +
354                                             err.status).then(isConfirmed => {});
355                                 }
356                                 $log.error('notificationService:addAdminNotification error:: ' + JSON.stringify(err));
357                             })
358                     }
359
360
361                 } else {
362                     $log.warn('please fill in all required fields');
363                     confirmBoxService.showInformation('Please fill in all required fields').then(isConfirmed => {});
364                 }
365             }
366             $scope.functionalMenuRes = {};
367             $scope.checkTreeSelect = function() {
368                 if ($scope.treedata) {
369                     for (var fi = 0; fi < $scope.treedata.length; fi++) {
370                         var fLevel = $scope.treedata[fi];
371                         if (fLevel.isSelected) {
372                             return true;
373                         }
374                         var sLevel = fLevel.child;
375                         if (sLevel) {
376                             for (var si = 0; si < sLevel.length; si++) {
377                                 if (sLevel[si].isSelected) {
378                                     return true;
379                                 }
380                                 var tLevel = sLevel[si].child;
381                                 if (tLevel) {
382                                     for (var ti = 0; ti < tLevel.length; ti++) {
383                                         if (tLevel[ti].isSelected) {
384                                             return true;
385                                         }
386                                     }
387                                 }
388                             }
389                         }
390                     }
391                 }
392                 return false;
393             }
394             // Populate the category list for category dropdown list
395             let getFunctionalMenu = () => {
396                 this.isLoadingTable = true;
397                 if (this.debug)
398                     $log.debug('getFunctionalMenu:init');
399
400                 functionalMenuService.getFunctionalMenuRole().then(role_res => {
401                     var menu_role_dict = {};
402                     if (this.debug)
403                         $log.debug('functionalMenuService:getFunctionalMenuRole:: getting result', role_res);
404
405                     for (var i in role_res) {
406                         // if first time appear in menu_role_dict
407                         if (!(role_res[i].menuId in menu_role_dict)) {
408                             menu_role_dict[role_res[i].menuId] = [role_res[i].roleId];
409                         } else {
410                             menu_role_dict[role_res[i].menuId].push(role_res[i].roleId);
411                         }
412                     }
413
414                     functionalMenuService.getManagedFunctionalMenuForNotificationTree().then(res => {
415                         if (this.debug)
416                             $log.debug('functionalMenuService:getManagedFunctionalMenuForNotificationTree:: getting result', res);
417                         var exclude_list = ['Favorites'];
418                         let actualData = [];
419                         $scope.functionalMenuRes = res;
420
421                         //Adding children and label attribute to all objects in res
422                         for (let i = 0; i < res.length; i++) {
423                             res[i].child = [];
424                             res[i].name = res[i].text;
425                             res[i].id = res[i].text;
426                             res[i].displayCheckbox = true;
427                             $scope.checkBoxObj = {
428                                 isAnyRoleSelected: false
429                             };
430                             res[i].roleId = menu_role_dict[res[i].menuId];
431                             res[i].onSelect = function() {
432                                 $scope.$apply(function() {
433                                     $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
434                                 })
435                             };
436
437                             if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
438                                 res[i].isSelected = true;
439                                 res[i].selected = true;
440                                 res[i].indeterminate = false;
441                             } else {
442                                 /*default*/
443                                 res[i].isSelected = false;
444                                 res[i].selected = false;
445                                 res[i].indeterminate = false;
446                             }
447                         }
448
449                         // Adding actual child items to children array in res
450                         // objects
451                         $scope.parentChildDict = {};
452                         $scope.parentChildRoleIdDict = {};
453                         for (let i = 0; i < res.length; i++) {
454                             let parentId = res[i].menuId;
455                             $scope.parentChildDict[parentId] = [];
456                             $scope.parentChildRoleIdDict[parentId] = [];
457                             for (let j = 0; j < res.length; j++) {
458                                 let childId = res[j].parentMenuId;
459                                 if (parentId === childId) {
460                                     res[i].child.push(res[j]);
461                                     $scope.parentChildDict[parentId].push(res[j].menuId);
462                                     //if res[j].roleId is defined
463                                     if (res[j].roleId) {
464                                         for (let k in res[j].roleId) {
465                                             $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
466                                         }
467
468                                     }
469                                 }
470                             }
471                         }
472                         //check if grand children exist
473                         for (var key in $scope.parentChildDict) {
474                             var child = $scope.parentChildDict[key];
475                             var isGrandParent = false;
476                             if (child.length > 0) {
477                                 for (var i in child) {
478                                     if ($scope.parentChildDict[child[i]].length > 0) {
479                                         isGrandParent = true;
480                                         break;
481                                     }
482                                 }
483                             }
484                             if (isGrandParent) {
485                                 for (var i in child) {
486                                     // if the child has children
487                                     if ($scope.parentChildDict[child[i]].length > 0) {
488                                         for (var j in $scope.parentChildRoleIdDict[child[i]]) {
489                                             if ($scope.parentChildRoleIdDict[key].indexOf($scope.parentChildRoleIdDict[child[i]][j]) === -1) {
490                                                 $scope.parentChildRoleIdDict[key].push($scope.parentChildRoleIdDict[child[i]][j]);
491                                             }
492                                         }
493                                     }
494                                 }
495                             }
496
497                         };
498
499                         // Sort the top-level menu items in order based on the column
500                         res.sort(function(a, b) {
501                             return a.column - b.column;
502                         });
503
504                         // Sort all the child in order based on the column
505                         for (let i = 0; i < res.length; i++) {
506                             res[i].child.sort(function(a, b) {
507                                 return a.column - b.column;
508                             });
509                         }
510
511                         //Forming actual parent items
512                         for (let i = 0; i < res.length; i++) {
513                             let parentId = res[i].parentMenuId;
514                             if (parentId === null) {
515                                 actualData.push(res[i]);
516                             }
517                         }
518                         var treedata = actualData[0].child;
519                         $scope.treedata = [];
520
521                         /*Remove favorite from the list */
522                         for (var i in treedata) {
523                             if (!(treedata[i].name.indexOf(exclude_list) > -1)) {
524                                 $scope.treedata.push(treedata[i])
525                             }
526                         }
527                         //setting b2b tree parameter
528                         $scope.settingTreeParam();
529
530                     }).catch(err => {
531                         $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
532                     }).finally(() => {
533                         this.isLoadingTable = false;
534                     })
535
536                 }).catch(err => {
537                     $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
538                 });
539             }
540
541
542             let getAppRoleIds = () => {
543                 $scope.notifObj = {
544                     isCategoriesFunctionalMenu: false
545                 };
546                 notificationService.getAppRoleIds().then(res => {
547                     if (this.debug)
548                         $log.debug('notificationService:getAppRoleIds:: getting result', res);
549                     res = res.data;
550                     let actualData = [];
551                     var app_id_name_list = {};
552                     $scope.checkBoxObj = {
553                         isAnyRoleSelected: false
554                     };
555                     for (let i = 0; i < res.length; i++) {
556                         if (!(res[i].appId in app_id_name_list)) {
557                             app_id_name_list[res[i].appId] = res[i].appName;
558                         }
559                         res[i].child = [];
560                         res[i].name = res[i].roleName;
561                         res[i].displayCheckbox = true;
562                         res[i].id = res[i].roleId;
563                         res[i].menuId = res[i].roleId;
564                         res[i].parentMenuId = res[i].appId;
565                         res[i].can_check = true;
566                         res[i].roleId = [res[i].roleId];
567                         res[i].onSelect = function() {
568                             $scope.$apply(function() {
569                                 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
570                             })
571                         };
572                         /*assigning selected value*/
573                         if (res[i].roleId && res[i].roleId.length == _.intersection(res[i].roleId, $scope.roleObj.notificationRoleIds).length) {
574                             res[i].isSelected = true;
575                             res[i].selected = true;
576                             res[i].indeterminate = false;
577                         } else {
578                             /*default*/
579                             res[i].isSelected = false;
580                             res[i].selected = false;
581                             res[i].indeterminate = false;
582                         }
583                     }
584
585                     for (var app_id in app_id_name_list) {
586                         var new_res = {};
587                         new_res.child = [];
588                         new_res.name = app_id_name_list[app_id];
589                         new_res.id = app_id;
590                         new_res.displayCheckbox = true;
591                         new_res.menuId = app_id;
592                         new_res.parentMenuId = null;
593                         new_res.appId = null;
594                         new_res.can_check = true;
595                         new_res.roleId = null;
596                         new_res.onSelect = function() {
597                             $scope.$apply(function() {
598                                 $scope.newNotifModel.anyTreeItemSelected = $scope.checkTreeSelect();
599                             })
600                         };
601                         res.push(new_res);
602                     }
603                     $scope.parentChildRoleIdDict = {};
604                     //Adding actual child items to child array in res objects
605                     for (let i = 0; i < res.length; i++) {
606                         let parentId = res[i].menuId;
607                         $scope.parentChildRoleIdDict[parentId] = [];
608                         for (let j = 0; j < res.length; j++) {
609                             let childId = res[j].parentMenuId;
610                             if (parentId == childId) {
611                                 res[i].child.push(res[j]);
612                                 if (res[j].roleId) {
613                                     for (let k in res[j].roleId) {
614                                         $scope.parentChildRoleIdDict[parentId].push(res[j].roleId[k]);
615                                     }
616
617                                 }
618                             }
619                         }
620                     }
621                     //Forming actual parent items
622                     for (let i = 0; i < res.length; i++) {
623                         let parentId = res[i].parentMenuId;
624                         if (parentId === null) {
625                             actualData.push(res[i]);
626                         }
627                     }
628
629                     $scope.treedata = actualData;
630                     //setting correct parameters for b2b tree
631                     $scope.settingTreeParam();
632                 }).catch(err => {
633                     $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ', err);
634                 }).finally(() => {
635                     this.isLoadingTable = false;
636                 })
637             }
638             $scope.getFunctionalMenu = function() {
639                 $scope.treeTitle = "Functional Menu";
640                 getFunctionalMenu();
641             }
642             $scope.getAppRoleIds = function() {
643                 $scope.treeTitle = "Applications/Roles";
644                 getAppRoleIds();
645             }
646
647
648
649             $scope.settingTreeParam = function() {
650                 /**************first level****************/
651                 for (var fi = 0; fi < $scope.treedata.length; fi++) {
652                     var fLevel = $scope.treedata[fi];
653                     var sLevel = $scope.treedata[fi].child;
654                     var sLevelSelectedCount = 0;
655                     var sLevelChildNumber = 0
656                     if (fLevel.child.length == 0 && fLevel.roleId == null) {
657                         delete fLevel.child;
658                     } else if (sLevel) {
659                         /**************Second level****************/
660                         var sLevelDelArray = [];
661                         for (var si = 0; si < sLevel.length; si++) {
662                             var deletThisSLev = false;
663                             if (sLevel[si].child.length == 0 && sLevel[si].roleId == null) {
664                                 sLevel[si].displayCheckbox = false;
665                                 sLevelDelArray.push(sLevel[si].name);
666                                 sLevel[si].name = '';
667                                 sLevel[si].active = false;
668                                 delete sLevel[si].child;
669                             } else if (sLevel[si].child.length == 0) {
670                                 delete sLevel[si].child;
671                             } else {
672                                 /**************Third level****************/
673                                 var tLevel = sLevel[si].child;
674                                 var tLevelSelectedCount = 0;
675                                 var tLevelChildNumber = 0;
676                                 if (tLevel) {
677                                     var tLevelDelArray = [];
678                                     var tLevelLen = tLevel.length;
679                                     var tLevelRoleIdUndefined = 0;
680                                     for (var ti = 0; ti < tLevel.length; ti++) {
681                                         delete tLevel[ti].child;
682                                         if (tLevel[ti].roleId == null) {
683                                             tLevel[ti].displayCheckbox = false;
684                                             tLevelDelArray.push(tLevel[ti].name);
685                                             tLevel[ti].name = '';
686                                             tLevel[ti].active = false;
687                                             tLevelRoleIdUndefined++
688                                         } else {
689                                             if (tLevel[ti].isSelected)
690                                                 tLevelSelectedCount++;
691
692                                             if (tLevel[ti].displayCheckbox)
693                                                 tLevelChildNumber++;
694                                         }
695                                     }
696                                     if (tLevelRoleIdUndefined == tLevelLen)
697                                         deletThisSLev = true;
698                                     if (tLevelSelectedCount == tLevelChildNumber) {
699                                         sLevel[si].isSelected = true;
700                                         sLevel[si].indeterminate = false;
701                                         sLevel[si].active = true;
702                                     } else if (tLevelSelectedCount > 0) {
703                                         sLevel[si].indeterminate = true;
704                                         sLevel[si].active = true;
705                                     }
706
707                                     /*Cleanup unused third level items*/
708                                     for (var i = 0; i < tLevelDelArray.length; i++) {
709                                         var name = tLevelDelArray[i];
710                                         for (var ti = 0; ti < tLevel.length; ti++) {
711                                             if (name == tLevel[ti].text) {
712                                                 tLevel.splice(ti, 1);
713                                                 break;
714                                             }
715                                         }
716                                     }
717                                 }
718                             }
719                             if (deletThisSLev) { //remove the whole second level item if all it's child has no roleId
720                                 sLevel[si].displayCheckbox = false;
721                                 sLevelDelArray.push(sLevel[si].name);
722                                 sLevel[si].name = '';
723                                 sLevel[si].active = false;
724                             } else {
725                                 if (sLevel[si].isSelected)
726                                     sLevelSelectedCount++;
727                                 if (sLevel[si].displayCheckbox)
728                                     sLevelChildNumber++;
729                             }
730                         }
731                         if (sLevelSelectedCount == sLevelChildNumber && sLevelChildNumber != 0) {
732                             fLevel.isSelected = true;
733                             fLevel.indeterminate = false;
734                             fLevel.active = true;
735                         } else if (sLevelSelectedCount > 0) {
736                             fLevel.indeterminate = true;
737                             fLevel.active = true;
738                         } else {
739                             //fLevel.active=false;
740                             fLevel.indeterminate = false;
741                         }
742                         /*Cleanup unused second level items*/
743                         for (var i = 0; i < sLevelDelArray.length; i++) {
744                             var name = sLevelDelArray[i];
745                             for (var si = 0; si < sLevel.length; si++) {
746                                 if (name == sLevel[si].text) {
747                                     sLevel.splice(si, 1);
748                                     break;
749                                 }
750                             }
751                         }
752                     }
753                 }
754             }
755             init();
756             getFunctionalMenu();
757
758         }
759
760     }
761
762     userNotificationsModalCtrl.$inject = ['$scope', '$log', 'functionalMenuService', 'confirmBoxService', 'notificationService', '$modal', 'ngDialog', '$state', '$filter', 'items'];
763     angular.module('ecompApp').controller('userNotificationsModalCtrl', userNotificationsModalCtrl);
764
765     angular.module('ecompApp').directive('attDatepickerCustom', ['$log', function($log) {
766         return {
767             restrict: 'A',
768             require: 'ngModel',
769             scope: {},
770
771             controller: ['$scope', '$element', '$attrs', '$compile', 'datepickerConfig', 'datepickerService', function($scope, $element, $attrs, $compile, datepickerConfig, datepickerService) {
772                 var dateFormatString = angular.isDefined($attrs.dateFormat) ? $scope.$parent.$eval($attrs.dateFormat) : datepickerConfig.dateFormat;
773                 var selectedDateMessage = '<div class="sr-focus hidden-spoken" tabindex="-1">the date you selected is {{$parent.current | date : \'' + dateFormatString + '\'}}</div>';
774                 $element.removeAttr('att-datepicker-custom');
775                 $element.removeAttr('ng-model');
776                 $element.attr('ng-value', '$parent.current |  date:"EEEE, MMMM d, y"');
777                 $element.attr('aria-describedby', 'datepicker');
778
779                 $element.attr('maxlength', 10);
780
781                 var wrapperElement = angular.element('<div></div>');
782                 wrapperElement.attr('datepicker-popup', '');
783                 wrapperElement.attr('current', 'current');
784
785                 datepickerService.setAttributes($attrs, wrapperElement);
786                 datepickerService.bindScope($attrs, $scope);
787
788                 wrapperElement.html('');
789                 wrapperElement.append($element.prop('outerHTML'));
790                 if (navigator.userAgent.match(/MSIE 8/) === null) {
791                     wrapperElement.append(selectedDateMessage);
792                 }
793                 var elm = wrapperElement.prop('outerHTML');
794                 elm = $compile(elm)($scope);
795                 $element.replaceWith(elm);
796             }],
797             link: function(scope, elem, attr, ctrl) {
798                 if (!ctrl) {
799                     // do nothing if no ng-model
800                     $log.error("ng-model is required.");
801                     return;
802                 }
803
804                 scope.$watch('current', function(value) {
805                     ctrl.$setViewValue(value);
806                 });
807                 ctrl.$render = function() {
808                     scope.current = ctrl.$viewValue;
809                 };
810
811             }
812         };
813     }]);
814
815
816 })();