Explorer

html-ui.batch-script

As of snapshot FWBeS680eO. See the latest

description
The small JavaScript of html-ui.batch-page: adds and removes the blocks of parts, reads a part with definy_get_part (POST /api/definy_get_part), and commits all the blocks in one commit (POST /api/commit). ?part= (repeatable) opens those parts
type
String
expression
"const form = document.getElementById(\"batch-form\");\nconst list = document.getElementById(\"batch-parts\");\nconst template = document.getElementById(\"batch-part\");\nconst out = document.getElementById(\"out\");\nconst show = (text, error) => {\n  out.textContent = text;\n  out.className = error ? \"error\" : \"\";\n};\nconst section = (text, start, end) => {\n  const i = text.indexOf(start);\n  if (i < 0) return \"\";\n  const from = i + start.length;\n  const j = end === \"\" ? -1 : text.indexOf(end, from);\n  return text.slice(from, j < 0 ? text.length : j);\n};\nconst post = async (tool, args) => {\n  const response = await fetch(\"/api/\" + tool, {\n    method: \"POST\",\n    headers: { \"content-type\": \"application/json\" },\n    body: JSON.stringify(args),\n  });\n  const text = await response.text();\n  if (!response.ok) {\n    let message = text;\n    try {\n      message = JSON.parse(text).message ?? text;\n    } catch {}\n    throw new Error(message);\n  }\n  return text;\n};\nconst load = async (block) => {\n  const part = block.querySelector(\"[name=part]\").value.trim();\n  if (part === \"\") return;\n  const status = block.querySelector(\".status\");\n  status.textContent = document.documentElement.dataset.busy;\n  try {\n    const text = await post(\"definy_get_part\", { project: form.dataset.project, branch: form.dataset.branch, part });\n    block.querySelector(\"[name=type]\").value = section(text, \"\\ntype:\\n\", \"\\n\\nexpression:\\n\");\n    block.querySelector(\"[name=expression]\").value = section(text, \"\\n\\nexpression:\\n\", \"\");\n    block.dataset.loaded = \"1\";\n    status.textContent = \"\";\n  } catch (err) {\n    status.textContent = String(err.message ?? err);\n  }\n};\nconst add = (part) => {\n  const block = template.content.firstElementChild.cloneNode(true);\n  block.querySelector(\"[name=part]\").value = part ?? \"\";\n  block.querySelector(\".load\").addEventListener(\"click\", () => load(block));\n  block.querySelector(\".remove\").addEventListener(\"click\", () => block.remove());\n  list.append(block);\n  if (part) load(block);\n  return block;\n};\ndocument.getElementById(\"batch-add\").addEventListener(\"click\", () => add().querySelector(\"[name=part]\").focus());\nconst initial = new URLSearchParams(location.search).getAll(\"part\").filter((p) => p !== \"\");\nif (initial.length === 0) add();\nfor (const part of initial) add(part);\nform.addEventListener(\"submit\", async (e) => {\n  e.preventDefault();\n  const button = form.querySelector(\"button[type=submit]\");\n  const changes = [];\n  for (const block of list.children) {\n    const part = block.querySelector(\"[name=part]\").value.trim();\n    if (part === \"\") continue;\n    if (block.querySelector(\"[name=delete]\").checked) {\n      changes.push({ part, delete: true });\n      continue;\n    }\n    const change = { part };\n    for (const key of [\"type\", \"expression\"]) {\n      const value = block.querySelector(\"[name=\" + key + \"]\").value;\n      if (value.trim() !== \"\" || block.dataset.loaded) change[key] = value;\n    }\n    const description = block.querySelector(\"[name=description]\").value;\n    if (description.trim() !== \"\") change.description = description;\n    changes.push(change);\n  }\n  if (changes.length === 0) return;\n  button.disabled = true;\n  show(document.documentElement.dataset.busy);\n  try {\n    const text = await post(\"commit\", {\n      project: form.dataset.project,\n      branch: form.dataset.branch,\n      message: form.message.value || \"Change \" + changes.map((c) => c.part).join(\", \"),\n      changes,\n    });\n    show(text);\n    location.href = \"/h/\" + encodeURIComponent(form.dataset.project) + \"/\" + encodeURIComponent(form.dataset.branch);\n  } catch (err) {\n    show(String(err.message ?? err), true);\n  } finally {\n    button.disabled = false;\n  }\n});\n"
visibility
public
html-ui.batch-script (project html-ui, branch edit-check, snapshot FWBeS680eOlm6c2NOnWcarkR0iM2Jt6ap5-Rv0zDiXk)
id: ILtZ_RldARsRqOvKD2GBIEYdkPezX0URQdorHQWZwoA
content hash: gmqtfFabFS6ZpPd5fNpjWN6FO50lhNVtygRcOaB4nXc