JqueryDaySettings

This commit is contained in:
Michael Snoyman 2010-10-13 18:06:35 +02:00
parent eaffbb93ff
commit 27a63b1b75
3 changed files with 56 additions and 8 deletions

View File

@ -9,6 +9,7 @@ module Yesod.Form.Jquery
, maybeJqueryAutocompleteField
, jqueryDayFieldProfile
, googleHostedJqueryUiCss
, JqueryDaySettings (..)
) where
import Yesod.Handler
@ -19,6 +20,7 @@ import Data.Time (UTCTime (..), Day, TimeOfDay (..), timeOfDayToTime,
timeToTimeOfDay)
import Yesod.Hamlet
import Data.Char (isSpace)
import Data.Default
-- | Gets the Google hosted jQuery UI 1.8 CSS file with the given theme.
googleHostedJqueryUiCss :: String -> String
@ -45,14 +47,21 @@ class YesodJquery a where
urlJqueryUiDateTimePicker :: a -> Either (Route a) String
urlJqueryUiDateTimePicker _ = Right "http://github.com/gregwebs/jquery.ui.datetimepicker/raw/master/jquery.ui.datetimepicker.js"
jqueryDayField :: YesodJquery y => FormFieldSettings -> FormletField sub y Day
jqueryDayField = requiredFieldHelper jqueryDayFieldProfile
jqueryDayField :: YesodJquery y
=> JqueryDaySettings
-> FormFieldSettings
-> FormletField sub y Day
jqueryDayField = requiredFieldHelper . jqueryDayFieldProfile
maybeJqueryDayField :: YesodJquery y => FormFieldSettings -> FormletField sub y (Maybe Day)
maybeJqueryDayField = optionalFieldHelper jqueryDayFieldProfile
maybeJqueryDayField :: YesodJquery y
=> JqueryDaySettings
-> FormFieldSettings
-> FormletField sub y (Maybe Day)
maybeJqueryDayField = optionalFieldHelper . jqueryDayFieldProfile
jqueryDayFieldProfile :: YesodJquery y => FieldProfile sub y Day
jqueryDayFieldProfile = FieldProfile
jqueryDayFieldProfile :: YesodJquery y
=> JqueryDaySettings -> FieldProfile sub y Day
jqueryDayFieldProfile jds = FieldProfile
{ fpParse = maybe
(Left "Invalid day, must be in YYYY-MM-DD format")
Right
@ -66,9 +75,26 @@ jqueryDayFieldProfile = FieldProfile
addScript' urlJqueryUiJs
addStylesheet' urlJqueryUiCss
addJavascript [$julius|
$(function(){$("#%theId%").datepicker({dateFormat:'yy-mm-dd'})});
$(function(){$("#%theId%").datepicker({
dateFormat:'yy-mm-dd',
changeMonth:%jsBool.jdsChangeMonth.jds%,
changeYear:%jsBool.jdsChangeYear.jds%,
numberOfMonths:%mos.jdsNumberOfMonths.jds%,
yearRange:"%jdsYearRange.jds%"
})});
|]
}
where
jsBool True = "true"
jsBool False = "false"
mos (Left i) = show i
mos (Right (x, y)) = concat
[ "["
, show x
, ","
, show y
, "]"
]
ifRight :: Either a b -> (b -> c) -> Either a c
ifRight e f = case e of
@ -162,3 +188,18 @@ readMay s = case reads s of
-- from http://hackage.haskell.org/packages/archive/cgi/3001.1.7.1/doc/html/src/Network-CGI-Protocol.html#replace
replace :: Eq a => a -> a -> [a] -> [a]
replace x y = map (\z -> if z == x then y else z)
data JqueryDaySettings = JqueryDaySettings
{ jdsChangeMonth :: Bool
, jdsChangeYear :: Bool
, jdsYearRange :: String
, jdsNumberOfMonths :: Either Int (Int, Int)
}
instance Default JqueryDaySettings where
def = JqueryDaySettings
{ jdsChangeMonth = False
, jdsChangeYear = False
, jdsYearRange = "c-10:c+10"
, jdsNumberOfMonths = Left 1
}

View File

@ -10,6 +10,7 @@ import Control.Applicative
import qualified Data.ByteString.Lazy as L
import System.Directory
import Control.Monad.Trans.Class
import Data.Default
data HW = HW { hwStatic :: Static }
mkYesod "HW" [$parseRoutes|
@ -64,7 +65,12 @@ handleFormR = do
<$> stringField (FormFieldSettings "My Field" "Some tooltip info" Nothing Nothing) Nothing
<*> stringField ("Another field") (Just "some default text")
<*> intField (FormFieldSettings "A number field" "some nums" Nothing Nothing) (Just 5)
<*> jqueryDayField ("A day field") Nothing
<*> jqueryDayField def
{ jdsChangeMonth = True
, jdsChangeYear = True
, jdsYearRange = "1900:c+10"
, jdsNumberOfMonths = Right (2, 3)
} ("A day field") Nothing
<*> timeField ("A time field") Nothing
<*> jqueryDayTimeField ("A day/time field") Nothing
<*> boolField FormFieldSettings

View File

@ -51,6 +51,7 @@ library
, process >= 1.0.1 && < 1.1
, web-routes >= 0.23 && < 0.24
, xss-sanitize >= 0.2 && < 0.3
, data-default >= 0.2 && < 0.3
exposed-modules: Yesod
Yesod.Content
Yesod.Dispatch