@@ -1,5 +1,4 @@
1−{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, LambdaCase, ConstraintKinds, TypeFamilies, FlexibleContexts, MultiParamTypeClasses, FlexibleInstances, RecursiveDo, GADTs, DataKinds, RankNTypes, TemplateHaskell #-}
2−
1+{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, LambdaCase, ConstraintKinds, TypeFamilies, FlexibleContexts, MultiParamTypeClasses, FlexibleInstances, RecursiveDo, GADTs, DataKinds, RankNTypes #-}
32module Reflex.Dom.Widget.Basic where
43
54import Reflex.Dom.Class
@@ -16,6 +15,7 @@ import Data.Set (Set)
1615import qualified Data.Set as Set
1716import Data.Foldable
1817import Data.Traversable
18+import Control.Arrow
1919import Control.Monad.Reader hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
2020import Control.Lens hiding (element, children)
2121import Data.These
@@ -65,12 +65,12 @@ listHoldWithKey initialChildren modifyChildren buildChild = do
6565 & deleteSelf .~ delete
6666 ph <- placeholder myCfg
6767 result <- deletable delete $ buildChild k v
68− return $ ChildResult (result, (fmap (fmap Just) $ _placeholder_insertedAbove ph) <> (Map.singleton k Nothing <$ _placeholder_deletedSelf ph)) --Note: we could also use the "deleted" output on deletable, if it had one; we're using this so that everything changes all at once, instead of deletions being prompt and insertions being delayed
68+ return $ ChildResult (result, (fmap Just <$> _placeholder_insertedAbove ph) <> (Map.singleton k Nothing <$ _placeholder_deletedSelf ph)) --Note: we could also use the "deleted" output on deletable, if it had one; we're using this so that everything changes all at once, instead of deletions being prompt and insertions being delayed
6969 rec initialAugmentedResults <- iforM initialChildren buildAugmentedChild
7070 augmentedResults <- foldDyn applyMap initialAugmentedResults $ newInsertedBelow <> newInsertedAbove
71− let newInsertedAbove = switch $ fmap (mconcat . reverse . fmap (snd . unChildResult) . Map.elems) $ current augmentedResults
71+ let newInsertedAbove = switch $ mconcat . reverse . fmap (snd . unChildResult) . Map.elems <$> current augmentedResults
7272 belowAll <- placeholder $ def & placeholderConfig_insertAbove .~ fmap (imapM buildAugmentedChild) (select placeChildSelector $ Const2 $ Right ())
73− let newInsertedBelow = fmap (fmap Just) $ _placeholder_insertedAbove belowAll
73+ let newInsertedBelow = fmap Just <$> _placeholder_insertedAbove belowAll
7474 mapDyn (fmap (fst . unChildResult)) augmentedResults
7575
7676text :: DomBuilder t m => Text -> m ()
@@ -101,7 +101,7 @@ dyn :: (DomBuilder t m, PostBuild t m) => Dynamic t (m a) -> m (Event t a)
101101dyn child = do
102102 postBuild <- getPostBuild
103103 let newChild = leftmost [updated child, tag (current child) postBuild]
104− liftM snd $ widgetHoldInternal (return ()) newChild
104+ snd <$> widgetHoldInternal (return ()) newChild
105105
106106-- | Given an initial widget and an Event of widget-creating actions, create a widget that is recreated whenever the Event fires.
107107-- The returned Dynamic of widget results occurs when the Event does.
@@ -167,7 +167,7 @@ listWithKey' = listWithKeyShallowDiff
167167listWithKeyShallowDiff :: (Ord k, DomBuilder t m, MonadFix m, MonadHold t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> Event t v -> m a) -> m (Dynamic t (Map k a))
168168listWithKeyShallowDiff initialVals valsChanged mkChild = do
169169 let childValChangedSelector = fanMap $ fmap (Map.mapMaybe id) valsChanged
170− sentVals <- foldDyn applyMap Map.empty $ fmap (fmap (fmap (\_ -> ()))) valsChanged
170+ sentVals <- foldDyn applyMap Map.empty $ fmap (fmap void) valsChanged
171171 let relevantPatch patch _ = case patch of
172172 Nothing -> Just Nothing -- Even if we let a Nothing through when the element doesn't already exist, this doesn't cause a problem because it is ignored
173173 Just _ -> Nothing -- We don't want to let spurious re-creations of items through
@@ -178,10 +178,10 @@ listWithKeyShallowDiff initialVals valsChanged mkChild = do
178178-- | Create a dynamically-changing set of Event-valued widgets.
179179-- This is like listWithKey, specialized for widgets returning (Event t a). listWithKey would return 'Dynamic t (Map k (Event t a))' in this scenario, but listViewWithKey flattens this to 'Event t (Map k a)' via 'switch'.
180180listViewWithKey :: (Ord k, DomBuilder t m, PostBuild t m, MonadHold t m, MonadFix m) => Dynamic t (Map k v) -> (k -> Dynamic t v -> m (Event t a)) -> m (Event t (Map k a))
181−listViewWithKey vals mkChild = liftM (switch . fmap mergeMap) $ listViewWithKey' vals mkChild
181+listViewWithKey vals mkChild = switch . fmap mergeMap <$> listViewWithKey' vals mkChild
182182
183183listViewWithKey' :: (Ord k, DomBuilder t m, PostBuild t m, MonadHold t m, MonadFix m) => Dynamic t (Map k v) -> (k -> Dynamic t v -> m a) -> m (Behavior t (Map k a))
184−listViewWithKey' vals mkChild = liftM current $ listWithKey vals mkChild
184+listViewWithKey' vals mkChild = current <$> listWithKey vals mkChild
185185
186186-- | Create a dynamically-changing set of widgets, one of which is selected at any time.
187187selectViewListWithKey :: forall t m k v a. (DomBuilder t m, Ord k, PostBuild t m, MonadHold t m, MonadFix m)
@@ -195,14 +195,14 @@ selectViewListWithKey selection vals mkChild = do
195195 selected <- getDemuxed selectionDemux k
196196 selectSelf <- mkChild k v selected
197197 return $ fmap ((,) k) selectSelf
198− liftM switchPromptlyDyn $ mapDyn (leftmost . Map.elems) selectChild
198+ return $ switchPromptlyDyn $ leftmost . Map.elems <$> selectChild
199199
200200selectViewListWithKey_ :: forall t m k v a. (DomBuilder t m, Ord k, PostBuild t m, MonadHold t m, MonadFix m)
201201 => Dynamic t k -- ^ Current selection key
202202 -> Dynamic t (Map k v) -- ^ Dynamic key/value map
203203 -> (k -> Dynamic t v -> Dynamic t Bool -> m (Event t a)) -- ^ Function to create a widget for a given key from Dynamic value and Dynamic Bool indicating if this widget is currently selected
204204 -> m (Event t k) -- ^ Event that fires when any child's return Event fires. Contains key of an arbitrary firing widget.
205−selectViewListWithKey_ selection vals mkChild = liftM (fmap fst) $ selectViewListWithKey selection vals mkChild
205+selectViewListWithKey_ selection vals mkChild = fmap fst <$> selectViewListWithKey selection vals mkChild
206206
207207{-
208208{-# INLINABLE elWith #-}
@@ -265,7 +265,7 @@ el' elementTag = element elementTag def
265265{-# INLINABLE elAttr' #-}
266266elAttr' :: forall t m a. DomBuilder t m => Text -> Map Text Text -> m a -> m (Element EventResult (DomBuilderSpace m) t, a)
267267elAttr' elementTag attrs = element elementTag $ def
268− & initialAttributes .~ (Map.fromList $ fmap (\(k, v) -> ((Nothing, k), v)) $ Map.toList $ attrs)
268+ & initialAttributes .~ Map.mapKeys (\k -> (Nothing, k)) attrs
269269
270270{-# INLINABLE elDynAttr #-}
271271elDynAttr :: forall t m a. (DomBuilder t m, PostBuild t m) => Text -> Dynamic t (Map Text Text) -> m a -> m a
@@ -276,7 +276,7 @@ el :: forall t m a. DomBuilder t m => Text -> m a -> m a
276276el elementTag child = snd <$> el' elementTag child
277277
278278elClass :: forall t m a. DomBuilder t m => Text -> Text -> m a -> m a
279−elClass elementTag c child = fmap snd $ element elementTag (def & initialAttributes .~ (Nothing, "class") =: c) child
279+elClass elementTag c child = snd <$> element elementTag (def & initialAttributes .~ (Nothing, "class") =: c) child
280280
281281--------------------------------------------------------------------------------
282282-- Copied and pasted from Reflex.Widget.Class
@@ -325,17 +325,17 @@ newtype Workflow t m a = Workflow { unWorkflow :: m (a, Event t (Workflow t m a)
325325
326326workflow :: forall t m a. (DomBuilder t m, MonadFix m, MonadHold t m) => Workflow t m a -> m (Dynamic t a)
327327workflow w0 = do
328− rec eResult <- widgetHold (unWorkflow w0) $ fmap unWorkflow $ switch $ fmap snd $ current eResult
329− mapDyn fst eResult
328+ rec eResult <- widgetHold (unWorkflow w0) $ fmap unWorkflow $ switch $ snd <$> current eResult
329+ return $ fmap fst eResult
330330
331331workflowView :: forall t m a. (DomBuilder t m, MonadFix m, MonadHold t m, PostBuild t m) => Workflow t m a -> m (Event t a)
332332workflowView w0 = do
333− rec eResult <- dyn =<< mapDyn unWorkflow =<< holdDyn w0 eReplace
334− eReplace <- liftM switch $ hold never $ fmap snd eResult
333+ rec eResult <- dyn . fmap unWorkflow =<< holdDyn w0 eReplace
334+ eReplace <- fmap switch $ hold never $ fmap snd eResult
335335 return $ fmap fst eResult
336336
337337mapWorkflow :: (DomBuilder t m) => (a -> b) -> Workflow t m a -> Workflow t m b
338−mapWorkflow f (Workflow x) = Workflow (fmap (\(v,e) -> (f v, fmap (mapWorkflow f) e)) x)
338+mapWorkflow f (Workflow x) = Workflow (fmap (f *** fmap (mapWorkflow f)) x)
339339
340340divClass :: forall t m a. DomBuilder t m => Text -> m a -> m a
341341divClass = elClass "div"
@@ -376,9 +376,9 @@ tabDisplay :: forall t m k. (MonadFix m, DomBuilder t m, MonadHold t m, PostBuil
376376tabDisplay ulClass activeClass tabItems = do
377377 let t0 = listToMaybe $ Map.keys tabItems
378378 rec currentTab :: Demux t (Maybe k) <- elAttr "ul" ("class" =: ulClass) $ do
379− tabClicksList :: [Event t k] <- (liftM Map.elems) $ imapM (\k (s,_) -> headerBarLink s k =<< getDemuxed currentTab (Just k)) tabItems
379+ tabClicksList :: [Event t k] <- Map.elems <$> imapM (\k (s,_) -> headerBarLink s k =<< getDemuxed currentTab (Just k)) tabItems
380380 let eTabClicks :: Event t k = leftmost tabClicksList
381− liftM demux $ holdDyn t0 $ fmap Just eTabClicks
381+ fmap demux $ holdDyn t0 $ fmap Just eTabClicks
382382 el "div" $ do
383383 iforM_ tabItems $ \k (_, w) -> do
384384 isSelected <- getDemuxed currentTab $ Just k