01763c6851dd5e36c615f477d6c0d68b39e62b01
[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 {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   isAllow(): boolean {
14     return FeatureFlagsService.getFlagState(Features.DRAG_AND_DROP_OPERATION, this.store);
15   }
16   /********************************************************************
17    * manage drawing-board drag and drop operation
18    * @param nodes - array with elements data.
19    * @param tree - tree instance
20    * @param node - element information
21    * @param from - element from information
22    * @param to - element to information
23    ************************************************************/
24
25   drag(store, instanceId : string , nodes, {from, to}) :void{
26     if (!store.getState().global.flags["DRAG_AND_DROP_OPERATION"]) return;
27
28     let firstLevelNames : DragAndDropModel[] = [
29         new DragAndDropModel('VF',true),
30         new DragAndDropModel('VL',true),
31         new DragAndDropModel('VFmodule',false)
32     ];
33
34     const fromObject = _.find(firstLevelNames, ['type', from.data.type]);
35     const toObject = _.find(firstLevelNames, ['type', to.parent.data.type]);
36
37     /***********************************************************************************************
38      if the type are the same and there in same level + same parent -> then change element position
39      ***********************************************************************************************/
40     if(fromObject.isFirstLevel === toObject.isFirstLevel){ // moving element in the same level and in the first level
41       if(fromObject.isFirstLevel){
42         this.array_move(nodes, from.index , to.parent.index, instanceId);
43       } else if(fromObject.isFirstLevel === toObject.isFirstLevel){
44         /* check if they have the same parent */
45         if(from.parent.data.trackById === to.parent.parent.data.trackById){
46           let vfModules = nodes.find((parents)=> {
47             return parents.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     }
53   }
54
55
56    /********************************************************************
57    * move element inside array with elements position
58    * @param arr - array with elements data.
59    * @param originalPosition - element original position
60    * @param destPosition - element dest position
61    * @param destPinstanceIdosition - instance id
62    ******************************************************************/
63   array_move(arr, originalPosition, destPosition, instanceId : string, parentStoreKey?) {
64     if (destPosition >= arr.length) {
65       let k = destPosition - arr.length + 1;
66       while (k--) {
67         arr.push(undefined);
68       }
69     }
70     arr.splice(destPosition, 0, arr.splice(originalPosition, 1)[0]);
71     arr.forEach((item, index) => {
72       if(item.position !== index){
73         item.position = index;
74         item.updatePoistionFunction(this, item, instanceId, parentStoreKey);
75       }
76     });
77   };
78 }