Added JqueryDay and NicHtml newtypes
This commit is contained in:
parent
6d05c9ec30
commit
55e0af467d
@ -16,6 +16,9 @@ module Yesod.Form
|
|||||||
, Enctype (..)
|
, Enctype (..)
|
||||||
, FieldInfo (..)
|
, FieldInfo (..)
|
||||||
, FieldProfile (..)
|
, FieldProfile (..)
|
||||||
|
-- * Newtype wrappers
|
||||||
|
, JqueryDay (..)
|
||||||
|
, NicHtml (..)
|
||||||
-- * Unwrapping functions
|
-- * Unwrapping functions
|
||||||
, runFormGet
|
, runFormGet
|
||||||
, runFormPost
|
, runFormPost
|
||||||
@ -59,7 +62,7 @@ import Control.Monad ((<=<), liftM, join)
|
|||||||
import Data.Monoid (Monoid (..))
|
import Data.Monoid (Monoid (..))
|
||||||
import Control.Monad.Trans.State
|
import Control.Monad.Trans.State
|
||||||
import Language.Haskell.TH.Syntax
|
import Language.Haskell.TH.Syntax
|
||||||
import Database.Persist.Base (EntityDef (..))
|
import Database.Persist.Base (EntityDef (..), PersistField)
|
||||||
import Data.Char (toUpper, isUpper)
|
import Data.Char (toUpper, isUpper)
|
||||||
import Data.Int (Int64)
|
import Data.Int (Int64)
|
||||||
import qualified Data.ByteString.Lazy.UTF8 as U
|
import qualified Data.ByteString.Lazy.UTF8 as U
|
||||||
@ -268,6 +271,23 @@ dayField = FieldProfile
|
|||||||
, fpRender = show
|
, fpRender = show
|
||||||
, fpHamlet = \name val isReq -> [$hamlet|
|
, fpHamlet = \name val isReq -> [$hamlet|
|
||||||
%input#$<name>$!name=$<name>$!type=date!:isReq:required!value=$<val>$
|
%input#$<name>$!name=$<name>$!type=date!:isReq:required!value=$<val>$
|
||||||
|
|]
|
||||||
|
, fpWidget = const $ return ()
|
||||||
|
}
|
||||||
|
instance IsFormField Day where
|
||||||
|
toFormField = requiredField dayField
|
||||||
|
instance IsFormField (Maybe Day) where
|
||||||
|
toFormField = optionalField dayField
|
||||||
|
|
||||||
|
jqueryDayField :: FieldProfile sub y JqueryDay
|
||||||
|
jqueryDayField = dayField
|
||||||
|
{ fpParse = maybe
|
||||||
|
(Left "Invalid day, must be in YYYY-MM-DD format")
|
||||||
|
(Right . JqueryDay)
|
||||||
|
. readMay
|
||||||
|
, fpRender = show . unJqueryDay
|
||||||
|
, fpHamlet = \name val isReq -> [$hamlet|
|
||||||
|
%input#$<name>$!name=$<name>$!type=date!:isReq:required!value=$<val>$
|
||||||
|]
|
|]
|
||||||
, fpWidget = \name -> do
|
, fpWidget = \name -> do
|
||||||
addScriptRemote "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
|
addScriptRemote "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
|
||||||
@ -275,10 +295,15 @@ dayField = FieldProfile
|
|||||||
addStylesheetRemote "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/cupertino/jquery-ui.css"
|
addStylesheetRemote "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/cupertino/jquery-ui.css"
|
||||||
addHead [$hamlet|%script $$(function(){$$("#$name$").datepicker({dateFormat:'yy-mm-dd'})})|]
|
addHead [$hamlet|%script $$(function(){$$("#$name$").datepicker({dateFormat:'yy-mm-dd'})})|]
|
||||||
}
|
}
|
||||||
instance IsFormField Day where
|
|
||||||
toFormField = requiredField dayField
|
-- | A newtype wrapper around 'Day', using jQuery UI date picker for the
|
||||||
instance IsFormField (Maybe Day) where
|
-- 'IsFormField' instance.
|
||||||
toFormField = optionalField dayField
|
newtype JqueryDay = JqueryDay { unJqueryDay :: Day }
|
||||||
|
deriving PersistField
|
||||||
|
instance IsFormField JqueryDay where
|
||||||
|
toFormField = requiredField jqueryDayField
|
||||||
|
instance IsFormField (Maybe JqueryDay) where
|
||||||
|
toFormField = optionalField jqueryDayField
|
||||||
|
|
||||||
parseTime :: String -> Either String TimeOfDay
|
parseTime :: String -> Either String TimeOfDay
|
||||||
parseTime (h2:':':m1:m2:[]) = parseTimeHelper ['0', h2, m1, m2, '0', '0']
|
parseTime (h2:':':m1:m2:[]) = parseTimeHelper ['0', h2, m1, m2, '0', '0']
|
||||||
@ -350,15 +375,32 @@ htmlField = FieldProfile
|
|||||||
, fpHamlet = \name val _isReq -> [$hamlet|
|
, fpHamlet = \name val _isReq -> [$hamlet|
|
||||||
%textarea.html#$<name>$!name=$<name>$ $<val>$
|
%textarea.html#$<name>$!name=$<name>$ $<val>$
|
||||||
|]
|
|]
|
||||||
, fpWidget = \name -> do
|
, fpWidget = const $ return ()
|
||||||
addScriptRemote "http://js.nicedit.com/nicEdit-latest.js"
|
|
||||||
addHead [$hamlet|%script bkLib.onDomLoaded(function(){new nicEditor({fullPanel:true}).panelInstance("$name$")})|]
|
|
||||||
}
|
}
|
||||||
instance IsFormField (Html ()) where
|
instance IsFormField (Html ()) where
|
||||||
toFormField = requiredField htmlField
|
toFormField = requiredField htmlField
|
||||||
instance IsFormField (Maybe (Html ())) where
|
instance IsFormField (Maybe (Html ())) where
|
||||||
toFormField = optionalField htmlField
|
toFormField = optionalField htmlField
|
||||||
|
|
||||||
|
newtype NicHtml = NicHtml { unNicHtml :: Html () }
|
||||||
|
deriving PersistField
|
||||||
|
|
||||||
|
nicHtmlField :: FieldProfile sub y NicHtml
|
||||||
|
nicHtmlField = FieldProfile
|
||||||
|
{ fpParse = Right . NicHtml . preEscapedString
|
||||||
|
, fpRender = U.toString . renderHtml . unNicHtml
|
||||||
|
, fpHamlet = \name val _isReq -> [$hamlet|
|
||||||
|
%textarea.html#$<name>$!name=$<name>$ $<val>$
|
||||||
|
|]
|
||||||
|
, fpWidget = \name -> do
|
||||||
|
addScriptRemote "http://js.nicedit.com/nicEdit-latest.js"
|
||||||
|
addHead [$hamlet|%script bkLib.onDomLoaded(function(){new nicEditor({fullPanel:true}).panelInstance("$name$")})|]
|
||||||
|
}
|
||||||
|
instance IsFormField NicHtml where
|
||||||
|
toFormField = requiredField nicHtmlField
|
||||||
|
instance IsFormField (Maybe NicHtml) where
|
||||||
|
toFormField = optionalField nicHtmlField
|
||||||
|
|
||||||
readMay :: Read a => String -> Maybe a
|
readMay :: Read a => String -> Maybe a
|
||||||
readMay s = case reads s of
|
readMay s = case reads s of
|
||||||
(x, _):_ -> Just x
|
(x, _):_ -> Just x
|
||||||
|
|||||||
9
blog.hs
9
blog.hs
@ -5,12 +5,11 @@ import Yesod.Helpers.Crud
|
|||||||
import Database.Persist.Sqlite
|
import Database.Persist.Sqlite
|
||||||
import Data.Time (Day)
|
import Data.Time (Day)
|
||||||
|
|
||||||
type Html' = Html ()
|
|
||||||
share2 mkPersist mkIsForm [$persist|
|
share2 mkPersist mkIsForm [$persist|
|
||||||
Entry
|
Entry
|
||||||
title String "label=Entry title" "tooltip=Make it something cool"
|
title String "label=Entry title" "tooltip=Make it something cool"
|
||||||
posted Day Desc
|
posted JqueryDay Desc
|
||||||
content Html'
|
content NicHtml
|
||||||
deriving
|
deriving
|
||||||
|]
|
|]
|
||||||
instance Item Entry where
|
instance Item Entry where
|
||||||
@ -101,8 +100,8 @@ getEntryR eid = do
|
|||||||
setTitle $ string $ entryTitle entry
|
setTitle $ string $ entryTitle entry
|
||||||
addBody [$hamlet|
|
addBody [$hamlet|
|
||||||
%h1 $entryTitle.entry$
|
%h1 $entryTitle.entry$
|
||||||
%h2 $show.entryPosted.entry$
|
%h2 $show.unJqueryDay.entryPosted.entry$
|
||||||
#content $<entryContent.entry>$
|
#content $<unNicHtml.entryContent.entry>$
|
||||||
|]
|
|]
|
||||||
main = withSqlite "blog.db3" $ \conn -> do
|
main = withSqlite "blog.db3" $ \conn -> do
|
||||||
flip runSqlite conn $ initialize (undefined :: Entry)
|
flip runSqlite conn $ initialize (undefined :: Entry)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user