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 pathangularjs.go
251 lines (203 loc) · 6.47 KB
/
angularjs.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package angularjs
import (
"reflect"
"github.com/gopherjs/gopherjs/js"
)
func init() {
RegisterResource(reflect.TypeOf(&Scope{}), "$scope", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&Scope{Object: obj})
})
RegisterResource(reflect.TypeOf(&RootScope{}), "$rootScope", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&RootScope{
&Scope{Object: obj},
})
})
RegisterResource(reflect.TypeOf(&Q{}), "$q", func(obj *js.Object) reflect.Value {
return reflect.ValueOf(&Q{Object: obj})
})
}
// NewModule makes a new angular module
func NewModule(name string, requires []string, configFn interface{}) *Module {
if configFn == nil {
configFn = func() {}
}
transformedFunc, err := MakeFuncInjectable(configFn)
if err != nil {
panic(err)
}
return &Module{js.Global.Get("angular").Call("module", name, requires, transformedFunc)}
}
// Service gets a service trough the global $injector
func Service(name string) *js.Object {
return js.Global.Get("angular").Call("element", js.Global.Get("document")).Call("injector").Call("get", name)
}
// Module is an angular module
type Module struct{ *js.Object }
// Run is the go implementation of .run in angularjs
func (m *Module) Run(fn interface{}) {
transformedFunc, err := MakeFuncInjectable(fn)
if err != nil {
panic(err)
}
m.Call("run", transformedFunc)
}
// NewController creates a new controller on the given module
// The arguments of the controller should only contain resources that have
// been added with a call to RegisterResouce
func (m *Module) NewController(name string, constructor interface{}) {
transformedFunc, err := MakeFuncInjectable(constructor)
if err != nil {
panic(err)
}
m.Call("controller", name, transformedFunc)
}
// NewDirective registers a directive.
// The fomat should be: func(.... resources) Directive
func (m *Module) NewDirective(name string, f interface{}) {
injects, callable, err := GetFuncInjectables(f)
if err != nil {
panic(err)
}
if callable.Type().NumOut() != 1 {
panic("expected 1 out arg for directive: " + name)
}
if callable.Type().Out(0) != reflect.TypeOf(Directive{}) {
panic("directive: " + name + " should have 1 return value of type: " + reflect.TypeOf(Directive{}).String() + " got: " + callable.Type().Out(0).String())
}
angularParamNames := make(js.S, 0)
transFormFuncs := make([]func(obj *js.Object) reflect.Value, 0)
for _, i := range injects {
angularParamNames = append(angularParamNames, i.angularName)
transFormFuncs = append(transFormFuncs, i.f)
}
m.Call("directive", []interface{}{name, append(angularParamNames, func(objs ...*js.Object) map[string]interface{} {
args := make([]reflect.Value, 0)
for i, obj := range objs {
args = append(args, transFormFuncs[i](obj))
}
ret := callable.Call(args)
if len(ret) == 0 {
return nil
}
directive := ret[0].Interface().(Directive)
directiveMap := make(map[string]interface{})
directiveMap["templateUrl"] = directive.TemplateURL
scope := make(map[string]string)
for _, bindAttr := range directive.Scope {
scope[bindAttr] = "="
}
directiveMap["scope"] = scope
directiveMap["restrict"] = directive.Restrict
if directive.Link != nil {
directiveMap["link"] = func(scope *js.Object, element *js.Object, attrs map[string]interface{}, controller *js.Object, transcludeFn *js.Object) {
directive.Link(&Scope{scope}, &Element{element}, attrs)
}
}
return directiveMap
})}...)
}
// Directive is the go variant of an angular directive
// At least a templateURL should be given.
type Directive struct {
Restrict string
Link func(scope *Scope, el *Element, attrs map[string]interface{})
TemplateURL string
Scope []string
}
// RootScope is the angular $rootScope
type RootScope struct {
*Scope
}
// Scope is the angular $scope
type Scope struct {
*js.Object
}
// Apply is the angular $scope.$apply and should be called when
// changing values trough goroutines or event listeners
func (s *Scope) Apply(f func()) {
s.Call("$apply", f)
}
// CopyScope is used to provide a *js.Object scope for structs inside a controller
func (s *Scope) CopyScope() *js.Object {
return s.Call("$new")
}
// Watch is the angular $watch
func (s *Scope) Watch(key string, f func(newValue interface{}, oldValue interface{})) {
s.Call("$watch", key, f)
}
func (s *Scope) EvalAsync(f func()) {
s.Call("$evalAsync", f)
}
// On registers the given listener for an event.
// Returns an unregister func for the event
// format of listener: func(ev *Event, args ...interface{})
func (s *Scope) On(event string, listener interface{}) (unregisterFunc func()) {
l := s.Call("$on", event, listener)
return func() {
l.Invoke()
}
}
// Listen binds to the event and removes it if the scope is destroyed
func (s *Scope) Listen(event string, listener interface{}) {
l := s.On(event, listener)
s.On("$destroy", l)
}
// Emit is a copy of $emit
func (s *Scope) Emit(event string, args ...interface{}) {
args = append([]interface{}{event}, args...)
s.Call("$emit", args...)
}
// Broadcast is a copy of $broadcast
func (s *Scope) Broadcast(event string, args ...interface{}) {
args = append([]interface{}{event}, args...)
s.Call("$broadcast", args...)
}
// Event is an angular event from $emit/$broadcast
type Event struct {
*js.Object
}
// TargetScope see $event.targetScope
func (e *Event) TargetScope() *Scope {
return &Scope{e.Get("targetScope")}
}
// CurrentScope see $event.currentScope
func (e *Event) CurrentScope() *Scope {
return &Scope{e.Get("currentScope")}
}
// Name see $event.name
func (e *Event) Name() string {
return e.Get("name").String()
}
// PreventDefault see $event.preventDefault()
func (e *Event) PreventDefault() {
e.Call("preventDefault")
}
// DefaultPrevented see $event.defaultPrevented
func (e *Event) DefaultPrevented() bool {
return e.Get("defaultPrevented").Bool()
}
// Q represets angulars $q
type Q struct {
*js.Object
}
func (q *Q) Defer() *Deferred {
return &Deferred{q.Call("defer")}
}
// Deferred is angulars defer object returned from $q.defer()
type Deferred struct {
*js.Object
}
// Promise is angulars deferred.promis
func (d *Deferred) Promise() *js.Object {
return d.Get("promise")
}
// Resolve will resolve the promise.
// Only js objects with an initialized object can be given.
// An empty javascript object can be created with 'js.Global.Get("Object").New()'
func (d *Deferred) Resolve(data interface{}) *js.Object {
return d.Call("resolve", data)
}
// Reject see Resolve
func (d *Deferred) Reject(data interface{}) *js.Object {
return d.Call("reject", data)
}