1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-23 12:15:06 +03:00

Move everything to the “/haskell” subpath

This commit is contained in:
Artyom 2016-03-20 02:36:16 +03:00
parent 59294cc321
commit eb0e13b94e
3 changed files with 76 additions and 58 deletions

View File

@ -118,6 +118,8 @@ instance JSParams a => JSFunction (a -> JS) where
assign :: ToJS x => JS -> x -> JS assign :: ToJS x => JS -> x -> JS
assign v x = JS $ format "{} = {};" (v, toJS x) assign v x = JS $ format "{} = {};" (v, toJS x)
-- TODO: all links here shouldn't be absolute [absolute-links]
replaceWithData :: JSFunction a => a replaceWithData :: JSFunction a => a
replaceWithData = replaceWithData =
makeJSFunction "replaceWithData" ["node"] makeJSFunction "replaceWithData" ["node"]
@ -224,7 +226,7 @@ addCategory :: JSFunction a => a
addCategory = addCategory =
makeJSFunction "addCategory" ["node", "s"] makeJSFunction "addCategory" ["node", "s"]
[text| [text|
$.post("/add/category", {content: s}) $.post("/haskell/add/category", {content: s})
.done(prependData(node)); .done(prependData(node));
|] |]
@ -233,7 +235,7 @@ addItem :: JSFunction a => a
addItem = addItem =
makeJSFunction "addItem" ["node", "catId", "s"] makeJSFunction "addItem" ["node", "catId", "s"]
[text| [text|
$.post("/add/category/"+catId+"/item", {name: s}) $.post("/haskell/add/category/"+catId+"/item", {name: s})
.done(appendData(node)); .done(appendData(node));
|] |]
@ -246,7 +248,7 @@ submitCategoryTitle :: JSFunction a => a
submitCategoryTitle = submitCategoryTitle =
makeJSFunction "submitCategoryTitle" ["node", "catId", "s"] makeJSFunction "submitCategoryTitle" ["node", "catId", "s"]
[text| [text|
$.post("/set/category/"+catId+"/title", {content: s}) $.post("/haskell/set/category/"+catId+"/title", {content: s})
.done(replaceWithData(node)); .done(replaceWithData(node));
|] |]
@ -254,7 +256,7 @@ submitCategoryNotes :: JSFunction a => a
submitCategoryNotes = submitCategoryNotes =
makeJSFunction "submitCategoryNotes" ["node", "catId", "s"] makeJSFunction "submitCategoryNotes" ["node", "catId", "s"]
[text| [text|
$.post("/set/category/"+catId+"/notes", {content: s}) $.post("/haskell/set/category/"+catId+"/notes", {content: s})
.done(replaceWithData(node)); .done(replaceWithData(node));
|] |]
@ -262,7 +264,7 @@ submitItemDescription :: JSFunction a => a
submitItemDescription = submitItemDescription =
makeJSFunction "submitItemDescription" ["node", "itemId", "s"] makeJSFunction "submitItemDescription" ["node", "itemId", "s"]
[text| [text|
$.post("/set/item/"+itemId+"/description", {content: s}) $.post("/haskell/set/item/"+itemId+"/description", {content: s})
.done(replaceWithData(node)); .done(replaceWithData(node));
|] |]
@ -270,7 +272,7 @@ submitItemEcosystem :: JSFunction a => a
submitItemEcosystem = submitItemEcosystem =
makeJSFunction "submitItemEcosystem" ["node", "itemId", "s"] makeJSFunction "submitItemEcosystem" ["node", "itemId", "s"]
[text| [text|
$.post("/set/item/"+itemId+"/ecosystem", {content: s}) $.post("/haskell/set/item/"+itemId+"/ecosystem", {content: s})
.done(replaceWithData(node)); .done(replaceWithData(node));
|] |]
@ -278,7 +280,7 @@ submitItemNotes :: JSFunction a => a
submitItemNotes = submitItemNotes =
makeJSFunction "submitItemNotes" ["node", "itemId", "s"] makeJSFunction "submitItemNotes" ["node", "itemId", "s"]
[text| [text|
$.post("/set/item/"+itemId+"/notes", {content: s}) $.post("/haskell/set/item/"+itemId+"/notes", {content: s})
.done(function (data) { .done(function (data) {
$(node).replaceWith(data); $(node).replaceWith(data);
switchSection(node, "expanded"); switchSection(node, "expanded");
@ -293,7 +295,7 @@ addPro :: JSFunction a => a
addPro = addPro =
makeJSFunction "addPro" ["node", "itemId", "s"] makeJSFunction "addPro" ["node", "itemId", "s"]
[text| [text|
$.post("/add/item/"+itemId+"/pro", {content: s}) $.post("/haskell/add/item/"+itemId+"/pro", {content: s})
.done(function (data) { .done(function (data) {
var jData = $(data); var jData = $(data);
jData.appendTo(node); jData.appendTo(node);
@ -306,7 +308,7 @@ addCon :: JSFunction a => a
addCon = addCon =
makeJSFunction "addCon" ["node", "itemId", "s"] makeJSFunction "addCon" ["node", "itemId", "s"]
[text| [text|
$.post("/add/item/"+itemId+"/con", {content: s}) $.post("/haskell/add/item/"+itemId+"/con", {content: s})
.done(function (data) { .done(function (data) {
var jData = $(data); var jData = $(data);
jData.appendTo(node); jData.appendTo(node);
@ -318,7 +320,7 @@ submitTrait :: JSFunction a => a
submitTrait = submitTrait =
makeJSFunction "submitTrait" ["node", "itemId", "traitId", "s"] makeJSFunction "submitTrait" ["node", "itemId", "traitId", "s"]
[text| [text|
$.post("/set/item/"+itemId+"/trait/"+traitId, {content: s}) $.post("/haskell/set/item/"+itemId+"/trait/"+traitId, {content: s})
.done(function (data) { .done(function (data) {
$(node).replaceWith(data); $(node).replaceWith(data);
switchSection(node, "editable"); switchSection(node, "editable");
@ -337,13 +339,13 @@ submitItemInfo =
// it would lose the item's state (e.g. what if the traits were // it would lose the item's state (e.g. what if the traits were
// being edited? etc). So, instead we query colors from the server // being edited? etc). So, instead we query colors from the server
// and change the color of the item's body manually. // and change the color of the item's body manually.
$.post("/set/item/"+itemId+"/info", $(form).serialize()) $.post("/haskell/set/item/"+itemId+"/info", $(form).serialize())
.done(function (data) { .done(function (data) {
// Note the order first we change the color, then we replace // Note the order first we change the color, then we replace
// the info node. The reason is that otherwise the bodyNode // the info node. The reason is that otherwise the bodyNode
// selector might become invalid (if it depends on the infoNode // selector might become invalid (if it depends on the infoNode
// selector). // selector).
$.get("/render/item/"+itemId+"/colors") $.get("/haskell/render/item/"+itemId+"/colors")
.done(function (colors) { .done(function (colors) {
$(bodyNode).css("background-color", colors.light); $(bodyNode).css("background-color", colors.light);
$(infoNode).replaceWith(data); $(infoNode).replaceWith(data);
@ -355,7 +357,7 @@ moveTraitUp :: JSFunction a => a
moveTraitUp = moveTraitUp =
makeJSFunction "moveTraitUp" ["itemId", "traitId", "traitNode"] makeJSFunction "moveTraitUp" ["itemId", "traitId", "traitNode"]
[text| [text|
$.post("/move/item/"+itemId+"/trait/"+traitId, {direction: "up"}); $.post("/haskell/move/item/"+itemId+"/trait/"+traitId, {direction: "up"});
moveNodeUp(traitNode); moveNodeUp(traitNode);
|] |]
@ -363,7 +365,7 @@ moveTraitDown :: JSFunction a => a
moveTraitDown = moveTraitDown =
makeJSFunction "moveTraitDown" ["itemId", "traitId", "traitNode"] makeJSFunction "moveTraitDown" ["itemId", "traitId", "traitNode"]
[text| [text|
$.post("/move/item/"+itemId+"/trait/"+traitId, {direction: "down"}); $.post("/haskell/move/item/"+itemId+"/trait/"+traitId, {direction: "down"});
moveNodeDown(traitNode); moveNodeDown(traitNode);
|] |]
@ -372,7 +374,7 @@ deleteTrait =
makeJSFunction "deleteTrait" ["itemId", "traitId", "traitNode"] makeJSFunction "deleteTrait" ["itemId", "traitId", "traitNode"]
[text| [text|
if (confirm("Confirm deletion?")) { if (confirm("Confirm deletion?")) {
$.post("/delete/item/"+itemId+"/trait/"+traitId); $.post("/haskell/delete/item/"+itemId+"/trait/"+traitId);
$(traitNode).remove(); $(traitNode).remove();
} }
|] |]
@ -381,7 +383,7 @@ moveItemUp :: JSFunction a => a
moveItemUp = moveItemUp =
makeJSFunction "moveItemUp" ["itemId", "itemNode"] makeJSFunction "moveItemUp" ["itemId", "itemNode"]
[text| [text|
$.post("/move/item/"+itemId, {direction: "up"}); $.post("/haskell/move/item/"+itemId, {direction: "up"});
moveNodeUp(itemNode); moveNodeUp(itemNode);
|] |]
@ -389,7 +391,7 @@ moveItemDown :: JSFunction a => a
moveItemDown = moveItemDown =
makeJSFunction "moveItemDown" ["itemId", "itemNode"] makeJSFunction "moveItemDown" ["itemId", "itemNode"]
[text| [text|
$.post("/move/item/"+itemId, {direction: "down"}); $.post("/haskell/move/item/"+itemId, {direction: "down"});
moveNodeDown(itemNode); moveNodeDown(itemNode);
|] |]
@ -398,7 +400,7 @@ deleteItem =
makeJSFunction "deleteItem" ["itemId", "itemNode"] makeJSFunction "deleteItem" ["itemId", "itemNode"]
[text| [text|
if (confirm("Confirm deletion?")) { if (confirm("Confirm deletion?")) {
$.post("/delete/item/"+itemId); $.post("/haskell/delete/item/"+itemId);
$(itemNode).remove(); $(itemNode).remove();
} }
|] |]

View File

@ -30,7 +30,7 @@ import System.FilePath ((</>))
import Web.Spock hiding (head, get, text) import Web.Spock hiding (head, get, text)
import qualified Web.Spock as Spock import qualified Web.Spock as Spock
import Web.Spock.Lucid import Web.Spock.Lucid
import qualified Lucid import Lucid
import Network.Wai.Middleware.Static import Network.Wai.Middleware.Static
-- Feeds -- Feeds
import qualified Text.Feed.Types as Feed import qualified Text.Feed.Types as Feed
@ -230,15 +230,6 @@ addMethods = Spock.subcomponent "add" $ do
otherMethods :: SpockM () () DB () otherMethods :: SpockM () () DB ()
otherMethods = do otherMethods = do
-- Javascript
Spock.get "js.js" $ do
setHeader "Content-Type" "application/javascript; charset=utf-8"
Spock.bytes $ T.encodeUtf8 (fromJS allJSFunctions)
-- CSS
Spock.get "highlight.css" $ do
setHeader "Content-Type" "text/css; charset=utf-8"
Spock.bytes $ T.encodeUtf8 (T.pack (styleToCss pygments))
-- Moving things -- Moving things
Spock.subcomponent "move" $ do Spock.subcomponent "move" $ do
-- Move item -- Move item
@ -260,7 +251,8 @@ otherMethods = do
dbUpdate (DeleteTrait itemId traitId) dbUpdate (DeleteTrait itemId traitId)
-- Feeds -- Feeds
baseUrl <- fromMaybe "" <$> liftIO (lookupEnv "GUIDE_URL") -- TODO: this link shouldn't be absolute [absolute-links]
baseUrl <- (</> "haskell") . fromMaybe "/" <$> liftIO (lookupEnv "GUIDE_URL")
Spock.subcomponent "feed" $ do Spock.subcomponent "feed" $ do
-- Feed for items in a category -- Feed for items in a category
Spock.get categoryVar $ \catId -> do Spock.get categoryVar $ \catId -> do
@ -344,38 +336,61 @@ main = do
runSpock 8080 $ spock config $ do runSpock 8080 $ spock config $ do
middleware (EKG.metrics waiMetrics) middleware (EKG.metrics waiMetrics)
middleware (staticPolicy (addBase "static")) middleware (staticPolicy (addBase "static"))
-- Javascript
Spock.get "/js.js" $ do
setHeader "Content-Type" "application/javascript; charset=utf-8"
Spock.bytes $ T.encodeUtf8 (fromJS allJSFunctions)
-- CSS
Spock.get "/highlight.css" $ do
setHeader "Content-Type" "text/css; charset=utf-8"
Spock.bytes $ T.encodeUtf8 (T.pack (styleToCss pygments))
-- (css.css is a static file and so isn't handled here)
-- Main page -- Main page
Spock.get root $ do Spock.get root $ lucidIO $ do
s <- dbQuery GetGlobalState head_ $ do
q <- param "q" title_ "Aelve Guide"
lucidIO $ renderRoot s q includeCSS "/css.css"
renderTracking
body_ $ do
h1_ "Aelve Guide"
h2_ (a_ [href_ "/haskell"] "Haskell")
-- Donation page -- Donation page
Spock.get "donate" $ do Spock.get "donate" $ do
lucidIO $ renderDonate lucidIO $ renderDonate
-- Category pages
Spock.get var $ \path -> do -- Haskell
-- The links look like /generating-feeds-gao238b1 (because it's nice Spock.subcomponent "haskell" $ do
-- when you can find out where a link leads just by looking at it) Spock.get root $ do
let (_, catId) = T.breakOnEnd "-" path s <- dbQuery GetGlobalState
when (T.null catId) $ q <- param "q"
Spock.jumpNext lucidIO $ renderRoot s q
mbCategory <- dbQuery (GetCategoryMaybe (Uid catId)) -- Category pages
case mbCategory of Spock.get var $ \path -> do
Nothing -> Spock.jumpNext -- The links look like /parsers-gao238b1 (because it's nice when
Just category -> do -- you can find out where a link leads just by looking at it)
-- If the slug in the url is old or something (i.e. if it doesn't let (_, catId) = T.breakOnEnd "-" path
-- match the one we would've generated now), let's do a redirect when (T.null catId) $
when (categorySlug category /= path) $ Spock.jumpNext
Spock.redirect ("/" <> categorySlug category) mbCategory <- dbQuery (GetCategoryMaybe (Uid catId))
lucidIO $ renderCategoryPage category case mbCategory of
-- The add/set methods return rendered parts of the structure (added Nothing -> Spock.jumpNext
-- categories, changed items, etc) so that the Javascript part could Just category -> do
-- take them and inject into the page. We don't want to duplicate -- If the slug in the url is old (i.e. if it doesn't match the
-- rendering on server side and on client side. -- one we would've generated now), let's do a redirect
renderMethods when (categorySlug category /= path) $
setMethods -- TODO: this link shouldn't be absolute [absolute-links]
addMethods Spock.redirect ("/haskell/" <> categorySlug category)
otherMethods lucidIO $ renderCategoryPage category
-- The add/set methods return rendered parts of the structure (added
-- categories, changed items, etc) so that the Javascript part could
-- take them and inject into the page. We don't want to duplicate
-- rendering on server side and on client side.
renderMethods
setMethods
addMethods
otherMethods
-- TODO: when a category with the same name exists, show an error message and -- TODO: when a category with the same name exists, show an error message and
-- redirect to that other category -- redirect to that other category

View File

@ -254,7 +254,8 @@ renderCategoryTitle category = do
this = JS.selectId thisId this = JS.selectId thisId
h2_ [id_ thisId] $ do h2_ [id_ thisId] $ do
sectionSpan "normal" [shown, noScriptShown] $ do sectionSpan "normal" [shown, noScriptShown] $ do
a_ [href_ ("/" <> categorySlug category)] $ -- TODO: this link shouldn't be absolute [absolute-links]
a_ [href_ ("/haskell/" <> categorySlug category)] $
toHtml (category^.title) toHtml (category^.title)
emptySpan "1em" emptySpan "1em"
textButton "edit" $ textButton "edit" $