<commit_before>import PIXI from 'pixi.js';
import CraftyBlockSpec from './CraftyBlockSpec.js';
import * as BLOCK_CONST from '../../constants/BlockConstants.js';
import * as LINE_CONST from '../../constants/LineConstants.js';
import CraftyBlockAnimator from './CraftyBlockAnimator.js';

export default class CraftyBlock extends PIXI.Container {
    constructor(blockInfo, childBlocks = []) {
        super();
        this.id = "block";
        this.blockInfo = blockInfo;
        this.childBlocks = childBlocks;
        this.parameterBlocks = [];
        this.lines = [];

        this.initialize();

        console.log(`DEBUG:::Created {${this.blockInfo.name}} block`);
    }

    static constantWithValue(value) {
        let blockInfo = new CraftyBlockSpec(value, CraftyBlock.CONSTANT);
        return new CraftyBlock(blockInfo);
    }

    static functionWithName(name) {
        let blockInfo = new CraftyBlockSpec.functionWithName(name);
        return new CraftyBlock(blockInfo);
    }

    initialize() {
        //  Create text and set style
        let text = new PIXI.Text(
            this.blockInfo.name,
            BLOCK_CONST.TEXT_STYLE
        );
        text.position.set(BLOCK_CONST.PADDING,BLOCK_CONST.PADDING);

        //  Create block graphics and set style
        let blockGraphics = new PIXI.Graphics();
        if (this.blockInfo.type == CraftyBlock.PARAMETER) { // if block is parameter, apply different style
            blockGraphics.beginFill(BLOCK_CONST.TYPE_PARAMETER_COLOR, BLOCK_CONST.OPACITY);
        } else if (this.blockInfo.type == CraftyBlock.CONSTANT) {
            blockGraphics.beginFill(BLOCK_CONST.TYPE_CONSTANT_COLOR, BLOCK_CONST.OPACITY);
        } else {
            blockGraphics.beginFill(BLOCK_CONST.TYPE_FUNCTION_COLOR, BLOCK_CONST.OPACITY);
        }
        blockGraphics.drawRoundedRect(0,0,text.width + 2 * BLOCK_CONST.PADDING, text.height + 2 * BLOCK_CONST.PADDING, BLOCK_CONST.CORNER_RADIUS);
        blockGraphics.endFill();

        //  Add PIXI Objects to parent container
        this.addChild(blockGraphics);
        this.addChild(text);

        //  Add parameter block to this(block) and make it invisible
        this.blockInfo.parameters.forEach((name) => {
            const parameterBlockInfo = new CraftyBlockSpec(name,CraftyBlock.PARAMETER);
            const newBlock = new CraftyBlock(parameterBlockInfo);
            this.parameterBlocks.push(newBlock);
            this.addChild(newBlock).visible = false;
        });

        //  Add child blocks to this
        this.childBlocks.forEach((block) => {
            this.addChild(block);
        });

        //  set interactivity of blocks
        CraftyBlockAnimator.makeInteractive(this)
    }

