Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 457363e509
Fetching contributors…

Cannot retrieve contributors at this time

file 235 lines (157 sloc) 6.788 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
module Language.UHC.JScript.JQuery.JQuery where

import Language.UHC.JScript.ECMA.String
import Language.UHC.JScript.Primitives
import Language.UHC.JScript.Types

import Language.UHC.JScript.Assorted (alert)

data JQueryPtr
type JQuery = JSPtr JQueryPtr

-------------------------------------------------------------------------------
-- jQuery Core

jQuery :: String -> IO JQuery
jQuery = _jQuery . toJS

jQuery' :: String -> JSPtr a -> IO JQuery
jQuery' s j = _jQuery' (toJS s) j

foreign import jscript "jQuery(%*)"
  _jQuery :: JSString -> IO JQuery

foreign import jscript "jQuery(%*)"
  _jQuery' :: JSString -> JSPtr a -> IO JQuery

foreign import jscript "jQuery(%*)"
  jQueryObj :: JSPtr a -> IO JQuery

foreign import jscript "jQuery()"
  jQuery_ :: IO JQuery


foreign import jscript "$.holdReady(%*)"
  holdReady :: Bool -> IO ()

foreign import jscript "$.noConflict()"
  noConflict :: IO ()

foreign import jscript "$.noConflict(%*)"
  noConflict' :: Bool -> IO ()

foreign import jscript "jQuery.sub()"
  sub :: IO JQuery

foreign import jscript "jQuery.when(%*)"
  when :: JSPtr a -> IO ()

foreign import jscript "jQuery.when(%*)"
  when' :: JSPtr a -> JSPtr a -> IO ()

foreign import jscript "jQuery.when(%*)"
  when'' :: JSPtr a -> JSPtr a -> JSPtr a -> IO ()

-------------------------------------------------------------------------------
-- DOM

foreign import jscript "%1.each(%2)"
  each :: JQuery -> JSFunPtr (Int -> JSPtr a -> IO ()) -> IO ()

foreign import jscript "jQuery.each(%*)"
  each' :: b -> JSFunPtr (Int -> JSPtr a -> IO ()) -> IO ()
  

foreign import jscript "wrapper"
  mkEachIterator :: (Int -> JSPtr a -> IO ()) -> IO (JSFunPtr (Int -> JSPtr a -> IO ()))
  
-------------------------------------------------------------------------------
-- DOM

findSelector :: JQuery -> String -> IO JQuery
findSelector jq = findSelector' jq . toJS

foreign import jscript "%1.find(%2)"
  findSelector' :: JQuery -> JSString -> IO JQuery

foreign import jscript "%1.find(%2)"
  findObject :: JQuery -> JQuery -> IO JQuery

foreign import jscript "%1.val()"
  valString :: JQuery -> IO JSString


-------------------------------------------------------------------------------
-- Manipulation

getHTML :: JQuery -> IO String
getHTML = fromJSM . _getHTML

foreign import jscript "%1.html()"
  _getHTML :: JQuery -> IO JSString


setHTML :: JQuery -> String -> IO ()
setHTML j s = _setHTML j (toJS s)


foreign import jscript "%1.html(%2)"
  _setHTML :: JQuery -> JSString -> IO ()
  
foreign import jscript "%1.hide()"
  hide :: JQuery -> IO ()

addClass :: JQuery -> String -> IO ()
addClass j s = _addClass j (toJS s)

wrapInner :: JQuery -> String -> IO ()
wrapInner j = _wrapInner j . toJS

foreign import jscript "%1.wrapInner(%2)"
  _wrapInner :: JQuery -> JSString -> IO ()

 -- Or return JQuery for chaining??? Does chaining even make sense?
foreign import jscript "%1.addClass(%2)"
  _addClass :: JQuery -> JSString -> IO ()

-------------------------------------------------------------------------------
-- Effects

fast, slow :: Int
fast = 200
slow = 600

