From 4f4e7489ceb5946fff838ceb716633feecc0a1cc Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Fri, 31 Mar 2017 19:53:01 +0300 Subject: [PATCH] fix: use replace state instead of pushState fixes #244 --- lib/services/hash.service.ts | 6 +++++- lib/services/menu.service.ts | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/services/hash.service.ts b/lib/services/hash.service.ts index b1d4dbd2..82d1ed61 100644 --- a/lib/services/hash.service.ts +++ b/lib/services/hash.service.ts @@ -27,8 +27,12 @@ export class Hash { }); } - update(hash: string|null) { + update(hash: string|null, rewriteHistory:boolean = false) { if (hash == undefined) return; + if (rewriteHistory) { + window.history.replaceState(null, '', '#' + hash); + return; + } this.noEmit = true; window.location.hash = hash; setTimeout(() => { diff --git a/lib/services/menu.service.ts b/lib/services/menu.service.ts index a43c4351..a8d1671b 100644 --- a/lib/services/menu.service.ts +++ b/lib/services/menu.service.ts @@ -202,14 +202,14 @@ export class MenuService { } } - activate(idx, force = false) { + activate(idx, force = false, replaceState = false) { let item = this.flatItems[idx]; if (!force && item && !item.ready) return; this.deactivate(this.activeIdx); this.activeIdx = idx; if (idx < 0) { - this.hash.update(''); + this.hash.update('', replaceState); return; } @@ -220,14 +220,14 @@ export class MenuService { cItem.parent.active = true; cItem = cItem.parent; } - this.hash.update(this.hashFor(item.id, item.metadata, item.parent && item.parent.id)); + this.hash.update(this.hashFor(item.id, item.metadata, item.parent && item.parent.id), replaceState); this.changedActiveItem.next(item); } changeActive(offset = 1):boolean { let noChange = (this.activeIdx <= 0 && offset === -1) || (this.activeIdx === this.flatItems.length - 1 && offset === 1); - this.activate(this.activeIdx + offset); + this.activate(this.activeIdx + offset, false, true); return noChange; }