]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(abstract): add go method
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 12 Oct 2019 14:22:02 +0000 (16:22 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 12 Oct 2019 14:22:02 +0000 (16:22 +0200)
src/history/abstract.2.ts

index 06e8feb1fc40b2f5ae98eacc7336dfe60162bc29..e3603204ab6051c4b49e994e2573038caca27826 100644 (file)
@@ -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<NavigationInformation, 'direction' | 'distance'>
   ): void {
-    // TODO: proper type
-    const info: Parameters<NavigationCallback>[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,
         })
       }
     },