aboutsummaryrefslogtreecommitdiff
path: root/js/src
diff options
context:
space:
mode:
authorJoris2020-02-02 13:53:54 +0100
committerJoris2020-02-02 13:58:19 +0100
commit0ce8744897b9aa13ea568a6985da9570e4aca90b (patch)
tree3619508ff9ec5d846c567c3562d05ca77e686c76 /js/src
parent13d29804ba4bb8d578fb0278ddeb6e187bbafa6f (diff)
Use zola generator
- Use CSS instead of Haskell with Clay - Use TypeScript instead of PureScript
Diffstat (limited to 'js/src')
-rw-r--r--js/src/Dom.js12
-rw-r--r--js/src/Dom.purs88
-rw-r--r--js/src/EditableNumber.purs55
-rw-r--r--js/src/Main.purs50
-rw-r--r--js/src/Number.purs22
-rw-r--r--js/src/Parser.purs76
6 files changed, 0 insertions, 303 deletions
diff --git a/js/src/Dom.js b/js/src/Dom.js
deleted file mode 100644
index 6835c1f..0000000
--- a/js/src/Dom.js
+++ /dev/null
@@ -1,12 +0,0 @@
-"use strict";
-
-exports.onInput = function (elt) {
- return function (f) {
- return function () {
- elt.oninput = function(e) {
- f(e.target.value)()
- }
- return {};
- };
- };
-};
diff --git a/js/src/Dom.purs b/js/src/Dom.purs
deleted file mode 100644
index 4176153..0000000
--- a/js/src/Dom.purs
+++ /dev/null
@@ -1,88 +0,0 @@
-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
diff --git a/js/src/EditableNumber.purs b/js/src/EditableNumber.purs
deleted file mode 100644
index 02ffe58..0000000
--- a/js/src/EditableNumber.purs
+++ /dev/null
@@ -1,55 +0,0 @@
-module EditableNumber
- ( NumberElem
- , set
- ) where
-
-import Data.Maybe (Maybe(..))
-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
-import Parser (TextWithNumber)
-import Parser (textWithNumber) as Parser
-
-type NumberElem =
- { elem :: Element
- , number :: Number
- }
-
-set :: { tag :: String, node :: Node } -> Effect (Maybe NumberElem)
-set { tag, node } = do
- content <- Node.textContent node
- case Parser.textWithNumber content of
- Just twn -> do
- textWithNumber <- textWithNumberElem tag twn
- Dom.replaceElement node (Element.toNode textWithNumber)
- pure (Just { elem: textWithNumber, number: twn.number })
- Nothing ->
- pure Nothing
-
-textWithNumberElem :: String -> TextWithNumber -> Effect Element
-textWithNumberElem tag { begin, number, end } = do
- document <- HTMLDocument.toDocument <$> (HTML.window >>= Window.document)
- elem <- Document.createElement tag document
- beginNode <- Text.toNode <$> Document.createTextNode begin document
- numberNode <- numberElem number
- endNode <- Text.toNode <$> Document.createTextNode end document
- Dom.appendNodes (Element.toNode elem) [ beginNode, Element.toNode numberNode, endNode ]
- pure elem
-
-numberElem :: Number -> Effect Element
-numberElem number = do
- 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
deleted file mode 100644
index 0ff5fd7..0000000
--- a/js/src/Main.purs
+++ /dev/null
@@ -1,50 +0,0 @@
-module Main (main) where
-
-import Data.Array (catMaybes) as Array
-import Data.Maybe (Maybe(..))
-import Data.Traversable (sequence, sequence_) as Traversable
-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)
-import EditableNumber (set) as EditableNumber
-import Number (format) as Number
-import Parser (number) as Parser
-
-main :: Effect Unit
-main = do
- tagElems <- getNumberElements
- numberElems <- Array.catMaybes <$> (Traversable.sequence $ map EditableNumber.set tagElems)
- Traversable.sequence_ $ map (onInput numberElems) numberElems
-
-getNumberElements :: Effect (Array { tag :: String, node :: Node })
-getNumberElements = do
- 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 :: Array NumberElem -> NumberElem -> Effect Unit
-onInput numberElems { elem, number } = do
- Dom.onInput elem (\value -> do
- case Parser.number value of
- Just newNumber ->
- numberElems
- # map (\ne -> do
- inputNode <- Dom.selectElementFrom ne.elem (QuerySelector "input")
- case inputNode of
- Just node -> Dom.setValue (Number.format (ne.number / number * newNumber)) node
- _ -> pure unit
- )
- # Traversable.sequence_
- _ ->
- pure unit
- )
-
-maybeToArray :: forall a. Maybe a -> Array a
-maybeToArray (Just x) = [ x ]
-maybeToArray _ = []
diff --git a/js/src/Number.purs b/js/src/Number.purs
deleted file mode 100644
index 0403f19..0000000
--- a/js/src/Number.purs
+++ /dev/null
@@ -1,22 +0,0 @@
-module Number
- ( format
- , roundAt
- ) where
-
-import Data.Int (round, toNumber, pow) as Int
-import Data.String (Pattern(..), Replacement(..))
-import Data.String (replace) as String
-import Math (round) as Math
-import Prelude
-
-format :: Number -> String
-format number =
- if Math.round number == number then
- show (Int.round number)
- else
- String.replace (Pattern ".") (Replacement ",") (show (roundAt 1 number))
-
-roundAt :: Int -> Number -> Number
-roundAt at n =
- let exp = Int.toNumber (Int.pow 10 at)
- in Math.round (n * exp) / exp
diff --git a/js/src/Parser.purs b/js/src/Parser.purs
deleted file mode 100644
index b378e96..0000000
--- a/js/src/Parser.purs
+++ /dev/null
@@ -1,76 +0,0 @@
-module Parser
- ( TextWithNumber
- , textWithNumber
- , number
- ) where
-
-import Control.Alt ((<|>))
-import Data.Array as Array
-import Data.Char as Char
-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.CodeUnits as String
-import Prelude
-import Text.Parsing.Parser (Parser)
-import Text.Parsing.Parser (runParser) as Parser
-import Text.Parsing.Parser.Combinators (optionMaybe) as Parser
-import Text.Parsing.Parser.String (satisfy, anyChar, string, eof) as Parser
-
-type TextWithNumber =
- { begin :: String
- , number :: Number
- , end :: String
- }
-
-textWithNumber :: String -> Maybe TextWithNumber
-textWithNumber input =
- case Parser.runParser input textWithNumberParser of
- Right x -> Just x
- _ -> Nothing
-
-number :: String -> Maybe Number
-number input =
- case Parser.runParser input (numberParser <* Parser.eof) of
- Right x -> Just x
- _ -> Nothing
-
-textWithNumberParser :: Parser String TextWithNumber
-textWithNumberParser = do
- begin <- String.fromCharArray <$> Array.many notDigit
- num <- numberParser
- end <- String.fromCharArray <$> Array.many Parser.anyChar
- pure { begin: begin, number: num, end: end }
-
-notDigit :: Parser String Char
-notDigit = Parser.satisfy (not <<< isDigit)
-
-numberParser :: Parser String Number
-numberParser = do
- whole <- numberFromIntArray <$> Array.some digit
- decimal <- Parser.optionMaybe $ do
- _ <- Parser.string "," <|> Parser.string "."
- digits <- Array.some digit
- let decimals = numberFromIntArray digits
- pure $ Int.toNumber decimals / Int.toNumber (Int.pow 10 (Array.length digits))
- pure (Int.toNumber whole + Maybe.fromMaybe 0.0 decimal)
-
-numberFromIntArray :: Array Int -> Int
-numberFromIntArray xs =
- Array.range 0 (Array.length xs - 1)
- # map (Int.pow 10)
- # Array.reverse
- # Array.zipWith (*) xs
- # Array.foldl (+) 0
-
-digit :: Parser String Int
-digit = map (\c -> Char.toCharCode c - zeroCode) $ Parser.satisfy isDigit
-
-isDigit :: Char -> Boolean
-isDigit char =
- let code = Char.toCharCode char
- in code >= zeroCode && code <= zeroCode + 9
-
-zeroCode :: Int
-zeroCode = 48