/** * Where the app's state comes from and goes to: the query string it loads * and the permalink it writes, gists it creates and updates, downloads. * Mixed onto ShExBaseApp; see ShExBaseApp.js. * * This is doc/ShExBaseApp-links.js's source (tsconfig.app.json compiles src/app/ into * doc/); edit here and run `npm run build`. */ mixin(ShExBaseApp, { /** * Load URL search parameters */ async loadSearchParameters() { // don't overwrite if we arrived here from going back and forth in history if (this.Caches.inputSchema.selection.val() !== "" || this.Caches.inputData.selection.val() !== "") return Promise.resolve(); const iface = this.parseQueryString(location.search); this.toggleControlsArrow("down"); $(".manifest li").text("no manifest schemas loaded"); if ("examples" in iface) { // deprecated ?examples= interface iface.manifestURL = iface.examples; delete iface.examples; } if (!("manifest" in iface) && !("manifestURL" in iface)) { iface.manifestURL = ["../examples/manifest.json"]; } // a gist-hosted manifest can be edited in place: reveal Menu → "/Update" const gistManifest = "manifestURL" in iface && iface.manifestURL[0].match(/^https:\/\/gist\.githubusercontent\.com\/([^\/]+)\/([0-9a-f]+)\/raw\/(?:([0-9a-f]+)\/)?/); if (gistManifest) { this.loadedGist = { owner: gistManifest[1], id: gistManifest[2], sha: gistManifest[3] }; $("#updateGist").show(); } // Load all known query parameters. Save load results into array like: /* [ [ "data", { "skipped": "skipped" } ], [ "manifest", { "fromUrl": { "url": "http://...", "data": "..." } } ], ] */ // Plugins first, and one at a time: what one adds -- panes, and the // parameters and manifest keys that fill them -- has to be in // QueryParams before the walk below reads QueryParams. ?plugin= and // ?pluginURL= are the same thing said two ways; a permalink writes the // second, and a person writes whichever they remember. await this.loadPlugins([].concat(iface.pluginURL || [], iface.plugin || [])); const loadedAsArray = await Promise.all(this.QueryParams.map(async (input) => { const label = input.queryStringParm; const parm = label; if (input.earlyLoad) return [label, { skipped: "skipped" }]; if (parm + "URL" in iface) { const url = iface[parm + "URL"][0]; if (url.length > 0) { // manifest= loads no manifest // !!! set anyways in asyncGet? input.cache.url = url; // all fooURL query parms are caches. try { const got = await input.cache.asyncGet(url); return [label, { fromUrl: got }]; } catch (e) { if ("fail" in input) { input.fail(e); } else { input.location.val(e.message); } this.resultsWidget.append($("
").text(e).addClass("error"));
                        return [label, { loadFailure: e instanceof Error ? e : Error(e) }];
                    }
                    ;
                }
            }
            else if (parm in iface) {
                const prepend = input.location.prop("tagName") === "TEXTAREA" ?
                    input.location.val() :
                    "";
                const value = prepend + (input.normalize
                    ? input.normalize((parm === "data" ? iface[parm].slice(0, 1) : iface[parm]).join(""))
                    : (parm === "data" ? iface[parm].slice(0, 1) : iface[parm]).join(""));
                const origValue = input.location.val();
                try {
                    if ("cache" in input) {
                        await input.cache.set(value, location.href);
                    }
                    else {
                        input.location.val(prepend + value);
                        if (input.location.val() === null)
                            throw Error(`Unable to set value to ${prepend + value}`);
                    }
                    return [label, { literal: value }];
                }
                catch (e) {
                    input.location.val(origValue);
                    if ("fail" in input) {
                        input.fail(e);
                    }
                    this.resultsWidget.append($("
").text("error setting " + label + ":\n" + e + "\n" + value).addClass("error"));
                    return [label, { failure: e }];
                }
            }
            else if ("deflt" in input) {
                input.location.val(input.deflt);
                return [label, { deflt: "deflt" }]; // flag that it was a default
            }
            return [label, { skipped: "skipped" }];
        }));
        // convert loaded array into Object:
        /* { "data": { "skipped": "skipped" },
           "manifest": { "fromUrl": { "url": "http://...", "data": "..." } }, } */
        const loaded = loadedAsArray.reduce((acc, fromArray) => {
            acc[fromArray[0]] = fromArray[1];
            return acc;
        }, {});
        // A data source may hold several documents: `data=` repeats, once per
        // pane in the order they are shown.  With `dataURL=` the first document
        // came from there and the `data=` values are the ones after it.
        if (iface.data && this.neighborhoods && iface.data.length > ("dataURL" in iface ? 0 : 1))
            this.neighborhoods.setDocuments(("dataURL" in iface ? [this.Caches.inputData.selection.val()] : [])
                .concat(iface.data));
        // Parse the shape-map using the prefixes and base.  What it reports is
        // rendered where the map is; this is called for what it fills in -- and
        // awaited, so that a link finishes loading with the map it named ready
        // to validate rather than still being built.  (It reports its errors
        // rather than throwing them, so a bad map is a message, not a failed
        // load.)
        if ($("#queryMap").val().trim().length > 0)
            await this.Caches.shapeMap.copyQueryMapToEditMap();
        else
            this.Caches.shapeMap.makeFreshEditMap();
        this.customizeInterface();
        $("body").keydown((e) => {
            const code = e.keyCode || e.charCode; // standards anyone?
            return !this.keyDownHandlers.find(h => h(e, code)); // if we find a handler, stop propagation
        });
        // A link that carries a schema does not validate on arrival.  It used to
        // say it did -- and threw a ReferenceError instead, for the two years
        // since these became methods of a class (`callValidator()`, with no
        // `this`), so nothing has ever run here.  Opening a link is not asking
        // for the walk behind it: a permalink may name an endpoint or a Wikibase,
        // where validating costs a hundred requests to somebody else's service.
        // Press validate.
        return loaded;
    },
    parseQueryString(query) {
        if (query[0] === '?')
            query = query.substr(1); // optional leading '?'
        const map = {};
        query.replace(/([^&,=]+)=?([^&,]*)(?:[&,]+|$)/g, (match, key, value) => {
            key = decodeURIComponent(key);
            value = decodeURIComponent(value);
            (map[key] = map[key] || []).push(value);
        });
        return map;
    },
    /**
     * update location with a current values of some inputs
     */
    async getPermalink() {
        let parms = [];
        // The map as it stands, whether or not the source can still answer it:
        // a link is about what is on screen, and a query map this source cannot
        // resolve -- a `SPARQL '''…'''` after a slurp handed the reader the
        // local store -- is still what the link should carry.  Failing to build
        // one leaves the menu with no link and says nothing about why.
        try {
            await this.Caches.shapeMap.copyEditMapToQueryMap();
        }
        catch (e) {
            // the query map keeps what it says, which is what goes in the link
        }
        parms = parms.concat(this.QueryParams.reduce((acc, input) => {
            let parm = input.queryStringParm;
            let val = input.location.val();
            // more than one plugin may be loaded, and the link has to bring
            // them all back
            if (input.cache && Array.isArray(input.cache.urls))
                return acc.concat(input.cache.urls.map(u => parm + "URL=" + encodeURIComponent(u)));
            if (input.cache && input.cache.url &&
                // Specifically avoid loading from DefaultBase?schema=blah
                // because that will load the HTML page.
                !input.cache.url.startsWith(DefaultBase)) {
                parm += "URL";
                val = input.cache.url;
            }
            // a parameter left out comes back as its default, so a link only
            // says what differs from it
            if ("deflt" in input && val === input.deflt)
                return acc;
            return val.length > 0 ?
                acc.concat(parm + "=" + encodeURIComponent(val)) :
                acc;
        }, []));
        // The data source may hold several documents (a measurement in one, its
        // patient in another): `data=` repeats, once per pane in the order they
        // are shown.  With `dataURL=` the first came from there, so the rest follow.
        const neighborhoods = this.neighborhoods;
        if (neighborhoods && neighborhoods.documents().length > 1) {
            const docs = neighborhoods.documents().map((d, at) => neighborhoods.docAt(at).text);
            const fromUrl = parms.some(p => p.startsWith("dataURL="));
            parms = parms.filter(p => !p.startsWith("data="));
            (fromUrl ? docs.slice(1) : docs).forEach(text => parms.push("data=" + encodeURIComponent(text)));
        }
        const s = parms.join("&");
        return location.origin + location.pathname + "?" + s;
    },
    /** Menu → "shorten": swap the (often very long) permalink for a TinyURL
     * short link.  TinyURL's keyless api-create.php is CORS-enabled and hands
     * back the short URL as plain text, so no server of our own is needed; we
     * show it beside Permalink and copy it to the clipboard.  TinyURL answers
     * 200 with an error string rather than an HTTP error when it refuses a URL
     * (an over-long one, say), so we vet the body, not just the status, and on
     * any failure keep the long link -- for a link too big to shorten, "Create
     * Gist" is the path that scales. */
    async shortenPermalink(evt) {
        if (evt)
            evt.preventDefault();
        const $btn = $("#shortenPermalink"), $out = $("#shortPermalink"), $row = $("#shortPermalinkRow");
        const label = $btn.text() || "shorten";
        // the menu builds the permalink as it opens; build one now in case the
        // click somehow beat that
        const long = $("#permalinkAnchor").attr("href") || await this.getPermalink();
        $btn.prop("disabled", true).text("shortening…");
        $row.hide();
        $out.removeAttr("href").text("");
        try {
            const resp = await fetch("https://tinyurl.com/api-create.php?url=" +
                encodeURIComponent(long));
            const body = (await resp.text()).trim();
            // TinyURL answers 200 with an error string (not an HTTP error) when it
            // refuses a URL -- an over-long one, say -- so vet the body, not the status
            if (!resp.ok || !/^https?:\/\//.test(body))
                throw new Error(body || ("HTTP " + resp.status));
            // show it beside Permalink, log it (a place to copy it from), and put it
            // on the clipboard
            $out.attr("href", body).text(body);
            $row.show();
            console.log("TinyURL short link: " + body);
            let copied = false;
            try {
                await navigator.clipboard.writeText(body);
                copied = true;
            }
            catch (e) { /* clipboard needs a secure context + permission; link still shows */ }
            $btn.text(copied ? "copied ✓" : "shortened");
        }
        catch (e) {
            $btn.text("shorten failed").attr("title", "TinyURL: " + e.message);
            console.warn("TinyURL shorten failed:", e);
        }
        window.setTimeout(() => $btn.prop("disabled", false).text(label)
            .attr("title", "Shorten this link with TinyURL"), 2500);
        return null;
    },
    /** Menu → "Create Gist": publish the inputs this app registered with a
     * manifest descriptor in its QueryParams (shex.html: schema, data,
     * queryMap; shexmap adds staticVars, outputSchema, outputShapeMap) as a
     * github gist (modeled on
     * )
     * and reload this page with ?manifestURL= pointing at the gist's
     * .manifest.yaml.  Texts over GIST_INLINE_LINES lines become separate
     * files (each descriptor's spillName) referenced by relative URLs. */
    async createGist(evt) {
        if (evt)
            evt.preventDefault();
        this.toggleControls();
        const title = prompt("Title for this gist:", "");
        if (title === null)
            return null; // canceled
        const token = this.getGistToken();
        if (!token)
            return null;
        const files = await this.assembleGistFiles();
        const ghApi = this.ghApi.bind(this, token);
        try {
            const created = await ghApi("https://api.github.com/gists", "POST", { description: title || "ShEx validation example", public: true, files });
            localStorage.setItem(GIST_TOKEN_KEY, token);
            // sha-less raw URL: always the latest revision, and relative
            // schemaURL/dataURL/queryMapURL references resolve beside it (per-file
            // blob-sha raw_urls don't serve sibling files)
            const gistBase = `https://gist.githubusercontent.com/${created.owner.login}/${created.id}/raw/`;
            const simplePath = (location.pathname.match(/\/packages\/.*$/)
                || ["/packages/shex-webapp/doc/shex.html"])[0];
            const md = `the [manifest](${created.html_url}#file-manifest-yaml) can be used in:\n`
                + `* ShEx.JS [validator](https://shex.js.org${simplePath}`
                + `?manifestURL=${gistBase}.manifest.yaml)\n`;
            const mdName = `-${title ? title.replace(/[\/\\]/g, "-") + " " : ""}ShEx Validation Manifest.md`;
            const patched = await ghApi(created.url, "PATCH", { files: { [mdName]: { content: md } } });
            // pin the address bar's manifestURL to the created revision so the
            // permalink outlives later edits to the gist
            const manifestURL = "history" in patched && patched.history.length
                ? `${gistBase}${patched.history[0].version}/.manifest.yaml`
                : gistBase + ".manifest.yaml";
            const parms = this.QueryParams
                .filter(q => this.Getables.indexOf(q) === -1) // controls only; content comes from the gist
                .map(q => q.queryStringParm + "=" + encodeURIComponent(q.location.val()))
                .concat(["manifestURL=" + encodeURIComponent(manifestURL)]);
            const search = "?" + parms.join("&");
            // the created gist's address (a popup here proved to break the
            // reload); stashed so the reloaded page can log it again -- the
            // navigation clears the console
            const trace = `created gist: ${created.html_url} manifest: ${manifestURL}`;
            console.log(trace);
            try {
                sessionStorage.setItem(GIST_CREATED_KEY, trace);
            }
            catch (e) { /* private mode */ }
            location.search = search; // navigates: reload from the gist manifest
            return search; // for tests, which can't navigate
        }
        catch (e) {
            this.resultsWidget.failMessage(e, "creating gist");
            return null;
        }
    },
    /** Menu → "Update": publish the current state back to the gist this page's
     * manifestURL was loaded from (this.loadedGist, revealed by
     * loadSearchParameters), nulling spill-over files the new revision no
     * longer references, then reload pinned to the new revision (sha-less raw
     * URLs are CDN-cached, so reloading unpinned could show stale content). */
    async updateGist(evt) {
        if (evt)
            evt.preventDefault();
        this.toggleControls();
        if (!this.loadedGist)
            return null;
        const token = this.getGistToken();
        if (!token)
            return null;
        const files = await this.assembleGistFiles();
        const ghApi = this.ghApi.bind(this, token);
        try {
            const gistApiUrl = `https://api.github.com/gists/${this.loadedGist.id}`;
            const current = await ghApi(gistApiUrl, "GET");
            // this page may hold a revision somebody -- maybe this user in another
            // window -- has updated since
            if (this.loadedGist.sha && current.history && current.history.length
                && current.history[0].version !== this.loadedGist.sha
                && !confirm(`This page holds revision ${this.loadedGist.sha.substr(0, 7)}`
                    + ` but the gist has moved on to ${current.history[0].version.substr(0, 7)}.`
                    + ` Overwrite the newer revision?`))
                return null;
            for (const q of this.QueryParams)
                if (q.manifest && "spillName" in q.manifest
                    && q.manifest.spillName in current.files && !(q.manifest.spillName in files))
                    files[q.manifest.spillName] = null; // delete spill-overs now recorded inline
            const patched = await ghApi(gistApiUrl, "PATCH", { files });
            localStorage.setItem(GIST_TOKEN_KEY, token);
            const gistBase = `https://gist.githubusercontent.com/${this.loadedGist.owner}/${this.loadedGist.id}/raw/`;
            const manifestURL = "history" in patched && patched.history.length
                ? `${gistBase}${patched.history[0].version}/.manifest.yaml`
                : gistBase + ".manifest.yaml";
            const parms = this.QueryParams
                .filter(q => this.Getables.indexOf(q) === -1) // controls only; content comes from the gist
                .map(q => q.queryStringParm + "=" + encodeURIComponent(q.location.val()))
                .concat(["manifestURL=" + encodeURIComponent(manifestURL)]);
            const search = "?" + parms.join("&");
            const trace = `updated gist: ${current.html_url} manifest: ${manifestURL}`;
            console.log(trace);
            try {
                sessionStorage.setItem(GIST_CREATED_KEY, trace);
            }
            catch (e) { /* private mode */ }
            location.search = search; // navigates: reload from the updated manifest
            return search; // for tests, which can't navigate
        }
        catch (e) {
            if (/ 404 /.test(e.message))
                e.message += " (is this your gist? updating needs its owner's token)";
            this.resultsWidget.failMessage(e, "updating gist");
            return null;
        }
    },
    /** the github token Create/Update Gist use, prompted for once and
     * remembered in localStorage (cleared again by a 401 in ghApi) */
    getGistToken() {
        return localStorage.getItem(GIST_TOKEN_KEY)
            || prompt("Creating a gist requires a github token with \"gist\" scope\n"
                + "(menu → \"get token\" creates one; menu → \"instructions\" explains;\n"
                + "remembered in this browser's localStorage):");
    },
    async ghApi(token, url, method, body) {
        const resp = await fetch(url, {
            method,
            headers: { "Content-Type": "application/json",
                "Accept": "application/vnd.github+json",
                "Authorization": "token " + token },
            body: body === undefined ? undefined : JSON.stringify(body),
        });
        if (!resp.ok) {
            if (resp.status === 401)
                localStorage.removeItem(GIST_TOKEN_KEY); // stale token; re-prompt next time
            throw Error(`${method} ${url} → ${resp.status} ${await resp.text()}`);
        }
        return resp.json();
    },
    /** the files Create/Update Gist publish: .manifest.yaml holding one entry
     * built from the QueryParams manifest descriptors, plus a spill-over file
     * per input whose text is over GIST_INLINE_LINES lines.
     *
     * The manifest is written in the CANONICAL form (@shexjs/manifest): an
     * @context stacking the core contexts (the ShEx manifest vocabulary's,
     * then shex.js's own terms) and a binding for each scoped
     * vocabulary the entry uses, and `entries`; a core attribute at the
     * entry, and an attribute some vocabulary's scope defines inside that
     * vocabulary's `:parms`, which also names the plugin that
     * implements it when that plugin is loaded here -- so the gist says by
     * itself what it needs.  An input left empty is left out: a reader
     * defaults it, and "" is no reference. */
    async assembleGistFiles() {
        await this.Caches.shapeMap.copyEditMapToQueryMap();
        const Manifest = ShExWebApp.Manifest;
        const status = $("#results .fails").length ? "nonconformant" : "conformant";
        const files = {};
        const part = (indent, parm, fileName, text) => {
            if (text.split("\n").length > GIST_INLINE_LINES) {
                files[fileName] = { content: text };
                return `${indent}${parm}URL: ${fileName}\n`;
            }
            return `${indent}${parm}: |\n` + text.replace(/\n+$/, "").split("\n")
                .map((l) => l.length ? indent + "  " + l : "").join("\n") + "\n";
        };
        // each QueryParams entry with a manifest descriptor contributes to the
        // manifest entry, so each app's input registry declares what a gist records
        const core = [];
        const scoped = new Map(); // a vocabulary -> the lines of its scope
        for (const q of this.QueryParams) {
            if (!("manifest" in q))
                continue;
            const m = q.manifest;
            const vocabulary = Manifest.vocabularyOfTerm(m.key);
            if (vocabulary && !scoped.has(vocabulary))
                scoped.set(vocabulary, []);
            const lines = vocabulary ? scoped.get(vocabulary) : core;
            const indent = vocabulary ? "    " : "  ";
            const text = q.location.val();
            if (m.asYamlObject) {
                const obj = JSON.parse(text.trim() || "{}");
                if (Object.keys(obj).length)
                    lines.push(`${indent}${m.key}:\n` + Object.entries(obj).map(([k, v]) => `${indent}  ${JSON.stringify(k)}: ${JSON.stringify(v)}\n`).join(""));
                continue;
            }
            if ("labelKey" in m)
                lines.push(`${indent}${m.labelKey}: ${m.label}\n`);
            if (text === "")
                continue;
            if ("spillName" in m)
                lines.push(part(indent, m.key, m.spillName, text));
            else
                lines.push(`${indent}${m.key}: ${JSON.stringify(text)}\n`); // short scalar, quoted
        }
        let context = `"@context":\n` + Manifest.CoreContextURLs.map((url) => `  - ${url}\n`).join("");
        let scopes = "";
        for (const [v, lines] of scoped) {
            // the plugin implementing the vocabulary: registered under its
            // namespace, or loaded from the file the roll-up names (ShExReduce's
            // namespace has a # its plugin's id, the semantic-action IRI, lacks)
            const plugin = ShExPlugins.byId(v.namespace) || (v.plugin === undefined ? undefined
                : ShExPlugins.all().find((d) => typeof d.baseUrl === "string"
                    && d.baseUrl.replace(/[?#].*$/, "").endsWith("/" + v.plugin)));
            const pluginLine = plugin && plugin.baseUrl ? `    pluginURL: ${JSON.stringify(plugin.baseUrl)}\n` : "";
            if (lines.length === 0 && pluginLine === "")
                continue;
            context += `  - ${v.prefix}: ${v.namespace}\n`
                + `    ${v.prefix}:parms: {"@context": ${v.context}}\n`;
            scopes += `  ${v.prefix}:parms:\n` + pluginLine + lines.join("");
        }
        const entry = core.join("") + scopes + `  status: ${status}\n`;
        files[".manifest.yaml"] = { content: context + "entries:\n-" + entry.substring(1) };
        return files;
    },
    downloadResults(evt) {
        const typed = [
            { type: "text/plain", name: "results.txt" },
            { type: "application/json", name: "results.json" }
        ][$("#interface").val() === "appinfo" ? 1 : 0];
        const blob = new Blob([this.resultsWidget.text()], { type: typed.type });
        $("#download-results-button")
            .attr("href", window.URL.createObjectURL(blob))
            .attr("download", typed.name);
        this.toggleControls();
        console.log(this.resultsWidget.text());
    },
});