-
Notifications
You must be signed in to change notification settings - Fork 11
/
modifiers.go
133 lines (107 loc) · 3.37 KB
/
modifiers.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
/*
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 (
"github.com/gopherjs/gopherjs/js"
"strings"
)
type textEl struct {
text string
}
// Text creates a text element.
func Text(i interface{}) Modifier {
text := toString(i)
return &textEl{text: text}
}
// Modify implements the Modifier interface.
func (s *textEl) Modify(in *Element) {
in.children = append(in.children, s)
}
// Node implements the component interface.
func (s *textEl) Node() *js.Object {
return js.Global.Get("Object").New(s.text)
}
type cssClasses []string
// CSS creates a CSS element with the provided classes.
// Note that duplicates are happily accepted.
func CSS(classes ...string) Modifier {
return cssClasses(classes)
}
// Data creates a data attribute, e.g. data-columns="3"
// See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes
func Data(name, val string) Modifier {
return Prop("data-"+name, val)
}
// Aria creates an accessibility attributes.
// See https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA
func Aria(name, val string) Modifier {
return Prop("aria-"+name, val)
}
type prop struct {
name string
value interface{}
}
// Modify implements the Modifier interface.
func (p *prop) Modify(element *Element) {
if element.properties == nil {
element.properties = make(map[string]interface{})
}
if _, ok := element.properties[p.name]; ok {
panic("Duplicate property: " + p.name)
}
element.properties[p.name] = p.value
}
// Prop adds a custom attribute.
func Prop(name string, value interface{}) Modifier {
return &prop{name: name, value: value}
}
// Modify implements the Modifier interface.
func (m cssClasses) Modify(element *Element) {
if existing, ok := element.properties["className"]; ok {
//merge with existing
element.properties["className"] = existing.(string) + " " + strings.Join(m, " ")
} else {
Prop("className", strings.Join(m, " ")).Modify(element)
}
}
type style struct {
name string
value interface{}
}
type discard int
// Modify implements the Modifier interface with a no-op.
func (d discard) Modify(element *Element) {
// Do nothing!
}
// Discard is a Modifier that does nothing.
var Discard = new(discard)
// Modify implements the Modifier interface.
func (s *style) Modify(element *Element) {
if element.style == nil {
element.style = make(map[string]interface{})
}
// last style with a given name wins
element.style[s.name] = s.value
}
// Style adds a inline CSS style.
func Style(name string, value interface{}) Modifier {
return &style{name: name, value: value}
}
type dynamicModifier int
// Dynamic is a Modifier that marks the element as dynamic,
// i.e. not static. This will turn off static features such as adding
// missing keys.
var Dynamic = new(dynamicModifier)
func (d dynamicModifier) Modify(element *Element) {
element.dynamic = true
}