summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE-os/client/src/views/functionalMenu/functionalMenu.controller.js
blob: dedc13752e421a19c0717ae5574eb55ad297fcb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*-
 * ================================================================================
 * ECOMP Portal
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
'use strict';
(function () {
    class FunctionalMenuCtrl {
        constructor($log, functionalMenuService, $scope,ngDialog, confirmBoxService) {
            $log.info('FunctionalMenuCtrl init');

            $scope.invokeDialog = () => {
                // alert("click dialog");
            };

            this.regenerateFunctionalMenuAncestors = () => {
                functionalMenuService.regenerateFunctionalMenuAncestors().then(res => {
                    $log.debug("FunctionalMenuCtrl:regenerateFunctionalMenuAncestors::returned from regenerateFunctionalMenuAncestors API call");
                    confirmBoxService.showInformation('You have successfully regenerated the menu.').then(isConfirmed => {
                    });
                })['catch'](function (err) {
                    $log.error("FunctionalMenuCtrl:regenerateFunctionalMenuAncestors:: error: " + err);
                    confirmBoxService.showInformation('There was an error while regenerating the menu.').then(isConfirmed => {
                    });
                });
            };

            let getFunctionalMenu = () => {
                this.isLoadingTable = true;
                functionalMenuService.getManagedFunctionalMenu().then(res => {

                    let actualData=[];

                    //Adding children and label attribute to all objects in res
                    for(let i = 0; i < res.length; i++){
                        res[i].children=[];
                        res[i].label=res[i].text;
                        res[i].id=res[i].text;

                    }
                    //Adding actual child items to children array in res objects
                    for(let i = 0; i < res.length; i++){

                        let parentId=res[i].menuId;
                        for(let j = 0; j < res.length; j++){
                            let childId=res[j].parentMenuId;
                            if(parentId===childId){
                                res[i].children.push(res[j]);

                            }
                        }
                    }

                    // Sort the top-level menu items in order based on the column
                    res.sort(function(a, b) {
                        return a.column-b.column;
                    });

                    // Sort all the children in order based on the column
                    for(let i = 0; i < res.length; i++){
                        res[i].children.sort(function(a, b){
                            return a.column-b.column;
                        });
                    }

                    //Forming actual parent items
                    for(let i = 0; i < res.length; i++){
                        let parentId=res[i].parentMenuId;
                        if(parentId===null){
                            actualData.push(res[i]);
                        }
                    }

                    $scope.treedata = actualData;

                }).catch(err => {
                    $log.error('FunctionalMenuCtrl:getFunctionalMenu:: error ',err);
                }).finally(()=> {
                    this.isLoadingTable = false;
                });

            };


            let init = () => {
                this.isLoadingTable = false;
                this.functionalMenu = [];
                getFunctionalMenu();
                this.searchString = '';


            };

            this.filterByDropdownValue = item => {
                if(this.filterByApp.value === ''){
                    return true;
                }
                return item.appName === this.filterByApp.value;
            };

            let getDialogTitle = (source) => {
                switch (source) {
                    case 'edit':
                        return "Functional Menu - Edit";
                    case 'view':
                        return "Functional Menu - View";
                    case 'add':
                        return "Functional Menu - Add";
                    default:
                        return "Functional Menu";
                };
            };

            $scope.reloadTreeStructure = (selectedItem,source) => {
                getFunctionalMenu();
            };
            $scope.openMenuDetailsModal = (selectedItem,source) => {
                let data = null;
                let selectedMenuDetails = null;
                console.log('selectedItem: ', selectedItem);

                functionalMenuService.getMenuDetails(selectedItem.menuId)
                    .then(function( resp ){
                        selectedMenuDetails = resp;
                        $log.info('FunctionalMenuCtrl::openMenuDetailsModal: getMenuDetails: ', resp );

                        if(selectedItem){
                            data = {
                                menuItem: {menu: _.clone(selectedItem),menuDetails:_.clone(selectedMenuDetails)},
                                source: source,
                                title: getDialogTitle(source)
                            };
                        }
                        ngDialog.open({
                            templateUrl: 'app/views/functionalMenu/functionalMenu-dialog/menu-details.modal.html',
                            controller: 'MenuDetailsModalCtrl',
                            controllerAs: 'functionalMenuDetails',
                            data: data
                        }).closePromise.then(needUpdate => {
                            if(needUpdate.value === true){
                                $log.debug('FunctionalMenuCtrl::openMenuDetailsModal: updating table data...');
                                if(source==="edit") {
                                    init();
                                }
                            }
                        });
                    });
            };


            $scope.createNewMenuItem = (selectedItem,source) => {

                if(selectedItem != null && selectedItem.getLevel() >= 4){
                    confirmBoxService.showInformation('You are not allowed to have a menu item at a level greater than 4.').then(isConfirmed => {

                    });
                    return ;
                }

                let data = null;
                let selectedMenuDetails = null;
                functionalMenuService.getMenuDetails(selectedItem.menuId)
                    .then(function( resp ){
                        selectedMenuDetails = resp;

                        if((selectedItem.children===null || !selectedItem.children.length>0) &&
                            (!!selectedMenuDetails.url || !!selectedMenuDetails.appid || !!selectedMenuDetails.roles)){
                            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 => {
                                return;
                            });
                        }else{
                            if(selectedItem){
                                data = {
                                    menuItem: {menu: _.clone(selectedItem)},
                                    source:source,
                                    title: getDialogTitle(source)
                                };
                            }

                            ngDialog.open({
                                templateUrl: 'app/views/functionalMenu/functionalMenu-dialog/menu-details.modal.html',
                                controller: 'MenuDetailsModalCtrl',
                                controllerAs: 'functionalMenuDetails',
                                data: data
                            }).closePromise.then(needUpdate => {
                                if(needUpdate.value === true){
                                    $log.debug('FunctionalMenuCtrl::getMenuDetails: updating table data...');
                                    init();
                                    //getOnboardingWidgets();
                                }
                            });
                        }
                    });
            };

            $scope.deleteMenuItem = (selectedItem,source) => {
                $log.info('FunctionalMenuCtrl:deleteMenuItem:: delete selectedItem: ', selectedItem);

                if(selectedItem.children!=null && selectedItem.children.length>0){
                    confirmBoxService.showInformation('You are not allowed to delete a menu item that has children. You can only delete leaf menu items.').then(isConfirmed => {

                    });
                }else{
                    confirmBoxService.deleteItem(selectedItem.name).then(isConfirmed => {
                        if(isConfirmed){
                            $log.info('FunctionalMenuCtrl:deleteMenuItem:: Deleting Menu Item :: name: '+selectedItem.name+'; menuId: '+selectedItem.menuId);
                            $log.info('FunctionalMenuCtrl:deleteMenuItem:: selectedItem: ', selectedItem);

                            functionalMenuService.deleteMenuItem(selectedItem.menuId).then(() => {
                                //TODO:Have to splice menu item
                                //this.widgetsList.splice(this.widgetsList.indexOf(widget), 1);
                                $log.info('FunctionalMenuCtrl:deleteMenuItem:: Removed Menu Item :: '+selectedItem.name);
                                init();
                            }).catch(err => {
                                $log.error(err);
                            });
                        }
                    }).catch(err => {
                        $log.error(err);
                    });
                }
            };

            init();
        }
    }
    FunctionalMenuCtrl.$inject = ['$log', 'functionalMenuService','$scope', 'ngDialog', 'confirmBoxService'];
    angular.module('ecompApp').controller('FunctionalMenuCtrl', FunctionalMenuCtrl);

    angular.module('ecompApp').directive('jqTree', ['functionalMenuService','$log','confirmBoxService',function(functionalMenuService,$log,confirmBoxService){
        return {
            templateUrl: 'jqtree-tmpl.html',
            link: function(scope, el, attrs){

                var $jqTree =  el.find('#jqTree').tree({
                    data: scope.treedata,
                    autoOpen: false,
                    dragAndDrop: true,
                    onCreateLi: function(node, $li) {
                        $li.attr('id', node.id.replace(/\s+/g,'_'));
                    }
                });

                el.find('#jqTree').bind('tree.move',  function(event){
                    event.preventDefault();
                    console.log('moved_node', event.move_info.moved_node);
                    console.log('target_node', event.move_info.target_node);
                    console.log('position', event.move_info.position);
                    console.log('previous_parent', event.move_info.previous_parent);



                    if(event.move_info.target_node != null &&
                        ((event.move_info.position === 'after' && event.move_info.target_node.getLevel() > 4) ||
                        (event.move_info.position === 'inside' && event.move_info.target_node.getLevel() > 3))){
                        confirmBoxService.showInformation('You are not allowed to have a menu item at a level greater than 4.').then(isConfirmed => {

                        });
                        return ;
                    }

                    var confMsg = 'Are you sure you want to move "'+event.move_info.moved_node.name+'" ?';
                    if ((event.move_info.position === "inside") && (event.move_info.target_node.url != "")) {
                        // If we are moving UNDER a node that has a url associated with it, warn the user
                        // that all the app information will be removed if they do this.
                        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.';
                    }
                    confirmBoxService.moveMenuItem(confMsg).then(isConfirmed => {
                        if(isConfirmed){
                            /*
                             {
                             "menuId": 129,
                             "column": 3,
                             "text": "",
                             "parentMenuId": 37,
                             "url": "",
                             "appid": null,
                             "roles": null
                             }

                             The menuId for the menu item being moved
                             The column it is being moved to
                             The parentMenuId for the parent it is being moved under
                             */

                            // The target_node is the node before the position we are
                            // moving to. If we are moving to a lower column number, or
                            // to a new parent, we must adjust the column to be after
                            // the target_node.
                            var new_column = event.move_info.target_node.column;
                            var old_column = event.move_info.moved_node.column;
                            if ((event.move_info.moved_node.parentMenuId !=
                                event.move_info.target_node.parentMenuId) ||
                                (new_column < old_column)
                            ) {
                                new_column += 1;
                            }
                            var activeMenuItem = {
                                menuId:event.move_info.moved_node.menuId,
                                column:new_column,
                                text:"",
                                parentMenuId:event.move_info.target_node.parentMenuId,
                                url:"",
                                appid: null,
                                roles:null
                            };
                            // When position is "inside", this is a special case,
                            // where you are moving to the first column under
                            // a parent. The target_node is the parent node.
                            // So we need to set the column to 1, and the parentMenuId to the menuId of
                            // the target move.
                            if (event.move_info.position === "inside") {
                                console.log("special case: target_node is parent");
                                activeMenuItem.column = 1;
                                activeMenuItem.parentMenuId = event.move_info.target_node.menuId;
                            }


                            functionalMenuService.saveEditedMenuItem(activeMenuItem)
                                .then(() => {
                                    $log.debug(' Menu Item moved');
                                    scope.reloadTreeStructure();
                                }).catch(err => {
                                $log.error(err);
                            }).finally(()=>{
                            });
                        }
                    }).catch(err => {
                        $log.error(err);
                    });

                    //event.move_info.do_move();
                });


                $jqTree.jqTreeContextMenu(el.find('ul.dropdown-menu'), {
                    "view": function (node) {scope.openMenuDetailsModal(node,'view'); },
                    "edit": function (node) {scope.openMenuDetailsModal(node,'edit'); },
                    "delete": function (node) { scope.deleteMenuItem(node,'delete') },
                    "add": function (node) {  scope.createNewMenuItem(node,'add') }
                });

                scope.$watch('treedata', function(oldValue, newValue){
                    if(oldValue !== newValue){
                        console.log('FunctionalMenuCtrl:: Tree value has changed in some way');
                        $jqTree.tree('loadData',  scope.treedata);
                        $jqTree.tree('reload', function() {
                            console.log('FunctionalMenuCtrl:: Tree is reloaded');
                        });
                    }
                });
            }
        };
    }]);

})();