on bd3f014 help sign in
42 lines 1.7 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ExplicitForAll #-}
3{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE RecursiveDo #-}
5#ifdef USE_REFLEX_OPTIMIZER
6{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
7#endif
9module Reflex.Widget.Basic where
11import Control.Monad.Fix (MonadFix)
12import Data.Map (Map)
13import Reflex.Class
14import Reflex.Patch.MapWithMove
17-- | Build sortable content in such a way that re-sorting it can cause minimal
18-- disruption to an existing context.
19--
20-- Naively re-sorting a list of images would destroy every image and add them back
21-- in the new order. This framework is able to avoid that by preserving the
22-- identity of each image and simply moving it to the new location.
23--
24-- Example:
25--
26-- > let sortByFst = buttonA $> comparing fst
27-- > sortBySnd = buttonB $> comparing snd
28-- > sortEvent = leftmost [sortByFst, sortBySnd]
29-- > sortableList
30-- > (\k v -> text $ "\n" ++ show k ++ " " ++ v) -- show each element on a new line
31-- > (Map.fromList $ zip [0..] [(3, "a"), (2, "b"), (1, "c")])
32-- > sortEvent
33sortableList :: forall t m k v a. (MonadHold t m, MonadFix m, Adjustable t m, Ord k)
34 => (k -> v -> m a) -- ^ Function to render the content for each key/value pair
35 -> Map k v -- ^ The sortable list with an initial ordering determined by the @Map@ keys in ascending order
36 -> Event t (v -> v -> Ordering) -- ^ An event carrying a sort function for the list
37 -> m (Map k a)
38sortableList f m0 reSortFunc = do
39 rec let reSortPatch = attachWith (flip patchThatSortsMapWith) (currentIncremental m) reSortFunc
40 m <- holdIncremental m0 reSortPatch
41 (results, _) <- mapMapWithAdjustWithMove f m0 reSortPatch
42 pure results