From 9f5ca27f556788d034527575f4e746caf7555329 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Tue, 20 Sep 2022 15:24:04 +0200 Subject: [PATCH] Add download button for pathbuilders --- internal/component/control/html/instance.html | 12 ++-- .../component/control/html/static/autolink.js | 39 ++++++------ .../component/control/html/static/instance.js | 60 +++++++++++++++++++ internal/component/instances/wisski_status.go | 6 +- 4 files changed, 87 insertions(+), 30 deletions(-) create mode 100644 internal/component/control/html/static/instance.js diff --git a/internal/component/control/html/instance.html b/internal/component/control/html/instance.html index e5cbae1..2c2017f 100644 --- a/internal/component/control/html/instance.html +++ b/internal/component/control/html/instance.html @@ -13,15 +13,16 @@ URL: {{ .Info.URL }}

Running: {{ .Info.Running }}
- Last Rebuild: {{ .Info.LastRebuild }}
+
- Created: {{ .Instance.Created }}
- OwnerEmail: {{ .Instance.OwnerEmail }}
+ Created: {{ .Instance.Created.Format "2006-01-02T15:04:05Z07:00" }}
+ Last Rebuild: {{ .Info.LastRebuild.Format "2006-01-02T15:04:05Z07:00" }}

FilesystemBase: {{ .Instance.FilesystemBase }}
AutoBlindUpdateEnabled: {{ .Instance.AutoBlindUpdateEnabled }}

- Pathbuilders: {{ .Info.Pathbuilders }}
+ Pathbuilders: {{ .Info.Pathbuilders }}
+
SqlDatabase: {{ .Instance.SqlDatabase }}
SqlUsername: {{ .Instance.SqlUsername }}
@@ -34,4 +35,5 @@ Generated at {{ .Time }} - \ No newline at end of file + + \ No newline at end of file diff --git a/internal/component/control/html/static/autolink.js b/internal/component/control/html/static/autolink.js index 970d570..0c0b396 100644 --- a/internal/component/control/html/static/autolink.js +++ b/internal/component/control/html/static/autolink.js @@ -1,24 +1,19 @@ -/** adding links to each item, see http://blog.parkermoore.de/2014/08/01/header-anchor-links-in-vanilla-javascript-for-github-pages-and-jekyll/ */ -var anchorForId = function (id) { - var anchor = document.createElement("a"); - anchor.className = "header-link"; - anchor.href = "#" + id; - anchor.innerHTML = "#"; - return anchor; -}; +/** Adapted from http://blog.parkermoore.de/2014/08/01/header-anchor-links-in-vanilla-javascript-for-github-pages-and-jekyll/ */ +const anchorForId = (id) => { + const anchor = document.createElement("a") + anchor.className = "header-link" + anchor.href = "#" + id + anchor.innerHTML = "#" + return anchor +} -var linkifyAnchors = function (level) { - var headers = document.getElementsByTagName("h" + level); - for (var h = 0; h < headers.length; h++) { - var header = headers[h]; +const linkifyAnchors = (level) => { + const headers = document.getElementsByTagName("h" + level); + Array.from(headers).forEach((header) => { + if (typeof header.id === "undefined" || header.id === "") return + header.appendChild(anchorForId(header.id)) + }) +} - if (typeof header.id !== "undefined" && header.id !== "") { - header.appendChild(anchorForId(header.id)); - } - } -}; - - -for (var level = 1; level <= 6; level++) { - linkifyAnchors(level); -} \ No newline at end of file +// linkify all the anchors from 1 ... 6 +Array(6).forEach((_, i) => linkifyAnchors(i + 1)) \ No newline at end of file diff --git a/internal/component/control/html/static/instance.js b/internal/component/control/html/static/instance.js new file mode 100644 index 0000000..b97e27c --- /dev/null +++ b/internal/component/control/html/static/instance.js @@ -0,0 +1,60 @@ +const types = { + "date": (element) => { + return (new Date(element.innerText)).toString() + }, + "pathbuilders": (element) => { + const pathbuilders = window.pathbuilders; // read from context! + const wrapper = document.createElement("span"); + + let found_one = false + Object.keys(pathbuilders).forEach(name => { + found_one = true + + const filename = name + ".xml" + const data = pathbuilders[name] + const mime = "application/xml" + wrapper.append(make_download_link(filename, name, data, mime)) + wrapper.append(document.createTextNode(" ")) + }) + + if (!found_one) return '(none)'; + + const small = document.createElement('small') + small.append(document.createTextNode("(click to download)")) + wrapper.append(small) + + return wrapper + } +} + +const make_download_link = (filename, title, content, type) => { + const blob = new Blob( + [content], + { + type: type ?? "text/plain" + } + ); + + const link = document.createElement("a") + link.target = "_blank" + link.download = filename + link.href = URL.createObjectURL(blob) + link.append(document.createTextNode(title)) + + return link +} + +Object.keys(types).forEach(key => { + const f = types[key]; + const elements = document.querySelectorAll("code." + key) + elements.forEach(element => { + const newElement = f(element) + if (typeof newElement === 'string') { + element.innerHTML = "" + element.appendChild(document.createTextNode(newElement)) + return + } + + element.parentNode.replaceChild(newElement, element) + }) +}) \ No newline at end of file diff --git a/internal/component/instances/wisski_status.go b/internal/component/instances/wisski_status.go index 00827d8..b8a83b3 100644 --- a/internal/component/instances/wisski_status.go +++ b/internal/component/instances/wisski_status.go @@ -15,8 +15,8 @@ type Info struct { LastRebuild time.Time - Running bool // is the instance running? - Pathbuilders []string // list of pathbuilders + Running bool // is the instance running? + Pathbuilders map[string]string // list of pathbuilders } // Info returns information about this WissKI instance. @@ -39,7 +39,7 @@ func (wisski *WissKI) Info(quick bool) (info Info, err error) { // these might execute php code or require additional database queries. if !quick { group.Go(func() error { - info.Pathbuilders, _ = wisski.Pathbuilders() + info.Pathbuilders, _ = wisski.AllPathbuilders() return nil }) group.Go(func() (err error) {