summaryrefslogtreecommitdiff
path: root/doc/play/playground.js
blob: 709136627ba548544bad6129f26d204b6ca3b333 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// opts is an object with these keys
//	codeEl - code editor element
//	outputEl - program output element
//	runEl - run button element
//	fmtEl - fmt button element (optional)
//	shareEl - share button element (optional)
//	shareURLEl - share URL text input element (optional)
//	shareRedirect - base URL to redirect to on share (optional)
//	toysEl - toys select element (optional)
//	enableHistory - enable using HTML5 history API (optional)
function playground(opts) {
	var code = $(opts['codeEl']);

	// autoindent helpers.
	function insertTabs(n) {
		// find the selection start and end
		var start = code[0].selectionStart;
		var end   = code[0].selectionEnd;
		// split the textarea content into two, and insert n tabs
		var v = code[0].value;
		var u = v.substr(0, start);
		for (var i=0; i<n; i++) {
			u += "\t";
		}
		u += v.substr(end);
		// set revised content
		code[0].value = u;
		// reset caret position after inserted tabs
		code[0].selectionStart = start+n;
		code[0].selectionEnd = start+n;
	}
	function autoindent(el) {
		var curpos = el.selectionStart;
		var tabs = 0;
		while (curpos > 0) {
			curpos--;
			if (el.value[curpos] == "\t") {
				tabs++;
			} else if (tabs > 0 || el.value[curpos] == "\n") {
				break;
			}
		}
		setTimeout(function() {
			insertTabs(tabs);
		}, 1);
	}

	function keyHandler(e) {
		if (e.keyCode == 9) { // tab
			insertTabs(1);
			e.preventDefault();
			return false;
		}
		if (e.keyCode == 13) { // enter
			if (e.shiftKey) { // +shift
				run();
				e.preventDefault();
				return false;
			} else {
				autoindent(e.target);
			}
		}
		return true;
	}
	code.unbind('keydown').bind('keydown', keyHandler);
	var output = $(opts['outputEl']);

	function body() {
		return $(opts['codeEl']).val();
	}
	function setBody(text) {
		$(opts['codeEl']).val(text);
	}
	function origin(href) {
		return (""+href).split("/").slice(0, 3).join("/");
	}
	function loading() {
		output.removeClass("error").html(
			'<div class="loading">Waiting for remote server...</div>'
		);
	}
	var playbackTimeout;
	function playback(pre, events) {
		function show(msg) {
			// ^L clears the screen.
			var msgs = msg.split("\x0c");
			if (msgs.length == 1) {
				pre.text(pre.text() + msg);
				return;
			}
			pre.text(msgs.pop());
		}
		function next() {
			if (events.length == 0) {
				var exit = $('<span class="exit"/>');
				exit.text("\nProgram exited.");
				exit.appendTo(pre);
				return;
			}
			var e = events.shift();
			if (e.Delay == 0) {
				show(e.Message);
				next();
			} else {
				playbackTimeout = setTimeout(function() {
					show(e.Message);
					next();
				}, e.Delay / 1000000);
			}
		}
		next();
	}
	function stopPlayback() {
		clearTimeout(playbackTimeout);
	}
	function setOutput(events, error) {
		stopPlayback();
		output.empty();
		$(".lineerror").removeClass("lineerror");

		// Display errors.
		if (error) {
			output.addClass("error");
			var regex = /prog.go:([0-9]+)/g;
			var r;
			while (r = regex.exec(error)) {
				$(".lines div").eq(r[1]-1).addClass("lineerror");
			}
			$("<pre/>").text(error).appendTo(output);
			return;
		}

		// Display image output.
		if (events.length > 0 && events[0].Message.indexOf("IMAGE:") == 0) {
			var out = "";
			for (var i = 0; i < events.length; i++) {
				out += events[i].Message;
			}
			var url = "data:image/png;base64," + out.substr(6);
			$("<img/>").attr("src", url).appendTo(output);
			return;
		}

		// Play back events.
		if (events !== null) {
			var pre = $("<pre/>").appendTo(output);
			playback(pre, events);
		}
	}

	var pushedEmpty = (window.location.pathname == "/");
	function inputChanged() {
		if (pushedEmpty) {
			return;
		}
		pushedEmpty = true;

		$(opts['shareURLEl']).hide();
		window.history.pushState(null, "", "/");
	}

	function popState(e) {
		if (e == null) {
			return;
		}

		if (e && e.state && e.state.code) {
			setBody(e.state.code);
		}
	}

	var rewriteHistory = false;

	if (window.history &&
		window.history.pushState &&
		window.addEventListener &&
		opts['enableHistory']) {
		rewriteHistory = true;
		code[0].addEventListener('input', inputChanged);
		window.addEventListener('popstate', popState)
	}

	var seq = 0;
	function run() {
		loading();
		seq++;
		var cur = seq;
		var data = {
			"version": 2,
			"body": body()
		};
		$.ajax("/compile", {
			data: data,
			type: "POST",
			dataType: "json",
			success: function(data) {
				if (seq != cur) {
					return;
				}
				if (!data) {
					return;
				}
				if (data.Errors) {
					setOutput(null, data.Errors);
					return;
				}
				setOutput(data.Events, false);
			},
			error: function() {
				output.addClass("error").text(
					"Error communicating with remote server."
				);
			}
		});
	}
	$(opts['runEl']).click(run);

	$(opts['fmtEl']).click(function() {
		loading();
		$.ajax("/fmt", {
			data: {"body": body()},
			type: "POST",
			dataType: "json",
			success: function(data) {
				if (data.Error) {
					setOutput(null, data.Error);
					return;
				}
				setBody(data.Body);
				setOutput(null);
			}
		});
	});

	if (opts['shareEl'] != null && (opts['shareURLEl'] != null || opts['shareRedirect'] != null)) {
		var shareURL;
		if (opts['shareURLEl']) {
			shareURL = $(opts['shareURLEl']).hide();
		}
		var sharing = false;
		$(opts['shareEl']).click(function() {
			if (sharing) return;
			sharing = true;
			var sharingData = body();
			$.ajax("/share", {
				processData: false,
				data: sharingData,
				type: "POST",
				complete: function(xhr) {
					sharing = false;
					if (xhr.status != 200) {
						alert("Server error; try again.");
						return;
					}
					if (opts['shareRedirect']) {
						window.location = opts['shareRedirect'] + xhr.responseText;
					}
					if (shareURL) {
						var path = "/p/" + xhr.responseText
						var url = origin(window.location) + path;
						shareURL.show().val(url).focus().select();

						if (rewriteHistory) {
							var historyData = {
								"code": sharingData,
							};
							window.history.pushState(historyData, "", path);
							pushedEmpty = false;
						}
					}
				}
			});
		});
	}

	if (opts['toysEl'] != null) {
		$(opts['toysEl']).bind('change', function() {
			var toy = $(this).val();
			$.ajax("/doc/play/"+toy, {
				processData: false,
				type: "GET",
				complete: function(xhr) {
					if (xhr.status != 200) {
						alert("Server error; try again.")
						return;
					}
					setBody(xhr.responseText);
				}
			});
		});
	}
}