Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Main where
- import Control.Monad.Eff
- import qualified Control.Monad.Eff.Ractive as Ract
- import Debug.Trace
- import Control.Apply ((*>),(<*))
- import Control.Bind ((>=>))
- import Control.Monad.Trans
- import Control.Monad.Cont.Trans
- import Data.Maybe
- import Network.HTTP
- import Network.XHR
- import Tutorial.Ractive
- type Template = String
- type TutorialPartials = {
- output :: Template,
- content :: Template}
- tutorialPartials :: Template -> Template -> TutorialPartials
- tutorialPartials output content = {output: output, content: content}
- type TutorialConfig = {
- contentTemplate :: URI,
- outputTemplate :: URI,
- element :: String,
- template :: String}
- data Launcher = Launcher TutorialConfig
- type TutorialConfigEff = Eff(xhr::XHR,ractiveM::Ract.RactiveM,trace::Trace)
- ractiveTemplate = "#ractive-template"
- ractiveElement = "ractive-element"
- template :: String -> String -> URI -> URI -> TutorialConfig
- template element template baseUri dir = {contentTemplate: content, outputTemplate: output, element: element, template: template}
- where
- content = baseUri ++ "/" ++ dir ++ "/" ++ "content.html"
- output = baseUri ++ "/" ++ dir ++ "/" ++ "output.html"
- templateTuto :: URI -> TutorialConfig
- templateTuto = template ractiveElement ractiveTemplate "http://localhost/templates"
- templateTuto1 :: TutorialConfig
- templateTuto1 = templateTuto "tut1"
- getFrom :: forall r. URI -> (RequestConfig r -> EffXHR r Unit)
- getFrom url = (flip $ flip get url) {}
- getTemplate :: forall e a b. (String -> a -> b) -> URI -> a -> ContT Unit (Eff (xhr::XHR | e)) b
- getTemplate fun uri = \v -> ContT \next -> do
- getFrom uri $ defaultConfig {onLoadEnd = \response -> next $ fun (responseText response) v}
- outputPartialTut :: forall e. URI -> String -> ContT Unit (Eff (xhr::XHR | e)) (String -> TutorialPartials)
- outputPartialTut = getTemplate \tmpl -> \_ -> tutorialPartials tmpl
- contentPartialTut :: forall e. URI -> (String -> TutorialPartials) -> ContT Unit (Eff (xhr::XHR | e)) TutorialPartials
- contentPartialTut = getTemplate \tmpl -> \fun -> fun tmpl
- ractiveTut :: forall e. TutorialConfig -> TutorialPartials -> ContT Unit (Eff (ractiveM::Ract.RactiveM | e)) Ract.Ractive
- ractiveTut config partials = ContT \rest -> do
- ractive <- Ract.ractive config.template config.element {}
- Ract.setPartial "outputP" partials.output ractive
- Ract.setPartial "contentP" partials.content ractive
- loadTutorial :: forall e. TutorialConfig -> String -> ContT Unit (Eff (ractiveM::Ract.RactiveM,xhr::XHR | e)) Ract.Ractive
- loadTutorial config = outputPartialTut config.outputTemplate >=> contentPartialTut config.contentTemplate >=> ractiveTut config
- launch :: forall e. Eff (trace :: Trace, ractiveM :: Ract.RactiveM, xhr :: XHR | e) Unit
- launch = runContT (loadTutorial templateTuto1 "DDD") $ \r ->
- trace "DONE"
Advertisement
Add Comment
Please, Sign In to add comment