This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroute.go
150 lines (117 loc) · 3.21 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package angularjs
import (
"reflect"
"github.com/gopherjs/gopherjs/js"
)
func init() {
RegisterResource(reflect.TypeOf(&RouteProvider{}), "$routeProvider", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&RouteProvider{Object: obj})
})
RegisterResource(reflect.TypeOf(&RouteParams{}), "$routeParams", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&RouteParams{Object: obj})
})
RegisterResource(reflect.TypeOf(&Location{}), "$location", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&Location{Object: obj})
})
RegisterResource(reflect.TypeOf(&LocationProvider{}), "$locationProvider", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&LocationProvider{Object: obj})
})
}
type LocationProvider struct {
*js.Object
}
func (l *LocationProvider) Html5Mode(on bool) {
l.Call("html5Mode", on)
}
// Location implements $location
type Location struct {
*js.Object
}
type LocationSearch map[string]string
func (l LocationSearch) Has(key string) bool {
_, ok := l[key]
return ok
}
func (l LocationSearch) Get(key string) string {
return l[key]
}
func (l *Location) Search() LocationSearch {
ret := make(LocationSearch)
for key, val := range l.Call("search").Interface().(map[string]interface{}) {
ret[key] = val.(string)
}
return ret
}
func (l *Location) Path(p string) {
l.Call("path", p)
}
func (l *Location) CurrentPath() string {
return l.Call("path").String()
}
// RouteProvider implements $routeProvider
type RouteProvider struct {
*js.Object
}
// When is used to register a new URL
func (r *RouteProvider) When(url string, config RouteConfig) {
r.Call("when", url, configToMap(config))
}
// Otherwise is used as a 404
func (r *RouteProvider) Otherwise(config RouteConfig) {
r.Call("otherwise", configToMap(config))
}
// configToMap transforms the struct to a lowercased map
func configToMap(config RouteConfig) map[string]interface{} {
args := map[string]interface{}{
"controller": config.Controller,
}
if config.TemplateURL != "" {
args["templateUrl"] = config.TemplateURL
} else {
args["template"] = config.Template
}
if config.Resolve != nil {
for key, val := range config.Resolve {
if reflect.ValueOf(val).Kind() != reflect.Func {
panic("only funcs can be passed to RouteConfig->Resolve")
}
transformedFunc, err := MakeFuncInjectable(val)
if err != nil {
panic(err)
}
config.Resolve[key] = transformedFunc
}
args["resolve"] = config.Resolve
}
return args
}
// RouteConfig is an implementation of the config used to $routeProvider.when
type RouteConfig struct {
TemplateURL string
Template string
Controller string
Resolve map[string]interface{}
}
// RouteParams is a copy of $routeParams
type RouteParams struct {
*js.Object
}
// Get returns the value at the given key
func (r RouteParams) Get(key string) string {
if r.Object.Get(key) == js.Undefined {
return ""
}
return r.Object.Get(key).String()
}
// Route is the angular $route
type Route struct {
*js.Object
}
// Params returns the $route.params value
func (r *Route) Params() map[string]string {
ret := make(map[string]string)
for key, val := range r.Get("params").Interface().(map[string]interface{}) {
ret[key] = val.(string)
}
return ret
}