-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
281 lines (252 loc) · 8.75 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>CoffeeBeans</title>
<script src="lib/jquery.js"></script>
<script src="lib/coffee-script.js"></script>
<script src="lib/underscore.js"></script>
<script src="lib/underscore.math/underscore.math.js"></script>
<script src="lib/backbone.js"></script>
<script src="lib/backbone-localstorage.js"></script>
<script src="lib/js2coffee.min.js"></script>
<script src="lib/require.js"></script>
<script src=lib/codemirror2/lib/codemirror.js></script>
<script src=lib/codemirror2/lib/util/runmode.js></script>
<script src=lib/codemirror2/mode/javascript/javascript.js></script>
<script src=coffeescript-mode.js></script>
<script src=lib/codemirror2/lib/util/simple-hint.js></script>
<script src=lib/codemirror2/lib/util/javascript-hint.js></script>
<link rel=stylesheet href=lib/codemirror2/lib/util/simple-hint.css>
<link rel=stylesheet href=lib/codemirror2/lib/codemirror.css>
<link rel=stylesheet href=css/coffeescript.css>
<link rel=stylesheet href=css/style.css>
</head>
<body>
<div id="console">
<div class="inner">
<div id="puts">
</div>
<div id="coffee-error">
<span id="compile-error">
</span>
</div>
<div class="buttons">
<button id="store-code">Store</button>
<button id="bloh">!</button>
<button id="blah">!</button>
<button id="bleh">!</button>
</div>
<div id="input-wrap"><textarea id="input" class="input" wrap="off" rows="1"></textarea></div>
</div>
</div>
<div id="main">
<h1>CoffeeBeans</h1>
<p>
A <a href="http://coffeescript.org/">CoffeeScript</a> console. Here's the <a href="https://github.com/syntagmatic/coffee-beans">source</a>.
</p>
<p>
The code editor is on the bottom left.
</p>
<div>
<div class="half">
<h2>Keyboard Shortcuts</h2>
<table class="shortcuts">
<tr>
<td>Shift-Enter</td>
<td>Run code</td>
</tr>
<tr>
<td>Tab</td>
<td>Autocomplete</td>
</tr>
<tr>
<td>Enter</td>
<td>New Line</td>
</tr>
<tr>
<td>Shift <span class="beans-arrow">→</span></td>
<td>Indent</td>
</tr>
<tr>
<td>Shift <span class="beans-arrow">←</span></td>
<td>Outdent</td>
</tr>
</table>
</div>
<div class="half">
<h2>History</h2>
<table class="shortcuts">
<tr>
<td>Up</td>
<td>Previous command</td>
</tr>
<tr>
<td>Down</td>
<td>Next command</td>
</tr>
</table>
</div>
<div class="clear"></div>
</div>
<p>
Eventually there will be a tutorial here for beginning programmers. For now, there are a few notes and code examples.
</p>
<h2>Get Pictures of Cats</h2>
<p>Let's get some adorable pictures from Reddit:</p>
<pre class="highlight">
json 'http://www.reddit.com/r/aww.json?jsonp=?', (data) ->
puts pretty data
window.aww = pluck(data['data']['children'], 'data')
</pre>
<p>When the data loads, it'll fill up the console. The part that we need, data about the links, will be saved to the global variable <em>aww</em>. Take a look at the first link:</p>
<pre class="highlight">pretty aww[0]</pre>
<p>The thumbnail property probably has the highest density of cuteness, so let's make a function that turns <em>aww[0]</em> into an image:</p>
<pre class="highlight">
thumbnail = (obj) ->
html "<img src='#{obj.thumbnail}'/>"
</pre>
<p>Let's test it out:</p>
<pre class="highlight">
thumbnail aww[0]
</pre>
<p>If a picture appeared, get ready for cuteness overload. Let's use <em>each</em> to see them all!</p>
<pre class="highlight">
each aww, (obj) -> puts thumbnail obj
</pre>
<h1>Basics</h1>
<h2>Arrays</h2>
<pre class="highlight">
five = [1,2,3,4,5]</pre>
<h2>Objects</h2>
<pre class="highlight">
fruit_colors =
apple: 'red'
banana: 'yellow'
kiwi: 'green'</pre>
<h2>Functions</h2>
<pre class="highlight">
# define a function
square = (x) -> x*x
# call the function
square(5)
</pre>
<h2>Comprehensions</h2>
<pre class="highlight">
three_squares = (x*x for x in [1,2,3])</pre>
<h2>Chained Comparisons</h2>
<pre class="highlight">
age = 14
teen = 12 < age < 20</pre>
<h2>type</h2>
<p>Get the type of an object</p>
<pre class="highlight">
type 2 # number
type "juice" # string
type {a:1} # object
type -> # function</pre>
<h1>Underscore Functions</h1>
<p>All these <a href="http://documentcloud.github.com/underscore/">Underscore.js</a> functions are available in the global namespace.</a> So instead of:</p>
<pre class="highlight">
_.each([1,2,3], puts)</pre>
<p>The _. can be omitted. The parens can also be omitted- CoffeeScript will take care of them</p>
<pre class="highlight">
each [1,2,3], puts</pre>
<h2><a href="http://documentcloud.github.com/underscore/#each">each</a></h2>
<p>Create three alerts:</p>
<pre class="highlight">
each ['here', 'we', 'go!'], alert</pre>
<h2><a href="http://documentcloud.github.com/underscore/#map">map</a></h2>
<p>Convert radians to degrees</p>
<pre class="highlight">
rad2deg = (x) -> x*180/pi
map [pi/4, 3*pi/8, pi/2], rad2deg</pre>
<h2><a href="http://documentcloud.github.com/underscore/#filter">filter</a></h2>
<p>Filter even numbers</p>
<pre class="highlight">
even = (x) -> x % 2 == 0
filter [1,2,3,4,5], even</pre>
<h2><a href="http://documentcloud.github.com/underscore/#pluck">pluck</a></h2>
<p>Extract names from a list of objects</p>
<pre class="highlight">
stooges = [
{name: 'moe', age: 40}
{name : 'larry', age : 50}
{name : 'curly', age : 60}
]
pluck stooges, 'name'
</pre>
<h2><a href="http://documentcloud.github.com/underscore/#size">size</a></h2>
<p>Return the number of values in the list</p>
<pre class="highlight">
size {one : 1, two : 2, three : 3}
</pre>
<h2><a href="http://documentcloud.github.com/underscore/#shuffle">shuffle</a></h2>
<p>Shuffle an array using <a href="http://bost.ocks.org/mike/shuffle/">Fisher-Yates</a></p>
<pre class="highlight">
shuffle [1,2,3,4,5,6]</pre>
<h2>Backbone Events</h2>
<p>A module which provides named <a href="http://documentcloud.github.com/backbone/#Events">events</a> on an object.</p>
<p>First, make a button:</p>
<pre class="highlight">
html "<button id='my-phone'>Call Me!</button>"
</pre>
<p>Now trigger an event when the button is clicked:</p>
<pre class="highlight">
phone = {}
extend phone, Backbone.Events
# bind the phone's "ring" event to the button's "click"
$('#my-phone').on 'click', ->
phone.trigger "ring"
# print a message when the phone rings
phone.on "ring", ->
puts "someone's calling..."
</pre>
<p>Click the button!</p>
<h2>Backbone Models</h2>
<p>A <a href="http://documentcloud.github.com/backbone/#Model">Model</a> is a container for interactive data. Let's make a model to play with this sidebar's color.</p>
<pre class="highlight">
Sidebar = Backbone.Model.extend
color: (new_color) ->
@set {color: new_color}
sidebar = new Sidebar;
sidebar.on 'change:color', (model, color) ->
$('#main').css {background: color}
</pre>
<p>Now pick a good <a href="http://www.colorschemer.com/online.html">color</a> for the sidebar.</p>
<pre class="highlight">
sidebar.color '#FFCC33'
</pre>
<h2>Colored puts</h2>
<p>Print out colored lines.</p>
<pre class="highlight">
color_puts = (text, color) ->
puts html "<pre style='color:#{color}'>#{text}</pre>"
return # no need to return anything
green_puts = (text) -> color_puts(text, '#00bd12')
red_puts = (text) -> color_puts(text, '#bd1107')
pink_puts = (text) -> color_puts(text, '#ff3366')
green_puts "eggs and ham"
red_puts "danger will robinson"
pink_puts "happy to be here"
</pre>
<h2>SVG</h2>
<p>Print an SVG shape.</p>
<pre class="highlight">
html "<svg height=100><circle cx=50 cy=50 r=40 fill=violet></svg>"</pre>
<h1>Resources</h1>
<ul>
<li><a href="http://coffeescript.org/">CoffeeScript</a></li>
<li><a href="http://documentcloud.github.com/underscore/">Underscore.js</a></li>
<li><a href="http://documentcloud.github.com/backbone/">Backbone.js</a></li>
<li><a href="http://codemirror.net/">CodeMirror</a></li>
<li><a href="http://eloquentjavascript.net/">Eloquent JavaScript</a></li>
</ul>
</div>
<script src=commands.js></script>
<script src=beans.js></script>
<script>
var beans = CoffeeBeans();
</script>
</body>
</html>