@@ -14,18 +14,16 @@ import qualified Data.Set as Set
14import Data.Dependent.Sum (DSum (..))
14import Data.Dependent.Sum (DSum (..))
15import Control.Monad.Trans
15import Control.Monad.Trans
16import Control.Monad.Reader
16import Control.Monad.Reader
17−import Control.Monad.State
17+import Control.Monad.State hiding (state)
18−import Control.Monad.Ref
19import GHCJS.DOM.Node
18import GHCJS.DOM.Node
20import GHCJS.DOM.UIEvent
19import GHCJS.DOM.UIEvent
21−import GHCJS.DOM.EventM (event, Signal, EventM)
20+import GHCJS.DOM.EventM (event, EventM)
22import GHCJS.DOM.Document
21import GHCJS.DOM.Document
23import GHCJS.DOM.Element
22import GHCJS.DOM.Element
24import GHCJS.DOM.HTMLElement
23import GHCJS.DOM.HTMLElement
25import GHCJS.DOM.Types hiding (Widget (..), unWidget, Event)
24import GHCJS.DOM.Types hiding (Widget (..), unWidget, Event)
26import GHCJS.DOM.NamedNodeMap
25import GHCJS.DOM.NamedNodeMap
27−import Control.Lens
26+import Control.Lens hiding (element, children)
29import Data.These
27import Data.These
30import Data.Align
28import Data.Align
@@ -121,7 +119,7 @@ dyn child = do
121 runFrameWithTriggerRef newChildBuiltTriggerRef (result, voidActions)
119 runFrameWithTriggerRef newChildBuiltTriggerRef (result, voidActions)
122 postBuild
120 postBuild
123 Just p <- liftIO $ nodeGetParentNode endPlaceholder
121 Just p <- liftIO $ nodeGetParentNode endPlaceholder
124− liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
122+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
125 return ()
123 return ()
126 schedulePostBuild $ do
124 schedulePostBuild $ do
127 c <- sample $ current child
125 c <- sample $ current child
@@ -151,7 +149,7 @@ widgetHold child0 newChild = do
151 case mp of
149 case mp of
152 Nothing -> return () --TODO: Is this right?
150 Nothing -> return () --TODO: Is this right?
153 Just p -> do
151 Just p -> do
154− liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
152+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
155 return ()
153 return ()
156 return ()
154 return ()
157 addVoidAction $ ffor newChild $ \c -> do
155 addVoidAction $ ffor newChild $ \c -> do
@@ -163,7 +161,6 @@ widgetHold child0 newChild = do
163listWithKey :: (Ord k, MonadWidget t m) => Dynamic t (Map k v) -> (k -> Dynamic t v -> m a) -> m (Dynamic t (Map k a))
161listWithKey :: (Ord k, MonadWidget t m) => Dynamic t (Map k v) -> (k -> Dynamic t v -> m a) -> m (Dynamic t (Map k a))
164listWithKey vals mkChild = do
162listWithKey vals mkChild = do
165 doc <- askDocument
163 doc <- askDocument
166− startPlaceholder <- text' ""
167 endPlaceholder <- text' ""
164 endPlaceholder <- text' ""
168 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
165 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
169 performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
166 performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
@@ -182,9 +179,9 @@ listWithKey vals mkChild = do
182 (result, postBuild, voidAction) <- buildChild df k v
179 (result, postBuild, voidAction) <- buildChild df k v
183 return ((result, voidAction), postBuild)
180 return ((result, voidAction), postBuild)
184 runFrameWithTriggerRef newChildrenTriggerRef $ fmap fst initialState --TODO: Do all these in a single runFrame
181 runFrameWithTriggerRef newChildrenTriggerRef $ fmap fst initialState --TODO: Do all these in a single runFrame
185− sequence $ fmap snd initialState
182+ sequence_ $ fmap snd initialState
186 Just p <- liftIO $ nodeGetParentNode endPlaceholder
183 Just p <- liftIO $ nodeGetParentNode endPlaceholder
187− liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
184+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
188 return ()
185 return ()
189 addVoidAction $ flip fmap (updated vals) $ \newVals -> do
186 addVoidAction $ flip fmap (updated vals) $ \newVals -> do
190 curState <- sample children
187 curState <- sample children
@@ -202,7 +199,7 @@ listWithKey vals mkChild = do
202 Nothing -> endPlaceholder
199 Nothing -> endPlaceholder
203 Just (_, ((_, (start, _)), _)) -> start
200 Just (_, ((_, (start, _)), _)) -> start
204 Just p <- liftIO $ nodeGetParentNode placeholder
201 Just p <- liftIO $ nodeGetParentNode placeholder
205− liftIO $ nodeInsertBefore p (Just df) (Just placeholder)
202+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just placeholder)
206 return $ Just s
203 return $ Just s
207 These state _ -> do
204 These state _ -> do
208 return $ Just state
205 return $ Just state
@@ -213,7 +210,6 @@ listWithKey vals mkChild = do
213listWithKey' :: forall t m k v a. (Ord k, MonadWidget t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> Event t v -> m a) -> m (Dynamic t (Map k a))
210listWithKey' :: forall t m k v a. (Ord k, MonadWidget t m) => Map k v -> Event t (Map k (Maybe v)) -> (k -> v -> Event t v -> m a) -> m (Dynamic t (Map k a))
214listWithKey' initialVals valsChanged mkChild = do
211listWithKey' initialVals valsChanged mkChild = do
215 doc <- askDocument
212 doc <- askDocument
216− startPlaceholder <- text' ""
217 endPlaceholder <- text' ""
213 endPlaceholder <- text' ""
218 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
214 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
219-- performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
215-- performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
@@ -226,12 +222,12 @@ listWithKey' initialVals valsChanged mkChild = do
226 result <- mkChild k v $ select childValChangedSelector $ Const2 k
222 result <- mkChild k v $ select childValChangedSelector $ Const2 k
227 childEnd <- text' ""
223 childEnd <- text' ""
228 return (result, (childStart, childEnd))
224 return (result, (childStart, childEnd))
229− Just df <- liftIO $ documentCreateDocumentFragment doc
225+ Just dfOrig <- liftIO $ documentCreateDocumentFragment doc
230− initialState <- iforM initialVals $ \k v -> subWidgetWithVoidActions (toNode df) $ wrapChild k v --Note: we have to use subWidgetWithVoidActions rather than runWidget here, because running post-build actions during build can cause not-yet-constructed values to be read
226+ initialState <- iforM initialVals $ \k v -> subWidgetWithVoidActions (toNode dfOrig) $ wrapChild k v --Note: we have to use subWidgetWithVoidActions rather than runWidget here, because running post-build actions during build can cause not-yet-constructed values to be read
231 children <- holdDyn initialState newChildren
227 children <- holdDyn initialState newChildren
232 addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) $ current children
228 addVoidAction $ switch $ fmap (mergeWith (>>) . map snd . Map.elems) $ current children
233− Just p <- liftIO $ nodeGetParentNode endPlaceholder
229+ Just pOrig <- liftIO $ nodeGetParentNode endPlaceholder
234− liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
230+ _ <- liftIO $ nodeInsertBefore pOrig (Just dfOrig) (Just endPlaceholder)
235 addVoidAction $ flip fmap valsChanged $ \newVals -> do
231 addVoidAction $ flip fmap valsChanged $ \newVals -> do
236 curState <- sample $ current children
232 curState <- sample $ current children
237 --TODO: Should we remove the parent from the DOM first to avoid reflows?
233 --TODO: Should we remove the parent from the DOM first to avoid reflows?
@@ -246,7 +242,7 @@ listWithKey' initialVals valsChanged mkChild = do
246 let s = (childResult, childVoidAction)
242 let s = (childResult, childVoidAction)
247 modify (>>childPostBuild)
243 modify (>>childPostBuild)
248 Just p <- liftIO $ nodeGetParentNode end
244 Just p <- liftIO $ nodeGetParentNode end
249− liftIO $ nodeInsertBefore p (Just df) (Just end)
245+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just end)
250 return $ Just s
246 return $ Just s
251 That Nothing -> return Nothing -- Deleting non-existent child
247 That Nothing -> return Nothing -- Deleting non-existent child
252 That (Just v) -> do -- Creating new child
248 That (Just v) -> do -- Creating new child
@@ -258,7 +254,7 @@ listWithKey' initialVals valsChanged mkChild = do
258 Nothing -> endPlaceholder
254 Nothing -> endPlaceholder
259 Just (_, ((_, (start, _)), _)) -> start
255 Just (_, ((_, (start, _)), _)) -> start
260 Just p <- liftIO $ nodeGetParentNode placeholder
256 Just p <- liftIO $ nodeGetParentNode placeholder
261− liftIO $ nodeInsertBefore p (Just df) (Just placeholder)
257+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just placeholder)
262 return $ Just s
258 return $ Just s
263 This state -> do -- No change
259 This state -> do -- No change
264 return $ Just state
260 return $ Just state
@@ -273,7 +269,6 @@ listViewWithKey vals mkChild = liftM (switch . fmap mergeMap) $ listViewWithKey'
273listViewWithKey' :: (Ord k, MonadWidget t m) => Dynamic t (Map k v) -> (k -> Dynamic t v -> m a) -> m (Behavior t (Map k a))
269listViewWithKey' :: (Ord k, MonadWidget t m) => Dynamic t (Map k v) -> (k -> Dynamic t v -> m a) -> m (Behavior t (Map k a))
274listViewWithKey' vals mkChild = do
270listViewWithKey' vals mkChild = do
275 doc <- askDocument
271 doc <- askDocument
276− startPlaceholder <- text' ""
277 endPlaceholder <- text' ""
272 endPlaceholder <- text' ""
278 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
273 (newChildren, newChildrenTriggerRef) <- newEventWithTriggerRef
279 performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
274 performEvent_ $ fmap (const $ return ()) newChildren --TODO: Get rid of this hack
@@ -292,9 +287,9 @@ listViewWithKey' vals mkChild = do
292 (result, postBuild, voidAction) <- buildChild df k v
287 (result, postBuild, voidAction) <- buildChild df k v
293 return ((result, voidAction), postBuild)
288 return ((result, voidAction), postBuild)
294 runFrameWithTriggerRef newChildrenTriggerRef $ fmap fst initialState --TODO: Do all these in a single runFrame
289 runFrameWithTriggerRef newChildrenTriggerRef $ fmap fst initialState --TODO: Do all these in a single runFrame
295− sequence $ fmap snd initialState
290+ sequence_ $ fmap snd initialState
296 Just p <- liftIO $ nodeGetParentNode endPlaceholder
291 Just p <- liftIO $ nodeGetParentNode endPlaceholder
297− liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
292+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just endPlaceholder)
298 return ()
293 return ()
299 addVoidAction $ flip fmap (updated vals) $ \newVals -> do
294 addVoidAction $ flip fmap (updated vals) $ \newVals -> do
300 curState <- sample children
295 curState <- sample children
@@ -312,7 +307,7 @@ listViewWithKey' vals mkChild = do
312 Nothing -> endPlaceholder
307 Nothing -> endPlaceholder
313 Just (_, ((_, (start, _)), _)) -> start
308 Just (_, ((_, (start, _)), _)) -> start
314 Just p <- liftIO $ nodeGetParentNode placeholder
309 Just p <- liftIO $ nodeGetParentNode placeholder
315− liftIO $ nodeInsertBefore p (Just df) (Just placeholder)
310+ _ <- liftIO $ nodeInsertBefore p (Just df) (Just placeholder)
316 return $ Just s
311 return $ Just s
317 These state _ -> do
312 These state _ -> do
318 return $ Just state
313 return $ Just state
@@ -336,6 +331,7 @@ selectViewListWithKey_ selection vals mkChild = do
336--------------------------------------------------------------------------------
331--------------------------------------------------------------------------------
338-- | s and e must both be children of the same node and s must precede e
333-- | s and e must both be children of the same node and s must precede e
334+deleteBetweenExclusive :: (IsNode start, IsNode end) => start -> end -> IO ()
339deleteBetweenExclusive s e = do
335deleteBetweenExclusive s e = do
340 mCurrentParent <- nodeGetParentNode e -- May be different than it was at initial construction, e.g., because the parent may have dumped us in from a DocumentFragment
336 mCurrentParent <- nodeGetParentNode e -- May be different than it was at initial construction, e.g., because the parent may have dumped us in from a DocumentFragment
341 case mCurrentParent of
337 case mCurrentParent of
@@ -344,19 +340,21 @@ deleteBetweenExclusive s e = do
344 let go = do
340 let go = do
345 Just x <- nodeGetPreviousSibling e -- This can't be Nothing because we should hit 's' first
341 Just x <- nodeGetPreviousSibling e -- This can't be Nothing because we should hit 's' first
346 when (unNode (toNode s) /= unNode (toNode x)) $ do
342 when (unNode (toNode s) /= unNode (toNode x)) $ do
347− nodeRemoveChild currentParent $ Just x
343+ _ <- nodeRemoveChild currentParent $ Just x
351-- | s and e must both be children of the same node and s must precede e
347-- | s and e must both be children of the same node and s must precede e
348+deleteBetweenInclusive :: (IsNode start, IsNode end) => start -> end -> IO ()
352deleteBetweenInclusive s e = do
349deleteBetweenInclusive s e = do
353 Just currentParent <- nodeGetParentNode e -- May be different than it was at initial construction, e.g., because the parent may have dumped us in from a DocumentFragment
350 Just currentParent <- nodeGetParentNode e -- May be different than it was at initial construction, e.g., because the parent may have dumped us in from a DocumentFragment
354 let go = do
351 let go = do
355 Just x <- nodeGetPreviousSibling e -- This can't be Nothing because we should hit 's' first
352 Just x <- nodeGetPreviousSibling e -- This can't be Nothing because we should hit 's' first
356− nodeRemoveChild currentParent $ Just x
353+ _ <- nodeRemoveChild currentParent $ Just x
357 when (unNode (toNode s) /= unNode (toNode x)) go
354 when (unNode (toNode s) /= unNode (toNode x)) go
359− nodeRemoveChild currentParent $ Just e
356+ _ <- nodeRemoveChild currentParent $ Just e
361--------------------------------------------------------------------------------
359--------------------------------------------------------------------------------
362-- Adapters
360-- Adapters
@@ -398,7 +396,7 @@ elAttr elementTag attrs child = do
399{-# INLINABLE el' #-}
397{-# INLINABLE el' #-}
400el' :: forall t m a. MonadWidget t m => String -> m a -> m (El t, a)
398el' :: forall t m a. MonadWidget t m => String -> m a -> m (El t, a)
401−el' tag child = elAttr' tag (Map.empty :: AttributeMap) child
399+el' elementTag child = elAttr' elementTag (Map.empty :: AttributeMap) child
403{-# INLINABLE elAttr' #-}
401{-# INLINABLE elAttr' #-}
404elAttr' :: forall t m a. MonadWidget t m => String -> Map String String -> m a -> m (El t, a)
402elAttr' :: forall t m a. MonadWidget t m => String -> Map String String -> m a -> m (El t, a)
@@ -415,15 +413,16 @@ elDynAttr elementTag attrs child = do
416{-# INLINABLE el #-}
414{-# INLINABLE el #-}
417el :: forall t m a. MonadWidget t m => String -> m a -> m a
415el :: forall t m a. MonadWidget t m => String -> m a -> m a
418−el tag child = elAttr tag Map.empty child
416+el elementTag child = elAttr elementTag Map.empty child
420elClass :: forall t m a. MonadWidget t m => String -> String -> m a -> m a
418elClass :: forall t m a. MonadWidget t m => String -> String -> m a -> m a
421−elClass tag c child = elAttr tag ("class" =: c) child
419+elClass elementTag c child = elAttr elementTag ("class" =: c) child
423--------------------------------------------------------------------------------
421--------------------------------------------------------------------------------
424-- Copied and pasted from Reflex.Widget.Class
422-- Copied and pasted from Reflex.Widget.Class
425--------------------------------------------------------------------------------
423--------------------------------------------------------------------------------
425+list :: (MonadWidget t m, Ord k) => Dynamic t (Map k v) -> (Dynamic t v -> m a) -> m (Dynamic t (Map k a))
427list dm mkChild = listWithKey dm (\_ dv -> mkChild dv)
426list dm mkChild = listWithKey dm (\_ dv -> mkChild dv)
429elDynHtml' :: MonadWidget t m => String -> Dynamic t String -> m (El t)
428elDynHtml' :: MonadWidget t m => String -> Dynamic t String -> m (El t)
@@ -492,7 +491,7 @@ tableDynAttr :: forall t m r k v. (MonadWidget t m, Show k, Ord k) => String ->
492tableDynAttr klass cols dRows rowAttrs = elAttr "div" (Map.singleton "style" "zoom: 1; overflow: auto; background: white;") $ do
491tableDynAttr klass cols dRows rowAttrs = elAttr "div" (Map.singleton "style" "zoom: 1; overflow: auto; background: white;") $ do
493 elAttr "table" (Map.singleton "class" klass) $ do
492 elAttr "table" (Map.singleton "class" klass) $ do
494 el "thead" $ el "tr" $ do
493 el "thead" $ el "tr" $ do
495− mapM (\(h, _) -> el "th" $ text h) cols
494+ mapM_ (\(h, _) -> el "th" $ text h) cols
496 el "tbody" $ do
495 el "tbody" $ do
497 listWithKey dRows (\k r -> do
496 listWithKey dRows (\k r -> do
498 dAttrs <- rowAttrs k
497 dAttrs <- rowAttrs k
@@ -508,7 +507,7 @@ tabDisplay ulClass activeClass tabItems = do
508 holdDyn Nothing $ fmap Just eTabClicks :: m (Dynamic t (Maybe k))
507 holdDyn Nothing $ fmap Just eTabClicks :: m (Dynamic t (Maybe k))
509 divClass "" $ do
508 divClass "" $ do
510 let dTabs :: Dynamic t (Map k (String, m ())) = constDyn tabItems
509 let dTabs :: Dynamic t (Map k (String, m ())) = constDyn tabItems
511− listWithKey dTabs (\k dTab -> do
510+ _ <- listWithKey dTabs (\k dTab -> do
512 dAttrs <- mapDyn (\sel -> do
511 dAttrs <- mapDyn (\sel -> do
513 let t1 = listToMaybe $ Map.keys tabItems
512 let t1 = listToMaybe $ Map.keys tabItems
514 if sel == Just k || (sel == Nothing && t1 == Just k) then Map.empty else Map.singleton "style" "display:none;") dCurrentTab
513 if sel == Just k || (sel == Nothing && t1 == Just k) then Map.empty else Map.singleton "style" "display:none;") dCurrentTab
@@ -526,5 +525,5 @@ tabDisplay ulClass activeClass tabItems = do
526unsafePlaceElement :: MonadWidget t m => HTMLElement -> m (El t)
525unsafePlaceElement :: MonadWidget t m => HTMLElement -> m (El t)
527unsafePlaceElement e = do
526unsafePlaceElement e = do
528 p <- askParent
527 p <- askParent
529− liftIO $ nodeAppendChild p $ Just e
528+ _ <- liftIO $ nodeAppendChild p $ Just e
530 wrapElement e
529 wrapElement e