nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / functionalMenu / functionalMenu.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 'use strict';
21 (function () {
22     class FunctionalMenuCtrl {
23         constructor($log, functionalMenuService, $scope,ngDialog, confirmBoxService) {
24             $log.info('FunctionalMenuCtrl init');
25
26             $scope.invokeDialog = () => {
27                 alert("click dialog");
28             };
29
30             this.regenerateFunctionalMenuAncestors = () => {
31                 functionalMenuService.regenerateFunctionalMenuAncestors().then(res => {
32                     $log.debug("FunctionalMenuCtrl:regenerateFunctionalMenuAncestors::returned from regenerateFunctionalMenuAncestors API call");
33                     confirmBoxService.showInformation('You have successfully regenerated the menu.').then(isConfirmed => {
34                     });
35                 })['catch'](function (err) {
36                     $log.error("FunctionalMenuCtrl:regenerateFunctionalMenuAncestors:: error: " + err);
37                     confirmBoxService.showInformation('There was an error while regenerating the menu.').then(isConfirmed => {
38                     });
39                 });
40             };
41
42             let getFunctionalMenu = () => {
43                 this.isLoadingTable = true;
44                 functionalMenuService.getManagedFunctionalMenu().then(res => {
45
46                     let actualData=[];
47
48                     for(let i = 0; i < res.length; i++){
49                         res[i].children=[];
50                         res[i].label=res[i].text;
51                         res[i].id=res[i].text;
52
53                     }
54
55                     for(let i = 0; i < res.length; i++){
56
57                         let parentId=res[i].menuId;
58                         for(let j = 0; j < res.length; j++){
59                             let childId=res[j].parentMenuId;
60                             if(parentId==childId){
61                                 res[i].children.push(res[j]);
62
63                             }
64                         }
65                     }
66
67                     res.sort(function(a, b) {
68                         return a.column-b.column;
69                     })
70
71                     for(let i = 0; i < res.length; i++){
72                         res[i].children.sort(function(a, b){
73                             return a.column-b.column;
74                         })
75                     }
76
77                     for(let i = 0; i < res.length; i++){
78                         let parentId=res[i].parentMenuId;
79                         if(parentId==null){
80                             actualData.push(res[i]);
81                         }
82                     }
83
84                     $scope.treedata = actualData;
85
86                 }).catch(err => {
87                     $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ',err);
88                 }).finally(()=> {
89                     this.isLoadingTable = false;
90                 });
91
92             };
93
94
95             let init = () => {
96                 this.isLoadingTable = false;
97                 this.functionalMenu = [];
98                 getFunctionalMenu();
99                 this.searchString = '';
100             };
101
102             this.filterByDropdownValue = item => {
103                 if(this.filterByApp.value === ''){
104                     return true;
105                 }
106                 return item.appName === this.filterByApp.value;
107             };
108
109             let getDialogTitle = (source) => {
110                 switch (source) {
111                     case 'edit':
112                         return "Functional Menu - Edit";
113                     case 'view':
114                         return "Functional Menu - View";
115                     case 'add':
116                         return "Functional Menu - Add";
117                     default:
118                         return "Functional Menu";
119                 }
120             }
121
122             $scope.reloadTreeStructure = (selectedItem,source) => {
123                 getFunctionalMenu();
124             };
125             $scope.openMenuDetailsModal = (selectedItem,source) => {
126                 let data = null;
127                 let selectedMenuDetails = null;
128                 console.log('selectedItem: ', selectedItem);
129
130                 functionalMenuService.getMenuDetails(selectedItem.menuId)
131                     .then(function( resp ){
132                         selectedMenuDetails = resp;
133                         $log.info('FunctionalMenuCtrl::openMenuDetailsModal: getMenuDetails: ', resp );
134
135                         if(selectedItem){
136                             data = {
137                                 menuItem: {menu: _.clone(selectedItem),menuDetails:_.clone(selectedMenuDetails)},
138                                 source: source,
139                                 title: getDialogTitle(source)
140                             }
141                         }
142                         ngDialog.open({
143                             templateUrl: 'app/views/functionalMenu/functionalMenu-dialog/menu-details.modal.html',
144                             controller: 'MenuDetailsModalCtrl',
145                             controllerAs: 'functionalMenuDetails',
146                             data: data
147                         }).closePromise.then(needUpdate => {
148                             if(needUpdate.value === true){
149                                 $log.debug('FunctionalMenuCtrl::openMenuDetailsModal: updating table data...');
150                                 if(source=="edit")
151                                     init();
152                             }
153                         });
154                     });
155             };
156
157
158             $scope.createNewMenuItem = (selectedItem,source) => {
159
160                 if(selectedItem != null && selectedItem.getLevel() >= 4){
161                     confirmBoxService.showInformation('You are not allowed to have a menu item at a level greater than 4.').then(isConfirmed => {
162
163                     });
164                     return ;
165                 }
166
167                 let data = null;
168                 let selectedMenuDetails = null;
169                 functionalMenuService.getMenuDetails(selectedItem.menuId)
170                     .then(function( resp ){
171                         selectedMenuDetails = resp;
172
173                         if((selectedItem.children==null || !selectedItem.children.length>0) &&
174                             (!!selectedMenuDetails.url || !!selectedMenuDetails.appid || !!selectedMenuDetails.roles)){
175                             confirmBoxService.showInformation('Warning: the child menu item "' + selectedItem.name + '" is already configured with an application. You can create a new mid-level menu item, and move this item under it.').then(isConfirmed => {
176                                 return;
177                             });
178                         }else{
179                             if(selectedItem){
180                                 data = {
181                                     menuItem: {menu: _.clone(selectedItem)},
182                                     source:source,
183                                     title: getDialogTitle(source)
184                                 }
185                             }
186
187                             ngDialog.open({
188                                 templateUrl: 'app/views/functionalMenu/functionalMenu-dialog/menu-details.modal.html',
189                                 controller: 'MenuDetailsModalCtrl',
190                                 controllerAs: 'functionalMenuDetails',
191                                 data: data
192                             }).closePromise.then(needUpdate => {
193                                 if(needUpdate.value === true){
194                                     $log.debug('FunctionalMenuCtrl::getMenuDetails: updating table data...');
195                                     init();
196                                 }
197                             });
198                         }
199                     });
200             };
201
202             $scope.deleteMenuItem = (selectedItem,source) => {
203                 $log.info('FunctionalMenuCtrl:deleteMenuItem:: delete selectedItem: ', selectedItem);
204
205                 if(selectedItem.children!=null && selectedItem.children.length>0){
206                     confirmBoxService.showInformation('You are not allowed to delete a menu item that has children. You can only delete leaf menu items.').then(isConfirmed => {
207
208                     });
209                 }else{
210                     confirmBoxService.deleteItem(selectedItem.name).then(isConfirmed => {
211                         if(isConfirmed){
212                             $log.info('FunctionalMenuCtrl:deleteMenuItem:: Deleting Menu Item :: name: '+selectedItem.name+'; menuId: '+selectedItem.menuId);
213                             $log.info('FunctionalMenuCtrl:deleteMenuItem:: selectedItem: ', selectedItem);
214
215                             functionalMenuService.deleteMenuItem(selectedItem.menuId).then(() => {
216                                 $log.info('FunctionalMenuCtrl:deleteMenuItem:: Removed Menu Item :: '+selectedItem.name);
217                                 init();
218                             }).catch(err => {
219                                 $log.error(err);
220                             });
221                         }
222                     }).catch(err => {
223                         $log.error(err);
224                     });
225                 }
226             };
227
228             init();
229         }
230     }
231     FunctionalMenuCtrl.$inject = ['$log', 'functionalMenuService','$scope', 'ngDialog', 'confirmBoxService'];
232     angular.module('ecompApp').controller('FunctionalMenuCtrl', FunctionalMenuCtrl);
233
234     angular.module('ecompApp').directive('jqTree', ['functionalMenuService','$log','confirmBoxService',function(functionalMenuService,$log,confirmBoxService){
235         return {
236             templateUrl: 'jqtree-tmpl.html',
237             link: function(scope, el, attrs){
238
239                 var $jqTree =  el.find('#jqTree').tree({
240                     data: scope.treedata,
241                     autoOpen: false,
242                     dragAndDrop: true,
243                     onCreateLi: function(node, $li) {
244                         $li.attr('id', node.id.replace(/\s+/g,"_"));
245                     }
246                 });
247
248                 el.find('#jqTree').bind('tree.move',  function(event){
249                     event.preventDefault();
250                     console.log('moved_node', event.move_info.moved_node);
251                     console.log('target_node', event.move_info.target_node);
252                     console.log('position', event.move_info.position);
253                     console.log('previous_parent', event.move_info.previous_parent);
254
255
256
257                     if(event.move_info.target_node != null &&
258                         ((event.move_info.position == 'after' && event.move_info.target_node.getLevel() > 4) ||
259                         (event.move_info.position == 'inside' && event.move_info.target_node.getLevel() > 3))){
260                         confirmBoxService.showInformation('You are not allowed to have a menu item at a level greater than 4.').then(isConfirmed => {
261
262                         });
263                         return ;
264                     }
265
266                     var confMsg = 'Are you sure you want to move "'+event.move_info.moved_node.name+'" ?';
267                     if ((event.move_info.position == "inside") && (event.move_info.target_node.url != "")) {
268                         confMsg = 'Warning: You are moving "'+event.move_info.moved_node.name+'" under "'+event.move_info.target_node.name+'", which has application information associated with it. This will cause all the application information from "'+event.move_info.target_node.name+'" to be deleted.';
269                     }
270                     confirmBoxService.moveMenuItem(confMsg).then(isConfirmed => {
271                         if(isConfirmed){
272                             var new_column = event.move_info.target_node.column;
273                             var old_column = event.move_info.moved_node.column;
274                             if ((event.move_info.moved_node.parentMenuId !=
275                                 event.move_info.target_node.parentMenuId) ||
276                                 (new_column < old_column)
277                             ) {
278                                 new_column += 1;
279                             }
280                             var activeMenuItem = {
281                                 menuId:event.move_info.moved_node.menuId,
282                                 column:new_column,
283                                 text:"",
284                                 parentMenuId:event.move_info.target_node.parentMenuId,
285                                 url:"",
286                                 appid: null,
287                                 roles:null
288                             };
289
290                             if (event.move_info.position == "inside") {
291                                 console.log("special case: target_node is parent");
292                                 activeMenuItem.column = 1;
293                                 activeMenuItem.parentMenuId = event.move_info.target_node.menuId;
294                             }
295
296                             functionalMenuService.saveEditedMenuItem(activeMenuItem)
297                                 .then(() => {
298                                     $log.debug(' Menu Item moved');
299                                     scope.reloadTreeStructure();
300                                 }).catch(err => {
301                                 $log.error(err);
302                             }).finally(()=>{
303                             });
304                         }
305                     }).catch(err => {
306                         $log.error(err);
307                     });
308                 });
309
310
311                 $jqTree.jqTreeContextMenu(el.find('ul.dropdown-menu'), {
312                     "view": function (node) {scope.openMenuDetailsModal(node,'view'); },
313                     "edit": function (node) {scope.openMenuDetailsModal(node,'edit'); },
314                     "delete": function (node) { scope.deleteMenuItem(node,'delete') },
315                     "add": function (node) {  scope.createNewMenuItem(node,'add') }
316                 });
317
318                 scope.$watch('treedata', function(oldValue, newValue){
319                     if(oldValue !== newValue){
320                         console.log('FunctionalMenuCtrl:: Tree value has changed in some way');
321                         $jqTree.tree('loadData',  scope.treedata);
322                         $jqTree.tree('reload', function() {
323                             console.log('FunctionalMenuCtrl:: Tree is reloaded');
324                         });
325                     }
326                 })
327             }
328         }
329     }]);
330
331 })();