<commit_before>{-# LANGUAGE EmptyDataDecls #-}
module JCU where

import Control.Monad (liftM)

import Language.UHC.JScript.Types (toJS)
import Language.UHC.JScript.Primitives
import Language.UHC.JScript.JQuery.JQuery
import Language.UHC.JScript.W3C.HTML5 as HTML5

import Language.UHC.JScript.Assorted (alert)

import Language.UHC.JScript.JQuery.Ajax
import qualified Language.UHC.JScript.JQuery.AjaxQueue as AQ
----
--  App
----

import Templates
import Models

ajaxQ :: String -> AjaxCallback a -> AjaxCallback a -> IO ()
ajaxQ url onSuccess onFail = do
  AQ.ajaxQ "jcu_app" 
           (AjaxOptions { ao_url         = url,
                          ao_requestType = "GET",
                          ao_contentType = "application/json",
                          ao_dataType    = "json"
                        })
           onSuccess
           onFail
  alert "meh"
  
    
register_events :: [(String, JEventType, JEventHandler)] -> IO ()    
register_events = mapM_ (\ (e, event, eh) -> do elem <- jQuery e
                                                bind elem
                                                     event 
                                                     eh)
    
-- main :: IO ()
-- main = do init    <- ioWrap initialize
--           loadJS' <- ioWrap $ loadJS init
-- 
--           onWindowLoad $ loadJS'
--           init   <- ioWrap initialize
--           
-- foreign import jscript "window.onload" 
--   onWindowLoad :: JSFunPtr (IO ()) -> IO ()
-- 
-- loadJS :: JSFunPtr (IO ()) -> IO ()          
-- loadJS f = do alert "hi"
--               dynLoad "../brunch/src/vendor/jquery-1.6.2.js"
--               alert "bye"
--               onDocumentReady f

main :: IO ()
main = do init <- ioWrap initialize
          onDocumentReady init
          
initialize :: IO () 
initialize = do -- Dynamiccaly include the necessary files
                -- Events loading
                hiAlert <- eventWrap (\x -> do alert "Hi!"
                                               return True)
                register_events [("#button", "click", hiAlert)]
                button <- jQuery "#button"
                click button hiAlert
                
                foobaarz <- constWrap (\x -> do alert "foO!"
                                                return ())
                                                
                nop <- noop
                
                ajaxQ "http://localhost:8000/rules/stored" foobaarz nop

-- addRules :: JSPtr [Rule] -> IO ()
-- addRules = undefined                


foreign import jscript "jQuery.noop()"
  noop :: IO (JSFunPtr (JSPtr a -> IO ()))
  
foreign import jscript "wrapper"
  eventWrap :: (JQuery -> IO Bool)-> IO (JSFunPtr (JQuery -> IO Bool))

foreign import jscript "wrapper" 
  constWrap :: (JSPtr a -> IO ()) -> IO (AjaxCallback a)
  
foreign import jscript "wrapper"
  ioWrap :: IO () -> IO (JSFunPtr (IO ()))
  
dynLoad :: String -> IO ()  
dynLoad src = do doc <- HTML5.document
                 node     <- documentCreateElement "script"
                 elementSetAttribute node "src"   src
                 elementSetAttribute node "type"  "text/javascript"
                 -- Append the tag
                 headTags <- documentGetElementsByTagName doc "head"
                 headTag  <- nodeListItem headTags 0
                 elementAppendChild headTag node<commit_msg>Use the new ajax stuff<commit_after>{-# LANGUAGE EmptyDataDecls #-}
module JCU where

import Control.Monad (liftM)

import Language.UHC.JScript.Types (toJS)
import Language.UHC.JScript.Primitives
import Language.UHC.JScript.JQuery.JQuery
import Language.UHC.JScript.W3C.HTML5 as HTML5

import Language.UHC.JScript.Assorted (alert)

import Language.UHC.JScript.JQuery.Ajax as Ajax
import qualified Language.UHC.JScript.JQuery.AjaxQueue as AQ
----
--  App
----

import Templates
import Models

ajaxQ :: String -> AjaxCallback a b -> AjaxCallback a b -> IO ()
ajaxQ url onSuccess onFail = do
  AQ.ajaxQ "jcu_app" 
           (AjaxOptions { ao_url         = url,
                          ao_requestType = Ajax.GET,
                          ao_contentType = "application/json",
                          ao_dataType    = "json"
                        })
           onSuccess
           onFail
  alert "meh"
  
    
register_events :: [(String, JEventType, JEventHandler)] -> IO ()    
register_events = mapM_ (\ (e, event, eh) -> do elem <- jQuery e
                                                bind elem
                                                     event 
                                                     eh)
    
-- main :: IO ()
-- main = do init    <- ioWrap initialize
--           loadJS' <- ioWrap $ loadJS init
-- 
--           onWindowLoad $ loadJS'
--           init   <- ioWrap initialize
--           
-- foreign import jscript "window.onload" 
--   onWindowLoad :: JSFunPtr (IO ()) -> IO ()
-- 
-- loadJS :: JSFunPtr (IO ()) -> IO ()          
-- loadJS f = do alert "hi"
--               dynLoad "../brunch/src/vendor/jquery-1.6.2.js"
--               alert "bye"
--               onDocumentReady f

main :: IO ()
main = do init <- ioWrap initialize
          onDocumentReady init
          
initialize :: IO () 
initialize = do -- Dynamiccaly include the necessary files
                -- Events loading
                hiAlert <- eventWrap (\x -> do alert "Hi!"
                                               return True)
                register_events [("#button", "click", hiAlert)]
                button <- jQuery "#button"
                click button hiAlert
                
                let foobaarz = (\x y z-> do alert "foO!"
                                            return ())
                                                
                nop <- noop
                
                ajaxQ "http://localhost:8000/rules/stored" foobaarz (\x y z -> return ())

-- addRules :: JSPtr [Rule] -> IO ()
-- addRules = undefined                


foreign import jscript "jQuery.noop()"
  noop :: IO (JSFunPtr (JSPtr a -> String -> JSPtr b -> IO()))
  
foreign import jscript "wrapper"
  eventWrap :: (JQuery -> IO Bool)-> IO (JSFunPtr (JQuery -> IO Bool))

foreign import jscript "wrapper"
  ioWrap :: IO () -> IO (JSFunPtr (IO ()))
  
dynLoad :: String -> IO ()  
dynLoad src = do doc <- HTML5.document
                 node     <- documentCreateElement "script"
                 elementSetAttribute node "src"   src
                 elementSetAttribute node "type"  "text/javascript"
                 -- Append the tag
                 headTags <- documentGetElementsByTagName doc "head"
                 headTag  <- nodeListItem headTags 0
                 elementAppendChild headTag node
{'fraction_non_alphanumeric': 0.106322696093051, 'fraction_numerical': 0.003578884580972264, 'mean_word_length': 1.378368794326241, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1524823', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1750, 'n_words': 508}