packages feed

yesod-pnotify 1.0.1 → 1.0.2

raw patch · 2 files changed

+86/−14 lines, 2 filesdep +aeson

Dependencies added: aeson

Files

Yesod/Goodies/PNotify.hs view
@@ -12,13 +12,17 @@ import Yesod import Yesod.Form.Jquery hiding (urlJqueryJs, urlJqueryUiCss) +import Control.Monad (mzero) import Control.Monad.Trans.Maybe+import Data.Aeson (FromJSON(..), ToJSON(..), encode, decode)+import Data.Aeson.Parser (value) import Data.Char (toLower) import Data.List (nub) import Data.Monoid ((<>), mempty) import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Lazy as TL+import qualified Data.Text.Lazy.Encoding as TL (decodeUtf8, encodeUtf8) import Text.Julius (RawJS(..))  data PNotify = PNotify @@ -29,12 +33,82 @@                }              deriving (Show, Read, Eq) +instance FromJSON PNotify where+  parseJSON (Object v) = PNotify <$>+                         v .: "styling" <*>+                         v .: "type" <*>+                         v .: "title" <*>+                         v .: "text"+  parseJSON _ = mzero++instance ToJSON PNotify where+  toJSON (PNotify sty typ ttl msg) = object ["styling" .= sty+                                            ,"type" .= typ+                                            ,"title" .= ttl+                                            ,"text" .= msg+                                            ]++instance RawJS [PNotify] where+  rawJS = rawJS . TL.decodeUtf8 . encode+ data NotifyType = Notice | Info | Success | Error-                deriving (Show, Read, Eq)+                deriving (Eq) +instance Read NotifyType where+  readsPrec d r = do+    (v, s') <- lex r+    return $ case v of+      "notice" -> (Notice, s')+      "info" -> (Info, s')+      "success" -> (Success, s')+      "error" -> (Error, s')+      _ -> error $ "invalid NotifyType: " ++ v++instance Show NotifyType where+  show Notice = "notice"+  show Info = "info"+  show Success = "success"+  show Error = "error"++instance FromJSON NotifyType where+  parseJSON (String v) = return $ read $ T.unpack v+  parseJSON _ = mzero++instance ToJSON NotifyType where+  toJSON Notice = String "notice"+  toJSON Info = String "info"+  toJSON Success = String "success"+  toJSON Error = String "error"+ data NotifyStyling = JqueryUI | Bootstrap3 | BrightTheme | FontAwesome-                   deriving (Show, Read, Eq)+                   deriving (Eq) +instance Read NotifyStyling where+  readsPrec d r = do+    (v, s') <- lex r+    return $ case v of+      "jqueryui" -> (JqueryUI, s')+      "bootstrap3" -> (Bootstrap3, s')+      "brighttheme" -> (BrightTheme, s')+      "fontawesome" -> (FontAwesome, s')+      _ -> error $ "invalid NotifyStyling: " ++ v++instance Show NotifyStyling where+  show JqueryUI = "jqueryui"+  show Bootstrap3 = "bootstrap3"+  show BrightTheme = "brighttheme"+  show FontAwesome = "fontawesome"++instance FromJSON NotifyStyling where+  parseJSON (String v) = return $ read $ T.unpack v+  parseJSON _ = mzero++instance ToJSON NotifyStyling where+  toJSON JqueryUI = String "jqueryui"+  toJSON Bootstrap3 = String "bootstrap3"+  toJSON BrightTheme = String "brighttheme"+  toJSON FontAwesome = String "fontawesome"+ class YesodJquery a => YesodJqueryPnotify a where   urlJqueryJs :: a -> Either (Route a) Text   urlJqueryJs _ = Right "//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"@@ -61,10 +135,10 @@ notifyKey = "_PNotify"  toText :: [PNotify] -> Text-toText = T.concat . TL.toChunks . TL.pack . show+toText = TL.toStrict . TL.decodeUtf8 . encode  fromText :: Text -> [PNotify]-fromText = read . T.unpack+fromText = maybe [] id . decode . TL.encodeUtf8 . TL.fromStrict  _setPNotify :: [PNotify] -> HandlerT site IO () _setPNotify = setSession notifyKey . toText@@ -109,12 +183,4 @@        optionalLoadJsCss y ps -      let toJs p = [julius|{styling:'#{rawJS $ map toLower $ show $ sty p}'-                           ,title:'#{rawJS $ ttl p}'-                           ,text:'#{rawJS $ msg p}'-                           ,type:'#{rawJS $ map toLower $ show $ typ p}'-                           },|]-          ws = foldr ((<>).toJs) mempty ps-      toWidget [julius|$(function(){var ws=[^{ws}];for(var i in ws){new PNotify(ws[i]);}});|]-    where-      when b f = if b then f else return ()+      toWidget [julius|$(function(){$.each(#{rawJS ps},function(i,v){new PNotify(v)});});|]
yesod-pnotify.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                yesod-pnotify-version:             1.0.1+version:             1.0.2 synopsis:            Yet another getMessage/setMessage using pnotify jquery plugins description:         Yet another getMessage/setMessage using pnotify jquery plugins homepage:            https://github.com/cutsea110/yesod-pnotify@@ -26,6 +26,8 @@                      , MultiParamTypeClasses                      , TypeFamilies                      , FlexibleContexts+                     , FlexibleInstances+                     , DeriveGeneric    build-depends:         base >= 4.6 && < 4.9                        , yesod >= 1.2 && < 1.5@@ -33,6 +35,7 @@                        , text >= 0.11 && < 1.3                        , transformers >= 0.3 && < 0.5                        , shakespeare >= 2.0 && < 2.1+                       , aeson >= 0.8 && < 0.9  executable sample   main-is: sample.hs@@ -43,6 +46,8 @@                      , MultiParamTypeClasses                      , TypeFamilies                      , FlexibleContexts+                     , FlexibleInstances+                     , DeriveGeneric    build-depends:         base >= 4.6 && < 4.9                        , yesod >= 1.2 && < 1.5@@ -50,4 +55,5 @@                        , text >= 0.11 && < 1.3                        , transformers >= 0.3 && < 0.5                        , shakespeare >= 2.0 && < 2.1+                       , aeson >= 0.8 && < 0.9