jsmw

Javascript Monadic Writer base package.

Latest on Hackage:0.1

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD3 licensed by Dmitry Golubovsky
Maintained by golubovsky@gmail.com

An EDSL inspired in part by HJ(ava)Script and HSP aimed at coding in typed Javascript. It uses WebBits as the underlying representation of Javascript.

This package provides the basic API sufficient to create simple dynamic web pages.

Below is a simple example of a program that increments or decrements a value in an input field depending on whether Enter or Shift-Enter was pressed.

Save this program in a file, and run runghc on the file. Javascript will be output to be placed into HEAD element of a blank HTML page. Give the page body attribute:

<body onload="javascript:main()">

to run the script when the page loads.

A live example of this program is available here:

http://code.haskell.org/yc2js/examples/ex1.html

module Main where

import Prelude hiding (putStrLn)
import System.IO.UTF8
import BrownPLT.JavaScript
import BrownPLT.JavaScript.PrettyPrint
import Control.Monad
import Language.JSMW
import Data.DOM
import Data.DOM.Dom
import Data.DOM.Html2
import Data.DOM.Events
import Data.DOM.KeyEvent
import Data.DOM.HTMLHRElement
import Data.DOM.HTMLInputElement

main = putStrLn $ show $ stmt $
  FunctionStmt undefined (Id undefined "main") [] (getBlock ( runJSMW 0 q))


q = do
  passive (mkText $ string
    "Example 1: Press Enter to increase value, Shift-Enter to decrease value")
  passive mkHr
  mkInput `container` (do
    setHandler "keypress" plusOne
    ask >>= set'value (string "0") >>= focus)

plusOne :: OnHandler TKeyEvent THTMLInputElement

plusOne e = do
  c <- getm'keyCode e
  switch (c) $ do
    cDOM_VK_ENTER --> do i <- ask
                         v <- getm'value i
                         vv <- switch v $ do
                           "" --> stringM "0"
                           none (return v)
                         n <- parseInt vv 0
                         shft <- get'shiftKey e
                         n2 <- switch shft $ do
                           True --> return (n - number 1)
                           False --> return (n + number 1)
                         once =<< (toString n2 >>= flip set'value i)
                         return false
    none (return true)
Depends on:
Used by 1 package:
comments powered byDisqus