--
-- The show() function
--
-- How can functions be passed? Which type should a callback function have?
-- Generally speaking, a callback functions as would be used in show here returns
-- void. What if we make callback functions return ()?
foreign import jscript "%1.show()" jqshow0 :: JQuery -> IO ()
foreign import jscript "%1.show(%*)" jqshow1 :: JQuery -> Int -> IO ()
foreign import jscript "%1.show(%*)" jqshow2 :: JQuery -> Int -> JSString -> IO ()
foreign import jscript "%1.show(%*)" jqshow2' :: JQuery -> Int -> IO () -> IO ()
foreign import jscript "%1.show(%*)" jqshow3 :: JQuery -> Int -> JSString -> IO () -> IO ()

jqshow :: JQuery -> Maybe Int -> Maybe String -> Maybe (IO ()) -> IO ()
jqshow j Nothing Nothing Nothing = jqshow0 j
jqshow j (Just n) Nothing Nothing = jqshow1 j n
jqshow j (Just n) (Just e) Nothing = jqshow2 j n (toJS e)
jqshow j (Just n) Nothing (Just c) = jqshow2' j n c
jqshow j (Just n) (Just e) (Just c) = jqshow3 j n (toJS e) c


foreign import jscript "%1.blur()"
  doBlur :: JQuery -> IO ()

-------------------------------------------------------------------------------
-- Events

data JUIPtr
type JUI = JSPtr JUIPtr

type EventHandler = JQuery -> JEventResult
type UIEventHandler = JQuery -> JUI -> JEventResult -- TODO: Split this off to JQueryUI or something :)
type UIThisEventHandler = JQuery -> JQuery -> JUI -> JEventResult

type JEventResult = IO Bool

type JEventHandler = JSFunPtr EventHandler
type JUIEventHandler = JSFunPtr UIEventHandler
type JUIThisEventHandler = JSFunPtr UIThisEventHandler
type JEventType = String

bind :: JQuery -> JEventType -> JEventHandler -> IO ()
bind jq event eh = do _bind jq (toJS event) eh

foreign import jscript "%1.bind(%*)"
  _bind :: JQuery -> JSString -> JEventHandler -> IO ()


blur :: JQuery -> JEventHandler -> IO ()
blur = undefined


click :: JQuery -> JEventHandler -> IO ()
click = _click

foreign import jscript "%1.click(%2)"
  _click :: JQuery -> JEventHandler -> IO ()


keypress :: JQuery -> JEventHandler -> IO ()
keypress = undefined


onDocumentReady :: JSFunPtr (IO ()) -> IO ()
onDocumentReady f = _ready f

foreign import jscript "$('document').ready(%1)"
  _ready :: JSFunPtr (IO ()) -> IO ()
  
foreign import jscript "wrapper"
  mkJEventHandler :: EventHandler -> IO JEventHandler
  
foreign import jscript "wrapper"
  mkJUIEventHandler :: UIEventHandler -> IO JUIEventHandler
  
foreign import jscript "wrapper"
  mkJUIThisEventHandler :: UIThisEventHandler -> IO JUIThisEventHandler
  
  
foreign import jscript "wrappedJQueryUIEvent(%1)"
  wrappedJQueryUIEvent :: JUIThisEventHandler -> IO JUIEventHandler
  
-------------------------------------------------------------------------------
-- DOM Manipulation

append :: JQuery -> JQuery -> IO ()
append = _append

appendString :: JQuery -> String -> IO ()
appendString jq str = do jq' <- jQuery str
                         _append jq jq'

foreign import jscript "%1.append(%*)"
  _append :: JQuery -> JQuery -> IO ()

replaceWith :: JQuery -> JQuery -> IO ()
replaceWith = _replaceWith

foreign import jscript "%1.replaceWith(%2)"
  _replaceWith :: JQuery -> JQuery -> IO ()
  
replaceWithString :: JQuery -> String -> IO ()
replaceWithString jq s = jQuery s >>= replaceWith jq

-------------------------------------------------------------------------------
-- Dynamic loading

loadSrcFile :: String -> IO ()
loadSrcFile src = do let src' = toJS src :: JSString
                     scriptTag <- jQuery "<script>"
                     scriptTag' <- setAttr "src" src' scriptTag
                     body <- jQuery "body"
                     append body scriptTag'
Something went wrong with that request. Please try again.