]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(abstract): add back and forward
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:22:20 +0000 (15:22 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 9 Oct 2019 13:22:20 +0000 (15:22 +0200)
src/history/abstract.2.ts
src/history/common.ts

index 3b24249b7f2c2cd8b7129ed40ce585d0e3f91e7c..06e8feb1fc40b2f5ae98eacc7336dfe60162bc29 100644 (file)
@@ -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<NavigationCallback>[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
 }
index e664beac3460032735940a87d06960dd97cf2e6c..222b81e4329a0c4a6f34d059426e6fb82508ca70 100644 (file)
@@ -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
 }