Browse Source

Added query selector functionality to gather collection of links to be registered

Wirecog 7 years ago
parent
commit
305a19ebc8
3 changed files with 13 additions and 10 deletions
  1. 1
    1
      route.go
  2. 9
    6
      router.go
  3. 3
    3
      templatebundle.go

+ 1
- 1
route.go View File

@@ -51,7 +51,7 @@ func NewRoute(path string, handler HandlerFunc) *Route {
51 51
 	for _, routePart := range routeParts {
52 52
 		if strings.HasPrefix(routePart, `{`) && strings.HasSuffix(routePart, `}`) {
53 53
 			routePattern += RouteWithParamsPattern
54
-			routePart = strings.TrimPrefix(path, `}`)
54
+			routePart = strings.TrimPrefix(path, `{`)
55 55
 			routePart = strings.TrimSuffix(path, `}`)
56 56
 			r.varNames = append(r.varNames, routePart)
57 57
 		} else {

+ 9
- 6
router.go View File

@@ -35,21 +35,24 @@ func (r *Router) HandleFunc(path string, handler HandlerFunc) *Route {
35 35
 }
36 36
 
37 37
 func (r *Router) Listen() {
38
-	r.RegisterLinks()
38
+	r.RegisterLinks("body a")
39 39
 }
40 40
 
41
-func (r *Router) RegisterLinks() {
41
+func (r *Router) RegisterLinks(querySelector string) {
42 42
 	document := dom.GetWindow().Document().(dom.HTMLDocument)
43
-	for _, link := range document.Links() {
43
+	links := document.QuerySelectorAll(querySelector)
44
+
45
+	for _, link := range links {
44 46
 		href := link.GetAttribute("href")
45 47
 		switch {
46 48
 
47 49
 		case strings.HasPrefix(href, "/") && !strings.HasPrefix(href, "//"):
48 50
 
49 51
 			if r.listener != nil {
50
-				link.RemoveEventListener("click", true, r.listener)
52
+				link.RemoveEventListener("click", false, r.listener)
51 53
 			}
52
-			r.listener = link.AddEventListener("click", true, r.linkHandler)
54
+
55
+			r.listener = link.AddEventListener("click", false, r.linkHandler)
53 56
 		}
54 57
 	}
55 58
 }
@@ -83,7 +86,7 @@ func (r *Router) linkHandler(event dom.Event) {
83 86
 		routeVars := make(map[string]string)
84 87
 
85 88
 		for i, part := range parts {
86
-			routeVars[matchedRoute.varNames[i]] = part
89
+			routeVars[matchedRoute.varNames[i]+`}`] = part
87 90
 		}
88 91
 
89 92
 		var ctx context.Context

+ 3
- 3
templatebundle.go View File

@@ -6,8 +6,8 @@
6 6
 package isokit
7 7
 
8 8
 import (
9
-	"fmt"
10 9
 	"io/ioutil"
10
+	"log"
11 11
 	"os"
12 12
 	"path/filepath"
13 13
 	"strings"
@@ -41,7 +41,7 @@ func (t *TemplateBundle) importTemplateFileContents(templatesPath string) error
41 41
 			t.items[name] = string(contents)
42 42
 
43 43
 			if err != nil {
44
-				fmt.Println("error encountered while walking directory: ", err)
44
+				log.Println("error encountered while walking directory: ", err)
45 45
 				return err
46 46
 			}
47 47
 
@@ -68,7 +68,7 @@ func (t *TemplateBundle) importTemplateFileContentsForCog(templatesPath string,
68 68
 			t.items[name] = string(contents)
69 69
 
70 70
 			if err != nil {
71
-				fmt.Println("error encountered while walking directory: ", err)
71
+				log.Println("error encountered while walking directory: ", err)
72 72
 				return err
73 73
 			}
74 74