Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HelloJs and GroceriesJs #210

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ Running a web-compatible recipe:
| | :heavy_check_mark: | [FindDomElementJs](recipes/FindDomElementJs) | This recipe shows how to find elements in the DOM by using query selectors. |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/FormsReactHooks/src/Main.purs)) | [FormsReactHooks](recipes/FormsReactHooks) | A React port of the ["User Interface - Forms" Elm Example](https://elm-lang.org/examples/forms). |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/GroceriesHalogenHooks/src/Main.purs)) | [GroceriesHalogenHooks](recipes/GroceriesHalogenHooks) | A Halogen port of the ["HTML - Groceries" Elm Example](https://elm-lang.org/examples/groceries). |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/GroceriesJs/src/Main.purs)) | [GroceriesJs](recipes/GroceriesJs) | A framework-free port of the ["HTML - Groceries" Elm Example](https://elm-lang.org/examples/groceries). |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/GroceriesReactHooks/src/Main.purs)) | [GroceriesReactHooks](recipes/GroceriesReactHooks) | A React port of the ["HTML - Groceries" Elm Example](https://elm-lang.org/examples/groceries). |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/HelloConcur/src/Main.purs) - [fixme](recipes/HelloConcur/tryFixMe.md)) | [HelloConcur](recipes/HelloConcur) | A Concur port of the ["HTML - Hello" Elm Example](https://elm-lang.org/examples/hello). |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/HelloHalogenHooks/src/Main.purs)) | [HelloHalogenHooks](recipes/HelloHalogenHooks) | A Halogen port of the ["HTML - Hello" Elm Example](https://elm-lang.org/examples/hello). |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/HelloJs/src/Main.purs)) | [HelloJs](recipes/HelloJs) | A framework-free port of the ["HTML - Hello" Elm Example](https://elm-lang.org/examples/hello). |
| :heavy_check_mark: | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/HelloLog/src/Main.purs)) | [HelloLog](recipes/HelloLog) | This recipe shows how to run a simple "Hello world!" program in either the node.js or web browser console. |
| | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/HelloReactHooks/src/Main.purs)) | [HelloReactHooks](recipes/HelloReactHooks) | A React port of the ["HTML - Hello" Elm Example](https://elm-lang.org/examples/hello). |
| :heavy_check_mark: | :heavy_check_mark: ([try](https://try.ps.ai/?github=JordanMartinez/purescript-cookbook/master/recipes/HeterogenousArrayLog/src/Main.purs)) | [HeterogenousArrayLog](recipes/HeterogenousArrayLog) | This recipe demonstrates how to create a heterogenous array and process its elements. |
Expand Down
13 changes: 13 additions & 0 deletions recipes/GroceriesJs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc-package/
/.psc*
/.purs*
/.psa*
/.spago
/web-dist/
/prod-dist/
/prod/
11 changes: 11 additions & 0 deletions recipes/GroceriesJs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# GroceriesJs

A framework-free port of the ["HTML - Groceries" Elm Example](https://elm-lang.org/examples/groceries).

Note that this example involves some lower-level DOM manipulation, so it's generally preferred to use a framework instead.

## Expected Behavior:

### Browser

The browser will display a header of text and an unordered list of grocery items.
6 changes: 6 additions & 0 deletions recipes/GroceriesJs/spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ name = "GroceriesJs"
, dependencies =
[ "console", "effect", "psci-support", "web-html" ]
, packages = ../../packages.dhall
, sources = [ "recipes/GroceriesJs/src/**/*.purs" ]
}
64 changes: 64 additions & 0 deletions recipes/GroceriesJs/src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module GroceriesJs.Main where

import Prelude
import Data.Foldable (traverse_)
import Data.Maybe (maybe)
import Effect (Effect)
import Effect.Exception (throw)
import Web.DOM (Document, Node)
import Web.DOM.Document (createElement)
import Web.DOM.Element as Element
import Web.DOM.Node (appendChild, setTextContent)
import Web.HTML (window)
import Web.HTML.HTMLDocument as HTMLDocument
import Web.HTML.HTMLElement as HTMLElement
import Web.HTML.Window (document)

main :: Effect Unit
main = do
htmlDoc <- document =<< window
body <- maybe (throw "Could not find body element") pure =<< HTMLDocument.body htmlDoc
let
doc = HTMLDocument.toDocument htmlDoc

-- Create DOM elements
divElem <- createElement "div" doc
h1Elem <- createElement "h1" doc
ulElem <- createElement "ul" doc

-- Convert DOM (or HTML) elements to DOM nodes
let
bodyNode = HTMLElement.toNode body
divNode = Element.toNode divElem
h1Node = Element.toNode h1Elem
ulNode = Element.toNode ulElem

-- Fill in some contents
setTextContent "My Grocery List" h1Node

-- Define structure in DOM
void $ appendChild divNode bodyNode
void $ appendChild h1Node divNode
void $ appendChild ulNode divNode
traverse_ (makeAndAppendLiNode doc ulNode) groceryList

makeAndAppendLiNode :: Document -> Node -> String -> Effect Node
makeAndAppendLiNode doc parentNode text = do
liElem <- createElement "li" doc
let
liNode = Element.toNode liElem
setTextContent text liNode
appendChild liNode parentNode

groceryList :: Array String
groceryList =
[ "Black Beans"
, "Limes"
, "Greek Yogurt"
, "Cilantro"
, "Honey"
, "Sweet Potatoes"
, "Cumin"
, "Chili Powder"
, "Quinoa"
]
13 changes: 13 additions & 0 deletions recipes/GroceriesJs/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>GroceriesJs</title>
</head>

<body>
<script src="./index.js"></script>
</body>

</html>
2 changes: 2 additions & 0 deletions recipes/GroceriesJs/web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
require("../../../output/GroceriesJs.Main/index.js").main();
13 changes: 13 additions & 0 deletions recipes/HelloJs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc-package/
/.psc*
/.purs*
/.psa*
/.spago
/web-dist/
/prod-dist/
/prod/
11 changes: 11 additions & 0 deletions recipes/HelloJs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# HelloJs

A framework-free port of the ["HTML - Hello" Elm Example](https://elm-lang.org/examples/hello).

See [GroceriesJs](../GroceriesJS) for a more elaborate example of rendering HTML without a framework.

## Expected Behavior:

### Browser

The browser will display "Hello!" as an HTML element.
6 changes: 6 additions & 0 deletions recipes/HelloJs/spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ name = "HelloJs"
, dependencies =
[ "console", "effect", "psci-support", "web-html" ]
, packages = ../../packages.dhall
, sources = [ "recipes/HelloJs/src/**/*.purs" ]
}
19 changes: 19 additions & 0 deletions recipes/HelloJs/src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module HelloJs.Main where

import Prelude
import Data.Maybe (maybe)
import Effect (Effect)
import Effect.Exception (throw)
import Web.DOM.Node (setTextContent)
import Web.HTML (window)
import Web.HTML.HTMLDocument as HTMLDocument
import Web.HTML.HTMLElement as HTMLElement
import Web.HTML.Window (document)

main :: Effect Unit
main = do
doc <- document =<< window
body <- maybe (throw "Could not find body element") pure =<< HTMLDocument.body doc
let
bodyNode = HTMLElement.toNode body
setTextContent "Hello!" bodyNode
13 changes: 13 additions & 0 deletions recipes/HelloJs/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>HelloJs</title>
</head>

<body>
<script src="./index.js"></script>
</body>

</html>
2 changes: 2 additions & 0 deletions recipes/HelloJs/web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
require("../../../output/HelloJs.Main/index.js").main();