From a9681ee7eb52e6c7ab665bf7cb3a892d524f3c4e Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Fri, 19 May 2023 23:30:22 +0800 Subject: [PATCH] Add event and properties for history navigation availability --- view.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/view.js b/view.js index 1a8adc3..3746ee1 100644 --- a/view.js +++ b/view.js @@ -10,6 +10,7 @@ class History extends EventTarget { if (last === x || last?.fraction && last.fraction === x.fraction) return this.#arr[++this.#index] = x this.#arr.length = this.#index + 1 + this.dispatchEvent(new Event('index-change')) } replaceState(x) { const index = this.#index @@ -21,6 +22,7 @@ class History extends EventTarget { const detail = { state: this.#arr[index - 1] } this.#index = index - 1 this.dispatchEvent(new CustomEvent('popstate', { detail })) + this.dispatchEvent(new Event('index-change')) } forward() { const index = this.#index @@ -28,6 +30,13 @@ class History extends EventTarget { const detail = { state: this.#arr[index + 1] } this.#index = index + 1 this.dispatchEvent(new CustomEvent('popstate', { detail })) + this.dispatchEvent(new Event('index-change')) + } + get canGoBack() { + return this.#index > 0 + } + get canGoForward() { + return this.#index < this.#arr.length - 1 } }