~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to doc/play/playground.js

  • Committer: Package Import Robot
  • Author(s): Ondřej Surý
  • Date: 2012-05-02 15:44:59 UTC
  • mfrom: (14.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20120502154459-wcmy8ao1325ml619
Tags: 2:1.0.1-1
* Imported Upstream version 1.0.1
* Apply godoc patch to display package list correctly (Closes: #669354)

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
//      codeEl - code editor element 
7
7
//      outputEl - program output element
8
8
//      runEl - run button element
 
9
//      fmtEl - fmt button element (optional)
9
10
//      shareEl - share button element (optional)
10
11
//      shareURLEl - share URL text input element (optional)
11
12
//      shareRedirect - base URL to redirect to on share (optional)
12
 
//      preCompile - callback to mutate request data before compiling
13
 
//      postCompile - callback to read response data after compiling
14
 
//      simple - use plain textarea instead of CodeMirror.
15
 
//      toysEl - select element with a list of toys.
 
13
//      preCompile - callback to mutate request data before compiling (optional)
 
14
//      postCompile - callback to read response data after compiling (optional)
 
15
//      simple - use plain textarea instead of CodeMirror. (optional)
 
16
//      toysEl - select element with a list of toys. (optional)
16
17
function playground(opts) {
17
18
        var simple = opts['simple'];
18
19
        var code = $(opts['codeEl']);
97
98
                if (!editor) {
98
99
                        return;
99
100
                }
100
 
                var errorRe = /[a-z]+\.go:([0-9]+): /g;
 
101
                var errorRe = /[a-z]+\.go:([0-9]+):/g;
101
102
                var result;
102
103
                while ((result = errorRe.exec(text)) != null) {
103
104
                        var line = result[1]*1-1;
120
121
        function origin(href) {
121
122
                return (""+href).split("/").slice(0, 3).join("/");
122
123
        }
 
124
        function loading() {
 
125
                output.removeClass("error").html(
 
126
                        '<div class="loading">Waiting for remote server...</div>'
 
127
                );
 
128
        }
 
129
        function setOutput(text, error) {
 
130
                output.empty();
 
131
                if (error) {
 
132
                        output.addClass("error");
 
133
                }
 
134
                $("<pre/>").text(text).appendTo(output);
 
135
        }
123
136
 
124
137
        var seq = 0;
125
138
        function run() {
126
139
                clearErrors();
127
 
                output.removeClass("error").html(
128
 
                        '<div class="loading">Waiting for remote server...</div>'
129
 
                );
 
140
                loading();
130
141
                seq++;
131
142
                var cur = seq;
132
143
                var data = {"body": body()};
141
152
                                if (seq != cur) {
142
153
                                        return;
143
154
                                }
144
 
                                pre = $("<pre/>");
145
 
                                output.empty().append(pre);
146
155
                                if (opts['postCompile']) {
147
156
                                        opts['postCompile'](data);
148
157
                                }
150
159
                                        return;
151
160
                                }
152
161
                                if (data.compile_errors != "") {
153
 
                                        pre.text(data.compile_errors);
154
 
                                        output.addClass("error");
 
162
                                        setOutput(data.compile_errors, true);
155
163
                                        highlightErrors(data.compile_errors);
156
164
                                        return;
157
165
                                }
164
172
                                        output.empty().append(img);
165
173
                                        return;
166
174
                                }
167
 
                                pre.text(out);
 
175
                                setOutput(out, false);
168
176
                        },
169
177
                        error: function(xhr) {
170
178
                                var text = "Error communicating with remote server.";
171
 
                                console.log(xhr.status);
172
179
                                if (xhr.status == 501) {
173
180
                                        text = xhr.responseText;
174
181
                                }
178
185
        }
179
186
        $(opts['runEl']).click(run);
180
187
 
 
188
        $(opts['fmtEl']).click(function() {
 
189
                loading();
 
190
                $.ajax("/fmt", {
 
191
                        data: {"body": body()},
 
192
                        type: "POST",
 
193
                        dataType: "json",
 
194
                        success: function(data) {
 
195
                                if (data.Error) {
 
196
                                        setOutput(data.Error, true);
 
197
                                        highlightErrors(data.Error);
 
198
                                        return;
 
199
                                }
 
200
                                setBody(data.Body);
 
201
                                setOutput("", false);
 
202
                        }
 
203
                });
 
204
        });
 
205
 
 
206
        $(opts['toysEl']).bind('change', function() {
 
207
                var toy = $(this).val();
 
208
                loading();
 
209
                $.ajax("/doc/play/"+toy, {
 
210
                        processData: false,
 
211
                        type: "GET",
 
212
                        complete: function(xhr) {
 
213
                                if (xhr.status != 200) {
 
214
                                        setOutput("Server error; try again.", true);
 
215
                                        return;
 
216
                                }
 
217
                                setBody(xhr.responseText);
 
218
                                setOutput("", false);
 
219
                        }
 
220
                });
 
221
        });
 
222
 
181
223
        if (opts['shareEl'] != null && (opts['shareURLEl'] != null || opts['shareRedirect'] != null)) {
182
224
                var shareURL;
183
225
                if (opts['shareURLEl']) {
213
255
                });
214
256
        }
215
257
 
216
 
        if (opts['toysEl'] != null) {
217
 
                $(opts['toysEl']).bind('change', function() {
218
 
                        var toy = $(this).val();
219
 
                        $.ajax("/doc/play/"+toy, {
220
 
                                processData: false,
221
 
                                type: "GET",
222
 
                                complete: function(xhr) {
223
 
                                        if (xhr.status != 200) {
224
 
                                                alert("Server error; try again.")
225
 
                                                return;
226
 
                                        }
227
 
                                        setBody(xhr.responseText);
228
 
                                }
229
 
                        });
230
 
                });
231
 
        }
232
 
 
233
258
        return editor;
234
259
}