-
Notifications
You must be signed in to change notification settings - Fork 216
/
Attributes.purs
280 lines (210 loc) · 6.96 KB
/
Attributes.purs
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
-- | This module enumerates some common HTML attributes, and provides additional
-- | helper functions for working with CSS classes.
module Halogen.HTML.Attributes
( ClassName()
, className
, runClassName
, AttributeName()
, attributeName
, runAttributeName
, EventName()
, eventName
, runEventName
, IsAttribute
, toAttrString
, ExistsR()
, mkExistsR
, runExistsR
, AttrF(..)
, HandlerF(..)
, Attr(..)
, attr
, handler
, initializer
, finalizer
, key
, alt
, charset
, class_
, classes
, colSpan
, rowSpan
, content
, for
, height
, href
, httpEquiv
, id_
, name
, rel
, src
, target
, title
, type_
, value
, width
, disabled
, required
, readonly
, spellcheck
, enabled
, checked
, selected
, placeholder
) where
import Prelude
import DOM
import Data.Maybe
import Data.Tuple
import Data.Either (either)
import Data.Foreign
import Data.Monoid (mempty)
import Data.String (joinWith)
import Data.Traversable (mapAccumL)
import Data.Exists
import Control.Monad.Eff
import Control.Monad.ST
import Halogen.Internal.VirtualDOM
import Halogen.HTML.Events.Types
import Halogen.HTML.Events.Handler
import Unsafe.Coerce (unsafeCoerce)
-- | We need a variant of `Exists` which works for type constructors which accept a _row_ of types.
data ExistsR (f :: # * -> *)
runExistsR :: forall f r. (forall a. f a -> r) -> ExistsR f -> r
runExistsR = unsafeCoerce
mkExistsR :: forall f a. f a -> ExistsR f
mkExistsR = unsafeCoerce
-- | The data which represents a typed attribute, hidden inside an existential package in
-- | the `Attr` type.
data AttrF value = AttrF (AttributeName value -> value -> String) (AttributeName value) value
-- | The data which represents a typed event handler, hidden inside an existential package in
-- | the `Attr` type.
data HandlerF i fields = HandlerF (EventName fields) (Event fields -> EventHandler i)
-- | A single attribute is either
-- |
-- | - An attribute
-- | - An event handler
data Attr i
= Attr (Exists AttrF)
| Handler (ExistsR (HandlerF i))
| Initializer i
| Finalizer i
instance functorAttr :: Functor Attr where
map _ (Attr e) = Attr e
map f (Handler e) = runExistsR (\(HandlerF name k) -> Handler (mkExistsR (HandlerF name (\e -> f <$> k e)))) e
map f (Initializer i) = Initializer (f i)
map f (Finalizer i) = Finalizer (f i)
-- | Create an attribute
attr :: forall value i. (IsAttribute value) => AttributeName value -> value -> Attr i
attr name v = Attr (mkExists (AttrF toAttrString name v))
-- | Create an event handler
handler :: forall fields i. EventName fields -> (Event fields -> EventHandler i) -> Attr i
handler name k = Handler (mkExistsR (HandlerF name k))
-- | Attach an initializer.
initializer :: forall i. i -> Attr i
initializer = Initializer
-- | Attach a finalizer.
finalizer :: forall i. i -> Attr i
finalizer = Finalizer
-- | A wrapper for strings which are used as CSS classes
newtype ClassName = ClassName String
-- Create a class name
className :: String -> ClassName
className = ClassName
-- | Unpack a class name
runClassName :: ClassName -> String
runClassName (ClassName s) = s
-- | A type-safe wrapper for attribute names
-- |
-- | The phantom type `value` describes the type of value which this attribute requires.
newtype AttributeName value = AttributeName String
-- | Create an attribute name
attributeName :: forall value. String -> AttributeName value
attributeName = AttributeName
-- | Unpack an attribute name
runAttributeName :: forall value. AttributeName value -> String
runAttributeName (AttributeName s) = s
-- | A type-safe wrapper for event names.
-- |
-- | The phantom type `fields` describes the event type which we can expect to exist on events
-- | corresponding to this name.
newtype EventName (fields :: # *) = EventName String
-- Create an event name
eventName :: forall fields. String -> EventName fields
eventName = EventName
-- | Unpack an event name
runEventName :: forall fields. EventName fields -> String
runEventName (EventName s) = s
-- | This type class captures those types which can be used as attribute values.
-- |
-- | `toAttrString` is an alternative to `show`, and is needed by `attr` in the string renderer.
class IsAttribute a where
toAttrString :: AttributeName a -> a -> String
instance stringIsAttribute :: IsAttribute String where
toAttrString _ s = s
instance numberIsAttribute :: IsAttribute Number where
toAttrString _ n = show n
instance booleanIsAttribute :: IsAttribute Boolean where
toAttrString name true = runAttributeName name
toAttrString _ false = ""
-- Smart constructors
-- | The `key` property associates a unique key with a node, which can be used to
-- | implement a more efficient diff/patch.
key :: forall i. String -> Attr i
key = attr $ attributeName "key"
alt :: forall i. String -> Attr i
alt = attr $ attributeName "alt"
charset :: forall i. String -> Attr i
charset = attr $ attributeName "charset"
class_ :: forall i. ClassName -> Attr i
class_ = attr (attributeName "className") <<< runClassName
classes :: forall i. Array ClassName -> Attr i
classes ss = attr (attributeName "className") (joinWith " " $ map runClassName ss)
colSpan :: forall i. Number -> Attr i
colSpan = attr (attributeName "colSpan") <<< show
rowSpan :: forall i. Number -> Attr i
rowSpan = attr (attributeName "rowSpan") <<< show
content :: forall i. String -> Attr i
content = attr $ attributeName "content"
for :: forall i. String -> Attr i
for = attr $ attributeName "htmlFor"
height :: forall i. Number -> Attr i
height = attr (attributeName "height") <<< show
href :: forall i. String -> Attr i
href = attr $ attributeName "href"
httpEquiv :: forall i. String -> Attr i
httpEquiv = attr $ attributeName "http-equiv"
id_ :: forall i. String -> Attr i
id_ = attr $ attributeName "id"
name :: forall i. String -> Attr i
name = attr $ attributeName "name"
rel :: forall i. String -> Attr i
rel = attr $ attributeName "rel"
src :: forall i. String -> Attr i
src = attr $ attributeName "src"
target :: forall i. String -> Attr i
target = attr $ attributeName "target"
title :: forall i. String -> Attr i
title = attr $ attributeName "title"
type_ :: forall i. String -> Attr i
type_ = attr $ attributeName "type"
value :: forall i. String -> Attr i
value = attr $ attributeName "value"
width :: forall i. Number -> Attr i
width = attr (attributeName "width") <<< show
disabled :: forall i. Boolean -> Attr i
disabled = attr $ attributeName "disabled"
required :: forall i. Boolean -> Attr i
required = attr $ attributeName "required"
readonly :: forall i. Boolean -> Attr i
readonly = attr $ attributeName "readonly"
spellcheck :: forall i. Boolean -> Attr i
spellcheck = attr $ attributeName "spellcheck"
enabled :: forall i. Boolean -> Attr i
enabled = disabled <<< not
checked :: forall i. Boolean -> Attr i
checked = attr $ attributeName "checked"
selected :: forall i. Boolean -> Attr i
selected = attr $ attributeName "selected"
placeholder :: forall i. String -> Attr i
placeholder = attr $ attributeName "placeholder"