[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-os / client / src / 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                     //Adding children and label attribute to all objects in res
49                     for(let i = 0; i < res.length; i++){
50                         res[i].children=[];
51                         res[i].label=res[i].text;
52                         res[i].id=res[i].text;
53
54                     }
55                     //Adding actual child items to children array in res objects
56                     for(let i = 0; i < res.length; i++){
57
58                         let parentId=res[i].menuId;
59                         for(let j = 0; j < res.length; j++){
60                             let childId=res[j].parentMenuId;
61                             if(parentId===childId){
62                                 res[i].children.push(res[j]);
63
64                             }
65                         }
66                     }
67
68                     // Sort the top-level menu items in order based on the column
69                     res.sort(function(a, b) {
70                         return a.column-b.column;
71                     });
72
73                     // Sort all the children in order based on the column
74                     for(let i = 0; i < res.length; i++){
75                         res[i].children.sort(function(a, b){
76                             return a.column-b.column;
77                         });
78                     }
79
80                     //Forming actual parent items
81                     for(let i = 0; i < res.length; i++){
82                         let parentId=res[i].parentMenuId;
83                         if(parentId===null){
84                             actualData.push(res[i]);
85                         }
86                     }
87
88                     $scope.treedata = actualData;
89
90                 }).catch(err => {
91                     $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ',err);
92                 }).finally(()=> {
93                     this.isLoadingTable = false;
94                 });
95
96             };
97
98
99             let init = () => {
100                 this.isLoadingTable = false;
101                 this.functionalMenu = [];
102                 getFunctionalMenu();
103                 this.searchString = '';
104
105
106             };
107
108             this.filterByDropdownValue = item => {
109                 if(this.filterByApp.value === ''){
110                     return true;
111                 }
112                 return item.appName === this.filterByApp.value;
113             };
114
115             let getDialogTitle = (source) => {
116                 switch (source) {
117                     case 'edit':
118                         return "Functional Menu - Edit";
119                     case 'view':
120                         return "Functional Menu - View";
121                     case 'add':
122                         return "Functional Menu - Add";
123                     default:
124                         return "Functional Menu";
125                 };
126             };
127
128             $scope.reloadTreeStructure = (selectedItem,source) => {
129                 getFunctionalMenu();
130             };
131             $scope.openMenuDetailsModal = (selectedItem,source) => {
132                 let data = null;
133                 let selectedMenuDetails = null;
134                 console.log('selectedItem: ', selectedItem);
135
136                 functionalMenuService.getMenuDetails(selectedItem.menuId)
137                     .then(function( resp ){
138                         selectedMenuDetails = resp;
139                         $log.info('FunctionalMenuCtrl::openMenuDetailsModal: getMenuDetails: ', resp );
140
141                         if(selectedItem){
142                             data = {
143                                 menuItem: {menu: _.clone(selectedItem),menuDetails:_.clone(selectedMenuDetails)},
144                                 source: source,
145                                 title: getDialogTitle(source)
146                             };
147                         }
148                         ngDialog.open({
149                             templateUrl: 'app/views/functionalMenu/functionalMenu-dialog/menu-details.modal.html',
150                             controller: 'MenuDetailsModalCtrl',
151                             controllerAs: 'functionalMenuDetails',
152                             data: data
153                         }).closePromise.then(needUpdate => {
154                             if(needUpdate.value === true){
155                                 $log.debug('FunctionalMenuCtrl::openMenuDetailsModal: updating table data...');
156                                 if(source==="edit") {
157                                     init();
158                                 }
159                             }
160                         });
161                     });
162             };
163
164
165             $scope.createNewMenuItem = (selectedItem,source) => {
166
167                 if(selectedItem != null && selectedItem.getLevel() >= 4){
168                     confirmBoxService.showInformation('You are not allowed to have a menu item at a level greater than 4.').then(isConfirmed => {
169
170                     });
171                     return ;
172                 }
173
174                 let data = null;
175                 let selectedMenuDetails = null;
176                 functionalMenuService.getMenuDetails(selectedItem.menuId)
177                     .then(function( resp ){
178                         selectedMenuDetails = resp;
179
180                         if((selectedItem.children===null || !selectedItem.children.length>0) &&
181                             (!!selectedMenuDetails.url || !!selectedMenuDetails.appid || !!selectedMenuDetails.roles)){
182                             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 => {
183                                 return;
184                             });
185                         }else{
186                             if(selectedItem){
187                                 data = {
188                                     menuItem: {menu: _.clone(selectedItem)},
189                                     source:source,
190                                     title: getDialogTitle(source)
191                                 };
192                             }
193
194                             ngDialog.open({
195                                 templateUrl: 'app/views/functionalMenu/functionalMenu-dialog/menu-details.modal.html',
196                                 controller: 'MenuDetailsModalCtrl',
197                                 controllerAs: 'functionalMenuDetails',
198                                 data: data
199                             }).closePromise.then(needUpdate => {
200                                 if(needUpdate.value === true){
201                                     $log.debug('FunctionalMenuCtrl::getMenuDetails: updating table data...');
202                                     init();
203                                     //getOnboardingWidgets();
204                                 }
205                             });
206                         }
207                     });
208             };
209
210             $scope.deleteMenuItem = (selectedItem,source) => {
211                 $log.info('FunctionalMenuCtrl:deleteMenuItem:: delete selectedItem: ', selectedItem);
212
213                 if(selectedItem.children!=null && selectedItem.children.length>0){
214                     confirmBoxService.showInformation('You are not allowed to delete a menu item that has children. You can only delete leaf menu items.').then(isConfirmed => {
215
216                     });
217                 }else{
218                     confirmBoxService.deleteItem(selectedItem.name).then(isConfirmed => {
219                         if(isConfirmed){
220                             $log.info('FunctionalMenuCtrl:deleteMenuItem:: Deleting Menu Item :: name: '+selectedItem.name+'; menuId: '+selectedItem.menuId);
221                             $log.info('FunctionalMenuCtrl:deleteMenuItem:: selectedItem: ', selectedItem);
222
223                             functionalMenuService.deleteMenuItem(selectedItem.menuId).then(() => {
224                                 //TODO:Have to splice menu item
225                                 //this.widgetsList.splice(this.widgetsList.indexOf(widget), 1);
226                                 $log.info('FunctionalMenuCtrl:deleteMenuItem:: Removed Menu Item :: '+selectedItem.name);
227                                 init();
228                             }).catch(err => {
229                                 $log.error(err);
230                             });
231                         }
232                     }).catch(err => {
233                         $log.error(err);
234                     });
235                 }
236             };
237
238             init();
239         }
240     }
241     FunctionalMenuCtrl.$inject = ['$log', 'functionalMenuService','$scope', 'ngDialog', 'confirmBoxService'];
242     angular.module('ecompApp').controller('FunctionalMenuCtrl', FunctionalMenuCtrl);
243
244     angular.module('ecompApp').directive('jqTree', ['functionalMenuService','$log','confirmBoxService',function(functionalMenuService,$log,confirmBoxService){
245         return {
246             templateUrl: 'jqtree-tmpl.html',
247             link: function(scope, el, attrs){
248
249                 var $jqTree =  el.find('#jqTree').tree({
250                     data: scope.treedata,
251                     autoOpen: false,
252                     dragAndDrop: true,
253                     onCreateLi: function(node, $li) {
254                         $li.attr('id', node.id.replace(/\s+/g,'_'));
255                     }
256                 });
257
258                 el.find('#jqTree').bind('tree.move',  function(event){
259                     event.preventDefault();
260                     console.log('moved_node', event.move_info.moved_node);
261                     console.log('target_node', event.move_info.target_node);
262                     console.log('position', event.move_info.position);
263                     console.log('previous_parent', event.move_info.previous_parent);
264
265
266
267                     if(event.move_info.target_node != null &&
268                         ((event.move_info.position === 'after' && event.move_info.target_node.getLevel() > 4) ||
269                         (event.move_info.position === 'inside' && event.move_info.target_node.getLevel() > 3))){
270                         confirmBoxService.showInformation('You are not allowed to have a menu item at a level greater than 4.').then(isConfirmed => {
271
272                         });
273                         return ;
274                     }
275
276                     var confMsg = 'Are you sure you want to move "'+event.move_info.moved_node.name+'" ?';
277                     if ((event.move_info.position === "inside") && (event.move_info.target_node.url != "")) {
278                         // If we are moving UNDER a node that has a url associated with it, warn the user
279                         // that all the app information will be removed if they do this.
280                         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.';
281                     }
282                     confirmBoxService.moveMenuItem(confMsg).then(isConfirmed => {
283                         if(isConfirmed){
284                             /*
285                              {
286                              "menuId": 129,
287                              "column": 3,
288                              "text": "",
289                              "parentMenuId": 37,
290                              "url": "",
291                              "appid": null,
292                              "roles": null
293                              }
294
295                              The menuId for the menu item being moved
296                              The column it is being moved to
297                              The parentMenuId for the parent it is being moved under
298                              */
299
300                             // The target_node is the node before the position we are
301                             // moving to. If we are moving to a lower column number, or
302                             // to a new parent, we must adjust the column to be after
303                             // the target_node.
304                             var new_column = event.move_info.target_node.column;
305                             var old_column = event.move_info.moved_node.column;
306                             if ((event.move_info.moved_node.parentMenuId !=
307                                 event.move_info.target_node.parentMenuId) ||
308                                 (new_column < old_column)
309                             ) {
310                                 new_column += 1;
311                             }
312                             var activeMenuItem = {
313                                 menuId:event.move_info.moved_node.menuId,
314                                 column:new_column,
315                                 text:"",
316                                 parentMenuId:event.move_info.target_node.parentMenuId,
317                                 url:"",
318                                 appid: null,
319                                 roles:null
320                             };
321                             // When position is "inside", this is a special case,
322                             // where you are moving to the first column under
323                             // a parent. The target_node is the parent node.
324                             // So we need to set the column to 1, and the parentMenuId to the menuId of
325                             // the target move.
326                             if (event.move_info.position === "inside") {
327                                 console.log("special case: target_node is parent");
328                                 activeMenuItem.column = 1;
329                                 activeMenuItem.parentMenuId = event.move_info.target_node.menuId;
330                             }
331
332
333                             functionalMenuService.saveEditedMenuItem(activeMenuItem)
334                                 .then(() => {
335                                     $log.debug(' Menu Item moved');
336                                     scope.reloadTreeStructure();
337                                 }).catch(err => {
338                                 $log.error(err);
339                             }).finally(()=>{
340                             });
341                         }
342                     }).catch(err => {
343                         $log.error(err);
344                     });
345
346                     //event.move_info.do_move();
347                 });
348
349
350                 $jqTree.jqTreeContextMenu(el.find('ul.dropdown-menu'), {
351                     "view": function (node) {scope.openMenuDetailsModal(node,'view'); },
352                     "edit": function (node) {scope.openMenuDetailsModal(node,'edit'); },
353                     "delete": function (node) { scope.deleteMenuItem(node,'delete') },
354                     "add": function (node) {  scope.createNewMenuItem(node,'add') }
355                 });
356
357                 scope.$watch('treedata', function(oldValue, newValue){
358                     if(oldValue !== newValue){
359                         console.log('FunctionalMenuCtrl:: Tree value has changed in some way');
360                         $jqTree.tree('loadData',  scope.treedata);
361                         $jqTree.tree('reload', function() {
362                             console.log('FunctionalMenuCtrl:: Tree is reloaded');
363                         });
364                     }
365                 });
366             }
367         };
368     }]);
369
370 })();
371
372