-- Common wrappers for HTML pages
module HackagePage (hackagePage, hackagePageWith, haddockPage) where

import Text.XHtml		hiding ( p )

import Locations		( stylesheetURL, introductionURL, pkgListURL,
				  searchURL, recentAdditionsURL, uploadURL,
				  accountsURL,
				  oceanStyleURL, classicStyleURL,
				  haddockJSURL )

-- | Create top-level HTML document by wrapping the Html with boilerplate.
hackagePage :: String -> [Html] -> Html
hackagePage = hackagePageWith []

hackagePageWith :: [Html] -> String -> [Html] -> Html
hackagePageWith links heading docs = toHtml [header << docHead, body << docBody]
  where docHead =
		meta ! [httpequiv "Content-type",
			content "text/html; charset=ISO-8859-1"] :
		thetitle << ("HackageDB: " ++ heading) :
		thelink ! [rel "stylesheet", href stylesheetURL,
			thetype "text/css"] << noHtml :
		links
	docBody = [thediv ! [theclass "header"] << docHeader,
		thediv ! [theclass "content"] << docs]
	docHeader = [h1 << hackageTitle,
		table ! [theclass "navigation"] << navigation]
	hackageTitle = "hackageDB :: [Package]"
	navigation = tr << [td << (anchor ! [href url] << lab) |
				(lab, url) <- navigationBar]

navigationBar :: [(String, URL)]
navigationBar =
	[ ("Introduction",	introductionURL)
	, ("Packages",		pkgListURL)
	-- , ("Search",            searchURL)
	, ("Hayoo!",		"http://holumbus.fh-wedel.de/hayoo/hayoo.html")
	, ("What's new",	recentAdditionsURL)
	, ("Upload",		uploadURL)
	, ("User accounts",	accountsURL)
	]

-- | Wrapper for pages with haddock styling
haddockPage :: HTML doc => String -> doc -> Html
haddockPage pkgname doc = toHtml [header << docHead, body << doc]
  where docHead = [
		meta ! [httpequiv "Content-type",
			content "text/html; charset=ISO-8859-1"],
		thetitle << ("HackageDB: " ++ pkgname),
		thelink ! [rel "stylesheet", thetype "text/css",
			href oceanStyleURL, title "Ocean"] << noHtml,
		thelink ! [rel "alternate stylesheet", thetype "text/css",
			href classicStyleURL, title "Classic"] << noHtml,
		script ! [thetype "text/javascript",
			src haddockJSURL] << noHtml,
		script ! [thetype "text/javascript"] <<
			"window.onload = function() {pageLoad();};"]
