2014-11-25 01:36:34 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- |
|
|
|
|
-- Module : Servant.JQuery
|
|
|
|
-- Copyright : (C) 2014 Alp Mestanogullari
|
|
|
|
-- License : BSD3
|
|
|
|
-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : non-portable
|
2014-11-25 19:42:52 +01:00
|
|
|
module Servant.JQuery
|
|
|
|
( jquery
|
|
|
|
, generateJS
|
|
|
|
, printJS
|
|
|
|
, module Servant.JQuery.Internal
|
|
|
|
) where
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
import Control.Lens
|
|
|
|
import Data.List
|
2014-12-01 17:23:25 +01:00
|
|
|
import Data.Monoid
|
2014-11-25 01:36:34 +01:00
|
|
|
import Data.Proxy
|
|
|
|
import Servant.JQuery.Internal
|
|
|
|
|
|
|
|
jquery :: HasJQ layout => Proxy layout -> JQ layout
|
|
|
|
jquery p = jqueryFor p defReq
|
|
|
|
|
|
|
|
-- js codegen
|
|
|
|
generateJS :: AjaxReq -> String
|
2014-12-01 17:23:25 +01:00
|
|
|
generateJS req = "\n" <>
|
|
|
|
"function " <> fname <> "(" <> argsStr <> ")\n"
|
|
|
|
<> "{\n"
|
|
|
|
<> " $.ajax(\n"
|
|
|
|
<> " { url: " <> url <> "\n"
|
|
|
|
<> " , success: onSuccess\n"
|
|
|
|
<> dataBody
|
2014-12-08 13:32:47 +01:00
|
|
|
<> reqheaders
|
2014-12-01 17:23:25 +01:00
|
|
|
<> " , error: onError\n"
|
|
|
|
<> " , type: '" <> method <> "'\n"
|
|
|
|
<> " });\n"
|
|
|
|
<> "}\n"
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
where argsStr = intercalate ", " args
|
|
|
|
args = captures
|
|
|
|
++ map (view argName) queryparams
|
|
|
|
++ body
|
2015-01-21 09:32:06 +01:00
|
|
|
++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
|
2014-11-25 01:36:34 +01:00
|
|
|
++ ["onSuccess", "onError"]
|
|
|
|
|
|
|
|
captures = map captureArg
|
|
|
|
. filter isCapture
|
|
|
|
$ req ^. reqUrl.path
|
|
|
|
|
2014-12-08 13:32:47 +01:00
|
|
|
hs = req ^. reqHeaders
|
|
|
|
|
2014-11-25 01:36:34 +01:00
|
|
|
queryparams = req ^.. reqUrl.queryStr.traverse
|
|
|
|
|
|
|
|
body = if req ^. reqBody
|
|
|
|
then ["body"]
|
|
|
|
else []
|
|
|
|
|
|
|
|
dataBody =
|
|
|
|
if req ^. reqBody
|
2014-12-01 17:23:25 +01:00
|
|
|
then "\n , data: JSON.stringify(body)\n"
|
2014-11-25 01:36:34 +01:00
|
|
|
else ""
|
|
|
|
|
2014-12-08 13:32:47 +01:00
|
|
|
reqheaders =
|
|
|
|
if null hs
|
|
|
|
then ""
|
2015-01-21 08:27:25 +01:00
|
|
|
else "\n , headers: { " ++ headersStr ++ " }\n"
|
2014-12-08 13:32:47 +01:00
|
|
|
|
2014-12-09 15:39:41 +01:00
|
|
|
where headersStr = intercalate ", " $ map headerStr hs
|
2015-01-21 09:32:06 +01:00
|
|
|
headerStr header = "\"" ++
|
|
|
|
headerArgName header ++
|
|
|
|
"\": " ++ show header
|
2014-12-08 13:32:47 +01:00
|
|
|
|
2014-11-25 01:36:34 +01:00
|
|
|
fname = req ^. funcName
|
|
|
|
method = req ^. reqMethod
|
|
|
|
url = "'"
|
|
|
|
++ urlArgs
|
|
|
|
++ queryArgs
|
|
|
|
|
|
|
|
urlArgs = jsSegments
|
|
|
|
$ req ^.. reqUrl.path.traverse
|
|
|
|
|
|
|
|
queryArgs = if null queryparams
|
|
|
|
then ""
|
|
|
|
else " + '?" ++ jsParams queryparams
|
|
|
|
|
|
|
|
printJS :: AjaxReq -> IO ()
|
|
|
|
printJS = putStrLn . generateJS
|