|
@@ -0,0 +1,97 @@
|
|
1
|
+// The Isomorphic Go Project
|
|
2
|
+// Copyright (c) Wirecog, LLC. All rights reserved.
|
|
3
|
+// Use of this source code is governed by a BSD-style
|
|
4
|
+// license, which can be found in the LICENSE file.
|
|
5
|
+
|
|
6
|
+package isokit
|
|
7
|
+
|
|
8
|
+import (
|
|
9
|
+ "context"
|
|
10
|
+ "strings"
|
|
11
|
+
|
|
12
|
+ "github.com/gopherjs/gopherjs/js"
|
|
13
|
+ "honnef.co/go/js/dom"
|
|
14
|
+)
|
|
15
|
+
|
|
16
|
+type Router struct {
|
|
17
|
+ routes []*Route
|
|
18
|
+ listener func(*js.Object)
|
|
19
|
+}
|
|
20
|
+
|
|
21
|
+func NewRouter() *Router {
|
|
22
|
+ return &Router{
|
|
23
|
+ routes: []*Route{},
|
|
24
|
+ }
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+func (r *Router) Handle(path string, handler Handler) *Route {
|
|
28
|
+ return r.HandleFunc(path, handler.(HandlerFunc))
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+func (r *Router) HandleFunc(path string, handler HandlerFunc) *Route {
|
|
32
|
+ route := NewRoute(path, handler)
|
|
33
|
+ r.routes = append(r.routes, route)
|
|
34
|
+ return route
|
|
35
|
+}
|
|
36
|
+
|
|
37
|
+func (r *Router) Listen() {
|
|
38
|
+ r.RegisterLinks()
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+func (r *Router) RegisterLinks() {
|
|
42
|
+ document := dom.GetWindow().Document().(dom.HTMLDocument)
|
|
43
|
+ for _, link := range document.Links() {
|
|
44
|
+ href := link.GetAttribute("href")
|
|
45
|
+ switch {
|
|
46
|
+
|
|
47
|
+ case strings.HasPrefix(href, "/") && !strings.HasPrefix(href, "//"):
|
|
48
|
+
|
|
49
|
+ if r.listener != nil {
|
|
50
|
+ link.RemoveEventListener("click", true, r.listener)
|
|
51
|
+ }
|
|
52
|
+ r.listener = link.AddEventListener("click", true, r.linkHandler)
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+func (r *Router) linkHandler(event dom.Event) {
|
|
58
|
+ uri := event.CurrentTarget().GetAttribute("href")
|
|
59
|
+ path := strings.Split(uri, "?")[0]
|
|
60
|
+ // leastParams := -1
|
|
61
|
+ var matchedRoute *Route
|
|
62
|
+ var parts []string
|
|
63
|
+ var lowestMatchCountSet bool = false
|
|
64
|
+ var lowestMatchCount int = -1
|
|
65
|
+
|
|
66
|
+ for _, route := range r.routes {
|
|
67
|
+
|
|
68
|
+ matches := route.regex.FindStringSubmatch(path)
|
|
69
|
+ matchesExist := len(matches) > 0 && matches != nil
|
|
70
|
+ isLowestMatchCount := (lowestMatchCountSet == false) || (len(matches) < lowestMatchCount)
|
|
71
|
+
|
|
72
|
+ if matchesExist && isLowestMatchCount {
|
|
73
|
+ matchedRoute = route
|
|
74
|
+ parts = matches[1:]
|
|
75
|
+ lowestMatchCount = len(matches)
|
|
76
|
+ lowestMatchCountSet = true
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ if matchedRoute != nil {
|
|
81
|
+ event.PreventDefault()
|
|
82
|
+ js.Global.Get("history").Call("pushState", nil, "", uri)
|
|
83
|
+ routeVars := make(map[string]string)
|
|
84
|
+
|
|
85
|
+ for i, part := range parts {
|
|
86
|
+ routeVars[matchedRoute.varNames[i]] = part
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ var ctx context.Context
|
|
90
|
+ ctx, cancel := context.WithCancel(context.Background())
|
|
91
|
+ defer cancel()
|
|
92
|
+
|
|
93
|
+ k := RouteVarsKey("Vars")
|
|
94
|
+ ctx = context.WithValue(ctx, k, routeVars)
|
|
95
|
+ go matchedRoute.handler.ServeRoute(ctx)
|
|
96
|
+ }
|
|
97
|
+}
|