/
Util.purs
35 lines (30 loc) · 1.15 KB
/
Util.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
module Game.Aff.Web.Util where
import Prelude
import Data.Filterable (filterMap)
import Effect (Effect)
import Game.Util.Maybe (liftBoth)
import Run (EFFECT, Run, liftEffect)
import Run.Except (FAIL)
import Web.DOM.Element (Element, fromNode)
import Web.DOM.NodeList (NodeList, toArray)
import Web.DOM.ParentNode (QuerySelector, querySelector, querySelectorAll)
import Web.HTML (window)
import Web.HTML.HTMLDocument (toParentNode)
import Web.HTML.Window (document)
-- | `querySelector` without having to supply a `ParentNode`, using the
-- | document as parent node.
qSel ∷
∀ r
. QuerySelector
→ Run (effect ∷ EFFECT, except ∷ FAIL | r) Element
qSel sel = liftBoth do
doc ← window >>= document <#> toParentNode
querySelector sel doc
-- | `querySelectorAll` without having to supply a `ParentNode`, using the
-- | document as parent node.
qSelAll ∷ ∀ r. QuerySelector → Run (effect ∷ EFFECT | r) (Array Element)
qSelAll sel = liftEffect do
doc ← window >>= document <#> toParentNode
querySelectorAll sel doc >>= nodeListToElems
nodeListToElems ∷ NodeList → Effect (Array Element)
nodeListToElems = toArray >>> map (filterMap fromNode)