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";
7 export class DragAndDropService {
9 constructor(private store: NgRedux<AppState>) {
13 return FeatureFlagsService.getFlagState(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE, this.store);
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);
24 private isSameParent(from: any, to: any): boolean {
26 return from.parent.data.trackById === to.parent.parent.data.trackById;
27 } catch (e) { //parent not found
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 ************************************************************/
41 drop(store, instanceId: string, nodes, {from, to}): void {
43 if (!this.isFlagOn()) return;
45 if (this.isAllowDrop(from, to)) {
46 let vfModules = nodes.find((parent) => {
47 return parent.trackById === to.parent.parent.data.trackById;
49 this.array_move(vfModules, from.index, to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
52 /* let firstLevelNames : DragAndDropModel[] = [
53 new DragAndDropModel('VF',true),
54 new DragAndDropModel('VL',true),
55 new DragAndDropModel('VFmodule',false)
58 const fromObject = _.find(firstLevelNames, ['type', from.data.type]);
59 const toObject = _.find(firstLevelNames, ['type', to.parent.data.type]);
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;
73 this.array_move(vfModules, from.index , to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
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> {
89 let moved_node = arr[originalPosition]
91 arr.splice(originalPosition, 1);
93 arr.splice(destPosition, 0, moved_node);
95 arr.forEach((item, index) => {
96 if (item.position !== index + 1) {
97 item.position = index + 1;
98 item.updatePoistionFunction(this, item, instanceId, parentStoreKey);