New team page refuses a title in place, with script
POST /pages answers JSON when asked: the page's address, or the refusal and the taken page's address. The popover holds 650ms, shows a refusal under the field with what was typed kept, and gives focus back to its button after Esc (WebKit leaves it on the body).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+140
−2
internal/mapa/handlers_pages.go+20 −2internal/mapa/static/mapa.js+93 −0internal/mapatest/share_json_test.go+27 −0
diff --git a/internal/mapa/handlers_pages.go b/internal/mapa/handlers_pages.go| index 596ffee..bbdd392 100644 |
| --- a/internal/mapa/handlers_pages.go |
| +++ b/internal/mapa/handlers_pages.go |
| @@ -168,17 +168,35 @@ func (app *app) createPage(writer http.ResponseWriter, |
| form := pageForm{Title: title, Body: body, Error: err.Error(), |
| Action: "/pages", Label: "Create page", |
| Taken: app.index.TitleHref(request.Context(), title)} |
| - app.showForm(writer, request, http.StatusUnprocessableEntity, |
| - "page_new", form) |
| + app.refuseCreate(writer, request, form) |
| return |
| } |
| if err != nil { |
| app.fail(writer, request, err) |
| return |
| } |
| + if wantsJSON(request) { |
| + answer(writer, map[string]any{"href": PagePath(page)}) |
| + return |
| + } |
| seeOther(writer, request, PagePath(page)) |
| } |
| +// refuseCreate asks for another title: the New page screen again, with |
| +// what was typed, or, for the sidebar's popover, the sentence as JSON, |
| +// shown where the title was typed (level-up §4.7). |
| +func (app *app) refuseCreate(writer http.ResponseWriter, |
| + request *http.Request, form pageForm) { |
| + if !wantsJSON(request) { |
| + app.showForm(writer, request, http.StatusUnprocessableEntity, |
| + "page_new", form) |
| + return |
| + } |
| + writer.Header().Set("Content-Type", "application/json; charset=utf-8") |
| + writer.WriteHeader(http.StatusUnprocessableEntity) |
| + answer(writer, map[string]any{"error": form.Error, "taken": form.Taken}) |
| +} |
| + |
| // notice is the sentence a redirect after a POST asked for. |
| func notice(request *http.Request) string { |
| query := request.URL.Query() |
diff --git a/internal/mapa/static/mapa.js b/internal/mapa/static/mapa.js| index 359e922..d3d7567 100644 |
| --- a/internal/mapa/static/mapa.js |
| +++ b/internal/mapa/static/mapa.js |
| @@ -715,6 +715,98 @@ |
| }); |
| } |
| + // ---- New team page --------------------------------------------------------- |
| + // The sidebar's popover (level-up §4.7) needs no script: it opens, closes |
| + // and takes focus by itself, and posts; a refusal is then the New page |
| + // screen. This keeps a refusal in the popover, beside what was typed, |
| + // holds the POST for HOLD_MS as busy.js holds a form (so busy.js leaves |
| + // this one alone), and gives focus back to the button after Esc: WebKit |
| + // does not focus a button on a click, so it would leave focus nowhere. |
| + var HOLD_MS = 650; |
| + |
| + function newTeamPage() { |
| + var pop = document.getElementById("mapa-new-page"); |
| + var form = pop && pop.querySelector("form[mapa-new-page-form]"); |
| + var opener = document.querySelector("[mapa-new-page-open]"); |
| + if (!form || !opener || typeof pop.showPopover !== "function") return; |
| + var field = form.querySelector('input[name="title"]'); |
| + var said = form.querySelector("[mapa-new-page-error]"); |
| + var button = form.querySelector('button[type="submit"]'); |
| + var escaped = false, sending = false; |
| + form.setAttribute("data-busy", "false"); |
| + pop.addEventListener("keydown", function (event) { |
| + if (event.key === "Escape") escaped = true; |
| + }); |
| + pop.addEventListener("toggle", function (event) { |
| + if (event.newState === "open") { escaped = false; return; } |
| + // WebKit still names the hidden field as focused here. |
| + var here = document.activeElement; |
| + if (escaped && (!here || here === document.body || pop.contains(here))) { |
| + opener.focus({ preventScroll: true }); |
| + } |
| + }); |
| + field.addEventListener("input", function () { say(""); }); |
| + |
| + // say shows a refusal under the field, or clears it. |
| + function say(text, taken) { |
| + said.textContent = text; |
| + said.hidden = !text; |
| + if (taken) { |
| + var link = document.createElement("a"); |
| + link.href = taken; |
| + link.textContent = "Open the page with that title"; |
| + said.append(" ", link); |
| + } |
| + if (text) { |
| + field.setAttribute("aria-invalid", "true"); |
| + field.setAttribute("aria-describedby", "mapa-new-page-error mapa-new-page-said"); |
| + } else { |
| + field.removeAttribute("aria-invalid"); |
| + field.setAttribute("aria-describedby", "mapa-new-page-said"); |
| + } |
| + } |
| + |
| + function busy(on) { |
| + sending = on; |
| + button.disabled = on; |
| + var spin = button.querySelector("[rst-spin]"); |
| + if (on && !spin) { |
| + spin = document.createElement("span"); |
| + spin.setAttribute("rst-spin", ""); |
| + spin.setAttribute("aria-hidden", "true"); |
| + button.insertBefore(spin, button.firstChild); |
| + } else if (!on && spin) { |
| + spin.remove(); |
| + } |
| + if (on) button.setAttribute("aria-busy", "true"); else button.removeAttribute("aria-busy"); |
| + } |
| + |
| + form.addEventListener("submit", function (event) { |
| + event.preventDefault(); |
| + if (sending) return; |
| + busy(true); |
| + var wait = new Promise(function (resolve) { setTimeout(resolve, HOLD_MS); }); |
| + var sent = post(form.action, { title: field.value }).then(function (response) { |
| + return response.json().then(function (reply) { return { ok: response.ok, reply: reply }; }); |
| + }); |
| + Promise.all([sent, wait]).then(function (both) { |
| + var answer = both[0]; |
| + if (answer.ok && answer.reply.href) { |
| + window.location.assign(answer.reply.href); |
| + return; |
| + } |
| + if (!answer.reply.error) throw new Error("no answer"); |
| + busy(false); |
| + say(answer.reply.error, answer.reply.taken); |
| + field.focus(); |
| + }).catch(function () { |
| + busy(false); |
| + say("The page could not be created just now. Try again."); |
| + field.focus(); |
| + }); |
| + }); |
| + } |
| + |
| // ---- Start ---------------------------------------------------------------- |
| function start() { |
| @@ -743,6 +835,7 @@ |
| fold(); |
| } |
| }); |
| + newTeamPage(); |
| offerZone(); |
| } |
diff --git a/internal/mapatest/share_json_test.go b/internal/mapatest/share_json_test.go| index 17f5b23..3c447ac 100644 |
| --- a/internal/mapatest/share_json_test.go |
| +++ b/internal/mapatest/share_json_test.go |
| @@ -39,3 +39,30 @@ func TestMakeTeamPageAnswersTheEditor(t *testing.T) { |
| t.Fatal("the new team page does not hold the blocks") |
| } |
| } |
| + |
| +// New team page's popover (level-up §4.7, build note 77): with Accept |
| +// JSON, POST /pages answers the new page's address, and a refused title |
| +// is the sentence, as JSON, with nothing written. |
| +func TestNewTeamPageAnswersThePopover(t *testing.T) { |
| + rig := newRig(t) |
| + taken := rig.createPage(t, Bo, "Taken", "") |
| + pages := rig.count(t, "pages") |
| + for _, title := range []string{"Taken", " "} { |
| + code, refused := rig.postJSON(Ana, "/pages", |
| + url.Values{"title": {title}}) |
| + if code != http.StatusUnprocessableEntity || refused["error"] == "" || |
| + rig.count(t, "pages") != pages { |
| + t.Fatalf("%q: %d %v", title, code, refused) |
| + } |
| + if title == "Taken" && refused["taken"] != taken { |
| + t.Fatalf("a taken title points to %v, not %s", refused["taken"], |
| + taken) |
| + } |
| + } |
| + code, reply := rig.postJSON(Ana, "/pages", url.Values{"title": {"Fresh"}}) |
| + href, _ := reply["href"].(string) |
| + if code != http.StatusOK || !strings.HasPrefix(href, "/p/") || |
| + !strings.Contains(rig.page(t, Bo, href), "Fresh") { |
| + t.Fatalf("POST /pages answered %d %v", code, reply) |
| + } |
| +} |