summaryrefslogtreecommitdiff
path: root/src/JavaScript
diff options
context:
space:
mode:
Diffstat (limited to 'src/JavaScript')
-rw-r--r--src/JavaScript/Element.hs38
-rw-r--r--src/JavaScript/Event.hs38
-rw-r--r--src/JavaScript/Val.hs24
-rw-r--r--src/JavaScript/Window.hs25
4 files changed, 125 insertions, 0 deletions
diff --git a/src/JavaScript/Element.hs b/src/JavaScript/Element.hs
new file mode 100644
index 0000000..2eac8f2
--- /dev/null
+++ b/src/JavaScript/Element.hs
@@ -0,0 +1,38 @@
+module JavaScript.Element (
+ Element, IsElement(..),
+ getElementById,
+ documentQuerySelector,
+ querySelector,
+) where
+
+import JavaScript.Event
+import JavaScript.Val
+
+
+newtype Element = Element JSVal
+ deriving (ToJSVal, FromJSVal)
+
+class IsElement a where
+ toElement :: a -> Element
+
+instance IsElement Element where
+ toElement = id
+
+instance IsEventTarget Element where
+ toEventTarget = fromJSValUnchecked . toJSVal
+
+
+getElementById :: String -> IO (Maybe Element)
+getElementById = fmap nullToNothing . js_document_getElementById . toJSString
+foreign import javascript unsafe "document.getElementById($1)"
+ js_document_getElementById :: JSString -> IO JSVal
+
+documentQuerySelector :: String -> IO (Maybe Element)
+documentQuerySelector = fmap nullToNothing . js_document_querySelector . toJSString
+foreign import javascript unsafe "document.querySelector($1)"
+ js_document_querySelector :: JSString -> IO JSVal
+
+querySelector :: IsElement e => String -> e -> IO (Maybe Element)
+querySelector sel e = nullToNothing <$> js_querySelector (toJSVal $ toElement e) (toJSString sel)
+foreign import javascript unsafe "$1.querySelector($2)"
+ js_querySelector :: JSVal -> JSString -> IO JSVal
diff --git a/src/JavaScript/Event.hs b/src/JavaScript/Event.hs
new file mode 100644
index 0000000..4bc61d2
--- /dev/null
+++ b/src/JavaScript/Event.hs
@@ -0,0 +1,38 @@
+module JavaScript.Event (
+ EventTarget, IsEventTarget(..),
+ Event, IsEvent(..),
+ asEventListener,
+ addEventListener,
+) where
+
+import JavaScript.Val
+
+
+newtype EventTarget = EventTarget JSVal
+ deriving (ToJSVal, FromJSVal)
+
+class IsEventTarget a where
+ toEventTarget :: a -> EventTarget
+
+instance IsEventTarget EventTarget where
+ toEventTarget = id
+
+
+newtype Event = Event JSVal
+ deriving (ToJSVal, FromJSVal)
+
+class IsEvent a where
+ toEvent :: a -> Event
+
+instance IsEvent Event where
+ toEvent = id
+
+
+foreign import javascript "wrapper"
+ asEventListener :: (JSVal -> IO ()) -> IO JSVal
+
+addEventListener :: IsEventTarget a => a -> String -> (Event -> IO ()) -> IO ()
+addEventListener target ev cb = do
+ js_addEventListener (toJSVal $ toEventTarget target) (toJSString ev) =<< asEventListener (cb . fromJSValUnchecked)
+foreign import javascript unsafe "$1.addEventListener($2, $3)"
+ js_addEventListener :: JSVal -> JSString -> JSVal -> IO ()
diff --git a/src/JavaScript/Val.hs b/src/JavaScript/Val.hs
new file mode 100644
index 0000000..e1879b4
--- /dev/null
+++ b/src/JavaScript/Val.hs
@@ -0,0 +1,24 @@
+module JavaScript.Val (
+ module GHC.Wasm.Prim,
+ ToJSVal(..), FromJSVal(..),
+ nullToNothing,
+) where
+
+import GHC.Wasm.Prim
+
+
+class ToJSVal a where
+ toJSVal :: a -> JSVal
+
+class FromJSVal a where
+ fromJSValUnchecked :: JSVal -> a
+
+instance ToJSVal JSVal where toJSVal = id
+instance FromJSVal JSVal where fromJSValUnchecked = id
+
+
+nullToNothing :: FromJSVal a => JSVal -> Maybe a
+nullToNothing val | isNull val = Nothing
+ | otherwise = Just $ fromJSValUnchecked val
+foreign import javascript unsafe "$1 === null"
+ isNull :: JSVal -> Bool
diff --git a/src/JavaScript/Window.hs b/src/JavaScript/Window.hs
new file mode 100644
index 0000000..db30c9d
--- /dev/null
+++ b/src/JavaScript/Window.hs
@@ -0,0 +1,25 @@
+module JavaScript.Window (
+ Window, IsWindow(..),
+ globalWindow,
+) where
+
+import JavaScript.Event
+import JavaScript.Val
+
+
+newtype Window = Window JSVal
+ deriving (ToJSVal, FromJSVal)
+
+class IsWindow a where
+ toWindow :: a -> Window
+
+instance IsWindow Window where
+ toWindow = id
+
+instance IsEventTarget Window where
+ toEventTarget = fromJSValUnchecked . toJSVal
+
+globalWindow :: Window
+globalWindow = Window js_window
+foreign import javascript unsafe "window"
+ js_window :: JSVal