Skip to content
Snippets Groups Projects
SetScrollContent.js 4.13 KiB
Newer Older
  • Learn to ignore specific revisions
  • "use strict";
    /**
     * A MMT..whatever. Somewhere between the first and second "meta-" MMT had to leave common nomenclature behind
     * @typedef {{kind : string, uri: string}} MMTReference
     */
    /**
     * Facts, as they are send by MMT. Not *quite* the same format as Unity uses \*sigh\*, and everything has shortened names to avoid five additional letters \*double sigh\*
     * @typedef {Object} MMTFact
     * @property {MMTReference} ref the reference MMT uses to identify this fact
     * @property {string} label the human readable lable to display this fact.
     * @property {?any} df the definition of the Fact. May be null for {@link MMTScroll.requiredFacts}
     * @property {?MMTReference} tp the MMT type of this fact
     * @property {string} kind since a Fact is a MMTThingy, they also have a {@link MMTReference.kind}
     */
    /**
     * A Slot is an interface that can stand in for either
     * - a {@link Fact}, or
     * - the {@link Backend_Object} used to define the {@link Scroll} (in the backend)
     *
     * depending on what is needed.
     *
     * If all Slots of a {@link Scroll} are assigned, it can compute its {@link Scroll.acquiredFacts}.
     */
    class Slot {
        /**
         * @param default_label The default label of the Slot. Fallback if no Fact is assigned.
         * @param uri The uri of the Slot
         * @param type The expected type of the assigned Fact
         * @param assignedFact The Fact assigned to this Slot (may not exist yet)
         */
        constructor(default_label, uri, type, assignedFact) {
            this.default_label = default_label;
            this.uri = uri;
            this.type = type;
            this.assignedFact = assignedFact;
            this.assignedFact = assignedFact;
        }
        /**
         * The MathML element to display when this Slot is referenced.
         *
         * This is either the label of the {@link assignedFact}, or {@link default_label} if the former doesn't exist.
         */
        get label() {
            if (this.assignedFact) {
                return this.assignedFact.label;
            }
            else {
                return this.default_label;
            }
        }
    }
    /**
     * A Scroll, as it is implemented in the backend.
     *
     * Can do fancy stuff elsewhere, here we only want display its {@link description} and (possibly) {@link depiction}.
     * Both will reference the {@link slots}, and need dynamic adaption to the {@link Fact}s assigned to them.
     * The {@link acquiredFacts} referenced do not change, but they are semantically important and deserve special treatment.
     */
    class Scroll {
        constructor(label, uri, slots, acquiredFacts, description, depiction) {
            this.label = label;
            this.uri = uri;
            this.slots = slots;
            this.acquiredFacts = acquiredFacts;
            this.description = description;
            this.depiction = depiction;
            this.type = "Scroll";
        }
        /** TODO */
        static fromString(scrollString) {
            throw new Error("Not implemented.");
        }
    }
    let currentScrollRef = ""; // ref of the scroll currently displayed 
    function RenderScroll() {
        const dataSource = document.querySelector("#Unity-Data-Interface");
        if (!(dataSource instanceof HTMLElement)
            || dataSource === null
            || dataSource.dataset.scrollDynamic === undefined) {
            console.error("No Scroll found in the Unity-Data-Interface");
            return;
        }
        const scroll = Scroll.fromString(dataSource.dataset.scrollDynamic);
        //console.log(scroll.requiredFacts);
        const scrollContainer = document.querySelector("#scrollContainer");
        // replace the description if the scroll changed, otherwise only its content needs update
        // if (scroll.ref != currentScrollRef) {
        //     currentScrollRef = scroll.ref;
        //     scrollContainer.innerHTML = scroll.description;
        // }
        // go through the facts in the scroll, show their labels, add dropzones and limit the allowed types
        scroll.slots.forEach(slot => {
            $(`[data-slot-id='${slot.uri}']`)
                .html(slot.label)
                .attr("dropzone", "copy");
        });
        // acquired facts only need updated labels
        scroll.acquiredFacts.forEach(fact => {
            $(`[data-solution-id='${fact.uri}']`)
                .html(fact.label);
        });
        console.log(scroll.label + 'Scroll rendered');
    }
    $(function () {
        RenderScroll();
    });