-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TabID and add smart constructors for Tab
- Loading branch information
1 parent
4bcd11f
commit b560da8
Showing
3 changed files
with
78 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,62 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Eventlog.Rendering.Types where | ||
|
||
import Data.Char | ||
import Data.String | ||
import qualified Data.Text as T | ||
import Text.Blaze.Html5 | ||
|
||
data IncludeTraceData | ||
= TraceData | ||
| NoTraceData | ||
|
||
type VizID = String | ||
-- | Tab IDs must be usable as HTML and Javascript identifiers, so we allow a | ||
-- limited selection of characters. This is enforced only at runtime by the | ||
-- 'IsString' instance, but that seems good enough for now. | ||
newtype TabID = TabID T.Text | ||
|
||
instance IsString TabID where | ||
fromString s | ||
| all valid s = TabID (T.pack s) | ||
| otherwise = error $ "mkTabID: invalid tab ID: " ++ s | ||
where | ||
valid c = isAscii c && (isAlpha c || isDigit c || c == '_') | ||
|
||
tabIDToVizID :: TabID -> T.Text | ||
tabIDToVizID (TabID t) = "vis" <> t | ||
|
||
tabIDToNavItemID :: TabID -> T.Text | ||
tabIDToNavItemID (TabID t) = t <> "-tab" | ||
|
||
tabIDToTabID :: TabID -> T.Text | ||
tabIDToTabID (TabID t) = t | ||
|
||
tabIDToHref :: TabID -> T.Text | ||
tabIDToHref (TabID t) = "#" <> t | ||
|
||
type Tabs = [TabGroup] | ||
|
||
data TabGroup = ManyTabs String [Tab] | ||
| SingleTab Tab | ||
|
||
data Tab = Tab { tabName :: String | ||
, tabId :: VizID | ||
, tabContent :: Maybe (VizID -> Html) | ||
, tabId :: TabID | ||
, tabContent :: Maybe Html | ||
, tabDocs :: Maybe Html | ||
, tabActive :: Bool -- ^ Active by default? | ||
, tabDisabled :: Bool | ||
} | ||
|
||
mkTab :: String -> TabID -> Html -> Maybe Html -> Tab | ||
mkTab name id_ content docs = Tab name id_ (Just content) docs False False | ||
|
||
mkUnavailableTab :: String -> TabID -> Html -> Tab | ||
mkUnavailableTab name id_ docs = Tab name id_ Nothing (Just docs) False True | ||
|
||
mkOptionalTab :: String -> TabID -> (a -> Html) -> Maybe Html -> Html -> Maybe a -> Tab | ||
mkOptionalTab name id_ mk_content docs no_docs mb = case mb of | ||
Nothing -> mkUnavailableTab name id_ no_docs | ||
Just v -> mkTab name id_ (mk_content v) docs | ||
|
||
noDocs :: Maybe Html | ||
noDocs = Nothing |