blob: 062eb1d77dac6252b95743c056f1091a69d1925b (
plain)
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
|
module Main (main) where
import Control.Monad.Eff (Eff)
import Data.Array (catMaybes) as Array
import Data.Maybe (Maybe(..))
import Data.Traversable (sequence, sequence_) as Traversable
import DOM (DOM)
import DOM.Node.ParentNode (QuerySelector(..))
import DOM.Node.Types (Node)
import DOM.Node.Types (elementToNode) as DOM
import Prelude
import Dom (selectElement, selectElementsFrom, onInput, setValue, selectElementFrom) as Dom
import EditableNumber (NumberElem)
import EditableNumber (set, formatNumber) as EditableNumber
import Parser (number) as Parser
main :: forall e. Eff (dom :: DOM | e) Unit
main = do
tagElems <- getNumberElements
numberElems <- Array.catMaybes <$> (Traversable.sequence $ map EditableNumber.set tagElems)
Traversable.sequence_ $ map (onInput numberElems) numberElems
getNumberElements :: forall e. Eff (dom :: DOM | e) (Array { tag :: String, node :: Node })
getNumberElements = do
h2 <- (map (\elem -> { tag: "h2", node: DOM.elementToNode elem})) <$> Dom.selectElement (QuerySelector "h2")
ul <- Dom.selectElement (QuerySelector "ul")
lis <- case ul of
Just elem -> do
myLis <- Dom.selectElementsFrom elem (QuerySelector "li")
pure $ map (\node -> { tag: "li", node: node }) myLis
_ -> do
pure []
pure $ (maybeToArray h2 <> lis)
onInput :: forall e. Array NumberElem -> NumberElem -> Eff (dom :: DOM | e) Unit
onInput numberElems { elem, number } = do
Dom.onInput elem (\value -> do
case Parser.number value of
Just newNumber ->
let mul = newNumber / number
in numberElems
# map (\ne -> do
inputNode <- Dom.selectElementFrom ne.elem (QuerySelector "input")
case inputNode of
Just node -> Dom.setValue (EditableNumber.formatNumber (ne.number * mul)) node
_ -> pure unit
)
# Traversable.sequence_
_ ->
pure unit
)
maybeToArray :: forall a. Maybe a -> Array a
maybeToArray (Just x) = [ x ]
maybeToArray _ = []
|