Skip to content

Commit 6bb3644

Browse files
committed
Renaming
1 parent a37bec3 commit 6bb3644

11 files changed

+395
-232
lines changed

Clickable.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Native GHC -}
33
module Clickable (module Exports) where
44

5-
import Clickable.Html as Exports
5+
import Clickable.HTML as Exports
66
import Clickable.Element as Exports
77
import Clickable.Property as Exports
88
import Clickable.Internal as Exports

Clickable/Element.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Shortcuts for most common HTML5 elements
88
-}
99
module Clickable.Element where
1010

11-
import Clickable.Html
11+
import Clickable.HTML
1212
import Clickable.Types
1313
import Data.Text
1414

@@ -28,13 +28,13 @@ class Term arg result | result -> arg where
2828
-> result -- ^ Result: either an element or an attribute.
2929

3030
-- | Given attributes, expect more child input.
31-
instance f ~ HtmlM a => Term [HtmlM ()] (f -> HtmlM a) where
31+
instance f ~ HTML a => Term [HTML ()] (f -> HTML a) where
3232
term name attrs = el name . (sequence_ attrs *>)
3333
{-# INLINE term #-}
3434

3535
-- | Given children immediately, just use that and expect no
3636
-- attributes.
37-
instance Term (HtmlM a) (HtmlM a) where
37+
instance Term (HTML a) (HTML a) where
3838
term = el
3939
{-# INLINE term #-}
4040

@@ -362,7 +362,7 @@ sup_ :: Term arg result => arg -> result
362362
sup_ = term "sup"
363363
{-# INLINE sup_ #-}
364364

365-
br_ :: HtmlM ()
365+
br_ :: HTML ()
366366
br_ = el "br" blank
367367
{-# INLINE br_ #-}
368368

Clickable/Html.hs renamed to Clickable/HTML.hs

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{-# LANGUAGE AllowAmbiguousTypes #-}
1010
{-# LANGUAGE DataKinds #-}
1111
{-# OPTIONS_GHC -Wall #-}
12-
module Clickable.Html where
12+
module Clickable.HTML where
1313

1414
import Clickable.Internal
1515
import Clickable.Types
@@ -19,90 +19,90 @@ import Data.Text (Text)
1919
import GHC.Generics (Generic)
2020
import Unsafe.Coerce (unsafeCoerce)
2121

22-
el :: Text -> HtmlM a -> HtmlM a
23-
el tagName child = HtmlM \s e -> do
22+
el :: Text -> HTML a -> HTML a
23+
el tagName child = HTML \s e -> do
2424
e.hte_send $ PushStack $ CreateElement tagName
25-
(r, _) <- child.unHtmlM Nothing e
25+
(r, _) <- child.unHTML Nothing e
2626
e.hte_send PopIns
2727
pure (r, s)
2828
{-# INLINE el #-}
2929

30-
elns :: Text -> Text -> HtmlM a -> HtmlM a
31-
elns ns tagName child = HtmlM \s e -> do
30+
elns :: Text -> Text -> HTML a -> HTML a
31+
elns ns tagName child = HTML \s e -> do
3232
e.hte_send $ PushStack $ CreateElementNS ns tagName
33-
(r, _) <- child.unHtmlM s e
33+
(r, _) <- child.unHTML s e
3434
e.hte_send PopIns
3535
pure (r, s)
3636
{-# INLINE elns #-}
3737

38-
text :: Text -> HtmlM ()
39-
text content = HtmlM \s e -> do
38+
text :: Text -> HTML ()
39+
text content = HTML \s e -> do
4040
e.hte_send $ PushStack $ CreateTextNode content
4141
e.hte_send PopIns
4242
return ((), s)
4343
{-# INLINE text #-}
4444

45-
dynText :: DynVal Text -> HtmlM ()
46-
dynText contentDyn = HtmlM \s e -> do
45+
dynText :: Dynamic Text -> HTML ()
46+
dynText contentDyn = HTML \s e -> do
4747
c <- readVal contentDyn
48-
refId <- newRefId.unClickM e
48+
refId <- newRefId.unJSM e
4949
e.hte_send $ PushStack $ CreateTextNode c
5050
e.hte_send $ AssignRef refId (PeekStack 0)
5151
e.hte_send PopIns
52-
let k nval = ClickM \e' ->
52+
let k nval = JSM \e' ->
5353
e'.hte_send $ UpdateTextNode (Ref refId) nval
54-
(subscribe contentDyn k).unClickM e
54+
(subscribe contentDyn k).unJSM e
5555
pure ((), s)
5656
{-# INLINEABLE dynText #-}
5757

58-
property :: ToValue val => Text -> val -> HtmlM ()
59-
property k v = HtmlM \s e -> do
58+
property :: ToValue val => Text -> val -> HTML ()
59+
property k v = HTML \s e -> do
6060
e.hte_send $ ElementProp (PeekStack 0) k $ toValue v
6161
pure ((), s)
6262
{-# INLINE property #-}
6363

64-
dynProp :: ToValue val => Text -> DynVal val -> HtmlM ()
65-
dynProp propName dynVal = HtmlM \s e -> do
66-
(refId, s') <- saveStackTip.unHtmlM s e
64+
dynProp :: ToValue val => Text -> Dynamic val -> HTML ()
65+
dynProp propName dynVal = HTML \s e -> do
66+
(refId, s') <- saveStackTip.unHTML s e
6767
initVal <- readVal dynVal
6868
e.hte_send $ ElementProp (PeekStack 0) propName $ toValue initVal
69-
let k nval = ClickM \e' ->
69+
let k nval = JSM \e' ->
7070
e'.hte_send $ ElementProp (Ref refId) propName $ toValue nval
71-
unClickM (subscribe dynVal k) e
71+
unJSM (subscribe dynVal k) e
7272
pure ((), s')
7373
{-# INLINE dynProp #-}
7474

75-
attribute :: Text -> Text -> HtmlM ()
76-
attribute k v = HtmlM \s e -> do
75+
attribute :: Text -> Text -> HTML ()
76+
attribute k v = HTML \s e -> do
7777
e.hte_send $ ElementAttr (PeekStack 0) k v
7878
pure ((), s)
7979
{-# INLINE attribute #-}
8080

81-
dynAttr :: Text -> DynVal Text -> HtmlM ()
82-
dynAttr propName dynVal = HtmlM \s e -> do
83-
(refId, s') <- saveStackTip.unHtmlM s e
81+
dynAttr :: Text -> Dynamic Text -> HTML ()
82+
dynAttr propName dynVal = HTML \s e -> do
83+
(refId, s') <- saveStackTip.unHTML s e
8484
initVal <- readVal dynVal
8585
e.hte_send $ ElementAttr (PeekStack 0) propName initVal
86-
let k nval = ClickM \e' ->
86+
let k nval = JSM \e' ->
8787
e'.hte_send $ ElementAttr (Ref refId) propName nval
88-
unClickM (subscribe dynVal k) e
88+
unJSM (subscribe dynVal k) e
8989
pure ((), s')
9090
{-# INLINE dynAttr #-}
9191

92-
toggleClass :: Text -> DynVal Bool -> HtmlM ()
93-
toggleClass className dynEnable = HtmlM \s e -> do
94-
(refId, s') <- saveStackTip.unHtmlM s e
92+
toggleClass :: Text -> Dynamic Bool -> HTML ()
93+
toggleClass className dynEnable = HTML \s e -> do
94+
(refId, s') <- saveStackTip.unHTML s e
9595
v <- readVal dynEnable
96-
let k enable = ClickM \e' -> e'.hte_send
96+
let k enable = JSM \e' -> e'.hte_send
9797
if enable
9898
then ClassListAdd (Ref refId) className
9999
else ClassListRemove (Ref refId) className
100-
unClickM (k v) e
101-
unClickM (subscribe dynEnable k) e
100+
unJSM (k v) e
101+
unJSM (subscribe dynEnable k) e
102102
pure ((), s')
103103
{-# INLINE toggleClass #-}
104104

105-
addEventListener :: FromValue a => (Event a -> Expr) -> (a -> ClickM ()) -> ClickM ()
105+
addEventListener :: FromValue a => (Event a -> Expr) -> (a -> JSM ()) -> JSM ()
106106
addEventListener connectScript k = do
107107
e <- reactive \scope s ->
108108
let k' = local (\e -> e {hte_scope = scope}) . k
@@ -116,13 +116,13 @@ addEventListener connectScript k = do
116116

117117
class IsEventName eventName where
118118
type EventListenerCb eventName :: Type
119-
connectEventName :: EventListenerCb eventName -> ClickM ()
119+
connectEventName :: EventListenerCb eventName -> JSM ()
120120

121-
on :: forall eventName. IsEventName eventName => EventListenerCb eventName -> HtmlM ()
122-
on k = liftC $ connectEventName @eventName k
121+
on :: forall eventName. IsEventName eventName => EventListenerCb eventName -> HTML ()
122+
on k = liftJSM $ connectEventName @eventName k
123123

124124
instance IsEventName "click" where
125-
type EventListenerCb "click" = ClickM ()
125+
type EventListenerCb "click" = JSM ()
126126
connectEventName k = addEventListener
127127
(genericEvent defaultEventListenerOptions "click" (PeekStack 0)) (const k)
128128

@@ -157,21 +157,21 @@ unsafeConnectEvent :: Expr -> UnsafeJavaScript -> Event a -> Expr
157157
unsafeConnectEvent target ujs (Event eid) =
158158
Eval ujs `Apply` [target, Lam (TriggerEvent eid (Arg 0))]
159159

160-
attachHtml :: Expr -> HtmlM a -> ClickM a
161-
attachHtml rootEl contents = ClickM \e -> do
160+
attachHTML :: Expr -> HTML a -> JSM a
161+
attachHTML rootEl contents = JSM \e -> do
162162
e.hte_send $ PushStack rootEl
163-
(r, _) <- contents.unHtmlM Nothing e
163+
(r, _) <- contents.unHTML Nothing e
164164
e.hte_send PopStack
165165
pure r
166166

167-
attachToBody :: HtmlM a -> ClickM a
168-
attachToBody = attachHtml $ Id "document" `Dot` "body"
167+
attachToBody :: HTML a -> JSM a
168+
attachToBody = attachHTML $ Id "document" `Dot` "body"
169169

170-
saveStackTip :: HtmlM RefId
171-
saveStackTip = HtmlM \s e ->
170+
saveStackTip :: HTML RefId
171+
saveStackTip = HTML \s e ->
172172
case s of
173173
Nothing -> do
174-
refId <- newRefId.unClickM e
174+
refId <- newRefId.unJSM e
175175
e.hte_send $ AssignRef refId $ PeekStack 0
176176
return (refId, Just refId)
177177
Just saved ->
@@ -180,3 +180,42 @@ saveStackTip = HtmlM \s e ->
180180
blank :: Applicative m => m ()
181181
blank = pure ()
182182
{-# INLINE blank #-}
183+
184+
data Location = Location {
185+
-- | A string containing the protocol scheme of the URL, including
186+
-- the final ':'
187+
protocol :: Text,
188+
-- | A string containing the domain of the URL.
189+
hostname :: Text,
190+
-- | A string containing the port number of the URL.
191+
port :: Text,
192+
-- | A string containing an initial '/' followed by the path of the
193+
-- URL, not including the query string or fragment.
194+
pathname :: Text,
195+
-- | String containing a '?' followed by the parameters or
196+
-- "querystring" of the URL
197+
search :: Text,
198+
-- | String containing a '#' followed by the fragment identifier
199+
-- of the URL.
200+
hash :: Text
201+
} deriving stock (Show, Eq, Generic)
202+
deriving anyclass (FromValue, ToValue)
203+
204+
-- https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event
205+
popstateEvent :: Event Location -> Expr
206+
popstateEvent (Event eventId) =
207+
Eval
208+
"(function(target, trigger){\n\
209+
\ function listener(){\n\
210+
\ trigger({\n\
211+
\ protocol: location.protocol,\n\
212+
\ hostname: location.hostname,\n\
213+
\ port: location.port,\n\
214+
\ pathname: location.pathname,\n\
215+
\ search: location.search,\n\
216+
\ hash: location.hash\n\
217+
\ });\n\
218+
\ }\n\
219+
\ target.addEventListener('popstate', listener);\n\
220+
\ return () => target.removeEventListener('popstate', listener);\n\
221+
\})" `Apply` [Id "window", Lam (TriggerEvent eventId (Arg 0))]

0 commit comments

Comments
 (0)