<commit_before>import { Position, Node } from "./node";
import { ITreeWidget, IDropHint, INodeElement, OnFinishOpenNode } from "./itree_widget";

export class NodeElement implements INodeElement {
    public node: Node;
    public $element: JQuery;
    protected tree_widget: ITreeWidget;

    constructor(node: Node, tree_widget: ITreeWidget) {
        this.init(node, tree_widget);
    }

    public init(node: Node, tree_widget: ITreeWidget) {
        this.node = node;
        this.tree_widget = tree_widget;

        if (! node.element) {
            node.element = this.tree_widget.element.get(0);
        }

        this.$element = $(node.element);
    }

    public addDropHint(position: number): IDropHint {
        if (position === Position.Inside) {
            return new BorderDropHint(this.$element);
        } else {
            return new GhostDropHint(this.node, this.$element, position);
        }
    }

    public select() {
        const $li = this.getLi();

        $li.addClass("jqtree-selected");
        $li.attr("aria-selected", "true");

        const $span = this.getSpan();
        $span.attr("tabindex", 0);
    }

    public deselect() {
        const $li = this.getLi();

        $li.removeClass("jqtree-selected");
        $li.attr("aria-selected", "false");

        const $span = this.getSpan();
        $span.attr("tabindex", -1);
    }

    protected getUl() {
        return this.$element.children("ul:first");
    }

    protected getSpan() {
        return this.$element.children(".jqtree-element").find("span.jqtree-title");
    }

    protected getLi() {
        return this.$element;
    }
}

export class FolderElement extends NodeElement {
    public open(on_finished: OnFinishOpenNode|null, slide = true) {
        if (! this.node.is_open) {
            this.node.is_open = true;

            const $button = this.getButton();
            $button.removeClass("jqtree-closed");
            $button.html("");

            const icon = this.tree_widget.renderer.opened_icon_element.cloneNode(false);
            $button.get(0).appendChild(icon);

            const doOpen = () => {
                const $li = this.getLi();
                $li.removeClass("jqtree-closed");

                const $span = this.getSpan();
                $span.attr("aria-expanded", "true");

                if (on_finished) {
                    on_finished(this.node);
                }

                this.tree_widget._triggerEvent("tree.open", {node: this.node});
            };

            if (slide) {
                this.getUl().slideDown("fast", doOpen);
            } else {
                this.getUl().show();
                doOpen();
            }
        }
    }

    public close(slide: boolean = true) {
        if (this.node.is_open) {
            this.node.is_open = false;
            const $button = this.getButton();
            $button.addClass("jqtree-closed");
            $button.html("");

            const icon = this.tree_widget.renderer.closed_icon_element.cloneNode(false);
            $button.get(0).appendChild(icon);

            const doClose = () => {
                const $li = this.getLi();
                $li.addClass("jqtree-closed");

                const $span = this.getSpan();
                $span.attr("aria-expanded", "false");

                this.tree_widget._triggerEvent("tree.close", {node: this.node});
            };

            if (slide) {
                this.getUl().slideUp("fast", doClose);
            } else {
                this.getUl().hide();
                doClose();
            }
        }
    }

    public addDropHint(position: number) {
        if (! this.node.is_open && position === Position.Inside) {
            return new BorderDropHint(this.$element);
        } else {
            return new GhostDropHint(this.node, this.$element, position);
        }
    }

    private getButton(): JQuery {<commit_msg>Issue #481: check if button element exists in 'open' and 'close' functions<commit_after>import { Position, Node } from "./node";
import { ITreeWidget, IDropHint, INodeElement, OnFinishOpenNode } from "./itree_widget";

export class NodeElement implements INodeElement {
    public node: Node;
    public $element: JQuery;
    protected tree_widget: ITreeWidget;

    constructor(node: Node, tree_widget: ITreeWidget) {
        this.init(node, tree_widget);
    }

    public init(node: Node, tree_widget: ITreeWidget) {
        this.node = node;
        this.tree_widget = tree_widget;

        if (! node.element) {
            node.element = this.tree_widget.element.get(0);
        }

        this.$element = $(node.element);
    }

    public addDropHint(position: number): IDropHint {
        if (position === Position.Inside) {
            return new BorderDropHint(this.$element);
        } else {
            return new GhostDropHint(this.node, this.$element, position);
        }
    }

    public select() {
        const $li = this.getLi();

        $li.addClass("jqtree-selected");
        $li.attr("aria-selected", "true");

        const $span = this.getSpan();
        $span.attr("tabindex", 0);
    }

    public deselect() {
        const $li = this.getLi();

        $li.removeClass("jqtree-selected");
        $li.attr("aria-selected", "false");

        const $span = this.getSpan();
        $span.attr("tabindex", -1);
    }

    protected getUl() {
        return this.$element.children("ul:first");
    }

    protected getSpan() {
        return this.$element.children(".jqtree-element").find("span.jqtree-title");
    }

    protected getLi() {
        return this.$element;
    }
}

export class FolderElement extends NodeElement {
    public open(on_finished: OnFinishOpenNode|null, slide = true) {
        if (! this.node.is_open) {
            this.node.is_open = true;

            const $button = this.getButton();
            $button.removeClass("jqtree-closed");
            $button.html("");

            const button_el = $button.get(0);

            if (button_el) {
                const icon = this.tree_widget.renderer.opened_icon_element.cloneNode(false);

                button_el.appendChild(icon);
            }

            const doOpen = () => {
                const $li = this.getLi();
                $li.removeClass("jqtree-closed");

                const $span = this.getSpan();
                $span.attr("aria-expanded", "true");

                if (on_finished) {
                    on_finished(this.node);
                }

                this.tree_widget._triggerEvent("tree.open", {node: this.node});
            };

            if (slide) {
                this.getUl().slideDown("fast", doOpen);
            } else {
                this.getUl().show();
                doOpen();
            }
        }
    }

    public close(slide: boolean = true) {
        if (this.node.is_open) {
            this.node.is_open = false;
            const $button = this.getButton();
            $button.addClass("jqtree-closed");
            $button.html("");

            const button_el = $button.get(0);

            if (button_el) {
                const icon = this.tree_widget.renderer.closed_icon_element.cloneNode(false);

                button_el.appendChild(icon);
            }

            const doClose = () => {
                const $li = this.getLi();
                $li.addClass("jqtree-closed");

                const $span = this.getSpan();
                $span.attr("aria-expanded", "false");

                this.tree_widget._triggerEvent("tree.close", {node: this.node});
            };

            if (slide) {
                this.getUl().slideUp("fast", doClose);
            } else {
                this.getUl().hide();
                doClose();
            }
        }
    }

    public addDropHint(position: number) {
        if (! this.node.is_open && position === Position.Inside) {
            return new BorderDropHint(this.$element);
        } else {
            return new GhostDropHint(this.node, this.$element, position);
        }
    }

    private getButton(): JQuery {
{'fraction_non_alphanumeric': 0.1370100160751824, 'fraction_numerical': 0.0016075182391492519, 'mean_word_length': 1.7717614804660726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '766904', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2229, 'n_words': 431}