From 34609eabb4afdbcc219c0bd461891e5ed83e73ba Mon Sep 17 00:00:00 2001
From: Adrian Cochrane
Date: Wed, 22 Nov 2023 17:18:25 +1300
Subject: [PATCH] Add timezone selector!
---
src/Text/HTML/Form/WebApp.hs | 9 ++++---
src/Text/HTML/Form/WebApp/Ginger/TZ.hs | 29 +++++++++++++++++-----
tpl/cal/timezone.html | 11 ++++++---
tpl/cal/year-numpad.html~ | 34 --------------------------
tpl/gregorian.html | 2 +-
tpl/number.html | 2 +-
6 files changed, 38 insertions(+), 49 deletions(-)
delete mode 100644 tpl/cal/year-numpad.html~
diff --git a/src/Text/HTML/Form/WebApp.hs b/src/Text/HTML/Form/WebApp.hs
index 5cc31ab..b98fb5b 100644
--- a/src/Text/HTML/Form/WebApp.hs
+++ b/src/Text/HTML/Form/WebApp.hs
@@ -17,7 +17,7 @@ import Text.HTML.Form.WebApp.Ginger (template, template', resolveSource, list')
import Text.HTML.Form.Query (renderQueryString, renderQuery')
import Text.HTML.Form.WebApp.Ginger.Hourglass (timeData, modifyTime', timeParseOrNow,
gSeqTo, gPad2)
-import Text.HTML.Form.WebApp.Ginger.TZ (tzdata)
+import Text.HTML.Form.WebApp.Ginger.TZ (tzdata, continents)
import Text.Ginger.GVal as V (GVal(..), toGVal, orderedDict, (~>), fromFunction)
import Text.Ginger.Html (html)
@@ -42,12 +42,13 @@ renderInput form ix input@Input { inputType = "date", inputName = name } ["year"
\prop -> case prop of
"T" -> timeData t'
_ -> toGVal ()
-renderInput form ix input@Input { inputType = "date", inputName = name } ["zone", ""] qs = do
+renderInput form ix input@Input { inputType = "date", inputName = name } ["zone", p] qs = do
t <- timeParseOrNow $ get name qs
let Elapsed (Seconds t') = timeGetElapsed $ localTimeToGlobal t
- template' "cal/year-numpad.html" form ix input qs $ \prop -> case prop of
+ template' "cal/timezone.html" form ix input qs $ \prop -> case prop of
"T" -> timeData t
- "zones" -> tzdata t'
+ "zones" -> tzdata t' $ unEscapeString $ Txt.unpack p
+ "continents" -> continents
_ -> toGVal ()
renderInput form ix input@Input { multiple = True } [p] qs
| '=':v' <- Txt.unpack p,
diff --git a/src/Text/HTML/Form/WebApp/Ginger/TZ.hs b/src/Text/HTML/Form/WebApp/Ginger/TZ.hs
index 5b68291..3a489b2 100644
--- a/src/Text/HTML/Form/WebApp/Ginger/TZ.hs
+++ b/src/Text/HTML/Form/WebApp/Ginger/TZ.hs
@@ -1,20 +1,37 @@
{-# LANGUAGE OverloadedStrings #-}
-module Text.HTML.Form.WebApp.Ginger.TZ(tzdata) where
+module Text.HTML.Form.WebApp.Ginger.TZ(tzdata, continents) where
-import Text.Ginger.GVal as V (GVal, orderedDict, (~>), list)
+import Text.Ginger.GVal as V (GVal, toGVal, orderedDict, (~>), list)
import qualified Data.Map.Strict as M
import Data.Time.Zones.All (tzNameLabelMap, tzByLabel)
import Data.Time.Zones (diffForPOSIX)
+
import Data.Int (Int64)
+import Data.List (nub)
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Char8 as BSC
-tzdata :: Int64 -> GVal m
-tzdata now = list [orderedDict [
+tzdata :: Int64 -> String -> GVal m
+tzdata now prefix = list [orderedDict [
"label" ~> label,
"value" ~> (diffForPOSIX tz' now `div` 60),
"offset" ~> formatOffset (diffForPOSIX tz' now `div` 60)
- ] | (label, tz) <- M.toList tzNameLabelMap, let tz' = tzByLabel tz]
+ ] | (label, tz) <- M.toList tzNameLabelMap,
+ BSC.pack prefix `contains` label,
+ let tz' = tzByLabel tz]
+ where
+ contains "" = BSC.notElem '/'
+ contains "..." = BSC.notElem '/'
+ contains x = BS.isPrefixOf x
formatOffset :: (Show a, Integral a) => a -> [Char]
-formatOffset offset = (show hours ++ ':': show minutes)
+formatOffset offset
+ | minutes < 10 = show hours ++ ':':'0': show minutes
+ | otherwise = show hours ++ ':': show minutes
where
hours = offset `div` 60
minutes = abs $ offset `rem` 60
+
+continents :: GVal m
+continents = list $ map toGVal $ nub $ "...":[prefix |
+ (label, _) <- M.toList tzNameLabelMap,
+ let (prefix, _) = BSC.breakEnd (== '/') label]
diff --git a/tpl/cal/timezone.html b/tpl/cal/timezone.html
index a1cf1af..9414f93 100644
--- a/tpl/cal/timezone.html
+++ b/tpl/cal/timezone.html
@@ -1,8 +1,13 @@
{% extends "/base.html" %}
{%- block main -%}
-
- {% for tz in timezones %}
+ {% for cont in continents %}
+ - {{ cont }}
+ {% endfor %}
+
+{% - endblock %-}
+{%- endblock -%}
diff --git a/tpl/cal/year-numpad.html~ b/tpl/cal/year-numpad.html~
deleted file mode 100644
index 15f7d53..0000000
--- a/tpl/cal/year-numpad.html~
+++ /dev/null
@@ -1,34 +0,0 @@
-{% extends "base.html" %}
-
-{%- block main -%}
- ↓
- {{ input.value }}
- ↑
-
-
-{%- endblock -%}
diff --git a/tpl/gregorian.html b/tpl/gregorian.html
index 9b9a0a1..b86f1d2 100644
--- a/tpl/gregorian.html
+++ b/tpl/gregorian.html
@@ -33,7 +33,7 @@
{{ T.meridiem }}
↓
- {{ T.zone }}
+ {{ T.zone }}
↑
Now
diff --git a/tpl/number.html b/tpl/number.html
index 77f3474..15f7d53 100644
--- a/tpl/number.html
+++ b/tpl/number.html
@@ -1,6 +1,6 @@
{% extends "base.html" %}
-{%- block control -%}
+{%- block main -%}
↓
{{ input.value }}
↑
--
2.30.2