~alcinnz/amphiarao

ref: 70de37cee9613aaeceedea91f0ac89cfd83bd8aa amphiarao/src/UI/Templates.hs -rw-r--r-- 3.8 KiB
70de37ce — Adrian Cochrane Show syntax-highlighted start tags for elements in HTML UI. 3 years ago
                                                                                
d4c10f89 Adrian Cochrane
70de37ce Adrian Cochrane
d4c10f89 Adrian Cochrane
70de37ce Adrian Cochrane
d4c10f89 Adrian Cochrane
70de37ce Adrian Cochrane
d4c10f89 Adrian Cochrane
8a39949f Adrian Cochrane
d4c10f89 Adrian Cochrane
afb3d55d Adrian Cochrane
8a39949f Adrian Cochrane
d4c10f89 Adrian Cochrane
8a39949f Adrian Cochrane
d4c10f89 Adrian Cochrane
8a39949f Adrian Cochrane
d4c10f89 Adrian Cochrane
afb3d55d Adrian Cochrane
d4c10f89 Adrian Cochrane
afb3d55d Adrian Cochrane
d4c10f89 Adrian Cochrane
8a39949f Adrian Cochrane
d4c10f89 Adrian Cochrane
70de37ce Adrian Cochrane
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
module UI.Templates(page, inspector, sessionForm, xmlNode) where

import Happstack.Lite
import Text.Blaze.Html5 as H
import Text.Blaze.Html5.Attributes as A
import Text.Blaze.Html
import Data.Text as Txt

import Internal
import Control.Monad.IO.Class (liftIO)
import Control.Monad (unless, when, forM)
import Control.Concurrent.MVar
import Data.UUID as ID

import Text.XML (Element(..), Node(..), Instruction(..), Name(..))
import qualified Data.Map as M

import Messages
import Happstack.Server.I18N

page :: (Response -> ServerPart Response) -> [Text] -> ([Text] -> Html) -> ServerPart Response
page return' title body' = do
    langs <- bestLanguage <$> acceptLanguage
    return' $ toResponse $ html $ do
        H.head $ do
            H.title $ text $ intercalate " — " title
        body $ body' langs

inspector :: (Response -> ServerPart Response) -> Text -> Session' -> ([Text] -> Html) -> ServerPart Response
inspector return' title session' body' = do
    let timeout = H.stringValue $ show $ pageLoad $ timeouts session'
    page return' [title, pack $ show $ currentURL $ session', "Amphiarao"] $ \langs -> do
        header $ do
            unless (Prelude.null $ backStack session') $ postButton "/nav/back" (l' langs Back') "🡸"
            unless (Prelude.null $ nextStack session') $ postButton "/nav/next" (l' langs Next') "🡺"
            postButton "/nav/reload" (l' langs Reload') "↻"
            hr
            H.form ! action' ["/", uuid', "/search"] ! alt (l' langs Search') $ do
                input ! type_ "search" ! name "q" ! placeholder (l' langs Search')
        body' langs
        footer $ do
            H.form ! action' ["/close/", uuid'] ! A.method "POST" ! alt (l' langs CloseSession') $ do
                button ! type_ "submit" $ l langs CloseSession
            hr
            H.form ! action' ["/", uuid', "/timeout"] ! A.method "POST" ! alt (l' langs LoadTimeout') $ p $ do
                H.label $ do
                    l langs LoadTimeout
                    input ! type_ "number" ! name "pageLoad" ! value timeout
                text "ms"

  where
    uuid' = ID.toString $ uuid_ session'
    action' = A.action . H.stringValue . Prelude.concat
    postButton target title' label = H.form ! action' ["/", uuid', target] ! alt title' ! A.method "POST" $ do
        button ! type_ "submit" ! A.title title' $ label

sessionForm langs = H.form ! A.method "POST" ! action "/" ! alt (l' langs CreateSession') $ do
    input ! type_ "url" ! name "target" ! placeholder "URL to debug"
    button ! type_ "submit" $ l langs CreateSession

--- XML formatting

xmlNode (NodeElement (Element name attrs childs)) = do
    symbolTok False "<"
    ident'Tok name
    forM (M.toList attrs) $ \(name, value) -> do
        text " "
        ident'Tok name
        symbolTok True "="
        stringTok $ show value -- quote it!
    when (Prelude.null childs) $ symbolTok True "/"
    symbolTok False ">"
  where
    ident'Tok (Name tag _ ns) = do
        case ns of
            Just ns' -> do
                qualifyTok ns'
                symbolTok False ":"
            Nothing -> return ()
        identTok tag
xmlNode (NodeInstruction (Instruction name value)) = do
    symbolTok False "<"
    symbolTok True "?"
    identTok name
    text " "
    stringTok $ unpack value
    symbolTok True "?"
    symbolTok False ">"

xmlNode (NodeContent text) = stringTok $ show text
xmlNode (NodeComment text) = do
    symbolTok False "<!--"
    commentTok text
    symbolTok False "-->"



symbolTok False = token "symbol silent"
symbolTok True = token "symbol"
qualifyTok = token "qualify"
identTok = token "ident"
stringTok = token "string" . pack
commentTok = token "comment"

token type_ txt = H.span ! class_ (stringValue ("tok-" ++ type_)) $ text txt