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';
9 export class DragAndDropService {
11 constructor(private store: NgRedux<AppState>) {
15 return FeatureFlagsService.getFlagState(Features.FLAG_1911_INSTANTIATION_ORDER_IN_ASYNC_ALACARTE, this.store);
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);
26 private isSameParent(from: any, to: any): boolean {
28 return from.parent.data.trackById === to.parent.parent.data.trackById;
29 } catch (e) { //parent not found
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 ************************************************************/
43 drop(store, instanceId: string, nodes, {from, to}): void {
45 if (!this.isFlagOn()) return;
47 if (this.isAllowDrop(from, to)) {
48 let vfModules = nodes.find((parent) => {
49 return parent.trackById === to.parent.parent.data.trackById;
51 this.array_move(vfModules, from.index, to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
54 /* let firstLevelNames : DragAndDropModel[] = [
55 new DragAndDropModel('VF',true),
56 new DragAndDropModel('VL',true),
57 new DragAndDropModel('VFmodule',false)
60 const fromObject = _.find(firstLevelNames, ['type', from.data.type]);
61 const toObject = _.find(firstLevelNames, ['type', to.parent.data.type]);
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;
75 this.array_move(vfModules, from.index , to.parent.index, instanceId, to.parent.parent.data.vnfStoreKey);
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> {
91 let moved_node = arr[originalPosition]
93 arr.splice(originalPosition, 1);
95 arr.splice(destPosition, 0, moved_node);
97 arr.forEach((item, index) => {
98 if (item.position !== index + 1) {
99 item.position = index + 1;
100 item.updatePoistionFunction(this, item, instanceId, parentStoreKey);