From 142e0c9fac9896f8cb134fd1e1b010753402e7b8 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 2 Feb 2019 17:14:33 +0100 Subject: Make it works and add “galette” recipe --- js/src/Dom.purs | 75 ++++++++++++++++++++-------------------------- js/src/EditableNumber.purs | 47 ++++++++++++++--------------- js/src/Main.purs | 17 +++++------ js/src/Parser.purs | 2 +- js/test/Main.purs | 6 ++-- 5 files changed, 68 insertions(+), 79 deletions(-) (limited to 'js') diff --git a/js/src/Dom.purs b/js/src/Dom.purs index 426c390..167da38 100644 --- a/js/src/Dom.purs +++ b/js/src/Dom.purs @@ -9,54 +9,51 @@ module Dom , setValue ) where -import Control.Monad.Eff (Eff) import Control.Monad.Except (runExcept) as Except import Data.Array (range, catMaybes) as Array -import Data.Either (Either(Right)) -import Data.Foreign (toForeign) as Foreign import Data.Maybe (Maybe(Nothing, Just)) import Data.Traversable (sequence) as Traversable +import Effect (Effect) import Prelude -import DOM (DOM) -import DOM.HTML (window) as DOM -import DOM.HTML.HTMLInputElement (setValue) as HTMLInputElement -import DOM.HTML.Types (htmlDocumentToParentNode, readHTMLInputElement) as DOM -import DOM.HTML.Window (document) as DOM -import DOM.Node.Node (replaceChild, parentNode, appendChild) as DOM -import DOM.Node.NodeList (length, item) as DOM -import DOM.Node.ParentNode (QuerySelector) -import DOM.Node.ParentNode (querySelector, querySelectorAll) as DOM -import DOM.Node.Types (Element, Node, NodeList) -import DOM.Node.Types (elementToParentNode, readElement) as DOM +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 :: forall e. Element -> (String -> Eff (dom :: DOM | e) Unit) -> Eff (dom :: DOM | e) Unit +foreign import onInput :: Element -> (String -> Effect Unit) -> Effect Unit -selectElement :: forall e. QuerySelector -> Eff (dom :: DOM | e) (Maybe Element) +selectElement :: QuerySelector -> Effect (Maybe Element) selectElement query = do - document <- DOM.window >>= DOM.document - DOM.querySelector query (DOM.htmlDocumentToParentNode document) + document <- HTML.window >>= Window.document + DOM.querySelector query (HTMLDocument.toParentNode document) -selectElements :: forall e. QuerySelector -> Eff (dom :: DOM | e) (Array Element) +selectElements :: QuerySelector -> Effect (Array Element) selectElements query = do - document <- DOM.window >>= DOM.document - nodeList <- DOM.querySelectorAll query (DOM.htmlDocumentToParentNode document) + document <- HTML.window >>= Window.document + nodeList <- DOM.querySelectorAll query (HTMLDocument.toParentNode document) getNodes nodeList -selectElementFrom :: forall e. Element -> QuerySelector -> Eff (dom :: DOM | e) (Maybe Element) +selectElementFrom :: Element -> QuerySelector -> Effect (Maybe Element) selectElementFrom elem query = - DOM.querySelector query (DOM.elementToParentNode elem) + DOM.querySelector query (Element.toParentNode elem) -selectElementsFrom :: forall e. Element -> QuerySelector -> Eff (dom :: DOM | e) (Array Element) +selectElementsFrom :: Element -> QuerySelector -> Effect (Array Element) selectElementsFrom elem query = do - nodeList <- DOM.querySelectorAll query (DOM.elementToParentNode elem) + nodeList <- DOM.querySelectorAll query (Element.toParentNode elem) getNodes nodeList -getNodes :: forall e. NodeList -> Eff (dom :: DOM | e) (Array Element) +getNodes :: NodeList -> Effect (Array Element) getNodes nodeList = do - length <- DOM.length nodeList + length <- NodeList.length nodeList Array.range 0 length - # (map (\i -> (concatMaybe <<< map nodeToElement) <$> DOM.item i nodeList)) + # (map (\i -> (concatMaybe <<< map Element.fromNode) <$> NodeList.item i nodeList)) # Traversable.sequence # map Array.catMaybes @@ -66,33 +63,27 @@ concatMaybe mma = Just x -> x Nothing -> Nothing -nodeToElement :: Node -> Maybe Element -nodeToElement node = - case Except.runExcept $ DOM.readElement (Foreign.toForeign node) of - Right element -> Just element - _ -> Nothing - -replaceElement :: forall e. Node -> Node -> Eff (dom :: DOM | e) Unit +replaceElement :: Node -> Node -> Effect Unit replaceElement before after = do - parent <- DOM.parentNode before + parent <- Node.parentNode before case parent of Just n -> do - _ <- DOM.replaceChild after before n + _ <- Node.replaceChild after before n pure unit Nothing -> pure unit -appendNodes :: forall e. Node -> Array Node -> Eff (dom :: DOM | e) Unit +appendNodes :: Node -> Array Node -> Effect Unit appendNodes parent nodes = nodes - # map (\n -> DOM.appendChild n parent) + # map (\n -> Node.appendChild n parent) # Traversable.sequence # map (const unit) -setValue :: forall e. String -> Element -> Eff (dom :: DOM | e) Unit +setValue :: String -> Element -> Effect Unit setValue value elem = - case Except.runExcept $ DOM.readHTMLInputElement (Foreign.toForeign elem) of - Right inputElem -> do + case HTMLInputElement.fromElement elem of + Just inputElem -> do HTMLInputElement.setValue value inputElem _ -> pure unit diff --git a/js/src/EditableNumber.purs b/js/src/EditableNumber.purs index 6a6e3a8..02ffe58 100644 --- a/js/src/EditableNumber.purs +++ b/js/src/EditableNumber.purs @@ -3,18 +3,17 @@ module EditableNumber , set ) where -import Control.Monad.Eff (Eff) import Data.Maybe (Maybe(..)) -import DOM (DOM) -import DOM.HTML (window) as DOM -import DOM.HTML.Types (htmlDocumentToDocument) as DOM -import DOM.HTML.Window (document) as DOM -import DOM.Node.Document (createElement, createTextNode) as DOM -import DOM.Node.Element (setClassName, setAttribute) as DOM -import DOM.Node.Node (textContent) as DOM -import DOM.Node.Types (Element, Node) -import DOM.Node.Types (elementToNode, textToNode) as DOM +import Effect (Effect) import Prelude +import Web.DOM.Document (createElement, createTextNode) as Document +import Web.DOM.Element (setClassName, setAttribute, toNode) as Element +import Web.DOM.Internal.Types (Element, Node) +import Web.DOM.Node (textContent) as Node +import Web.DOM.Text (toNode) as Text +import Web.HTML (window) as HTML +import Web.HTML.HTMLDocument (toDocument) as HTMLDocument +import Web.HTML.Window (document) as Window import Dom (replaceElement, appendNodes) as Dom import Number (format) as Number @@ -26,31 +25,31 @@ type NumberElem = , number :: Number } -set :: forall e. { tag :: String, node :: Node } -> Eff (dom :: DOM | e) (Maybe NumberElem) +set :: { tag :: String, node :: Node } -> Effect (Maybe NumberElem) set { tag, node } = do - content <- DOM.textContent node + content <- Node.textContent node case Parser.textWithNumber content of Just twn -> do textWithNumber <- textWithNumberElem tag twn - Dom.replaceElement node (DOM.elementToNode textWithNumber) + Dom.replaceElement node (Element.toNode textWithNumber) pure (Just { elem: textWithNumber, number: twn.number }) Nothing -> pure Nothing -textWithNumberElem :: forall e. String -> TextWithNumber -> Eff (dom :: DOM | e) Element +textWithNumberElem :: String -> TextWithNumber -> Effect Element textWithNumberElem tag { begin, number, end } = do - document <- DOM.htmlDocumentToDocument <$> (DOM.window >>= DOM.document) - elem <- DOM.createElement tag document - beginNode <- DOM.textToNode <$> DOM.createTextNode begin document + document <- HTMLDocument.toDocument <$> (HTML.window >>= Window.document) + elem <- Document.createElement tag document + beginNode <- Text.toNode <$> Document.createTextNode begin document numberNode <- numberElem number - endNode <- DOM.textToNode <$> DOM.createTextNode end document - Dom.appendNodes (DOM.elementToNode elem) [ beginNode, DOM.elementToNode numberNode, endNode ] + endNode <- Text.toNode <$> Document.createTextNode end document + Dom.appendNodes (Element.toNode elem) [ beginNode, Element.toNode numberNode, endNode ] pure elem -numberElem :: forall e. Number -> Eff (dom :: DOM | e) Element +numberElem :: Number -> Effect Element numberElem number = do - document <- DOM.htmlDocumentToDocument <$> (DOM.window >>= DOM.document) - container <- DOM.createElement "input" document - DOM.setClassName "number" container - DOM.setAttribute "value" (Number.format number) container + document <- HTMLDocument.toDocument <$> (HTML.window >>= Window.document) + container <- Document.createElement "input" document + Element.setClassName "number" container + Element.setAttribute "value" (Number.format number) container pure container diff --git a/js/src/Main.purs b/js/src/Main.purs index a16b2cd..0ff5fd7 100644 --- a/js/src/Main.purs +++ b/js/src/Main.purs @@ -1,14 +1,13 @@ 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 (elementToNode) as DOM -import DOM.Node.Types (Node) +import Effect (Effect) import Prelude +import Web.DOM.Element (toNode) as Element +import Web.DOM.Internal.Types (Node) +import Web.DOM.ParentNode (QuerySelector(..)) import Dom (selectElement, selectElements, onInput, setValue, selectElementFrom) as Dom import EditableNumber (NumberElem) @@ -16,20 +15,20 @@ import EditableNumber (set) as EditableNumber import Number (format) as Number import Parser (number) as Parser -main :: forall e. Eff (dom :: DOM | e) Unit +main :: Effect 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 :: Effect (Array { tag :: String, node :: Node }) getNumberElements = do - let fromElem tag elem = { tag: tag, node: DOM.elementToNode elem } + let fromElem tag elem = { tag: tag, node: Element.toNode elem } h1 <- map (fromElem "h1") <$> Dom.selectElement (QuerySelector "h1") lis <- map (fromElem "li") <$> Dom.selectElements (QuerySelector "ul > li") pure $ (maybeToArray h1 <> lis) -onInput :: forall e. Array NumberElem -> NumberElem -> Eff (dom :: DOM | e) Unit +onInput :: Array NumberElem -> NumberElem -> Effect Unit onInput numberElems { elem, number } = do Dom.onInput elem (\value -> do case Parser.number value of diff --git a/js/src/Parser.purs b/js/src/Parser.purs index 787c09e..b378e96 100644 --- a/js/src/Parser.purs +++ b/js/src/Parser.purs @@ -11,7 +11,7 @@ import Data.Either (Either(Right)) import Data.Int as Int import Data.Maybe (fromMaybe) as Maybe import Data.Maybe (Maybe(Just, Nothing)) -import Data.String as String +import Data.String.CodeUnits as String import Prelude import Text.Parsing.Parser (Parser) import Text.Parsing.Parser (runParser) as Parser diff --git a/js/test/Main.purs b/js/test/Main.purs index e23f9e2..3a4f03c 100644 --- a/js/test/Main.purs +++ b/js/test/Main.purs @@ -2,19 +2,19 @@ module Test.Main ( main ) where -import Control.Monad.Eff (Eff) import Data.Maybe (Maybe(Just, Nothing)) +import Effect (Effect) import Prelude import Test.Spec (describe, it) import Test.Spec.Assertions (shouldEqual) import Test.Spec.Reporter.Console (consoleReporter) -import Test.Spec.Runner (RunnerEffects, run) +import Test.Spec.Runner (run) import Number (roundAt, format) as Number import Parser (TextWithNumber) import Parser (textWithNumber, number) as Parser -main :: Eff (RunnerEffects ()) Unit +main :: Effect Unit main = run [consoleReporter] do describe "Number" do -- cgit v1.2.3