<commit_before>﻿import { Inject } from '@angular/core';
import { IPioneerTreeExpandedNode } from '../models/pioneer-tree-expanded-node.model';
import { PioneerTreeConfiguration, IPioneerTreeConfiguration } from '../models/pioneer-tree-configuration.model';

export interface IPioneerTreeDropChildService {
  /**
   * Sort a node dropped on a...
   *  1) Sort dropzone in same parent node
   *  2) Sort dropzone in new parent node
   * @param dropzone Node being dropped on
   * @param nodeToDrop Node to drop
   * @param droppedSortIndex Sort index of the node being dropped on
   */
  dropNode(dropzone: IPioneerTreeExpandedNode, nodeToDrop: IPioneerTreeExpandedNode, droppedSortIndex: number): void;
}

export class PioneerTreeDropChildService implements IPioneerTreeDropChildService {

  constructor(
    @Inject(PioneerTreeConfiguration) private config: IPioneerTreeConfiguration
  ) { }

  dropNode(dropzone: IPioneerTreeExpandedNode, nodeToDrop: IPioneerTreeExpandedNode, droppedSortIndex: number): void {
    var parentCollection = nodeToDrop.pioneerTreeNode.parentNode ?
      nodeToDrop.pioneerTreeNode.parentNode[this.config.childPropertyName] :
      nodeToDrop.pioneerTreeNode.treeRootNodes;
    this.prune(parentCollection, nodeToDrop.pioneerTreeNode.getId())

    dropzone.pioneerTreeNode.parentNode[this.config.childPropertyName].splice(droppedSortIndex, 0, nodeToDrop);

    this.adjustIndexes(dropzone.pioneerTreeNode.parentNode[this.config.childPropertyName]);
  }

  /**
   * Search tree and remove target node
   * @param nodes Tree(s) to traverse
   * @param nodeId Node id to target
   */
  private prune(nodes: IPioneerTreeExpandedNode[], nodeId: string) {
    for (let i = 0; i < nodes.length; i++) {
      if (nodes[i].pioneerTreeNode.getId() == nodeId) {
        nodes.splice(i, 1);
        return;
      }
    }
  }

  private adjustIndexes(collection: IPioneerTreeExpandedNode[]): void {
    for (let i = 0; i < collection.length; i++) {
      collection[i].pioneerTreeNode.sortIndex = i;
      if (collection[i][this.config.sortPropertyName]) {
        collection[i][this.config.sortPropertyName] = collection[i].pioneerTreeNode.sortIndex;
      }
    }
  }
}<commit_msg>Add drop child end logic<commit_after>﻿import { Inject } from '@angular/core';
import { IPioneerTreeExpandedNode } from '../models/pioneer-tree-expanded-node.model';
import { PioneerTreeConfiguration, IPioneerTreeConfiguration } from '../models/pioneer-tree-configuration.model';

export interface IPioneerTreeDropChildService {
  /**
   * Sort a node dropped on a...
   *  1) Sort dropzone in same parent node
   *  2) Sort dropzone in new parent node
   * @param dropzone Node being dropped on
   * @param nodeToDrop Node to drop
   * @param droppedSortIndex Sort index of the node being dropped on
   */
  dropNode(dropzone: IPioneerTreeExpandedNode, nodeToDrop: IPioneerTreeExpandedNode, droppedSortIndex: number): void;
}

export class PioneerTreeDropChildService implements IPioneerTreeDropChildService {

  constructor(
    @Inject(PioneerTreeConfiguration) private config: IPioneerTreeConfiguration
  ) { }

  dropNode(dropzone: IPioneerTreeExpandedNode, nodeToDrop: IPioneerTreeExpandedNode, droppedSortIndex: number): void {


    var parentCollection = nodeToDrop.pioneerTreeNode.parentNode ?
      nodeToDrop.pioneerTreeNode.parentNode[this.config.childPropertyName] :
      nodeToDrop.pioneerTreeNode.treeRootNodes;

    if (droppedSortIndex == dropzone.pioneerTreeNode.parentNode[this.config.childPropertyName].length - 1) {
      ++droppedSortIndex;
    }

    this.prune(parentCollection, nodeToDrop.pioneerTreeNode.getId())

    dropzone.pioneerTreeNode.parentNode[this.config.childPropertyName].splice(droppedSortIndex, 0, nodeToDrop);

    this.adjustIndexes(dropzone.pioneerTreeNode.parentNode[this.config.childPropertyName]);

  }

  /**
   * Search tree and remove target node
   * @param nodes Tree(s) to traverse
   * @param nodeId Node id to target
   */
  private prune(nodes: IPioneerTreeExpandedNode[], nodeId: string) {
    for (let i = 0; i < nodes.length; i++) {
      if (nodes[i].pioneerTreeNode.getId() == nodeId) {
        nodes.splice(i, 1);
        return;
      }
    }
  }

  private adjustIndexes(collection: IPioneerTreeExpandedNode[]): void {
    for (let i = 0; i < collection.length; i++) {
      collection[i].pioneerTreeNode.sortIndex = i;
      if (collection[i][this.config.sortPropertyName]) {
        collection[i][this.config.sortPropertyName] = collection[i].pioneerTreeNode.sortIndex;
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.10379635725257845, 'fraction_numerical': 0.0028527540048277375, 'mean_word_length': 4.221076746849943, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2367937', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1398, 'n_words': 297}