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