/
examples.html
161 lines (148 loc) · 4.95 KB
/
examples.html
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!--
To view these examples live, visit:
http://htmlpreview.github.io/?https://raw.github.com/baconjs/bacon.js/master/examples/examples.html
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="../lib/jquery.js"></script>
<script src="../lib/underscore.js"></script>
<script src="../dist/Bacon.js"></script>
<script>
function isEmpty(s) { return s.length == 0 }
function always(value) { return function() { return value }}
function keyCodeIs(keyCode) {
return function(event) { return event.keyCode == keyCode }
}
function keyDownEvents(keyCode) {
return $(document).asEventStream("keydown").filter(keyCodeIs(keyCode))
}
function keyUpEvents(keyCode) {
return $(document).asEventStream("keyup").filter(keyCodeIs(keyCode))
}
function keyStateProperty(keyCode) {
return keyDownEvents(keyCode).map(always("DOWN"))
.merge(keyUpEvents(keyCode).map(always("UP")))
.toProperty("UP")
}
$(function() {
// Simple click example
$("#clikme")
.asEventStream("click")
.subscribe(function(event) { alert("mmmm... bacon!") })
// Combinators example
keyStateProperty(32)
.onValue(function(value) { $("#spacebar-value").text(value) })
// Enable/disable example
$("#enabling input")
.asEventStream("keyup")
.map(function(event) { return $(event.target).val() })
.toProperty("")
.map(isEmpty)
.assign($("#enabling button"), "attr", "disabled")
// Echo example
$("#echo input")
.asEventStream("keyup")
.map(function(event) { return $(event.target).val() })
.toProperty("")
.assign($("#echo .output"), "text")
// Combination lock example
function selectElementValue(el) {
function getValue() {
return el.val()
}
return el.asEventStream("change")
.map(getValue)
.toProperty(getValue())
}
Bacon.combineAsArray(
$("#combo select").map(function() {
return selectElementValue($(this)).map(parseInt)
}).get()
).map(_.isEqual, [1,8,0])
.toProperty()
.not()
.assign($("#combo button"), "attr", "disabled")
$("#combo button").click(function() { $("#combo .vault-contents").slideDown() })
// Strikeout (skip/take) example
function strikeout () {
$("#batter-up .ump-call").text("Strike three, you're out!")
}
$("#batter-up button")
.asEventStream("click").skip(2).take(1).onValue(strikeout)
})
</script>
</head>
<body>
<h1>bacon.js example page</h1>
<div>
<h2>Simplest example:</h2>
<span id="clikme">click me</span>
</div>
<div>
<h2>Combinators example:</h2>
SPACEBAR value=<span id="spacebar-value"></span>
</div>
<div id="enabling">
<h2>Enable/disable example:</h2>
<input type="text" placeholder="required field">
<button>Don't push me</button>
</div>
<div id="echo">
<h2>Echo example:</h2>
<input type="text">
<span class="output"></span>
</div>
<div id="combo">
<h2>Combination lock:</h2>
<select>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option selected="selected">9</option>
</select>
<select>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option selected="selected">9</option>
</select>
<select>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option selected="selected">9</option>
</select>
<button>Open vault</button>
<small>Hint: Try combo <b>180</b></small>
<div class="vault-contents" style="display:none">
<p style="font-style:italic">“Your bacon, sir.” <span style="font-weight:bold; color:red">≈</span></p>
<p><small>Fun fact: It takes approximately 180 seconds to microwave two slices of bacon</small></p>
</div>
</div>
<div id="batter-up">
<h2>Batter up! (take/skip example):</h2>
<button>Swing</button>
<span class="ump-call" style="font-weight:bold"></span>
<p style="font-style:italic">♪♫ ...for it's one, two, three strikes you're out at the old ball game ♪♫ </p>
</div>
</body>
</html>