Spaces:
Running
Running
Download app.js from CGIAR/ai-use-declaration: direct link, hf CLI and curl.
- Browser
- Download file 11.8 kB
-
https://huggingface.co/spaces/CGIAR/ai-use-declaration/resolve/main/app.js
- Command line
-
hf download hf://spaces/CGIAR/ai-use-declaration/app.js
-
curl -L -o app.js https://huggingface.co/spaces/CGIAR/ai-use-declaration/resolve/main/app.js
11.8 kB
| // UI wiring. Sentence logic is in compose.js; wording in data/templates.js; taxonomy in data/gaidet.js. | |
| (function () { | |
| "use strict"; | |
| var D = window.GAIDET, T = window.TEMPLATES, C = window.Compose; | |
| var $ = function (id) { return document.getElementById(id); }; | |
| var state = { | |
| used: true, style: "document", tasks: [], tools: [], | |
| partyId: "authors", party: "the authors", plural: true, | |
| workId: "document", work: "document", | |
| review: true, cite: false, rollup: false | |
| }; | |
| var toastEl; | |
| // ---------- build static parts ---------- | |
| function buildTasks() { | |
| var host = $("task-groups"); | |
| D.groups.forEach(function (g, i) { | |
| var det = document.createElement("details"); | |
| det.className = "group"; det.open = i === 0; det.dataset.key = g.key; | |
| var sum = document.createElement("summary"); | |
| sum.innerHTML = '<span class="title"></span><span class="count" data-count="' + g.key + '"></span>'; | |
| sum.querySelector(".title").textContent = g.label; | |
| det.appendChild(sum); | |
| var box = document.createElement("div"); box.className = "terms"; | |
| g.terms.forEach(function (t) { | |
| var lab = document.createElement("label"); | |
| var cb = document.createElement("input"); cb.type = "checkbox"; cb.value = t.id; cb.dataset.group = g.key; | |
| var span = document.createElement("span"); span.className = "term-text"; | |
| span.appendChild(document.createTextNode(t.label)); | |
| if (t.note) { var note = document.createElement("small"); note.className = "note"; note.textContent = t.note; span.appendChild(note); } | |
| var idEl = document.createElement("span"); idEl.className = "def"; idEl.textContent = "GAIDeT " + t.id; | |
| idEl.title = "Permanent identifier: " + t.uri; | |
| lab.title = "GAIDeT identifier " + t.id + (t.note ? ". " + t.note : ""); | |
| lab.appendChild(cb); lab.appendChild(span); lab.appendChild(idEl); | |
| box.appendChild(lab); | |
| }); | |
| det.appendChild(box); | |
| host.appendChild(det); | |
| }); | |
| host.addEventListener("change", function (e) { | |
| if (e.target.type !== "checkbox") return; | |
| state.tasks = Array.prototype.map.call(host.querySelectorAll("input:checked"), function (cb) { return cb.value; }); | |
| render(); | |
| }); | |
| } | |
| function buildSelects() { | |
| var party = $("party"); | |
| T.parties.forEach(function (p) { var o = document.createElement("option"); o.value = p.id; o.textContent = p.text; party.appendChild(o); }); | |
| var work = $("work"); | |
| T.works.forEach(function (w) { var o = document.createElement("option"); o.value = w.id; o.textContent = w.id; work.appendChild(o); }); | |
| var other = document.createElement("option"); other.value = "custom"; other.textContent = "Other (type below)"; work.appendChild(other); | |
| var dl = $("tool-suggestions"); | |
| T.toolSuggestions.forEach(function (s) { var o = document.createElement("option"); o.value = s; dl.appendChild(o); }); | |
| } | |
| function buildTabs() { | |
| var tabs = $("style-tabs"); | |
| Object.keys(T.styles).forEach(function (k) { | |
| var b = document.createElement("button"); b.type = "button"; b.role = "tab"; b.dataset.style = k; b.textContent = T.styles[k].name; | |
| b.addEventListener("click", function () { state.style = k; render(); }); | |
| tabs.appendChild(b); | |
| }); | |
| } | |
| // ---------- state -> DOM ---------- | |
| function syncForm() { | |
| document.querySelector('input[name="used"][value="' + (state.used ? "1" : "0") + '"]').checked = true; | |
| $("tasks-card").classList.toggle("hidden", !state.used); | |
| $("tools-card").classList.toggle("hidden", !state.used); | |
| $("opt-review").disabled = !state.used; $("opt-cite").disabled = !state.used; $("opt-rollup").disabled = !state.used; | |
| var sel = {}; state.tasks.forEach(function (id) { sel[id] = true; }); | |
| var counts = {}; | |
| Array.prototype.forEach.call($("task-groups").querySelectorAll('input[type="checkbox"]'), function (cb) { | |
| cb.checked = !!sel[cb.value]; | |
| if (cb.checked) counts[cb.dataset.group] = (counts[cb.dataset.group] || 0) + 1; | |
| }); | |
| Array.prototype.forEach.call(document.querySelectorAll("[data-count]"), function (el) { | |
| var n = counts[el.dataset.count] || 0; el.textContent = n ? String(n) : ""; | |
| }); | |
| var chips = $("tool-chips"); chips.innerHTML = ""; | |
| state.tools.forEach(function (t, i) { | |
| var li = document.createElement("li"); li.appendChild(document.createTextNode(t)); | |
| var x = document.createElement("button"); x.type = "button"; x.textContent = "×"; x.setAttribute("aria-label", "Remove " + t); | |
| x.addEventListener("click", function () { state.tools.splice(i, 1); render(); }); | |
| li.appendChild(x); chips.appendChild(li); | |
| }); | |
| $("party").value = state.partyId; | |
| $("party-custom").classList.toggle("hidden", state.partyId !== "custom"); | |
| if (state.partyId === "custom") { $("party-text").value = state.party; $("party-plural").checked = state.plural; } | |
| $("work").value = state.workId; | |
| $("work-custom").classList.toggle("hidden", state.workId !== "custom"); | |
| if (state.workId === "custom") $("work-text").value = state.work; | |
| $("opt-review").checked = state.review; $("opt-cite").checked = state.cite; $("opt-rollup").checked = state.rollup; | |
| Array.prototype.forEach.call($("style-tabs").children, function (b) { | |
| b.setAttribute("aria-selected", b.dataset.style === state.style ? "true" : "false"); | |
| }); | |
| $("style-hint").textContent = T.styles[state.style].hint; | |
| } | |
| function render() { | |
| syncForm(); | |
| var r = C.compose(state, D, T); | |
| var out = $("output"); | |
| out.textContent = r.text; | |
| out.classList.toggle("invalid", !r.ok); | |
| $("char-count").textContent = r.ok ? r.text.length + " characters" : ""; | |
| $("copy-text").disabled = !r.ok; | |
| var notesEl = $("notes"); notesEl.innerHTML = ""; | |
| C.notes(state, D, T).forEach(function (n) { var li = document.createElement("li"); li.textContent = n; notesEl.appendChild(li); }); | |
| notesEl.classList.toggle("hidden", notesEl.children.length === 0); | |
| var q = C.toParams(state).toString(); | |
| history.replaceState(null, "", location.pathname + (q ? "?" + q : "") + location.hash); | |
| } | |
| // ---------- DOM -> state ---------- | |
| function applyParty() { | |
| state.partyId = $("party").value; | |
| if (state.partyId === "custom") { | |
| state.party = $("party-text").value.trim() || "the authors"; | |
| state.plural = $("party-plural").checked; | |
| } else { | |
| var p = T.parties.filter(function (x) { return x.id === state.partyId; })[0]; | |
| state.party = p.text; state.plural = p.plural; | |
| } | |
| } | |
| function applyWork() { | |
| state.workId = $("work").value; | |
| state.work = state.workId === "custom" ? ($("work-text").value.trim() || "document") : state.workId; | |
| } | |
| function addTool() { | |
| var inp = $("tool-input"); | |
| inp.value.split(/[;,]/).map(function (s) { return s.trim(); }).filter(Boolean).forEach(function (t) { | |
| if (state.tools.indexOf(t) === -1) state.tools.push(t); | |
| }); | |
| inp.value = ""; render(); inp.focus(); | |
| } | |
| function wire() { | |
| Array.prototype.forEach.call(document.querySelectorAll('input[name="used"]'), function (r) { | |
| r.addEventListener("change", function () { state.used = this.value === "1"; render(); }); | |
| }); | |
| $("clear-tasks").addEventListener("click", function () { state.tasks = []; render(); }); | |
| $("tool-add").addEventListener("click", addTool); | |
| $("tool-input").addEventListener("keydown", function (e) { if (e.key === "Enter") { e.preventDefault(); addTool(); } }); | |
| $("tool-input").addEventListener("change", function () { if (this.value.trim() && T.toolSuggestions.indexOf(this.value.trim()) !== -1) addTool(); }); | |
| $("party").addEventListener("change", function () { applyParty(); render(); if (state.partyId === "custom") $("party-text").focus(); }); | |
| $("party-text").addEventListener("input", function () { applyParty(); render(); }); | |
| $("party-plural").addEventListener("change", function () { applyParty(); render(); }); | |
| $("work").addEventListener("change", function () { applyWork(); render(); if (state.workId === "custom") $("work-text").focus(); }); | |
| $("work-text").addEventListener("input", function () { applyWork(); render(); }); | |
| $("opt-review").addEventListener("change", function () { state.review = this.checked; render(); }); | |
| $("opt-cite").addEventListener("change", function () { state.cite = this.checked; render(); }); | |
| $("opt-rollup").addEventListener("change", function () { state.rollup = this.checked; render(); }); | |
| $("copy-text").addEventListener("click", function () { copy($("output").textContent, "Declaration copied"); }); | |
| $("copy-link").addEventListener("click", function () { copy(location.href, "Link copied"); }); | |
| $("reset").addEventListener("click", function () { | |
| state.used = true; state.tasks = []; state.tools = []; state.partyId = "authors"; state.partyId = "authors"; | |
| applyParty(); state.workId = "document"; state.work = "document"; state.review = true; state.cite = false; state.rollup = false; | |
| render(); | |
| }); | |
| } | |
| function copy(text, msg) { | |
| function done() { toast(msg); } | |
| function fallback() { | |
| var ta = document.createElement("textarea"); ta.value = text; ta.style.position = "fixed"; ta.style.opacity = "0"; | |
| document.body.appendChild(ta); ta.select(); | |
| try { document.execCommand("copy"); done(); } catch (e) { toast("Copy failed; select the text and copy manually"); } | |
| document.body.removeChild(ta); | |
| } | |
| if (navigator.clipboard && navigator.clipboard.writeText) navigator.clipboard.writeText(text).then(done, fallback); | |
| else fallback(); | |
| } | |
| function toast(msg) { | |
| if (!toastEl) { toastEl = document.createElement("div"); toastEl.className = "toast"; toastEl.setAttribute("role", "status"); document.body.appendChild(toastEl); } | |
| toastEl.textContent = msg; toastEl.classList.add("show"); | |
| clearTimeout(toastEl._t); toastEl._t = setTimeout(function () { toastEl.classList.remove("show"); }, 1800); | |
| } | |
| // ---------- load from URL ---------- | |
| function loadFromUrl() { | |
| var s = C.fromParams(new URLSearchParams(location.search)); | |
| var validIds = {}; D.groups.forEach(function (g) { g.terms.forEach(function (t) { validIds[t.id] = true; }); }); | |
| if (s.used === false) state.used = false; | |
| if (s.style && T.styles[s.style]) state.style = s.style; | |
| if (s.tasks) state.tasks = s.tasks.filter(function (id) { return validIds[id]; }); | |
| if (s.tools) state.tools = s.tools.slice(0, 10).map(function (t) { return t.slice(0, 80); }); | |
| if (s.partyId && T.parties.some(function (p) { return p.id === s.partyId; })) { | |
| state.partyId = s.partyId; | |
| if (s.partyId === "custom") { state.party = (s.party || "").slice(0, 80) || "the authors"; state.plural = s.plural !== false; } | |
| else { var p = T.parties.filter(function (x) { return x.id === s.partyId; })[0]; state.party = p.text; state.plural = p.plural; } | |
| } | |
| if (s.workId && (s.workId === "custom" || T.works.some(function (w) { return w.id === s.workId; }))) { | |
| state.workId = s.workId; | |
| state.work = s.workId === "custom" ? ((s.work || "").slice(0, 80) || "document") : s.workId; | |
| } | |
| if (typeof s.review === "boolean") state.review = s.review; | |
| if (typeof s.cite === "boolean") state.cite = s.cite; | |
| if (typeof s.rollup === "boolean") state.rollup = s.rollup; | |
| } | |
| buildTasks(); buildSelects(); buildTabs(); wire(); loadFromUrl(); render(); | |
| $("tax-version").textContent = "generated " + D.generated; | |
| $("tpl-version").textContent = T.version; | |
| // Open any group that has a selection when arriving via link. | |
| Array.prototype.forEach.call(document.querySelectorAll("details.group"), function (d) { | |
| if (d.querySelector("input:checked")) d.open = true; | |
| }); | |
| })(); | |