From: Eduardo San Martin Morote Date: Wed, 9 Oct 2019 13:22:20 +0000 (+0200) Subject: feat(abstract): add back and forward X-Git-Tag: v4.0.0-alpha.0~209 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9de5a52564d8e8e3d44ac22f3af488d14ed77595;p=thirdparty%2Fvuejs%2Frouter.git feat(abstract): add back and forward --- diff --git a/src/history/abstract.2.ts b/src/history/abstract.2.ts index 3b24249b..06e8feb1 100644 --- a/src/history/abstract.2.ts +++ b/src/history/abstract.2.ts @@ -6,6 +6,8 @@ import { normalizeLocation, HistoryLocationNormalized, HistoryState, + NavigationType, + NavigationDirection, } from './common' // TODO: implement navigation direction in listeners @@ -31,6 +33,21 @@ export default function createAbstractHistory(): RouterHistory { } } + function triggerListeners( + to: HistoryLocationNormalized, + from: HistoryLocationNormalized, + { direction }: { direction: NavigationDirection } + ): void { + // TODO: proper type + const info: Parameters[2] = { + direction, + type: NavigationType.pop, + } + for (let callback of listeners) { + callback(to, from, info) + } + } + const routerHistory: RouterHistory = { // rewritten by Object.defineProperty location: START, @@ -57,26 +74,30 @@ export default function createAbstractHistory(): RouterHistory { listeners = [] }, - // back(triggerListeners: boolean = true) { - // const from = this.location - // if (this.position > 0) this.position-- - // if (triggerListeners) { - // this.triggerListeners(this.location, from, { - // direction: NavigationDirection.back, - // }) - // } - // }, + back(shouldTrigger = true) { + const from = this.location + if (position > 0) position-- + if (shouldTrigger) { + triggerListeners(this.location, from, { + direction: NavigationDirection.back, + }) + } + }, - // forward(triggerListeners: boolean = true) { - // const from = this.location - // if (this.position < this.queue.length - 1) this.position++ - // if (triggerListeners) { - // this.triggerListeners(this.location, from, { - // direction: NavigationDirection.forward, - // }) - // } - // }, + forward(shouldTrigger: boolean = true) { + const from = this.location + if (position < queue.length - 1) position++ + if (shouldTrigger) { + triggerListeners(this.location, from, { + direction: NavigationDirection.forward, + }) + } + }, } + Object.defineProperty(routerHistory, 'location', { + get: () => queue[position], + }) + return routerHistory } diff --git a/src/history/common.ts b/src/history/common.ts index e664beac..222b81e4 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -73,11 +73,11 @@ export interface RouterHistory { location: HistoryLocationNormalized push(to: RawHistoryLocation, data?: any): void replace(to: RawHistoryLocation): void - listen(callback: NavigationCallback): ListenerRemover - // back(triggerListeners?: boolean): void - // forward(triggerListeners?: boolean): void + back(triggerListeners?: boolean): void + forward(triggerListeners?: boolean): void + listen(callback: NavigationCallback): ListenerRemover destroy(): void }