@@ -63,6 +63,8 @@ import Reflex.Dom.Builder.Class
63
63
import Language.Javascript.JSaddle
64
64
import Reflex.Dom.Core
65
65
import qualified GHCJS.DOM.Types as DOM
66
+ import qualified GHCJS.DOM.Window as Window
67
+ import qualified GHCJS.DOM as DOM
66
68
import Network.URI
67
69
import Data.Maybe (Maybe (.. ), fromMaybe )
68
70
@@ -187,41 +189,47 @@ routePkgIdxTs pn k0 setIdx opt ddConf = do
187
189
pure dd
188
190
189
191
createRoutePackage :: PkgN -> Set PkgIdxTs -> PkgIdxTs -> Maybe FragRoute
190
- createRoutePackage pn _ (PkgIdxTs 0 ) = Nothing
192
+ createRoutePackage _ _ (PkgIdxTs 0 ) = Nothing
191
193
createRoutePackage pn setIdx pkgIdx
192
194
| Just maxIdx <- Set. lookupMax setIdx
193
195
, True <- maxIdx /= pkgIdx
194
196
= Just $ RoutePackage (pn, Just pkgIdx)
195
197
| otherwise = Just $ RoutePackage (pn, Nothing )
196
198
197
- runRouteViewT :: forall t m a . (TriggerEvent t m , PerformEvent t m , MonadHold t m , MonadJSM m , MonadJSM (Performable m ), MonadFix m )
198
- => (Dynamic t FragRoute -> SetRouteT t FragRoute m a )
199
- -> m a
199
+ switchHistoryItem :: HistoryCommand -> HistoryItem -> HistoryItem
200
+ switchHistoryItem hC hI
201
+ | (HistoryCommand_PushState su) <- hC
202
+ , (Just uri) <- _historyStateUpdate_uri su
203
+ = hI { _historyItem_uri = uri }
204
+ | otherwise = hI
205
+
206
+ runRouteViewT :: forall t m . (Adjustable t m , TriggerEvent t m , PerformEvent t m , MonadHold t m , MonadJSM m , MonadJSM (Performable m ), MonadFix m )
207
+ => (FragRoute -> SetRouteT t FragRoute m () )
208
+ -> m (Dynamic t (Maybe Text ))
200
209
runRouteViewT app = mdo
201
210
historyState <- manageHistory $ HistoryCommand_PushState <$> setState
202
- -- dynLoc <- browserHistoryWith getLocationUri
203
- (result, changeStateE) <- runSetRouteT $ app route -- changeStateE :: Event t (Endo (Fragroute -> FragRoute)
211
+
204
212
let
205
213
dynLoc = _historyItem_uri <$> historyState
206
214
207
215
route :: Dynamic t FragRoute
208
216
route = decodeFrag . T. pack . uriFragment <$> dynLoc
209
217
210
- f (currentHistoryState, oldR) chStateE = -- chState :: Endo (FragRoute -> FragRoute)
211
- let newRoute = encodeFrag $ appEndo chStateE oldR
212
- -- oldRoute = case encodeFrag oldR of
213
- -- (Just a) -> a
214
- -- Nothing -> T.empty
215
- in do
216
- oldRoute <- encodeFrag oldR
217
- newUri <- applyEncoding oldRoute newRoute (_historyItem_uri currentHistoryState)
218
- pure $ HistoryStateUpdate
219
- { _historyStateUpdate_state = DOM. SerializedScriptValue jsNull
220
- , _historyStateUpdate_title = " "
221
- , _historyStateUpdate_uri = Just newUri
222
- }
223
- setState = fmapMaybe id $ attachWith f ( (,) <$> current historyState <*> current route) changeStateE
224
- pure result
218
+ setState = fmapMaybe id $ attachWith switchRoutingState ( (,) <$> current historyState <*> current route) changeStateE
219
+ (result, changeStateE) <- runSetRouteT $ strictDynWidget_ app route
220
+ pure ( encodeFrag <$> route)
221
+
222
+ switchRoutingState :: ( HistoryItem , FragRoute ) -> Endo FragRoute -> Maybe HistoryStateUpdate
223
+ switchRoutingState (currentHS, oldR) chStateE = -- chState :: Endo (FragRoute -> FragRoute)
224
+ let newRoute = encodeFrag $ appEndo chStateE oldR
225
+ in do
226
+ oldRoute <- encodeFrag oldR
227
+ newUri <- applyEncoding oldRoute newRoute (_historyItem_uri currentHS)
228
+ pure $ HistoryStateUpdate
229
+ { _historyStateUpdate_state = DOM. SerializedScriptValue jsNull
230
+ , _historyStateUpdate_title = " "
231
+ , _historyStateUpdate_uri = Just newUri
232
+ }
225
233
226
234
switchPkgRoute :: Maybe FragRoute -> FragRoute -> FragRoute
227
235
switchPkgRoute newFrag oldFrag = fromMaybe oldFrag newFrag
@@ -261,4 +269,10 @@ decodeFrag frag = case frag of
261
269
, T. all (\ c -> C. isAsciiLower c || C. isAsciiUpper c || C. isDigit c || c == ' _' ) sfx
262
270
-> RouteUser sfx
263
271
264
- | otherwise -> RouteUnknown frag
272
+ | otherwise -> RouteUnknown frag
273
+
274
+ strictDynWidget_ :: forall t m . ( MonadSample t m , MonadHold t m , Adjustable t m ) => (FragRoute -> m () ) -> Dynamic t FragRoute -> m ()
275
+ strictDynWidget_ f r = do
276
+ r0 <- sample $ current r
277
+ (_, _) <- runWithReplace (f r0) $ f <$> updated r
278
+ pure ()
0 commit comments