<commit_before>/**
 * arranger.js
 * <NAME> - 2017
 */

import * as relationsTable from "./relationsTable";
import * as treeTableConversions from "./table2tree";
import * as tree from "./tree";

/**
 * Describes the arranging algorithm.
 * The algorithm takes as input a table of boxes, those 
 * boxes will be modified, not copied.
 * 
 * @export
 * @class Arranger
 */
export class Arranger {
    /**
     * Creates an instance of Arranger.
     * @param {relationsTable.RelationsTable} table 
     * @memberof Arranger
     */
    constructor(table) {
        if (!table) throw "table cannot be null or undefined";

        this.table = table;
        this.root = null; // The root of the tree
    }

    /**
     * Runs the algorithm.
     * 
     * @memberof Arranger
     */
    run() {
        if (!this.root) {
            this.root = new treeTableConversions.Table2Tree(this.table).convert();
            if (!this.root) throw "Error during table2tree conversion";
        }

        // Variables used to keep track of the max values for the positions in the 4 directions
        let maxX = 0,
            minX = 0,
            maxY = 0,
            minY = 0;
        
        const margin = 10; // In pixels

        // Here we should have the tree
        let traverser = new TreeTraverser(this.root);

        // Initialize
        traverser.traverse(function(node, type) {
            let box = node.content;
            if (!box) throw "Node content (box) not present";

            box.x = 0;
            box.y = 0;

            // TODO: Add checks for dimensions
        });

        // Arrange
        traverser.traverse(function(node, type) {
            let box = node.content;
            if (!box) throw "Node content (box) not present";

            if (type === -1) { // Current node is the root
                // Update position variable
                maxX += box.width;
                maxY += box.height;
                minX += 0;
                minY += 0;

                return;
            }

            if (type === tree.Arc.D) { // Current node is DOWN with its parent
                // Update node's position
                box.y = maxY;

                // Update position variable
                maxY += box.height;

                return;
            }

            if (type === tree.Arc.U) { // Current node is UP with its parent
                // Update node's position
                box.y = minY;

                // Update position variable
                minY -= box.height;

                return;
            }

            if (type === tree.Arc.L) { // Current node is LEFT with its parent
                // Update node's position
                box.x = maxX;

                // Update position variable
                maxX += box.width;

                return;
            }

            if (type === tree.Arc.R) { // Current node is RIGHT with its parent
                // Update node's position
                box.x = minX;

                // Update position variable
                minX -= box.width;

                return;
            }

            throw `Unrecognized relation type: ${type}`;
        });
    }
}

/**
 * Traverses a tree.
 * Strategy implemented: from first to last node (left to right) + depth-first
 * 
 * @class TreeTraverser
 */
class TreeTraverser {
    /**
     * Creates an instance of TreeTraverser.
     * @param {any} node 
     * @memberof TreeTraverser
     */
    constructor(node) {
        if (!node) throw "node cannot be null or undefined";

        this.root = node;
    }

    /**<commit_msg>Including margins<commit_after>/**
 * arranger.js
 * <NAME> - 2017
 */

import * as relationsTable from "./relationsTable";
import * as treeTableConversions from "./table2tree";
import * as tree from "./tree";

/**
 * Describes the arranging algorithm.
 * The algorithm takes as input a table of boxes, those 
 * boxes will be modified, not copied.
 * 
 * @export
 * @class Arranger
 */
export class Arranger {
    /**
     * Creates an instance of Arranger.
     * @param {relationsTable.RelationsTable} table 
     * @memberof Arranger
     */
    constructor(table) {
        if (!table) throw "table cannot be null or undefined";

        this.table = table;
        this.root = null; // The root of the tree
    }

    /**
     * Runs the algorithm.
     * 
     * @memberof Arranger
     */
    run() {
        if (!this.root) {
            this.root = new treeTableConversions.Table2Tree(this.table).convert();
            if (!this.root) throw "Error during table2tree conversion";
        }

        // Variables used to keep track of the max values for the positions in the 4 directions
        let maxX = 0,
            minX = 0,
            maxY = 0,
            minY = 0;
        
        const marginX = 10; // In pixels
        const marginY = 10; // In pixels

        // Here we should have the tree
        let traverser = new TreeTraverser(this.root);

        // Initialize
        traverser.traverse(function(node, type) {
            let box = node.content;
            if (!box) throw "Node content (box) not present";

            box.x = 0;
            box.y = 0;

            // TODO: Add checks for dimensions
        });

        // Arrange
        traverser.traverse(function(node, type) {
            let box = node.content;
            if (!box) throw "Node content (box) not present";

            if (type === -1) { // Current node is the root
                // Update position variable
                maxX += box.width;
                maxY += box.height;
                minX += 0;
                minY += 0;

                return;
            }

            if (type === tree.Arc.D) { // Current node is DOWN with its parent
                // Update node's position
                box.y = maxY;

                // Update position variable
                maxY += (box.height + marginY);

                return;
            }

            if (type === tree.Arc.U) { // Current node is UP with its parent
                // Update node's position
                box.y = minY;

                // Update position variable
                minY -= (box.height + marginY);

                return;
            }

            if (type === tree.Arc.L) { // Current node is LEFT with its parent
                // Update node's position
                box.x = maxX;

                // Update position variable
                maxX += (box.width + marginX);

                return;
            }

            if (type === tree.Arc.R) { // Current node is RIGHT with its parent
                // Update node's position
                box.x = minX;

                // Update position variable
                minX -= (box.width + marginX);

                return;
            }

            throw `Unrecognized relation type: ${type}`;
        });
    }
}

/**
 * Traverses a tree.
 * Strategy implemented: from first to last node (left to right) + depth-first
 * 
 * @class TreeTraverser
 */
class TreeTraverser {
    /**
     * Creates an instance of TreeTraverser.
     * @param {any} node 
     * @memberof TreeTraverser
     */
    constructor(node) {
        if (!node) throw "node cannot be null or undefined";

        this.root = node;
    }

    /**
{'fraction_non_alphanumeric': 0.10350492880613363, 'fraction_numerical': 0.00547645125958379, 'mean_word_length': 1.3602584814216478, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2433766', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1888, 'n_words': 650}