From ed1df9173bdddbcaa677f8cc05134bca5d117a35 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 25 Feb 2016 01:44:56 +0000 Subject: Fixed draft loading --- webgui/src/Main.hs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/webgui/src/Main.hs b/webgui/src/Main.hs index be2a0a1..4f2c83c 100644 --- a/webgui/src/Main.hs +++ b/webgui/src/Main.hs @@ -195,7 +195,7 @@ setup Config{..} window (split -> (socketErr, dataUpdate)) = void $ do on UI.click printButton $ const printAction on UI.click discardButton $ const discardAction - return changeEditorStatus + return modifyStatus saveable s@EditorState{..} = isRight ePrintout && discardable s printable EditorState{..} = isRight ePrintout && not (null eText) @@ -208,7 +208,7 @@ setup Config{..} window (split -> (socketErr, dataUpdate)) = void $ do deletion <- stepper deletion' $ UI.checkedChange allowDeletion let toTable :: Map DraftId (Maybe DraftTitle) -> UI [Element] - toTable = mapM toLine . Map.toList + toTable = mapM toLine . Map.toList toLine (id@(DraftId (show -> tId)), fromMaybe "" . fmap T.unpack -> title) = do id' <- UI.td # set TP.text tId @@ -216,7 +216,7 @@ setup Config{..} window (split -> (socketErr, dataUpdate)) = void $ do delete <- UI.button # set TP.text "Delete" # sink UI.enabled deletion - on UI.click delete . const $ draftDelete id + on UI.click delete . const $ draftDelete id >> changeEditorState (\s@(EditorState{..}) -> if associatedDraft == Just id then def else s) load <- UI.button # set TP.text "Load" on UI.click load . const $ loadDraft id @@ -224,15 +224,21 @@ setup Config{..} window (split -> (socketErr, dataUpdate)) = void $ do UI.tr # set children [id', title', actions] loadDraft id = do (title, po) <- draft id - time <- liftIO $ getCurrentTime - let newState = def - { eTitle = fmap T.unpack title - , eText = "unimplemented" -- TODO: implement -- drops data as it is - , ePrintout = Right po - , associatedDraft = Just id - , lastSaved = Just (time, newState) - } - changeEditorState newState + let + t = cobbcode po + case t of + Left (show -> errStr) -> emitError $ "Could not load draft: " ++ errStr + Right text -> do + time <- liftIO $ getCurrentTime + let newState = def + { eTitle = fmap T.unpack title + , eText = T.unpack text + , ePrintout = Right po + , associatedDraft = Just id + , lastSaved = Just (time, newState) + } + changeEditorState (const newState) + runFunction $ switchTab "editor" table <- fatal' "Could not find draft table" =<< getElementById window "draftListBody" initialContent <- toTable =<< drafts return table # set children initialContent @@ -248,7 +254,9 @@ setup Config{..} window (split -> (socketErr, dataUpdate)) = void $ do | p == "drafts" = True | "draft/" `isPrefixOf` p = True | otherwise = False - + +switchTab :: String -> JSFunction () +switchTab = ffi "$(%1).first().trigger(\"click\")" . (\p -> "a[href='#" ++ p ++ "']") data EditorState = EditorState { eTitle :: Maybe String -- cgit v1.2.3