    //** render: positions child/parameter blocks and draws lines
    renderFrom(childIndex) {
        //console.log(`DEBUG::: Render {${this.blockInfo.name}} from index ${childIndex}`);

        const blockWidth = this.getChildAt(0).width;
        const blockHeight = this.getChildAt(0).height;
        let lineStartPosition = new PIXI.Point(blockWidth, blockHeight/2 - (LINE_CONST.STROKE_WIDTH + LINE_CONST.SPACING)*this.parameterBlocks.length/2 + childIndex*(LINE_CONST.STROKE_WIDTH + LINE_CONST.SPACING));
        let childBlockPosition = new PIXI.Point(blockWidth + BLOCK_CONST.SPACING_H, 0);
<commit_msg>Implement delayed addition of child blocks<commit_after>import PIXI from 'pixi.js';
import CraftyBlockSpec from './CraftyBlockSpec.js';
import * as BLOCK_CONST from '../../constants/BlockConstants.js';
import * as LINE_CONST from '../../constants/LineConstants.js';
import CraftyBlockAnimator from './CraftyBlockAnimator.js';

export default class CraftyBlock extends PIXI.Container {
    constructor(blockInfo, childBlocks = []) {
        super();
        this.id = "block";
        this.blockInfo = blockInfo;
        this.childBlocks = childBlocks;
        this.parameterBlocks = [];
        this.lines = [];

        this.initialize();

        console.log(`DEBUG:::Created {${this.blockInfo.name}} block`);
    }

    static constantWithValue(value) {
        let blockInfo = new CraftyBlockSpec(value, CraftyBlock.CONSTANT);
        return new CraftyBlock(blockInfo);
    }

    static functionWithName(name) {
        let blockInfo = new CraftyBlockSpec.functionWithName(name);
        return new CraftyBlock(blockInfo);
    }

    initialize() {
        //  Create text and set style
        let text = new PIXI.Text(
            this.blockInfo.name,
            BLOCK_CONST.TEXT_STYLE
        );
        text.position.set(BLOCK_CONST.PADDING,BLOCK_CONST.PADDING);

        //  Create block graphics and set style
        let blockGraphics = new PIXI.Graphics();
        if (this.blockInfo.type == CraftyBlock.PARAMETER) { // if block is parameter, apply different style
            blockGraphics.beginFill(BLOCK_CONST.TYPE_PARAMETER_COLOR, BLOCK_CONST.OPACITY);
        } else if (this.blockInfo.type == CraftyBlock.CONSTANT) {
            blockGraphics.beginFill(BLOCK_CONST.TYPE_CONSTANT_COLOR, BLOCK_CONST.OPACITY);
        } else {
            blockGraphics.beginFill(BLOCK_CONST.TYPE_FUNCTION_COLOR, BLOCK_CONST.OPACITY);
        }
        blockGraphics.drawRoundedRect(0,0,text.width + 2 * BLOCK_CONST.PADDING, text.height + 2 * BLOCK_CONST.PADDING, BLOCK_CONST.CORNER_RADIUS);
        blockGraphics.endFill();

        //  Add PIXI Objects to parent container
        this.addChild(blockGraphics);
        this.addChild(text);

        //  Add parameter block to this(block) and make it invisible
        this.blockInfo.parameters.forEach((name) => {
            const parameterBlockInfo = new CraftyBlockSpec(name,CraftyBlock.PARAMETER);
            const newBlock = new CraftyBlock(parameterBlockInfo);
            this.parameterBlocks.push(newBlock);
            this.addChild(newBlock).visible = false;
        });

        //  Add child blocks to this
        this.childBlocks.forEach((block) => {
            this.addChild(block);
        });

        //  set interactivity of blocks
        CraftyBlockAnimator.makeInteractive(this)
    }

    setChildBlocks (arr) {
        console.log("DEBUG::: childBlocks are set");
        this.childBlocks = arr;
        this.childBlocks.forEach((block) => {
            this.addChild(block);
        });
        this.renderFrom(0);
    }

    //** render: positions child/parameter blocks and draws lines
    renderFrom(childIndex) {
        //console.log(`DEBUG::: Render {${this.blockInfo.name}} from index ${childIndex}`);

        const blockWidth = this.getChildAt(0).width;
        const blockHeight = this.getChildAt(0).height;
        let lineStartPosition = new PIXI.Point(blockWidth, blockHeight/2 - (LINE_CONST.STROKE_WIDTH + LINE_CONST.SPACING)*this.parameterBlocks.length/2 + childIndex*(LINE_CONST.STROKE_WIDTH + LINE_CONST.SPACING));
        let childBlockPosition = new PIXI.Point(blockWidth + BLOCK_CONST.SPACING_H, 0);

{'fraction_non_alphanumeric': 0.10891517599538372, 'fraction_numerical': 0.002740911713791114, 'mean_word_length': 2.7843886462882095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '2226008', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1975, 'n_words': 396}