-
Notifications
You must be signed in to change notification settings - Fork 11
/
this.go
259 lines (215 loc) · 6.97 KB
/
this.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
252
253
254
255
256
257
258
259
/*
Copyright 2016 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gr
import (
"fmt"
"reflect"
"github.com/gopherjs/gopherjs/js"
)
// This is named for what it represents: The this context representation from the
// JavaScript side of the fence.
type This struct {
This *js.Object
}
// SetThis implements the ThisSetter interface.
func (t *This) SetThis(that *js.Object) {
t.This = that
}
// ThisSetter must be implemented by a component if JavaScript's this is needed.
// The simplest way of doing this is to just embed a This on the component struct.
type ThisSetter interface {
SetThis(this *js.Object)
}
// Props returns the properties set; this is what you would expect to find in
// this.props in React.
func (t *This) Props() Props {
return objectToMap(t.This.Get("props"))
}
// Context returns the context set; what you would expect to find in
// this.context in React.
func (t *This) Context() Context {
return objectToMap(t.This.Get("context"))
}
// Component returns a component stored in props by its name.
func (t *This) Component(name string) Modifier {
props := t.Props()
if main, ok := props[name]; ok {
return NewPreparedElement(main.(*js.Object))
}
return Discard
}
// IsMounted reports whether this component is mounted.
func (t *This) IsMounted() bool {
return t.This.Call("isMounted").Bool()
}
// State returns the state; what you would expect to find in
// this.properties in React.
func (t *This) State() State {
return objectToMap(t.This.Get("state"))
}
// Int is convenience method to lookup a int value from state.
func (s State) Int(key string) int {
if val, ok := s[key]; ok {
return val.(*js.Object).Int()
}
return 0
}
// Bool is convenience method to lookup a bool value from state.
func (s State) Bool(key string) bool {
if val, ok := s[key]; ok {
return val.(*js.Object).Bool()
}
panic(fmt.Sprintf("State variable %q not found", key))
}
// String is convenience method to lookup a bool value from state.
func (s State) String(key string) string {
if val, ok := s[key]; ok {
return val.(*js.Object).String()
}
return ""
}
// Interface is a convenience method to lookup an interface value from state.
func (s State) Interface(key string) interface{} {
if val, ok := s[key]; ok {
return val.(*js.Object).Interface()
}
return nil
}
// SetState sets the state with a map of Go interface{} values.
func (t *This) SetState(s State) {
t.This.Call("setState", s)
}
// Refs returns the component references.
// See https://facebook.github.io/react/docs/more-about-refs.html
func (t *This) Refs() Refs {
return objectToMap(t.This.Get("refs"))
}
// GetDOMNode returns the component from refs if it has been mounted into the DOM.
func (r Refs) GetDOMNode(key string) *js.Object {
if o, ok := r[key]; ok {
return reactDOM.Call("findDOMNode", o)
}
return nil
}
// ForceUpdate forces a re-render of the component.
func (t *This) ForceUpdate() {
t.This.Call("forceUpdate")
}
// NewThis creates a new This based on a JavaScript object representation.
func NewThis(that *js.Object) *This {
return &This{This: that}
}
// Context holds the React context.
type Context map[string]interface{}
// Props holds the React properties.
type Props map[string]interface{}
// State holds the React state.
type State map[string]interface{}
// Refs holds a reference to component references.
// See https://facebook.github.io/react/docs/more-about-refs.html
type Refs map[string]interface{}
// Copy creates a copy of this Props.
func (p Props) Copy() Props {
copy := Props{}
for k, v := range p {
copy[k] = v
}
return copy
}
// Call calls a func with the given name in Props with the given args.
func (p Props) Call(name string, args ...interface{}) *js.Object {
f := p.Func(name)
return f(args...)
}
// Func returns the func with the given name in Props.
func (p Props) Func(name string) func(args ...interface{}) *js.Object {
if o, ok := p[name]; ok {
return o.(*js.Object).Interface().(func(args ...interface{}) *js.Object)
}
panic(fmt.Sprintf("func %s not found in properties", name))
}
// Interface is a convenience method to lookup an interface value from props.
func (p Props) Interface(key string) interface{} {
if val, ok := p[key]; ok {
return val.(*js.Object).Interface()
}
return nil
}
// Int is convenience method to lookup a int value from props.
func (p Props) Int(key string) int {
if val, ok := p[key]; ok {
return val.(*js.Object).Int()
}
return 0
}
// Bool is convenience method to lookup a bool value from props.
func (p Props) Bool(key string) bool {
if val, ok := p[key]; ok {
return val.(*js.Object).Bool()
}
panic(fmt.Sprintf("Props variable %q not found", key))
}
// String is convenience method to lookup a bool value from props.
func (p Props) String(key string) string {
if val, ok := p[key]; ok {
return val.(*js.Object).String()
}
return ""
}
// Children represents a component's child component(s).
// This is a fairly complex topic in Facebook's React, and more support may arrive here, eventually.
// See https://facebook.github.io/react/tips/children-props-type.html
type Children struct {
*js.Object
}
// Children returns this component's children, if any.
func (t *This) Children() *Children {
o := t.This.Get("props").Get("children")
if o == js.Undefined {
return nil
}
return &Children{o}
}
// Element returns the children as an Element ready to render.
func (c *Children) Element() *Element {
return NewPreparedElement(c.Object)
}
// HasChanged reports whether the value of the property with any of the given keys has changed.
func (p Props) HasChanged(nextProps Props, keys ...string) bool {
return hasChanged(p, nextProps, keys...)
}
// HasChanged reports whether the value of the state with any of the given keys has changed.
// Note: This does only reference checking, so no deep equality.
// See https://github.com/gopherjs/gopherjs/issues/473
func (s State) HasChanged(nextState State, keys ...string) bool {
return hasChanged(s, nextState, keys...)
}
func hasChanged(m1, m2 map[string]interface{}, keys ...string) bool {
for _, key := range keys {
// TODO(bep) DeepEqual
if m1[key] != m2[key] {
return true
}
}
return false
}
// HasChangedDeeply reports whether the value of the state with any of the given keys has changed.
// This uses reflect.DeepEqual. For shallow equality checking, see HasChanged.
func (s State) HasChangedDeeply(nextState State, keys ...string) bool {
for _, key := range keys {
if !reflect.DeepEqual(s[key], nextState[key]) {
return true
}
}
return false
}