From: Eduardo San Martin Morote Date: Sat, 12 Oct 2019 14:22:02 +0000 (+0200) Subject: feat(abstract): add go method X-Git-Tag: v4.0.0-alpha.0~202 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0d9c6c8f88ed68d9fc07904fd7e6cf6b9300a9c9;p=thirdparty%2Fvuejs%2Frouter.git feat(abstract): add go method --- diff --git a/src/history/abstract.2.ts b/src/history/abstract.2.ts index 06e8feb1..e3603204 100644 --- a/src/history/abstract.2.ts +++ b/src/history/abstract.2.ts @@ -8,6 +8,7 @@ import { HistoryState, NavigationType, NavigationDirection, + NavigationInformation, } from './common' // TODO: implement navigation direction in listeners @@ -36,11 +37,14 @@ export default function createAbstractHistory(): RouterHistory { function triggerListeners( to: HistoryLocationNormalized, from: HistoryLocationNormalized, - { direction }: { direction: NavigationDirection } + { + direction, + distance, + }: Pick ): void { - // TODO: proper type - const info: Parameters[2] = { + const info: NavigationInformation = { direction, + distance, type: NavigationType.pop, } for (let callback of listeners) { @@ -75,21 +79,25 @@ export default function createAbstractHistory(): RouterHistory { }, back(shouldTrigger = true) { - const from = this.location - if (position > 0) position-- - if (shouldTrigger) { - triggerListeners(this.location, from, { - direction: NavigationDirection.back, - }) - } + this.go(-1, shouldTrigger) + }, + + forward(shouldTrigger = true) { + this.go(1, shouldTrigger) }, - forward(shouldTrigger: boolean = true) { + go(distance, shouldTrigger = true) { const from = this.location - if (position < queue.length - 1) position++ + const direction: NavigationDirection = + // we are considering distance === 0 going forward, but in abstract mode + // using 0 for the distance doesn't make sense like it does in html5 where + // it reloads the page + distance < 0 ? NavigationDirection.back : NavigationDirection.forward + position = Math.max(0, Math.min(position + distance, queue.length - 1)) if (shouldTrigger) { triggerListeners(this.location, from, { - direction: NavigationDirection.forward, + direction, + distance, }) } },