on develop help sign in
Merge remote-tracking branch 'origin/dynamic-writer' into fix-dyn-pattern-match
53d9e1cAli Abrar
README.md+6−6
@@ -1,17 +1,17 @@
1## Reflex-DOM
1## Reflex-DOM
2### A Framework for Functional Reactive Web Apps in Haskell
2### A Framework for Functional Reactive Web Apps in Haskell
3
3
4−Reflex-DOM is a framework for building web applications in Haskell, based on the [Functional Reactive Programming](https://wiki.haskell.org/Functional_Reactive_Programming) library [Reflex](https://github.com/ryantrinkle/reflex).
4+Reflex-DOM is a framework for building web applications in Haskell, based on the [Functional Reactive Programming](https://wiki.haskell.org/Functional_Reactive_Programming) library [Reflex](https://github.com/reflex-frp/reflex).
5
5
6−Comprehensive documentation is still a work in progress, but a [demo](https://github.com/ryantrinkle/try-reflex) is available and an introductory talk is available at [Part 1](https://www.youtube.com/watch?v=mYvkcskJbc4) / [Part 2](https://www.youtube.com/watch?v=3qfc9XFVo2c) ([Slides](https://obsidian.systems/reflex-nyhug/)).
6+Comprehensive documentation is still a work in progress, but a [demo](https://github.com/reflex-frp/reflex-platform) is available and an introductory talk is available at [Part 1](https://www.youtube.com/watch?v=mYvkcskJbc4) / [Part 2](https://www.youtube.com/watch?v=3qfc9XFVo2c) ([Slides](https://obsidian.systems/reflex-nyhug/)).
7
7
8A summary of Reflex-DOM functions is available in the [quick reference](Quickref.md).
8A summary of Reflex-DOM functions is available in the [quick reference](Quickref.md).
9
9
10### Additional resources
10### Additional resources
11−[try reflex](https://github.com/ryantrinkle/try-reflex)
11+[Get started with Reflex](https://github.com/reflex-frp/reflex-platform)
12
12
13−[reddit/r/reflexfrp](http://www.reddit.com/r/reflexfrp)
13+[/r/reflexfrp](https://www.reddit.com/r/reflexfrp)
14
14
15−[reflex-examples](https://github.com/reflex-frp/reflex-examples)
15+[hackage](https://hackage.haskell.org/package/reflex)
16
16
17−irc.freenode.net #reflex-frp
17+[irc.freenode.net #reflex-frp](http://webchat.freenode.net?channels=%23reflex-frp&uio=d4)
default.nix+5−5
@@ -1,9 +1,9 @@
1−{ mkDerivation, dependent-map, ghcjs-dom, lens
1+{ mkDerivation, pkgs, dependent-map, ghcjs-dom, lens
2−, mtl, ref-tf, reflex, safe, text, these
2+, mtl, ref-tf, reflex, text, these
3, transformers, data-default, semigroups, aeson
3, transformers, data-default, semigroups, aeson
4, ghc, webkitgtk3-javascriptcore, exception-transformers
4, ghc, webkitgtk3-javascriptcore, exception-transformers
5−, webkitgtk24x, dependent-sum-template, bifunctors, zenc
5+, webkitgtk24x, dependent-sum-template, bifunctors, bimap
6−, random, raw-strings-qq
6+, raw-strings-qq, zenc, random
7}:
7}:
8
8
9mkDerivation {
9mkDerivation {
@@ -18,7 +18,6 @@ mkDerivation {
18 these
18 these
19 lens
19 lens
20 ghcjs-dom
20 ghcjs-dom
21− safe
22 text
21 text
23 data-default
22 data-default
24 semigroups
23 semigroups
@@ -29,6 +28,7 @@ mkDerivation {
29 bifunctors
28 bifunctors
30 zenc
29 zenc
31 random
30 random
31+ bimap
32 ] ++ (if (ghc.pname or null) == "ghcjs" then [ ] else [ webkitgtk3-javascriptcore raw-strings-qq ]);
32 ] ++ (if (ghc.pname or null) == "ghcjs" then [ ] else [ webkitgtk3-javascriptcore raw-strings-qq ]);
33 pkgconfigDepends = if (ghc.pname or null) == "ghcjs" then [ ] else [ webkitgtk24x webkitgtk3-javascriptcore raw-strings-qq ];
33 pkgconfigDepends = if (ghc.pname or null) == "ghcjs" then [ ] else [ webkitgtk24x webkitgtk3-javascriptcore raw-strings-qq ];
34 license = null;
34 license = null;
reflex-dom.cabal+5−3
@@ -13,8 +13,10 @@ Cabal-version: >=1.9.2
13-- Deal with https://github.com/haskell/cabal/issues/2544 / https://github.com/haskell/cabal/issues/367
13-- Deal with https://github.com/haskell/cabal/issues/2544 / https://github.com/haskell/cabal/issues/367
14extra-source-files: src-ghc/Reflex/Dom/Internal/Foreign.hs
14extra-source-files: src-ghc/Reflex/Dom/Internal/Foreign.hs
15 src-ghc/Reflex/Dom/Xhr/Foreign.hs
15 src-ghc/Reflex/Dom/Xhr/Foreign.hs
16+ src-ghc/Reflex/Dom/WebSocket/Foreign.hs
16 src-ghcjs/Reflex/Dom/Internal/Foreign.hs
17 src-ghcjs/Reflex/Dom/Internal/Foreign.hs
17 src-ghcjs/Reflex/Dom/Xhr/Foreign.hs
18 src-ghcjs/Reflex/Dom/Xhr/Foreign.hs
19+ src-ghcjs/Reflex/Dom/WebSocket/Foreign.hs
18 src/Reflex/Dom/Xhr/ResponseType.hs
20 src/Reflex/Dom/Xhr/ResponseType.hs
19 src/Reflex/Dom/Xhr/Exception.hs
21 src/Reflex/Dom/Xhr/Exception.hs
20
22
@@ -35,7 +37,6 @@ library
35 ghcjs-dom >= 0.2.1 && < 0.3,
37 ghcjs-dom >= 0.2.1 && < 0.3,
36 transformers == 0.3.* || == 0.4.*,
38 transformers == 0.3.* || == 0.4.*,
37 lens >= 4.7 && < 4.14,
39 lens >= 4.7 && < 4.14,
38− safe == 0.3.*,
39 text == 1.2.*,
40 text == 1.2.*,
40 bytestring == 0.10.*,
41 bytestring == 0.10.*,
41 data-default == 0.5.*,
42 data-default == 0.5.*,
@@ -45,7 +46,8 @@ library
45 directory == 1.2.*,
46 directory == 1.2.*,
46 dependent-sum-template >= 0.0.0.4 && < 0.1,
47 dependent-sum-template >= 0.0.0.4 && < 0.1,
47 zenc == 0.1.*,
48 zenc == 0.1.*,
48− bifunctors >= 4.2 && < 6
49+ bifunctors >= 4.2 && < 6,
50+ bimap >= 0.3 && < 0.4
49
51
50 if impl(ghcjs)
52 if impl(ghcjs)
51 hs-source-dirs: src-ghcjs
53 hs-source-dirs: src-ghcjs
@@ -92,4 +94,4 @@ library
92
94
93source-repository head
95source-repository head
94 type: git
96 type: git
95− location: https://github.com/ryantrinkle/reflex
97+ location: https://github.com/reflex-frp/reflex-dom
src/Reflex/Dom.hs+2
@@ -4,6 +4,7 @@ module Reflex.Dom ( module Reflex
4 , module Reflex.Dom.Widget
4 , module Reflex.Dom.Widget
5 , module Reflex.Dom.Xhr
5 , module Reflex.Dom.Xhr
6 , module Reflex.Dom.Time
6 , module Reflex.Dom.Time
7+ , module Reflex.Dom.DynamicWriter
7 , module Reflex.Dom.WebSocket
8 , module Reflex.Dom.WebSocket
8 , module Reflex.Dom.DynamicWriter
9 , module Reflex.Dom.DynamicWriter
9 , module Reflex.Dom.Location
10 , module Reflex.Dom.Location
@@ -15,6 +16,7 @@ import Reflex.Dom.Internal
15import Reflex.Dom.Widget
16import Reflex.Dom.Widget
16import Reflex.Dom.Xhr
17import Reflex.Dom.Xhr
17import Reflex.Dom.Time
18import Reflex.Dom.Time
19+import Reflex.Dom.DynamicWriter
18import Reflex.Dom.WebSocket
20import Reflex.Dom.WebSocket
19import Reflex.Dom.DynamicWriter
21import Reflex.Dom.DynamicWriter
20import Reflex.Dom.Location
22import Reflex.Dom.Location
src/Reflex/Dom/Widget/Basic.hs+6−3
@@ -130,7 +130,7 @@ display a = dynText =<< mapDyn show a
130--TODO: Should this be renamed to 'widgetView' for consistency with 'widgetHold'?
130--TODO: Should this be renamed to 'widgetView' for consistency with 'widgetHold'?
131-- | Given a Dynamic of widget-creating actions, create a widget that is recreated whenever the Dynamic updates.
131-- | Given a Dynamic of widget-creating actions, create a widget that is recreated whenever the Dynamic updates.
132-- The returned Event of widget results occurs when the Dynamic does.
132-- The returned Event of widget results occurs when the Dynamic does.
133−-- Note: Often, the type 'a' is an Event, in which case the return value is an Event-of-Events that would typically be flattened.
133+-- Note: Often, the type 'a' is an Event, in which case the return value is an Event-of-Events that would typically be flattened (via 'switchPromptly').
134dyn :: MonadWidget t m => Dynamic t (m a) -> m (Event t a)
134dyn :: MonadWidget t m => Dynamic t (m a) -> m (Event t a)
135dyn child = do
135dyn child = do
136 postBuild <- getPostBuild
136 postBuild <- getPostBuild
@@ -235,8 +235,8 @@ listHoldWithKey initialVals valsChanged mkChild = do
235 stateRef <- liftIO $ newIORef initialState
235 stateRef <- liftIO $ newIORef initialState
236 children <- holdDyn initialState newChildren
236 children <- holdDyn initialState newChildren
237 tellWidgetOutput =<< mapDyn (fmap snd) children
237 tellWidgetOutput =<< mapDyn (fmap snd) children
238− mpOrig <- getParentNode endPlaceholder
238+ mpOrig <- liftIO $ getParentNode endPlaceholder
239− forM_ mpOrig $ \pOrig -> insertBefore pOrig (Just dfOrig) (Just endPlaceholder)
239+ forM_ mpOrig $ \pOrig -> liftIO $ insertBefore pOrig (Just dfOrig) (Just endPlaceholder)
240 addVoidAction $ flip fmap valsChanged $ \newVals -> do
240 addVoidAction $ flip fmap valsChanged $ \newVals -> do
241 curState <- liftIO $ readIORef stateRef
241 curState <- liftIO $ readIORef stateRef
242 --TODO: Should we remove the parent from the DOM first to avoid reflows?
242 --TODO: Should we remove the parent from the DOM first to avoid reflows?
@@ -827,6 +827,9 @@ workflowView w0 = do
827 eReplace <- liftM switch $ hold never $ fmap snd eResult
827 eReplace <- liftM switch $ hold never $ fmap snd eResult
828 return $ fmap fst eResult
828 return $ fmap fst eResult
829
829
830+mapWorkflow :: (MonadWidget t m) => (a -> b) -> Workflow t m a -> Workflow t m b
831+mapWorkflow f (Workflow x) = Workflow (fmap (\(v,e) -> (f v, fmap (mapWorkflow f) e)) x)
832+
830divClass :: forall t m a. MonadWidget t m => String -> m a -> m a
833divClass :: forall t m a. MonadWidget t m => String -> m a -> m a
831divClass = elClass "div"
834divClass = elClass "div"
832
835
src/Reflex/Dom/Widget/Input.hs+23−18
@@ -16,14 +16,16 @@ import GHCJS.DOM.EventM
16import GHCJS.DOM.File
16import GHCJS.DOM.File
17import qualified GHCJS.DOM.FileList as FileList
17import qualified GHCJS.DOM.FileList as FileList
18import Data.Monoid
18import Data.Monoid
19−import Data.Map as Map
19+import qualified Data.Map as Map
20−import Control.Lens
20+import Control.Lens hiding (ix)
21import Control.Monad hiding (forM_)
21import Control.Monad hiding (forM_)
22import Control.Monad.IO.Class
22import Control.Monad.IO.Class
23+import qualified Data.Bimap as B
23import Data.Default
24import Data.Default
24−import Data.Maybe
25−import Safe
26import Data.Dependent.Sum (DSum (..))
25import Data.Dependent.Sum (DSum (..))
26+import Data.Map (Map)
27+import Data.Maybe
28+import Text.Read
27
29
28data TextInput t
30data TextInput t
29 = TextInput { _textInput_value :: Dynamic t String
31 = TextInput { _textInput_value :: Dynamic t String
@@ -191,26 +193,29 @@ instance (Reflex t, Ord k, Show k, Read k) => Default (DropdownConfig t k) where
191 }
193 }
192
194
193--TODO: We should allow the user to specify an ordering instead of relying on the ordering of the Map
195--TODO: We should allow the user to specify an ordering instead of relying on the ordering of the Map
194−--TODO: Get rid of Show k and Read k by indexing the possible values ourselves
195-- | Create a dropdown box
196-- | Create a dropdown box
196-- The first argument gives the initial value of the dropdown; if it is not present in the map of options provided, it will be added with an empty string as its text
197-- The first argument gives the initial value of the dropdown; if it is not present in the map of options provided, it will be added with an empty string as its text
197−dropdown :: forall k t m. (MonadWidget t m, Ord k, Show k, Read k) => k -> Dynamic t (Map k String) -> DropdownConfig t k -> m (Dropdown t k)
198+dropdown :: forall k t m. (MonadWidget t m, Ord k) => k -> Dynamic t (Map k String) -> DropdownConfig t k -> m (Dropdown t k)
198dropdown k0 options (DropdownConfig setK attrs) = do
199dropdown k0 options (DropdownConfig setK attrs) = do
200+ optionsWithAddedKeys <- combineDyn Map.union options <=< foldDyn Map.union (k0 =: "") $ fmap (=: "") setK
201+ defaultKey <- holdDyn k0 setK
202+ (indexedOptions, ixKeys) <- splitDyn <=< forDyn optionsWithAddedKeys $ \os ->
203+ let xs = map (\(ix, (k, v)) -> ((ix, k), ((ix, k), v))) $ zip [0::Int ..] $ Map.toList os
204+ ixVals = Map.fromList $ map snd xs
205+ ixKeys = B.fromList $ map fst xs
206+ in (ixVals, ixKeys)
199− (eRaw, _) <- elDynAttr' "select" attrs $ do
207+ (eRaw, _) <- elDynAttr' "select" attrs $ listWithKey indexedOptions $ \(ix, k) v -> do
200− optionsWithDefault <- mapDyn (`Map.union` (k0 =: "")) options
201− listWithKey optionsWithDefault $ \k v -> do
202− elAttr "option" ("value" =: show k <> if k == k0 then "selected" =: "selected" else mempty) $ dynText v
208+ optionAttrs <- mapDyn (\dk -> "value" =: show ix <> if dk == k then "selected" =: "selected" else mempty) defaultKey
209+ elDynAttr "option" optionAttrs $ dynText v
203 let e = castToHTMLSelectElement $ _el_element eRaw
210 let e = castToHTMLSelectElement $ _el_element eRaw
204− performEvent_ $ fmap (Select.setValue e . Just . show) setK
211+ performEvent_ $ fmap (Select.setValue e . Just . show) $ attachDynWithMaybe (flip B.lookupR) ixKeys setK
205− eChange <- wrapDomEvent e (`on` change) $ do
212+ eChange <- attachDynWith (\ks s -> join $ B.lookup <$> join (readMaybe <$> s) <*> pure ks) ixKeys <$> (wrapDomEvent e (`on` change) $ Select.getValue e)
206− kStr <- fromMaybe "" <$> Select.getValue e
207− return $ readMay kStr
208− let readKey opts mk = fromMaybe k0 $ do
213+ let readKey keys mk = fromMaybe k0 $ do
209 k <- mk
214 k <- mk
210− guard $ Map.member k opts
215+ guard $ B.memberR k keys
211 return k
216 return k
212− dValue <- combineDyn readKey options =<< holdDyn (Just k0) (leftmost [eChange, fmap Just setK])
217+ dValue <- combineDyn readKey ixKeys <=< holdDyn (Just k0) $ leftmost [eChange, fmap Just setK]
213− return $ Dropdown dValue (attachDynWith readKey options eChange)
218+ return $ Dropdown dValue (attachDynWith readKey ixKeys eChange)
214
219
215liftM concat $ mapM makeLenses
220liftM concat $ mapM makeLenses
216 [ ''TextAreaConfig
221 [ ''TextAreaConfig