From b8059ce62a1331c4b6b007f399420b97a13b9187 Mon Sep 17 00:00:00 2001 From: nate smith Date: Tue, 13 Feb 2024 21:53:34 -0800 Subject: [PATCH] dynamically disabled up/down buttons --- assets/main.js | 49 ++++++++++++++++++++++++++++++++++++--------- cmd/phraser/main.go | 2 ++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/assets/main.js b/assets/main.js index 700f014..37a3abf 100644 --- a/assets/main.js +++ b/assets/main.js @@ -1,6 +1,7 @@ // nostalgia for a simpler, more complicated time const $ = document.querySelector.bind(document); const $$ = document.querySelectorAll.bind(document); +const initialLines = 10; class Button extends HTMLButtonElement { constructor() { @@ -25,24 +26,54 @@ class LineUpper extends Button { click() { const l = this.closest("div.linecontainer").parentElement; const s = l.previousElementSibling; - if (s == null) { - return; - } s.before(l); + this.checkDisabled(); + s.querySelector("button[is=line-upper]").checkDisabled() + s.querySelector("button[is=line-downer]").checkDisabled() + } + + checkDisabled() { + const l = this.closest("div.linecontainer").parentElement; + if (l.previousElementSibling == null) { + this.setAttribute("disabled", "yeah"); + } else { + this.removeAttribute("disabled"); + } + } + + connectedCallback() { + this.checkDisabled(); } - // TODO connectedCallback to disable this if first in list - // TODO change callback (i forget what it's called but i think i saw it) to enable if not first in list } class LineDowner extends Button { click() { const l = this.closest("div.linecontainer").parentElement; const s = l.nextElementSibling; - if (s == null) return; s.after(l); + this.checkDisabled() + s.querySelector("button[is=line-downer]").checkDisabled() + s.querySelector("button[is=line-upper]").checkDisabled() + } + + checkDisabled() { + const l = this.closest("div.linecontainer").parentElement; + if (l.nextElementSibling == null) { + this.setAttribute("disabled", "yeah"); + } else { + this.removeAttribute("disabled"); + } + } + + connectedCallback() { + const count = $$("button[is=line-downer]").length; + console.log(count); + console.log(initialLines); + if (count != initialLines) { + return; + } + this.checkDisabled(); } - // TODO connectedCallback to disable this if last in list - // TODO change callback (i forget what it's called but i think i saw it) to enable if not last in list } class LineAdder extends Button { @@ -97,7 +128,7 @@ class Lines extends HTMLDivElement { if (this.connected) { return; } - for (var i = 0; i < 10; i++) { + for (var i = 0; i < initialLines; i++) { this.add(); } this.connected = true diff --git a/cmd/phraser/main.go b/cmd/phraser/main.go index 8f08824..97fccdf 100644 --- a/cmd/phraser/main.go +++ b/cmd/phraser/main.go @@ -35,6 +35,8 @@ func main() { '=': true, '`': true, '-': true, + // TODO try adding |. it breaks up content that we don't generally want, + // anyway, and will lead to short garby phrases being discarded. i think. } s := bufio.NewScanner(os.Stdin)