2daaaaf71a3363bf8547a2c8b837dd3c49fcb052
[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   isFlagOn(): boolean {
13     return FeatureFlagsService.getFlagState(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE, this.store);
14   }
15
16
17   /***********************************************************************************************
18    if the falg is ON and nodes have same parent
19    ***********************************************************************************************/
20   isAllowDrop(from: any, to: any): boolean {
21     return this.isFlagOn() && this.isSameParent(from, to);
22   }
23
24   private isSameParent(from: any, to: any): boolean {
25     try {
26       return from.parent.data.trackById === to.parent.parent.data.trackById;
27     } catch (e) { //parent not found
28       return false;
29     }
30   }
31
32   /********************************************************************
33    * manage drawing-board drag and drop operation
34    * @param nodes - array with elements data.
35    * @param tree - tree instance
36    * @param node - element information
37    * @param from - element from information
38    * @param to - element to information
39    ************************************************************/
40
41   drop(store, instanceId: string, nodes, {from, to}): void {
42
43     if (!this.isFlagOn()) return;
44
45     if (this.isAllowDrop(from, to)) {
46       let vfModules = nodes.find((parent) => {
47         return parent.trackById === to.parent.parent.data.trackById;
48       }).children;
49       this.array_move(vfModules, from.index, to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
50     }
51
52     /*  let firstLevelNames : DragAndDropModel[] = [
53         new DragAndDropModel('VF',true),
54         new DragAndDropModel('VL',true),
55         new DragAndDropModel('VFmodule',false)
56       ];
57
58       const fromObject = _.find(firstLevelNames, ['type', from.data.type]);
59       const toObject = _.find(firstLevelNames, ['type', to.parent.data.type]);
60
61       /!***********************************************************************************************
62        if the type are the same and there in same level + same parent -> then change element position
63        ***********************************************************************************************!/
64       if(fromObject.isFirstLevel === toObject.isFirstLevel){ // moving element in the same level and in the first level
65         if(fromObject.isFirstLevel){
66           this.array_move(nodes, from.index , to.parent.index, instanceId);
67         } else if(fromObject.isFirstLevel === toObject.isFirstLevel){
68           /!* check if they have the same parent *!/
69           if(from.parent.data.trackById === to.parent.parent.data.trackById){
70             let vfModules = nodes.find((parents)=> {
71               return parents.trackById === to.parent.parent.data.trackById;
72             }).children;
73             this.array_move(vfModules, from.index , to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
74           }
75         }
76       }*/
77   }
78
79
80   /********************************************************************
81    * move element inside array with elements position
82    * @param arr - array with elements data.
83    * @param originalPosition - element original position
84    * @param destPosition - element dest position
85    * @param destPinstanceIdosition - instance id
86    ******************************************************************/
87   array_move(arr, originalPosition, destPosition, instanceId: string, parentStoreKey?): Array<any> {
88
89     let moved_node = arr[originalPosition]
90
91     arr.splice(originalPosition, 1);
92
93     arr.splice(destPosition, 0, moved_node);
94
95     arr.forEach((item, index) => {
96       if (item.position !== index + 1) {
97         item.position = index + 1;
98         item.updatePoistionFunction(this, item, instanceId, parentStoreKey);
99       }
100     });
101
102     return arr;
103   };
104 }