<commit_before>/* -*- mode: espresso; espresso-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set softtabstop=2 shiftwidth=2 tabstop=2 expandtab: */

// TODO connectors are screwed up when zooming in/out
// TODO check all other TODOS

/** Namespace where Node instances are created and edited. */
var SkeletonElements = new function()
{
  var active_skeleton_color = "rgb(255,255,0)";
  var inactive_skeleton_color = "rgb(255,0,255)";
  var inactive_skeleton_color_above = "rgb(0,0,255)";
  var inactive_skeleton_color_below = "rgb(255,0,0)";
  var root_node_color = "rgb(255, 0, 0)";

  var TYPE_NODE = "treenode";
  var TYPE_CONNECTORNODE = "connector";

  var CATCH_RADIUS = 8;

  var DISABLED = -1; // ID of the disabled nodes

  // Two arrays containing all created Node and ConnectorNode, for their reuse.
  var nodePool = [];
  var connectorPool = [];
  // The two corresponding indices in the pool for the next available instance for reuse
  var nextNodeIndex = 0;
  var nextConnectorIndex = 0;
  var firstDisabledNodeIndex = -1;

  this.resetCache = function() {
    nextNodeIndex = 0;
    nextConnectorIndex = 0;
  };

  this.clearCache = function() {
    nodePool = [];
    connectorPool = [];
    nextNodeIndex = 0;
    nextConnectorIndex = 0;
    firstDisabledNodeIndex = -1;
  }

  /** Disable all cached Node instances at or beyond the cutoff index. */
  this.disableBeyond = function(nodeCuttoff, connectorCuttoff) {
    var i;
    for (i = nodeCuttoff; i < nodePool.length; ++i) {
      disableNode(nodePool[i]);
    }
    for (i = connectorCuttoff; i < connectorPool.length; ++i) {
      disableConnectorNode(connectorPool[i]);
    }

    //console.log(nodePool.length, nextNodeIndex, nodeCuttoff);
  };

  /** Surrogate constructor that may reuse an existing, cached Node instance currently not in use.
   * Appends any newly created instances to the pool. */
  this.newNode = function(
    id, // unique id for the node from the database
    paper, // the raphael paper this node is drawn to
    parent, // the parent node, if present within the subset of nodes retrieved for display; otherwise null.
    r, // the radius
    x, // the x coordinate in pixel coordinates
    y, // y coordinates
    z, // z coordinates
    zdiff, // the different from the current slices
    skeleton_id,
    is_root_node) // the id of the skeleton this node is an element of
  {
    var node;
    if (nextNodeIndex < nodePool.length) {
      node = nodePool[nextNodeIndex];
      reuseNode(node, id, r, x, y, z, zdiff, skeleton_id, is_root_node);
      if (is_root_node && node.line) node.line.hide();
    } else {
      node = new this.Node(id, paper, parent, r, x, y, z, zdiff, skeleton_id, is_root_node);
      nodePool.push(node);
      if (node.line) node.line.toBack();
    }
    nextNodeIndex += 1;
    return node;
  };

  /** Constructor for Node instances. */
  this.Node = function(
    id, // unique id for the node from the database
    paper, // the raphael paper this node is drawn to
    parent, // the parent node
    r, // the radius
    x, // the x coordinate in pixel coordinates
    y, // y coordinates
    z, // z coordinates
    zdiff, // the different from the current slices
    skeleton_id,
    is_root_node) // the id of the skeleton this node is an element of
  {
    this.id = id;
    this.type = TYPE_NODE;
    this.paper = paper;
    this.parent = parent;
    this.children = {};
    this.connectors = {};
    this.r = r < 0 ? 3 : r;
    this.x = x;
    this.y = y;
    this.z = z;
    this.zdiff = zdiff;
    this.skeleton_id = skeleton_id;
    this.isroot = is_root_node;
    this.fillcolor = inactive_skeleton_color;
    this.c = null; // The Raphael circle for drawing
    this.mc = null; // The Raphael circle for mouse actions (it's a bit larger)
    this.line = paper.path(); // The Raphael line element that represents an edge between nodes

    // The member functions:
    this.setXY = setXY;
    this.drawEdges = nodeDrawEdges;
    this.draw = draw;
    this.deleteall = nodeDeleteAll;
    this.deletenode = nodeDelete;
    this.setColor = setColor;
    this.colorFromZDiff = nodeColorFromZDiff;
    this.createCircle = createCircle;

    // Init block
    // 1. Add this node to the parent's children if it exists
    if (parent) parent.children[id] = this;
  };

  /** Before reusing a node, clear all the member variables that
   * are relevant to the skeleton structure.
   * All numeric variables will be overwritten,
   * and the c, mc and line will be reused. */
  var disableNode = function(node)
  {
    node.id = DISABLED;
    node.parent = null;
    node.children = {};
    node.connectors = {};
    if (node.c) {
      node.c.hide();
      node.mc.hide();
    }
    if (node.line) {
      node.line.hide();
    }
  };

  /** Takes an existing Node and sets all the proper members as given, and resets the children and connectors. */
  var reuseNode = function(node, id, r, x, y, z, zdiff, skeleton_id, isroot)
  {
    node.id = id;
    node.parent = null;
    node.children = {};
    node.connectors = {};
    node.r = r < 0 ? 3 : r;<commit_msg>reuseNode would always set parent to null, so lines to the parent would not be drawn<commit_after>/* -*- mode: espresso; espresso-indent-level: 2; indent-tabs-mode: nil -*- */
/* vim: set softtabstop=2 shiftwidth=2 tabstop=2 expandtab: */

// TODO connectors are screwed up when zooming in/out
// TODO check all other TODOS

/** Namespace where Node instances are created and edited. */
var SkeletonElements = new function()
{
  var active_skeleton_color = "rgb(255,255,0)";
  var inactive_skeleton_color = "rgb(255,0,255)";
  var inactive_skeleton_color_above = "rgb(0,0,255)";
  var inactive_skeleton_color_below = "rgb(255,0,0)";
  var root_node_color = "rgb(255, 0, 0)";

  var TYPE_NODE = "treenode";
  var TYPE_CONNECTORNODE = "connector";

  var CATCH_RADIUS = 8;

  var DISABLED = -1; // ID of the disabled nodes

  // Two arrays containing all created Node and ConnectorNode, for their reuse.
  var nodePool = [];
  var connectorPool = [];
  // The two corresponding indices in the pool for the next available instance for reuse
  var nextNodeIndex = 0;
  var nextConnectorIndex = 0;
  var firstDisabledNodeIndex = -1;

  this.resetCache = function() {
    nextNodeIndex = 0;
    nextConnectorIndex = 0;
  };

  this.clearCache = function() {
    nodePool = [];
    connectorPool = [];
    nextNodeIndex = 0;
    nextConnectorIndex = 0;
    firstDisabledNodeIndex = -1;
  }

  /** Disable all cached Node instances at or beyond the cutoff index. */
  this.disableBeyond = function(nodeCuttoff, connectorCuttoff) {
    var i;
    for (i = nodeCuttoff; i < nodePool.length; ++i) {
      disableNode(nodePool[i]);
    }
    for (i = connectorCuttoff; i < connectorPool.length; ++i) {
      disableConnectorNode(connectorPool[i]);
    }

    //console.log(nodePool.length, nextNodeIndex, nodeCuttoff);
  };

  /** Surrogate constructor that may reuse an existing, cached Node instance currently not in use.
   * Appends any newly created instances to the pool. */
  this.newNode = function(
    id, // unique id for the node from the database
    paper, // the raphael paper this node is drawn to
    parent, // the parent node, if present within the subset of nodes retrieved for display; otherwise null.
    r, // the radius
    x, // the x coordinate in pixel coordinates
    y, // y coordinates
    z, // z coordinates
    zdiff, // the different from the current slices
    skeleton_id,
    is_root_node) // the id of the skeleton this node is an element of
  {
    var node;
    if (nextNodeIndex < nodePool.length) {
      node = nodePool[nextNodeIndex];
      reuseNode(node, id, parent, r, x, y, z, zdiff, skeleton_id, is_root_node);
      if (is_root_node && node.line) node.line.hide();
    } else {
      node = new this.Node(id, paper, parent, r, x, y, z, zdiff, skeleton_id, is_root_node);
      nodePool.push(node);
      if (node.line) node.line.toBack();
    }
    nextNodeIndex += 1;
    return node;
  };

  /** Constructor for Node instances. */
  this.Node = function(
    id, // unique id for the node from the database
    paper, // the raphael paper this node is drawn to
    parent, // the parent node
    r, // the radius
    x, // the x coordinate in pixel coordinates
    y, // y coordinates
    z, // z coordinates
    zdiff, // the different from the current slices
    skeleton_id,
    is_root_node) // the id of the skeleton this node is an element of
  {
    this.id = id;
    this.type = TYPE_NODE;
    this.paper = paper;
    this.parent = parent;
    this.children = {};
    this.connectors = {};
    this.r = r < 0 ? 3 : r;
    this.x = x;
    this.y = y;
    this.z = z;
    this.zdiff = zdiff;
    this.skeleton_id = skeleton_id;
    this.isroot = is_root_node;
    this.fillcolor = inactive_skeleton_color;
    this.c = null; // The Raphael circle for drawing
    this.mc = null; // The Raphael circle for mouse actions (it's a bit larger)
    this.line = paper.path(); // The Raphael line element that represents an edge between nodes

    // The member functions:
    this.setXY = setXY;
    this.drawEdges = nodeDrawEdges;
    this.draw = draw;
    this.deleteall = nodeDeleteAll;
    this.deletenode = nodeDelete;
    this.setColor = setColor;
    this.colorFromZDiff = nodeColorFromZDiff;
    this.createCircle = createCircle;

    // Init block
    // 1. Add this node to the parent's children if it exists
    if (parent) parent.children[id] = this;
  };

  /** Before reusing a node, clear all the member variables that
   * are relevant to the skeleton structure.
   * All numeric variables will be overwritten,
   * and the c, mc and line will be reused. */
  var disableNode = function(node)
  {
    node.id = DISABLED;
    node.parent = null;
    node.children = {};
    node.connectors = {};
    if (node.c) {
      node.c.hide();
      node.mc.hide();
    }
    if (node.line) {
      node.line.hide();
    }
  };

  /** Takes an existing Node and sets all the proper members as given, and resets the children and connectors. */
  var reuseNode = function(node, id, parent, r, x, y, z, zdiff, skeleton_id, isroot)
  {
    node.id = id;
    node.parent = parent;
    node.children = {};
    node.connectors = {};
    node.r = r < 0 ? 3 : r;
{'fraction_non_alphanumeric': 0.11108955079072474, 'fraction_numerical': 0.009508101290385176, 'mean_word_length': 2.764791818845873, 'pattern_counts': {'":': 0, '<': 13, '<?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': '6697044', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3237, 'n_words': 1098}