/** * Which data source answers the data pane, and the documents and * parameters each source takes: the neighborhood selector, its panes and * fields, slurping. * * This is doc/ShExNeighborhoodConfig.js's source (tsconfig.app.json compiles src/app/ into * doc/); edit here and run `npm run build`. */ /** The data source picker and the configuration it draws. * * Where the data comes from is the user's choice, made from a list of the * neighborhood modules loaded into this app -- so this class knows about * data sources in general and about none of them in particular. What each * one needs it reads from that module's declarations (dbParams): values to * type become fields, documents to edit become panes shown one at a time. * A query service is then all fields and no panes; a local store is one * mandatory Turtle document; a Wikibase is a growable set of entity pages, * each one an edit to try before making it. * * One textarea holds whichever pane is showing -- the same textarea the app * has always had, so drag-and-drop, dirty tracking, permalinks and the * editors keep working -- and the panes not showing live here as text. */ class NeighborhoodConfig { /** * @param modules the loaded neighborhood modules, in picklist order * @param textarea the data pane's textarea (jQuery selection) * @param onChange called when the user changes anything the db is built from * @param onShown called when the document area becomes visible again */ constructor(modules, textarea, onChange, onShown) { this.modules = modules; this.textarea = textarea; this.onChange = onChange; /** called when the document area becomes visible again */ this.onShown = onShown; this.moduleId = ShExWebApp.NeighborhoodApi.moduleId(modules[0]); /** A parameter's name means the same thing wherever it is declared -- * that is what lets a manifest entry or a permalink say `endpoint=` * without naming a module twice -- but its *value* belongs to the * source that asked for it: `data` is a graph to one source and an * entity page to another, and neither wants the other's document. */ this.fieldsByModule = {}; this.panesByModule = {}; this.showing = 0; this.onSettings = false; } /** the selected source's fields: parameter name -> value */ get fields() { return this.fieldsByModule[this.moduleId] || (this.fieldsByModule[this.moduleId] = {}); } /** the selected source's documents: parameter name -> [text, ...] */ get panes() { return this.panesFor(this.moduleId); } panesFor(moduleId) { return this.panesByModule[moduleId] || (this.panesByModule[moduleId] = {}); } get module() { const { moduleId } = ShExWebApp.NeighborhoodApi; return this.modules.find(m => moduleId(m) === this.moduleId) || this.modules[0]; } get paneParams() { return ShExWebApp.NeighborhoodApi.paneParams(this.module.dbParams || []); } get fieldParams() { return ShExWebApp.NeighborhoodApi.fieldParams(this.module.dbParams || []); } /** Every document the selected source takes, in declaration order: a * flat list, because that is what the tabs are. A parameter that must * have a document always shows one, even before anything is in it. */ documents() { const out = []; for (const param of this.paneParams) { const texts = this.panes[param.name] || (param.pane.min ? [""] : []); texts.forEach((text, index) => out.push({ param, index, text })); } return out; } /** The document a tab is showing, with the textarea's text in it. * * ...while a document is showing: with the settings tab up the textarea * is holding whatever was there last, which belongs to no document. * Reading it as one wrote it over the page a slurp had just put there. */ docAt(n) { const docs = this.documents(); if (n < 0 || n >= docs.length) return null; const doc = docs[n]; return Object.assign({}, doc, n === this.showing && !this.onSettings ? { text: this.textarea.val() } : {}); } /** the parameter whose document is showing (for its language) */ get paneParam() { const doc = this.docAt(this.showing); return doc ? doc.param : (this.paneParams[0] || null); } /** texts of one parameter's documents, the showing one included */ texts(paramName) { const name = paramName || (this.paneParam || {}).name; if (!name) return []; const texts = (this.panes[name] || []).slice(); const doc = this.onSettings ? null : this.docAt(this.showing); if (doc && doc.param.name === name) { while (texts.length <= doc.index) texts.push(""); texts[doc.index] = this.textarea.val(); } return texts; } /** what to hand the module's fromParams: a pane nobody has written in is * not a document, however much room it is taking up */ params() { const params = Object.assign({}, this.fields); for (const param of this.paneParams) params[param.name] = this.texts(param.name).filter(text => text.trim() !== ""); return params; } /** the text a permalink or manifest means by "data": the first document * of the first parameter, whichever pane happens to be showing */ primaryText() { const texts = this.texts((this.paneParams[0] || {}).name); return texts.length > 0 ? texts[0] : ""; } /** Put documents where the source says they go. A source that can sort * its own documents out (a Wikibase, told an entity page, knows it is a * page and which ids it is about) does; otherwise they are documents of * the first parameter, which is what a data pane has always meant. */ setDocuments(texts) { const first = this.paneParams[0]; if (!first) return; const distribute = this.module.distributeDocuments; const bySpec = distribute ? distribute(texts.filter(text => text.trim() !== "")) : { [first.name]: texts }; for (const param of this.paneParams) this.panes[param.name] = (bySpec[param.name] || []).slice(); this.showing = 0; this.textarea.val((this.documents()[0] || {}).text || ""); this.render(); } /** Throw away every source's documents, back to the empty panes a source * insists on (`pane.min`). Every source's, not just the selected one: * this is what "there is no data now" means, and a stash left under a * source nobody is looking at reappears the moment they choose it. */ forgetDocuments() { this.panesByModule = {}; this.showing = 0; this.textarea.val(""); this.render(); } select(moduleId) { if (this.moduleId === moduleId) return; this.stash(); this.moduleId = moduleId; this.showing = 0; this.onSettings = false; // show the document; settings are a tab away $("#neighborhood").val(moduleId); this.render(); this.textarea.val((this.panes[(this.paneParam || {}).name] || [""])[0] || ""); this.onChange({ language: true }); // a different source, a different language } /** keep the showing document's text before something replaces it */ stash() { const doc = this.docAt(this.showing); if (doc) this.panes[doc.param.name] = this.texts(doc.param.name); } show(n) { if (this.showingPane) return; // render() moved the tab set; not a new choice // one source's panes need not share a language -- wikidata's list of // entity ids and the entity pages themselves don't -- so moving between // them is a language change as much as changing source is const was = this.paneParam; this.showingPane = true; try { this.stash(); // read the document before moving to it: docAt reports the showing // one through the textarea, which is about to be overwritten const text = (this.documents()[n] || {}).text || ""; this.showing = n; this.textarea.val(text); this.render(); } finally { this.showingPane = false; } this.onChange(was === this.paneParam ? undefined : { language: true }); } /** the parameter a new document would be added to */ get creatableParam() { return this.paneParams.find(p => p.pane.creatable) || null; } addPane(text) { const spec = this.creatableParam; if (!spec) return; this.stash(); const texts = this.panes[spec.name] || []; texts.push(text === undefined ? (spec.pane.template || "") : text); this.panes[spec.name] = texts; const at = this.documents().findIndex(d => d.param.name === spec.name && d.index === texts.length - 1); // The new document may land where `showing` already points -- at the // first document a source has, when the settings tab was all there was // to show. The textarea is not holding that document, so say so: // otherwise show()'s stash writes the empty textarea over the template. if (at === this.showing) this.showing = -1; // ...and a source whose settings were all it had to show has a document // now, which is what the reader asked for by opening one this.onSettings = false; this.show(at); } removePane(n) { const doc = this.docAt(n); if (!doc) return; this.stash(); const texts = this.panes[doc.param.name] || []; if (texts.length <= (doc.param.pane.min || 0)) return; texts.splice(doc.index, 1); this.panes[doc.param.name] = texts; this.show(Math.min(this.showing, Math.max(0, this.documents().length - 1))); } /** Draw what the selected source asks for: its settings in the leftmost * pane, and one pane per document it takes to the right of them -- the * same tab set the shape map uses, so the data pane reads the way the * rest of the app does. * * The documents' panes are placeholders: one editing area is moved into * whichever is showing, so everything that has ever talked to "the data * textarea" -- drag and drop, dirty tracking, permalinks, the editors -- * goes on talking to one element. */ render() { const { moduleId } = ShExWebApp.NeighborhoodApi; const spec = this.paneParam; const container = $("#dataSource-tabs"); const initialized = container.hasClass("ui-tabs"); const fields = $("#neighborhoodFields").empty(); const shown = this.fieldParams.filter(p => !(p.ui && p.ui.hidden)); for (const param of shown.concat(this.hostParams())) fields.append(this.fieldFor(param)); if (fields.children().length === 0) fields.append($("", { class: "noSettings" }) .text("nothing to configure for " + (this.module.label || this.module.name)) .attr("title", this.module.description || "")); // one tab and one (empty) panel per document, in the order the source // declared its parameters const tabs = $("#dataPaneTabs"); tabs.children().not(":first").remove(); container.children("div.dataPanePanel").remove(); const docs = this.documents(); docs.forEach(({ param, index, text }, n) => { const id = "dataPanePanel-" + n; // the textarea holds the showing document -- while one is showing: // with the settings up it holds whatever was there last, which is not // what to name a tab after const showingText = n === this.showing && !this.onSettings ? this.textarea.val() : text; const title = (param.pane.titleOf && param.pane.titleOf(showingText)) || (param.pane.max === 1 ? param.pane.label : param.pane.label + " " + (index + 1)); tabs.append($("
  • ").append($("", { href: "#" + id, title: param.pane.label }).text(title))); container.append($("
    ", { id, class: "dataPanePanel" })); }); if (this.showing >= docs.length) this.showing = Math.max(0, docs.length - 1); const removable = this.docAt(this.showing); $("#dataPaneControls").toggle(docs.length > 0 || !!this.creatableParam); $("#addDataPane").toggle(!!this.creatableParam); $("#removeDataPane").toggle(!!removable && (this.panes[removable.param.name] || []).length > (removable.param.pane.min || 0)); // a source with no document has only its settings to show if (docs.length === 0) this.onSettings = true; if (initialized) { // refreshing re-activates a tab, which would answer the question this // is about to ask; say what should be active and ignore the widget's // own opinion while it settles const active = this.onSettings ? 0 : this.showing + 1; // +1: settings is leftmost this.rendering = true; try { container.tabs("refresh"); container.tabs("option", "active", active); } finally { this.rendering = false; } } this.showDocumentArea(); $("#neighborhood").val(moduleId(this.module)); } /** one labelled input for a parameter, remembering what's typed into it */ fieldFor(param) { const id = "nbhd-" + param.name; const value = this.fields[param.name]; const input = param.schema.type === "boolean" ? $("", { type: "checkbox", id }) .prop("checked", value === undefined ? !!param.schema.default : !!value) : $("", { type: param.schema.type === "integer" || param.schema.type === "number" ? "number" : "text", id, placeholder: param.schema.default === undefined ? "" : String(param.schema.default), value: value === undefined ? "" : value }); input.attr("title", param.description || ""); input.on("change keyup", () => { this.fields[param.name] = param.schema.type === "boolean" ? input.prop("checked") : input.val(); this.onChange(); }); return $("