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
|
module Dom
( onInput
, selectElement
, selectElements
, selectElementFrom
, selectElementsFrom
, replaceElement
, appendNodes
, setValue
) where
import Data.Array (range, catMaybes) as Array
import Data.Maybe (Maybe(Nothing, Just))
import Data.Traversable (sequence) as Traversable
import Effect (Effect)
import Prelude
import Web.DOM.Element (toParentNode, fromNode) as Element
import Web.DOM.Internal.Types (Element, Node, NodeList)
import Web.DOM.Node (replaceChild, parentNode, appendChild) as Node
import Web.DOM.NodeList (length, item) as NodeList
import Web.DOM.ParentNode (QuerySelector)
import Web.DOM.ParentNode (querySelector, querySelectorAll) as DOM
import Web.HTML (window) as HTML
import Web.HTML.HTMLDocument as HTMLDocument
import Web.HTML.HTMLInputElement (setValue, fromElement) as HTMLInputElement
import Web.HTML.Window (document) as Window
foreign import onInput :: Element -> (String -> Effect Unit) -> Effect Unit
selectElement :: QuerySelector -> Effect (Maybe Element)
selectElement query = do
document <- HTML.window >>= Window.document
DOM.querySelector query (HTMLDocument.toParentNode document)
selectElements :: QuerySelector -> Effect (Array Element)
selectElements query = do
document <- HTML.window >>= Window.document
nodeList <- DOM.querySelectorAll query (HTMLDocument.toParentNode document)
getNodes nodeList
selectElementFrom :: Element -> QuerySelector -> Effect (Maybe Element)
selectElementFrom elem query =
DOM.querySelector query (Element.toParentNode elem)
selectElementsFrom :: Element -> QuerySelector -> Effect (Array Element)
selectElementsFrom elem query = do
nodeList <- DOM.querySelectorAll query (Element.toParentNode elem)
getNodes nodeList
getNodes :: NodeList -> Effect (Array Element)
getNodes nodeList = do
length <- NodeList.length nodeList
Array.range 0 length
# (map (\i -> (concatMaybe <<< map Element.fromNode) <$> NodeList.item i nodeList))
# Traversable.sequence
# map Array.catMaybes
concatMaybe :: forall a. Maybe (Maybe a) -> Maybe a
concatMaybe mma =
case mma of
Just x -> x
Nothing -> Nothing
replaceElement :: Node -> Node -> Effect Unit
replaceElement before after = do
parent <- Node.parentNode before
case parent of
Just n -> do
_ <- Node.replaceChild after before n
pure unit
Nothing ->
pure unit
appendNodes :: Node -> Array Node -> Effect Unit
appendNodes parent nodes =
nodes
# map (\n -> Node.appendChild n parent)
# Traversable.sequence
# map (const unit)
setValue :: String -> Element -> Effect Unit
setValue value elem =
case HTMLInputElement.fromElement elem of
Just inputElem -> do
HTMLInputElement.setValue value inputElem
_ ->
pure unit
|