-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReactiveJQueryTest.purs
128 lines (102 loc) · 3.54 KB
/
ReactiveJQueryTest.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module ReactiveJQueryTest where
import Prelude ((+), (++), (<$>), (<*>), ($), (<<<), flip, return, show)
import Control.Monad
import Control.Monad.Eff
import Control.Monad.JQuery
import Control.Reactive
import Control.Reactive.JQuery
import Data.Array (map, head, length)
import Data.Foldable
import Data.Foreign
import Data.Maybe
import Data.Monoid
import Data.Traversable
import Debug.Trace
import Global (parseInt)
main = do
personDemo
todoListDemo
greet firstName lastName = "Hello, " ++ firstName ++ " " ++ lastName ++ "!"
personDemo = do
-- Create new reactive variables to hold the user's names
firstName <- newRVar "John"
lastName <- newRVar "Smith"
-- Get the document body
b <- body
-- Create a text box for the first name
firstNameDiv <- create "<div>"
firstNameInput <- create "<input>"
"First Name: " `appendText` firstNameDiv
firstNameInput `append` firstNameDiv
firstNameDiv `append` b
-- Create a text box for the last name
lastNameDiv <- create "<div>"
lastNameInput <- create "<input>"
"Last Name: " `appendText` lastNameDiv
lastNameInput `append` lastNameDiv
lastNameDiv `append` b
-- Bind the text box values to the name variables
bindValueTwoWay firstName firstNameInput
bindValueTwoWay lastName lastNameInput
-- Create a paragraph to display a greeting
greeting <- create "<p>"
{ color: "red" } `css` greeting
greeting `append` b
-- Bind the text property of the greeting paragraph to a computed property
let greetingC = greet <$> toComputed firstName <*> toComputed lastName
bindTextOneWay greetingC greeting
todoListDemo = do
-- Get the document body
b <- body
-- Create an array
arr <- newRArray
text1 <- newRVar "Learn PureScript"
comp1 <- newRVar false
insertRArray arr { text: text1, completed: comp1 } 0
ul <- create "<ul>"
-- Bind the ul to the array
bindArray arr ul $ \entry indexR -> do
li <- create "<li>"
completedInput <- create "<input>"
setAttr "type" "checkbox" completedInput
completedInput `append` li
sub1 <- bindCheckedTwoWay entry.completed completedInput
textInput <- create "<input>"
textInput `append` li
sub2 <- bindValueTwoWay entry.text textInput
btn <- create "<button>"
"Remove" `appendText` btn
flip (on "click") btn $ do
index <- readRVar indexR
removeRArray arr index
btn `append` li
return { el: li, subscription: sub1 <> sub2 }
ul `append` b
-- Add button
newEntryDiv <- create "<div>"
btn <- create "<button>"
"Add" `appendText` btn
btn `append` newEntryDiv
newEntryDiv `append` b
flip (on "click") btn $ do
text <- newRVar ""
completed <- newRVar false
arr' <- readRArray arr
insertRArray arr { text: text, completed: completed } (length arr')
-- Create a paragraph to display the next task
nextTaskLabel <- create "<p>"
nextTaskLabel `append` b
let nextTask = do
task <- head <$> toComputedArray arr
case task of
Nothing -> return "Done!"
Just { text = text } -> (++) "Next task: " <$> toComputed text
bindTextOneWay nextTask nextTaskLabel
-- Create a paragraph to display the task counter
counterLabel <- create "<p>"
counterLabel `append` b
let counter = (flip (++) " tasks remaining") <<< show <$> do
rs <- toComputedArray arr
cs <- map (\c -> if c then 0 else 1) <$> traverse (\entry -> toComputed entry.completed) rs
return $ foldl (+) 0 cs
bindTextOneWay counter counterLabel