Visualization of the VF Module Sequencing
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / drawing-board-tree / dragAndDrop / dragAndDrop.service.ts
1 import {Injectable} from "@angular/core";
2 import {NgRedux} from "@angular-redux/store";
3 import {AppState} from "../../../../shared/store/reducers";
4 import {FeatureFlagsService, Features} from "../../../../shared/services/featureFlag/feature-flags.service";
5
6 @Injectable()
7 export class DragAndDropService {
8
9   constructor(private store: NgRedux<AppState>) {
10   }
11
12   checkFeatureFlag(flagValue):boolean {
13     let featureFlag :boolean;
14     featureFlag = FeatureFlagsService.getFlagState(flagValue, this.store);
15     return featureFlag;
16   }
17
18   /***********************************************************************************************
19    if the dragged node is a base module instance
20    ***********************************************************************************************/
21   isBaseModule(serviceInstanceId, from): boolean {
22     try {
23       let baseModuleFlag = this.store.getState().service.serviceHierarchy[serviceInstanceId].vnfs[from.parent.data.vnfStoreKey].vfModules[from.data.modelName].properties.baseModule;
24       return (baseModuleFlag != 'undefined' ? baseModuleFlag : false);
25     }catch(e) {
26       return false;
27     }
28   }
29
30
31   /***********************************************************************************************
32    if the flag is ON and nodes have same parent
33    ***********************************************************************************************/
34   isAllowDrop(from: any, to: any): boolean {
35     return this.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE) && this.isSameParent(from, to);
36   }
37
38   private isSameParent(from: any, to: any): boolean {
39     try {
40       return from.parent.data.trackById === to.parent.parent.data.trackById;
41     } catch (e) { //parent not found
42       return false;
43     }
44   }
45
46   /********************************************************************
47    * manage drawing-board drag and drop operation
48    * @param nodes - array with elements data.
49    * @param tree - tree instance
50    * @param node - element information
51    * @param from - element from information
52    * @param to - element to information
53    ************************************************************/
54
55   drop(store, instanceId: string, nodes, {from, to}): void {
56
57     if (!this.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE)) return;
58
59     if(this.checkFeatureFlag(Features.FLAG_2008_DISABLE_DRAG_FOR_BASE_MODULE)) {
60       if ((to.parent.index == 0 && this.isBaseModule(instanceId, to.parent)) || this.isBaseModule(instanceId, from )) return;
61     }
62
63     if (this.isAllowDrop(from, to)) {
64       let vfModules = nodes.find((parent) => {
65         return parent.trackById === to.parent.parent.data.trackById;
66       }).children;
67       this.array_move(vfModules, from.index, to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
68     }
69
70     /*  let firstLevelNames : DragAndDropModel[] = [
71         new DragAndDropModel('VF',true),
72         new DragAndDropModel('VL',true),
73         new DragAndDropModel('VFmodule',false)
74       ];
75
76       const fromObject = _.find(firstLevelNames, ['type', from.data.type]);
77       const toObject = _.find(firstLevelNames, ['type', to.parent.data.type]);
78
79       /!***********************************************************************************************
80        if the type are the same and there in same level + same parent -> then change element position
81        ***********************************************************************************************!/
82       if(fromObject.isFirstLevel === toObject.isFirstLevel){ // moving element in the same level and in the first level
83         if(fromObject.isFirstLevel){
84           this.array_move(nodes, from.index , to.parent.index, instanceId);
85         } else if(fromObject.isFirstLevel === toObject.isFirstLevel){
86           /!* check if they have the same parent *!/
87           if(from.parent.data.trackById === to.parent.parent.data.trackById){
88             let vfModules = nodes.find((parents)=> {
89               return parents.trackById === to.parent.parent.data.trackById;
90             }).children;
91             this.array_move(vfModules, from.index , to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
92           }
93         }
94       }*/
95   }
96
97
98   /********************************************************************
99    * move element inside array with elements position
100    * @param arr - array with elements data.
101    * @param originalPosition - element original position
102    * @param destPosition - element dest position
103    * @param destPinstanceIdosition - instance id
104    ******************************************************************/
105   array_move(arr, originalPosition, destPosition, instanceId: string, parentStoreKey?): Array<any> {
106
107     let moved_node = arr[originalPosition]
108
109     arr.splice(originalPosition, 1);
110
111     arr.splice(destPosition, 0, moved_node);
112
113     arr.forEach((item, index) => {
114       if (item.position !== index + 1) {
115         item.position = index + 1;
116         item.updatePoistionFunction(this, item, instanceId, parentStoreKey);
117       }
118     });
119
120     return arr;
121   };
122
123   drag(store, serviceModelId: string, fromObj): boolean {
124     if(!this.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE)) {
125       return;
126     } else{
127       if (this.checkFeatureFlag(Features.FLAG_2008_DISABLE_DRAG_FOR_BASE_MODULE)) {
128         if(this.isBaseModule(serviceModelId,fromObj)) {
129           return;
130         }
131       }
132     }
133
134   }
135 }