<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

import $ from "jquery";

var PREVIEW_MARGIN = 15;

export var Preview = {

    init(container, presentation, selection, viewport, controller) {
        this.container = container;
        this.presentation = presentation;
        this.selection = selection;
        this.viewport = viewport;
        this.controller = controller;

        controller.addListener("loadSVG", this.onLoadSVG.bind(this));
        $(window).resize(this.repaint.bind(this));

        return this;
    },

    onLoadSVG() {
        this.viewport.addListener("click", this.onClick.bind(this));
        this.viewport.addListener("userChangeState", this.controller.updateCameraStates.bind(this.controller));
        this.controller.addListener("repaint", this.repaint.bind(this));
        this.container.addEventListener("mouseenter", this.onMouseEnter.bind(this), false);
        this.container.addEventListener("mouseleave", this.onMouseLeave.bind(this), false);

        $("html head title").text(this.presentation.title);
        $(this.container).html(this.presentation.document.root);
        this.viewport.onLoad();
    },

    repaint() {
        var parent = $(this.container).parent();
        var parentWidth  = parent.innerWidth();
        var parentHeight = parent.innerHeight();

        var maxWidth  = parentWidth  - 2 * PREVIEW_MARGIN;
        var maxHeight = parentHeight - 2 * PREVIEW_MARGIN;

        var width  = Math.min(maxWidth, maxHeight * this.presentation.aspectWidth / this.presentation.aspectHeight);
        var height = Math.min(maxHeight, maxWidth * this.presentation.aspectHeight / this.presentation.aspectWidth);

        $(this.container).css({
            left:   (parentWidth  - width)  / 2 + "px",
            top:    (parentHeight - height) / 2 + "px",
            width:  width + "px",
            height: height + "px"
        });

        if (this.selection.currentFrame) {
            this.viewport.setAtStates(this.selection.currentFrame.cameraStates);
        }

        this.viewport.repaint();
    },

    onClick(button, evt) {
        if (button === 0 && evt.altKey) {
            var referenceElement = evt.target;
            if (referenceElement.hasAttribute("id") && referenceElement.getBBox) {
                this.controller.setReferenceElement(referenceElement);
            }
        }
    },

    /*
     * When the mouse enters the preview area,
     * show the document outside the clipping rectangle
     * and show the hidden SVG elements.
     */
    onMouseEnter() {
        this.viewport.cameras.forEach(camera => {
            if (camera.selected) {
                camera.revealClipping();
            }
        });
        this.viewport.showHiddenElements = true;
        this.viewport.repaint();
    },

    /*
     * When the mouse leaves the preview area,
     * hide the document outside the clipping rectangle<commit_msg>Fixed undefined SVG root when no SVG is open<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

import $ from "jquery";

var PREVIEW_MARGIN = 15;

export var Preview = {

    init(container, presentation, selection, viewport, controller) {
        this.container = container;
        this.presentation = presentation;
        this.selection = selection;
        this.viewport = viewport;
        this.controller = controller;

        controller.addListener("loadSVG", this.onLoadSVG.bind(this));
        $(window).resize(this.repaint.bind(this));

        return this;
    },

    onLoadSVG() {
        this.viewport.addListener("click", this.onClick.bind(this));
        this.viewport.addListener("userChangeState", this.controller.updateCameraStates.bind(this.controller));
        this.controller.addListener("repaint", this.repaint.bind(this));
        this.container.addEventListener("mouseenter", this.onMouseEnter.bind(this), false);
        this.container.addEventListener("mouseleave", this.onMouseLeave.bind(this), false);

        $("html head title").text(this.presentation.title);
        $(this.container).html(this.presentation.document.root);
        this.viewport.onLoad();
    },

    repaint() {
        var parent = $(this.container).parent();
        var parentWidth  = parent.innerWidth();
        var parentHeight = parent.innerHeight();

        var maxWidth  = parentWidth  - 2 * PREVIEW_MARGIN;
        var maxHeight = parentHeight - 2 * PREVIEW_MARGIN;

        var width  = Math.min(maxWidth, maxHeight * this.presentation.aspectWidth / this.presentation.aspectHeight);
        var height = Math.min(maxHeight, maxWidth * this.presentation.aspectHeight / this.presentation.aspectWidth);

        $(this.container).css({
            left:   (parentWidth  - width)  / 2 + "px",
            top:    (parentHeight - height) / 2 + "px",
            width:  width + "px",
            height: height + "px"
        });

        if (this.selection.currentFrame) {
            this.viewport.setAtStates(this.selection.currentFrame.cameraStates);
        }

        if (this.viewport.svgRoot) {
            this.viewport.repaint();
        }
    },

    onClick(button, evt) {
        if (button === 0 && evt.altKey) {
            var referenceElement = evt.target;
            if (referenceElement.hasAttribute("id") && referenceElement.getBBox) {
                this.controller.setReferenceElement(referenceElement);
            }
        }
    },

    /*
     * When the mouse enters the preview area,
     * show the document outside the clipping rectangle
     * and show the hidden SVG elements.
     */
    onMouseEnter() {
        this.viewport.cameras.forEach(camera => {
            if (camera.selected) {
                camera.revealClipping();
            }
        });
        this.viewport.showHiddenElements = true;
        this.viewport.repaint();
    },

    /*
     * When the mouse leaves the preview area,
     * hide the document outside the clipping rectangle
{'fraction_non_alphanumeric': 0.11927480916030535, 'fraction_numerical': 0.003498727735368957, 'mean_word_length': 2.416078218359587, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2925965', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1716, 'n_words': 393}