From a438dc0c02059c05fb82ee16e7c450a71b48a2eb Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 19 Mar 2017 16:23:41 +0100 Subject: [PATCH] handle url hash changes --- src/typescript/DOM.ts | 4 ++-- src/typescript/Initialisation.ts | 11 +---------- src/typescript/UI.ts | 21 +++++++++++++++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/typescript/DOM.ts b/src/typescript/DOM.ts index eeb70ab..5c499b9 100644 --- a/src/typescript/DOM.ts +++ b/src/typescript/DOM.ts @@ -61,8 +61,8 @@ namespace NP } // Shorthand for the first element of getElementsByClassName - public static Class(className: string): NodeListOf { - return >document.getElementsByClassName(className); + public static Class(className: string): NodeListOf { + return >document.getElementsByClassName(className); } // Shorthand for prepending diff --git a/src/typescript/Initialisation.ts b/src/typescript/Initialisation.ts index 96b3b89..dbbb5f3 100644 --- a/src/typescript/Initialisation.ts +++ b/src/typescript/Initialisation.ts @@ -1,13 +1,4 @@ window.addEventListener("load", () => { - var user: string = location.hash.substring(2); - NP.UI.RegisterHooks(); - - if (user.length < 1) { - NP.UI.Mode(NP.Mode.INDEX); - NP.UI.Background(NP.Background.COLOURFADE); - } else { - NP.UI.Mode(NP.Mode.USER); - NP.Watcher.Start(user); - } + NP.UI.Update(); }); diff --git a/src/typescript/UI.ts b/src/typescript/UI.ts index 2a4a42d..96f282a 100644 --- a/src/typescript/UI.ts +++ b/src/typescript/UI.ts @@ -87,19 +87,32 @@ namespace NP } } - public static RegisterHooks(): void { - UI.InfoBack.addEventListener('click', () => { + public static Update(): void { + var user: string = location.hash.substring(2); + + if (user.length > 0) { + Watcher.Start(user); + UI.Mode(Mode.USER); + } else { Watcher.Stop(); UI.Mode(Mode.INDEX); - NP.UI.Background(NP.Background.COLOURFADE); + UI.Background(Background.COLOURFADE); + } + } + + public static RegisterHooks(): void { + UI.InfoBack.addEventListener('click', () => { location.hash = ''; }); var enter: Function = () => { location.hash = '#/' + UI.FormUsername.value; - Watcher.Start(UI.FormUsername.value); }; + window.addEventListener('hashchange', () => { + UI.Update(); + }); + UI.FormSubmit.addEventListener('click', () => { enter.call(this); });