The jsmw package

[Tags:bsd3, library]

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:

 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)


Versions 0.1
Dependencies base (>=4.0.0), DOM (==2.0.1), mtl, WebBits (==0.15) [details]
License BSD3
Copyright 2009, Dmitry Golubovsky
Author Dmitry Golubovsky
Stability Unknown
Category Language
Uploaded Sun Apr 26 03:43:02 UTC 2009 by DmitryGolubovsky
Distributions NixOS:0.1
Downloads 272 total (3 in the last 30 days)
0 []
Status Docs uploaded by user
Build status unknown [no reports yet]




Maintainer's Corner

For package maintainers and hackage trustees