forked from norm2782/uhc-jscript
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/spockz/uhc-jscript
- Loading branch information
Showing
7 changed files
with
242 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module Language.UHC.JScript.JQuery.Ajax (AjaxOptions(..), JSAjaxOptions(..), AjaxCallback, AjaxRequestType(..), ajaxBackend, ajax, toJSOptions, mkJSAjaxCallback) where | ||
|
||
import Language.UHC.JScript.ECMA.String | ||
import Language.UHC.JScript.Types | ||
|
||
import Language.UHC.JScript.Primitives | ||
|
||
import Data.List | ||
|
||
data JQXHRPtr | ||
type JQXHR = JSPtr JQXHRPtr | ||
|
||
|
||
-- type AjaxCallback r = JS r => r -> String -> JQXHR -> IO() | ||
type AjaxCallback r = r -> String -> JQXHR -> IO() | ||
type JSAjaxCallback r = JSFunPtr (AjaxCallback r) | ||
|
||
data AjaxRequestType = GET | POST | ||
deriving Show | ||
|
||
|
||
data AjaxOptions a = AjaxOptions { | ||
ao_url :: String, | ||
ao_requestType :: AjaxRequestType, | ||
ao_contentType :: String, | ||
ao_dataType :: String | ||
} | ||
|
||
|
||
data JSAjaxOptions a = JSAjaxOptions { | ||
url :: JSString, | ||
requestType :: JSString, | ||
contentType :: JSString, | ||
dataType :: JSString | ||
} | ||
|
||
instance Show (AjaxOptions a) where | ||
show jsopt= "AjaxOptions: " ++ intercalate " " [show $ ao_url jsopt] | ||
|
||
instance Show (JSAjaxOptions a) where | ||
show jsopt = "JSAjaxOptions: " ++ intercalate " " [show $ url jsopt] | ||
|
||
toJSOptions :: AjaxOptions a -> JSAjaxOptions a | ||
toJSOptions options = let url' = toJS (ao_url options) | ||
requestType' = toJS (show $ ao_requestType options) | ||
contentType' = toJS (ao_contentType options) | ||
dataType' = toJS (ao_dataType options) | ||
in JSAjaxOptions { url = url' | ||
, requestType = requestType' | ||
, contentType = contentType' | ||
, dataType = dataType' | ||
} | ||
|
||
|
||
ajaxBackend :: JS r => (JSPtr a -> IO ()) -> AjaxOptions a -> AjaxCallback r -> AjaxCallback r -> IO () | ||
ajaxBackend cont options onSuccess onFailure = | ||
do let jsOptions = toJSOptions options | ||
onSuccess' <- mkJSAjaxCallback onSuccess | ||
onFailure' <- mkJSAjaxCallback onFailure | ||
o <- mkObj jsOptions | ||
_ <- setAttr "type" (requestType jsOptions) o | ||
_ <- setAttr "success" onSuccess' o | ||
_ <- setAttr "error" onFailure' o | ||
_ajaxQ (toJS "jcu_app") o | ||
|
||
ajax :: JS r => AjaxOptions a -> AjaxCallback r -> AjaxCallback r -> IO () | ||
ajax = ajaxBackend _ajax | ||
|
||
|
||
|
||
foreign import jscript "wrapper" | ||
mkJSAjaxCallback :: AjaxCallback r -> IO (JSAjaxCallback r) | ||
|
||
|
||
foreign import jscript "$.ajax(%1)" | ||
_ajax :: JSPtr a -> IO () | ||
|
||
foreign import jscript "$.ajaxq(%*)" | ||
_ajaxQ :: JSString -> JSPtr a -> IO () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Language.UHC.JScript.JQuery.AjaxQueue (ajaxQ) where | ||
|
||
import Language.UHC.JScript.Primitives | ||
import Language.UHC.JScript.Types | ||
|
||
import Language.UHC.JScript.ECMA.String | ||
import Language.UHC.JScript.JQuery.Ajax | ||
|
||
import Language.UHC.JScript.Assorted (alert, _alert) | ||
|
||
ajaxQ :: JS r => String -> AjaxOptions a -> AjaxCallback r -> AjaxCallback r -> IO () | ||
ajaxQ queuename = ajaxBackend (_ajaxQ $ toJS queuename) | ||
|
||
foreign import jscript "$.ajaxq(%*)" | ||
_ajaxQ :: JSString -> JSPtr a -> IO () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters