MyNixOS website logo
Description

Javascript Monadic Writer base package.

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)
Metadata

Version

0.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows