Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 50993
1
// Generated by CoffeeScript 1.9.2
2
var AnswerMultipleChoiceQuestion, AnswerRadioInput, DoodleArea, IntervalMixin, MyForm, MyForm2, NeatComponent, NeatComponent2, NumberComponent, RichText, Since2014, Timer, done, log, num, page, rclass, rtypes, the_choices, uniqueId,
3
slice = [].slice;
4
5
rclass = React.createClass;
6
7
rtypes = React.PropTypes;
8
9
log = function() {
10
var s;
11
s = 1 <= arguments.length ? slice.call(arguments, 0) : [];
12
return console.log.apply(console, s);
13
};
14
15
uniqueId = function(s) {
16
return "" + s + (Math.random());
17
};
18
19
AnswerRadioInput = rclass({
20
propTypes: {
21
id: rtypes.string,
22
name: rtypes.string.isRequired,
23
label: rtypes.string.isRequired,
24
value: rtypes.string.isRequired,
25
checked: rtypes.bool,
26
onChanged: rtypes.func.isRequired
27
},
28
getDefaultProps: function() {
29
return {
30
id: null,
31
checked: false
32
};
33
},
34
componentWillReceiveProps: function(nextProps) {
35
if (nextProps.checked != null) {
36
return this.setState({
37
checked: nextProps.checked
38
});
39
}
40
},
41
getInitialState: function() {
42
var id;
43
id = this.props.id ? this.props.id : uniqueId("radio-");
44
return {
45
checked: !!this.props.checked,
46
id: id,
47
name: id
48
};
49
},
50
handleChanged: function(e) {
51
var checked;
52
checked = e.target.checked;
53
this.setState({
54
checked: checked
55
});
56
if (checked) {
57
return this.props.onChanged(this.props.value);
58
}
59
},
60
render: function() {
61
return React.createElement("div", {
62
"className": "radio"
63
}, React.createElement("label", {
64
"htmlFor": this.props.id
65
}, React.createElement("input", {
66
"type": "radio",
67
"name": this.props.name,
68
"id": this.props.id,
69
"value": this.props.value,
70
"checked": this.state.checked,
71
"onChange": this.handleChanged
72
}), "\u00a0\u00a0", this.props.label));
73
}
74
});
75
76
AnswerMultipleChoiceQuestion = rclass({
77
propTypes: {
78
value: rtypes.string,
79
choices: rtypes.array.isRequired,
80
onCompleted: rtypes.func.isRequired
81
},
82
getInitialState: function() {
83
return {
84
id: uniqueId('multiple-choices-'),
85
value: this.props.value
86
};
87
},
88
handleChanged: function(value) {
89
log("AnswerMultipleChoiceQuestion.handleChanged ", value);
90
this.setState({
91
value: value
92
});
93
return this.props.onCompleted(value);
94
},
95
renderChoices: function() {
96
var choice, i, j, len, ref, results;
97
ref = this.props.choices;
98
results = [];
99
for (i = j = 0, len = ref.length; j < len; i = ++j) {
100
choice = ref[i];
101
results.push(React.createElement(AnswerRadioInput, {
102
"id": "choice-" + i,
103
"key": "choice-" + i,
104
"name": this.state.id,
105
"label": choice,
106
"value": choice,
107
"checked": this.state.value === choice,
108
"onChanged": this.handleChanged
109
}));
110
}
111
return results;
112
},
113
render: function() {
114
return React.createElement("div", {
115
"className": "form-group"
116
}, React.createElement(Timer, null), React.createElement("label", {
117
"className": "survey-item-label",
118
"htmlFor": this.state.id
119
}, this.props.label), React.createElement("div", {
120
"className": "survey-item-content"
121
}, this.renderChoices()));
122
}
123
});
124
125
NeatComponent = rclass({
126
render: function() {
127
var n;
128
return React.createElement("div", {
129
"className": "neat-component"
130
}, (this.props.showTitle ? React.createElement("h1", null, "Showing this title") : void 0), React.createElement("hr", null), (function() {
131
var j, results;
132
results = [];
133
for (n = j = 1; j <= 5; n = ++j) {
134
results.push(React.createElement("p", {
135
"key": n
136
}, "This line has been printed ", n, " times"));
137
}
138
return results;
139
})());
140
}
141
});
142
143
NeatComponent2 = rclass({
144
title: function() {
145
if (this.props.showTitle) {
146
return React.createElement("h1", null, "Showing this title");
147
} else {
148
return React.createElement("h2", null, "Instead, just a subtitle");
149
}
150
},
151
loop0: function() {
152
var j, n, results;
153
results = [];
154
for (n = j = 1; j <= 10; n = ++j) {
155
results.push(React.createElement("div", {
156
"key": n
157
}, "Line ", n));
158
}
159
return results;
160
},
161
loop: function() {
162
var j, n, results;
163
results = [];
164
for (n = j = 1; j <= 3; n = ++j) {
165
results.push(React.createElement("div", {
166
"key": n
167
}, "Line ", n));
168
}
169
return results;
170
},
171
render: function() {
172
return React.createElement("div", {
173
"className": "neat-component"
174
}, React.createElement(NeatComponent, null), this.title(), React.createElement("hr", null), this.loop());
175
}
176
});
177
178
Timer = rclass({
179
getInitialState: function() {
180
return {
181
secondsElapsed: 0
182
};
183
},
184
tick: function() {
185
return this.setState({
186
secondsElapsed: this.state.secondsElapsed + 1
187
});
188
},
189
componentDidMount: function() {
190
return this.interval = setInterval(this.tick, 1000);
191
},
192
componentWillUnmount: function() {
193
return clearInterval(this.interval);
194
},
195
render: function() {
196
return React.createElement("div", null, " Seconds Elapsed: ", this.state.secondsElapsed, " ");
197
}
198
});
199
200
IntervalMixin = {
201
setInterval: function(cb, interval) {
202
var token;
203
token = setInterval(cb, interval);
204
this.__intervals.push(token);
205
return token;
206
},
207
componentDidMount: function() {
208
return this.__intervals = [];
209
},
210
componentWillUnmount: function() {
211
var j, len, ref, results, token;
212
ref = this.__intervals;
213
results = [];
214
for (j = 0, len = ref.length; j < len; j++) {
215
token = ref[j];
216
results.push(clearInterval(token));
217
}
218
return results;
219
}
220
};
221
222
Since2014 = rclass({
223
mixins: [IntervalMixin],
224
componentDidMount: function() {
225
return this.setInterval(this.forceUpdate.bind(this), 1000);
226
},
227
render: function() {
228
var from, to;
229
from = Number(new Date(2014, 0, 1));
230
to = Date.now();
231
return React.createElement("div", null, "Seconds since 2014: ", Math.round((to - from) / 1000));
232
}
233
});
234
235
DoodleArea = rclass({
236
render: function() {
237
return React.createElement("div", null, "Describe your doodle: ", React.createElement(RichText, null), React.createElement("canvas", {
238
"ref": "mainCanvas",
239
"width": "200",
240
"height": "100",
241
"style": {
242
border: '1px solid #000000'
243
}
244
}));
245
},
246
componentDidMount: function() {
247
var canvasNode, ctx;
248
canvasNode = this.refs.mainCanvas.getDOMNode();
249
ctx = canvasNode.getContext("2d");
250
ctx.fillStyle = "#0000ff";
251
return ctx.fillRect(0, 0, 150, 75);
252
}
253
});
254
255
RichText = rclass({
256
render: function() {
257
return React.createElement("div", {
258
"ref": "editableDiv",
259
"contentEditable": "true",
260
"width": "200",
261
"height": "100",
262
"style": {
263
border: '1px solid #000000'
264
},
265
"onKeyUp": this.handleKeyUp
266
}, window.localStorage.doodle);
267
},
268
handleKeyUp: function() {
269
var editor, html;
270
editor = this.refs.editableDiv.getDOMNode();
271
html = editor.innerHTML;
272
window.localStorage.doodle = html;
273
return console.log(html);
274
}
275
});
276
277
MyForm = rclass({
278
submitHandler: function(event) {
279
var helloTo;
280
event.preventDefault();
281
helloTo = this.refs.helloTo.getDOMNode().value;
282
return log(helloTo);
283
},
284
render: function() {
285
return React.createElement("form", {
286
"onSubmit": this.submitHandler
287
}, React.createElement("input", {
288
"ref": "helloTo",
289
"type": "text",
290
"defaultValue": "Hello World!"
291
}), React.createElement("br", null), React.createElement("button", {
292
"type": "submit"
293
}, "Speak"));
294
}
295
});
296
297
MyForm2 = rclass({
298
getInitialState: function() {
299
return {
300
helloTo: 'Hello world!'
301
};
302
},
303
handleChange: function(event) {
304
return this.setState({
305
helloTo: event.target.value.toUpperCase()
306
});
307
},
308
submitHandler: function(event) {
309
event.preventDefault();
310
return log(this.state.helloTo);
311
},
312
render: function() {
313
return React.createElement("form", {
314
"onSubmit": this.submitHandler
315
}, React.createElement("input", {
316
"type": "text",
317
"value": this.state.helloTo,
318
"onChange": this.handleChange
319
}), React.createElement("br", null), "(", this.state.helloTo.length, " characters)", React.createElement("button", {
320
"type": 'submit'
321
}, "Speak"));
322
}
323
});
324
325
NumberComponent = rclass({
326
render: function() {
327
return React.createElement("div", null, this.props.number);
328
}
329
});
330
331
num = Math.random();
332
333
console.log(React.renderToString(React.createElement(NumberComponent, {
334
"number": num
335
})));
336
337
done = function(event) {
338
return console.log("done", event);
339
};
340
341
the_choices = ['First', 'Next', 'Last', 'Final Option'];
342
343
page = function() {
344
return React.createElement("div", null, React.createElement(MyForm, null), React.createElement(MyForm2, null), React.createElement(DoodleArea, null), React.createElement(Since2014, null), React.createElement("h1", null, "React Testbed"), React.createElement(Timer, null), React.createElement("hr", null), React.createElement(AnswerMultipleChoiceQuestion, {
345
"choices": the_choices,
346
"onCompleted": done
347
}));
348
};
349
350
console.log(React.renderToString(page()));
351
352
console.log(React.renderToStaticMarkup(page()));
353
354
React.render(page(), document.getElementById('example'));
355
356