summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/drawingBoard/service-planning/drawing-board-tree/dragAndDrop/dragAndDrop.service.ts
blob: 2f86b0aa8a599539a5107e6fb91120f86b911185 (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
import {Injectable} from "@angular/core";
import {NgRedux} from "@angular-redux/store";
import {AppState} from "../../../../shared/store/reducers";
import {FeatureFlagsService, Features} from "../../../../shared/services/featureFlag/feature-flags.service";

@Injectable()
export class DragAndDropService {

  constructor(private store: NgRedux<AppState>) {
  }

  checkFeatureFlag(flagValue):boolean {
    let featureFlag :boolean;
    featureFlag = FeatureFlagsService.getFlagState(flagValue, this.store);
    return featureFlag;
  }

  /***********************************************************************************************
   if the dragged node is a base module instance
   ***********************************************************************************************/
  isBaseModule(serviceInstanceId, from): boolean {
    try {
      let baseModuleFlag = this.store.getState().service.serviceHierarchy[serviceInstanceId].vnfs[from.parent.data.vnfStoreKey].vfModules[from.data.modelName].properties.baseModule;
      return (baseModuleFlag != 'undefined' ? baseModuleFlag : false);
    }catch(e) {
      return false;
    }
  }


  /***********************************************************************************************
   if the flag is ON and nodes have same parent
   ***********************************************************************************************/
  isAllowDrop(from: any, to: any): boolean {
    return this.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE) && this.isSameParent(from, to);
  }

  private isSameParent(from: any, to: any): boolean {
    try {
      return from.parent.data.trackById === to.parent.parent.data.trackById;
    } catch (e) { //parent not found
      return false;
    }
  }

  /********************************************************************
   * manage drawing-board drag and drop operation
   * @param nodes - array with elements data.
   * @param tree - tree instance
   * @param node - element information
   * @param from - element from information
   * @param to - element to information
   ************************************************************/

  drop(store, instanceId: string, nodes, {from, to}): void {

    if (!this.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE)) return;

    if(this.checkFeatureFlag(Features.FLAG_2008_DISABLE_DRAG_FOR_BASE_MODULE)) {
      if ((to.parent.index == 0 && this.isBaseModule(instanceId, to.parent)) || this.isBaseModule(instanceId, from )) return;
    }

    if (this.isAllowDrop(from, to)) {
      let vfModules = nodes.find((parent) => {
        return parent.trackById === to.parent.parent.data.trackById;
      }).children;
      this.array_move(vfModules, from.index, to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
    }

    /*  let firstLevelNames : DragAndDropModel[] = [
        new DragAndDropModel('VF',true),
        new DragAndDropModel('VL',true),
        new DragAndDropModel('VFmodule',false)
      ];

      const fromObject = _.find(firstLevelNames, ['type', from.data.type]);
      const toObject = _.find(firstLevelNames, ['type', to.parent.data.type]);

      /!***********************************************************************************************
       if the type are the same and there in same level + same parent -> then change element position
       ***********************************************************************************************!/
      if(fromObject.isFirstLevel === toObject.isFirstLevel){ // moving element in the same level and in the first level
        if(fromObject.isFirstLevel){
          this.array_move(nodes, from.index , to.parent.index, instanceId);
        } else if(fromObject.isFirstLevel === toObject.isFirstLevel){
          /!* check if they have the same parent *!/
          if(from.parent.data.trackById === to.parent.parent.data.trackById){
            let vfModules = nodes.find((parents)=> {
              return parents.trackById === to.parent.parent.data.trackById;
            }).children;
            this.array_move(vfModules, from.index , to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
          }
        }
      }*/
  }


  /********************************************************************
   * move element inside array with elements position
   * @param arr - array with elements data.
   * @param originalPosition - element original position
   * @param destPosition - element dest position
   * @param destPinstanceIdosition - instance id
   ******************************************************************/
  array_move(arr, originalPosition, destPosition, instanceId: string, parentStoreKey?): Array<any> {

    let moved_node = arr[originalPosition]

    arr.splice(originalPosition, 1);

    arr.splice(destPosition, 0, moved_node);

    arr.forEach((item, index) => {
      if (item.position !== index + 1) {
        item.position = index + 1;
        item.updatePoistionFunction(this, item, instanceId, parentStoreKey);
      }
    });

    return arr;
  };

  drag(store, serviceModelId: string, fromObj): boolean {
    if(!this.checkFeatureFlag(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE)) {
      return;
    } else{
      if (this.checkFeatureFlag(Features.FLAG_2008_DISABLE_DRAG_FOR_BASE_MODULE)) {
        if(this.isBaseModule(serviceModelId,fromObj)) {
          return;
        }
      }
    }

  }
}