/** * The app as a host for plugins (doc/plugins.md): applying and unloading a * descriptor, the screens, panes, controls and keys it declares, the * results tabs, the links between panes, and the hooks a validation runs. * Mixed onto ShExBaseApp; see ShExBaseApp.js. * * This is doc/ShExBaseApp-plugins.js's source (tsconfig.app.json compiles src/app/ into * doc/); edit here and run `npm run build`. */ mixin(ShExBaseApp, { /** * Put on the page what one plugin adds to it: styles, panes, controls, * verbs, results (§5). * * Idempotent, and callable at any time -- at construction for what the * page loaded, and again for a plugin that arrives later, which is what * makes loading one by URL possible. Styles go after the page's own * rules, so a plugin may say differently what the page said. * * @returns {Promise|undefined} a promise only when something had to be * fetched first. Deliberately not always a promise: a page script * registers while this app is being constructed and start() follows the * constructor immediately, so a descriptor with nothing to fetch must be * on the page before either returns. */ applyPlugin(ext) { // What it runs on, if the page hasn't got it: a module of its own, // fetched before anything of it is built. const pending = this.loadPluginScripts(ext); return pending ? pending.then(() => this.applyPluginNow(ext)) : this.applyPluginNow(ext); }, /** * The scripts a plugin says it needs, injected in the order it said * them, or null if the page already has them all (§5 phase 3). * * A classic page has no module system, so this is what "load a plugin's * code by URL" means here: `scripts` are resolved against the plugin * rather than against the page, since the plugin knows where its own * bundle sits and the page has never heard of it. */ loadPluginScripts(ext) { const urls = (ext.scripts || []) .map((src) => new URL(src, ext.baseUrl || DefaultBase).href) .filter((url) => $("head script, body script").filter((i, s) => s.src === url).length === 0); if (urls.length === 0) return null; return urls.reduce((sofar, url) => sofar.then(() => new Promise((resolve, reject) => { const script = document.createElement("script"); script.src = url; script.onload = () => resolve(); script.onerror = () => reject(Error("no plugin script at <" + url + ">")); document.head.appendChild(script); })), Promise.resolve()); }, applyPluginNow(ext) { if (typeof ext.css === "string" && ext.css.trim().length > 0 && $("head style[data-plugin]").filter((i, e) => $(e).attr("data-plugin") === ext.id).length === 0) $("