diff --git a/src/JS.hs b/src/JS.hs index a35bd54..2713e8b 100644 --- a/src/JS.hs +++ b/src/JS.hs @@ -34,6 +34,7 @@ allJSFunctions = JS . T.unlines . map fromJS $ [ -- Utilities replaceWithData, prependData, appendData, moveNodeUp, moveNodeDown, + switchSection, -- Help showOrHideHelp, showHelp, hideHelp, -- Search @@ -160,13 +161,24 @@ moveNodeDown = el.next().after(el); |] +-- TODO: document the way hiding/showing works + +-- | Given something that contains section divs (or spans), show one and +-- hide the rest. The div/span with the given @class@ will be chosen. +switchSection :: JSFunction a => a +switchSection = + makeJSFunction "switchSection" ["node", "section"] + [text| + $(node).children(".section").removeClass("shown"); + $(node).children(".section."+section).addClass("shown"); + |] + showHelp :: JSFunction a => a showHelp = makeJSFunction "showHelp" ["node", "version"] [text| localStorage.removeItem("help-hidden-"+version); - $.get("/render/help", {mode: "shown"}) - .done(replaceWithData(node)); + switchSection(node, "expanded"); |] hideHelp :: JSFunction a => a @@ -174,20 +186,19 @@ hideHelp = makeJSFunction "hideHelp" ["node", "version"] [text| localStorage.setItem("help-hidden-"+version, ""); - $.get("/render/help", {mode: "hidden"}) - .done(replaceWithData(node)); + switchSection(node, "collapsed"); |] +-- TODO: find a better name for this (to distinguish it from 'showHelp' and +-- 'hideHelp') showOrHideHelp :: JSFunction a => a showOrHideHelp = makeJSFunction "showOrHideHelp" ["node", "version"] [text| if (localStorage.getItem("help-hidden-"+version) === null) - $.get("/render/help", {mode: "shown"}) - .done(replaceWithData(node)) + showHelp(node, version) else - $.get("/render/help", {mode: "hidden"}) - .done(replaceWithData(node)); + hideHelp(node, version); |] search :: JSFunction a => a diff --git a/src/Main.hs b/src/Main.hs index 41c7f49..636b836 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -636,10 +636,6 @@ traitVar = "trait" var renderMethods :: SpockM () () DB () renderMethods = Spock.subcomponent "render" $ do - -- Help - Spock.get "help" $ do - visible <- param' "mode" - lucid $ renderHelp visible -- Title of a category Spock.get (categoryVar "title") $ \catId -> do category <- dbQuery (GetCategory catId) @@ -839,23 +835,31 @@ renderRoot globalState = do -- this file. (This isn't an actual file, so don't look for it in the -- static folder – it's generated and served in 'otherMethods'.) includeJS "/js.js" - h1_ "Collaborative notes on Haskell libraries and tools" - -- By default help is rendered hidden, and then showOrHideHelp reads a - -- value from local storage and decides whether to show help or not. On one - -- hand, it means that people with Javascript turned off won't be able to - -- see help; on another hand, those people don't need help anyway because - -- they won't be able to edit anything either. - renderHelp Hidden - onPageLoad $ JS.showOrHideHelp (selectId "help", helpVersion) - noscript_ $ renderMarkdownBlock [text| - You have Javascript disabled! This site works fine without Javascript, - but since all editing needs Javascript to work, you won't be able to edit - anything. Also, show/hide buttons need Javascript too, so you won't be - able to see the notes for libraries (which I should really fix by making - them shown by default and *then* hiding them with Javascript). Also also, - search doesn't work without Javascript either (another thing I should - really fix – sorry!). + -- CSS that makes 'shown' and 'noScriptShown' work + noscript_ $ style_ [text| + .section:not(.noscript-shown) {display:none;} |] + script_ [text| + var sheet = document.createElement('style'); + sheet.innerHTML = '.section:not(.shown) {display:none;}'; + // “head” instead of “body” because body isn't loaded yet + document.head.appendChild(sheet); + |] + -- Okay, here goes the actual page + -- TODO: this header looks bad when the page is narrow + h1_ "Collaborative notes on Haskell libraries and tools" + noscript_ $ div_ [id_ "noscript-message"] $ + renderMarkdownBlock [text| + You have Javascript disabled! This site works fine without Javascript, + but since all editing needs Javascript to work, you won't be able to edit + anything. Also, show/hide buttons need Javascript too, so you won't be + able to see the notes for libraries (which I should really fix by making + them shown by default and *then* hiding them with Javascript). Also also, + search doesn't work without Javascript either (another thing I should + really fix – sorry!). + |] + renderHelp + onPageLoad $ JS.showOrHideHelp (selectId "help", helpVersion) -- TODO: show notes when Javascript is disabled – perhaps by hiding them -- by default but putting a “show everything” CSS into a