ghcjs-ajax (empty) → 0.1.0.0
raw patch · 5 files changed
+158/−0 lines, 5 filesdep +basedep +ghcjs-basedep +http-typessetup-changed
Dependencies added: base, ghcjs-base, http-types, text
Files
- LICENSE +7/−0
- Setup.hs +2/−0
- ghcjs-ajax.cabal +29/−0
- jsbits/core.js +61/−0
- src/JavaScript/Ajax.hs +59/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2016 Alexander Thiemann++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ ghcjs-ajax.cabal view
@@ -0,0 +1,29 @@+name: ghcjs-ajax+version: 0.1.0.0+synopsis: Crossbrowser AJAX Bindings for GHCJS+description: Please see README.md+homepage: https://github.com/agrafix/ghcjs-ajax#readme+license: MIT+license-file: LICENSE+author: Alexander Thiemann+maintainer: mail@athiemann.net+copyright: 2016 Alexander Thiemann <mail@athiemann.net>+category: Web+build-type: Simple+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: JavaScript.Ajax+ ghc-options: -Wall+ build-depends: base >=4.7 && <5+ , text+ , http-types+ default-language: Haskell2010+ js-sources: jsbits/core.js+ if impl(ghcjs)+ build-depends: ghcjs-base++source-repository head+ type: git+ location: https://github.com/agrafix/ghcjs-ajax
+ jsbits/core.js view
@@ -0,0 +1,61 @@+/**+ * Send AJAX request+ *+ * @param {string} url Request target url+ * @param {string} method HTTP Method, eg "GET" or "POST"+ * @param {string|null} requestBody Request Body+ * @param {string|null} contentType The content type+ * @param {function(number, string)} onComplete success handler+ */+function ghcjsajax$sendRequest(url, method, requestBody, contentType, onComplete) {+ var req = ghcjsajax$createXMLHTTPObject();+ if (!req) {+ onComplete(500, "Browser not supported");+ return;+ }+ req.open(method, url, true);+ if (requestBody && contentType) {+ req.setRequestHeader('Content-Type', contentType);+ }+ req.onreadystatechange = function () {+ if (req.readyState != 4) {+ return;+ }+ if (req.status != 200 && req.status != 304) {+ onComplete(req.status, req.responseText);+ return;+ }+ onComplete(req.status, req.responseText);+ };+ if (req.readyState == 4) {+ onComplete(500, "Bad ready state");+ return;+ }++ if (requestBody) {+ req.send(requestBody);+ } else {+ req.send();+ }+}++var ghcjsajax$XMLHttpFactories = [+ function () { return new XMLHttpRequest(); },+ function () { return new ActiveXObject("Msxml2.XMLHTTP"); },+ function () { return new ActiveXObject("Msxml3.XMLHTTP"); },+ function () { return new ActiveXObject("Microsoft.XMLHTTP"); }+];++function ghcjsajax$createXMLHTTPObject() {+ var xmlhttp = false;+ for (var i=0; i<ghcjsajax$XMLHttpFactories.length; i++) {+ try {+ xmlhttp = ghcjsajax$XMLHttpFactories[i]();+ }+ catch (e) {+ continue;+ }+ break;+ }+ return xmlhttp;+}
+ src/JavaScript/Ajax.hs view
@@ -0,0 +1,59 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE OverloadedStrings #-}+module JavaScript.Ajax+ ( sendRequest, StdMethod(..), Status(..)+ , RequestBody, ContentType+ )+where++import Network.HTTP.Types.Method+import Network.HTTP.Types.Status+import qualified Data.Text as T++#ifdef __GHCJS__+import Data.JSString+import Data.JSString.Text (textToJSString)+import GHCJS.Types+import GHCJS.Marshal (ToJSVal(..), FromJSVal(..))+import GHCJS.Foreign.Callback+#endif++type RequestBody = T.Text+type ContentType = T.Text++-- | Send an ajax request provided a HTTP-Method, a target url, optional a request+-- body and content type and a completion callback+sendRequest :: StdMethod -> T.Text -> Maybe RequestBody -> Maybe ContentType -> (Status -> T.Text -> IO ()) -> IO ()+#ifdef __GHCJS__+sendRequest method url mBody mContentType callback =+ do jsCt <- toJSVal mContentType+ jsBody <- toJSVal mBody+ jsCallback <-+ asyncCallback2 $ \jsStatus jsInnerText ->+ do status <- mkStatus <$> fromJSValUnchecked jsStatus <*> pure ""+ text <- fromJSValUnchecked jsInnerText+ callback status text+ js_sendRequest (textToJSString url) jsMethod jsBody jsCt jsCallback+ where+ jsMethod =+ case method of+ GET -> "GET"+ POST -> "POST"+ HEAD -> "HEAD"+ PUT -> "PUT"+ DELETE -> "DELETE"+ TRACE -> "TRACE"+ CONNECT -> "CONNECT"+ OPTIONS -> "OPTIONS"+ PATCH -> "PATCH"+#else+sendRequest = undefined+#endif++#ifdef __GHCJS__+foreign import javascript unsafe+ "ghcjsajax$sendRequest($1, $2, $3, $4, $5)"+ js_sendRequest ::+ JSString -> JSString -> JSVal -> JSVal+ -> Callback (JSVal -> JSVal -> IO ()) -> IO ()+#endif