-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patheditor.js
299 lines (251 loc) · 9.9 KB
/
editor.js
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
297
298
299
/* global require, CodeMirror, $, setTimeout */
var evaluate = require('./evaluate').evaluate,
format_result = require('./evaluate').format_result;
var d3 = require('d3');
var CodeMirror = require("codemirror");
require("./cm-church");
require("./cm-brackets");
require("./cm-comments");
require("./viz");
var Backbone = require('backbone');
var folding = require("./cm-folding");
CodeMirror.keyMap.default["Tab"] = "indentAuto";
CodeMirror.keyMap.default["Cmd-;"] = "toggleComment";
CodeMirror.keyMap.default["Cmd-."] = function(cm){cm.foldCode(cm.getCursor(), folding.myRangeFinder); };
CodeMirror.keyMap.default["Ctrl-;"] = "toggleComment";
CodeMirror.keyMap.default["Ctrl-."] = function(cm){cm.foldCode(cm.getCursor(), folding.myRangeFinder); };
var _ = require('underscore');
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
var runners = {};
runners['webchurch'] = makewebchurchrunner();
runners['webchurch-opt'] = makewebchurchrunner({precompile: true});
runners['cosh'] = function(ed) {
alert('cosh engine down right now :(')
ed.trigger('run.finish')
}
runners['bher'] = function(ed) {
alert('bher engine down right now :(')
ed.trigger('run.finish')
}
runners['mit-church'] = function(ed) {
alert('mit-church engine down right now :(')
ed.trigger('run.finish')
}
function wrap(tag, content) {
return _.template("<{{tag}}>{{content}}</{{tag}}>",
{tag: tag,
content: content
});
}
function makewebchurchrunner(engineOptions){
if (engineOptions === undefined) {
engineOptions = {}
}
return function(ed) {
var cm = ed.get('cm');
var code = ed.get('code');
// make new $results div, which will replace
// the old one stored in ed.display.$results
// NB: it is correct (though hacky) that $results is global
// (we modify this for vega-consistency in viz.js)
$results = $("<div class='results'>");
if (cm.errormark != undefined) {
cm.errormark.clear();
}
ed.trigger('run.start');
try {
var runResult = evaluate(code, engineOptions);
var underlyingData;
// render all side effects
sideEffects.forEach(function(e) {
if (e.type == "string") {
$results.append( $("<pre>"+e.data+"</pre>") );
}
if (e.type == "svg") {
$results.append( $("<div></div>").append(e.data));
}
if (e.type == "table") {
var tableString = wrap("table", e.data.map(function(row) {
var cols = row.map(function(x) { return wrap('td', x); }).join("");
return wrap("tr", cols);
}).join("\n"));
$results.append( $(tableString) );
}
if (e.type == "function") {
e.data($results);
}
});
underlyingData = runResult;
runResult = format_result(runResult);
if (!(runResult == "undefined")) {
$results.append($("<pre>"+runResult+'</pre>'));
}
} catch (e) {
ed.set('error',
e
);
var error = e.message;
$results
.append( $("<p></p>")
.addClass('error')
.text(error) );
if (e.stackarray != undefined) {
var churchStack = $("<pre></pre>");
churchStack.text(e.stackarray
.map(function(x) { return _.template("{{text}}: {{start}}-{{end}}", x) })
.join("\n"))
$results.append( '<div><u>Church stack array:</u></div>', churchStack);
var start=e.start.split(":"), end=e.end.split(":");
cm.errormark = cm.markText({line: Number(start[0])-1, ch: Number(start[1])-1},
{line: Number(end[0])-1, ch: Number(end[1])},
{className: "CodeMirrorError", clearOnEnter: true});
}
if (typeof e.jsStack !== "undefined") {
var jsStack = $("<pre></pre>");
jsStack.text(e.jsStack.join('\n'));
$results.append('<div><u>JS stack:</u></div>', jsStack );
}
} finally {
ed.display.$results.replaceWith($results);
ed.display.$results = $results;
ed.trigger('run.finish');
}
};
};
var EditorModel = Backbone.Model.extend({
initialize: function(options) {
var ed = this;
ed.set('initialOptions', options);
var cm = CodeMirror(
function(el) {
// defer this - we might not want to display immediately...
// (e.g., when we want to first fetch network results)
},
{
value: options.code,
lineNumbers: false,
matchBrackets: true,
continueComments: "Enter",
viewportMargin: Infinity,
autoCloseBrackets: true,
mode: 'scheme'
});
ed.set('cm', cm);
// when text in codemirror changes, update editormodel
cm.on('change', function(cmInstance) {
ed.codeChangedFromUser = true;
ed.set({code: cmInstance.getValue() });
});
// when code changes programmatically, update text
ed.on('change:code', function(instance, newCode, options) {
if (options.programmatic) {
cm.setValue(newCode);
}
})
//fold ";;;fold:" parts:
var lastLine = cm.lastLine();
for(var i=0;i<=lastLine;i++) {
var txt = cm.getLine(i),
pos = txt.indexOf(";;;fold:");
if (pos==0) {cm.foldCode(CodeMirror.Pos(i,pos),folding.tripleCommentRangeFinder);}
}
// results div
var $results = $("<div class='results'>");
$results.hide();
// engine selector
var engines = _(runners).keys(),
engineSelectorString = "<select>\n" + _(engines).map(
function(engine) {
var tmpl = _.template('<option value="{{ engine }}" {{ selectedString }}> {{ engine }} </option>'),
str = tmpl({
engine: engine,
selectedString: engine == ed.get('engine') ? "selected" : ""
});
return str;
}
).join("\n") + "\n</select>",
$engineSelector = $(engineSelectorString);
// when user modifies engine selector, update model
$engineSelector.change(function(e) {
ed.set('engine', $(this).val() );
});
// for programmatic changes to engine, update engine selector widget
ed.on('change:engine', function(instance, newEngine, options) {
if (options.programmatic) {
// strange: i can't reference $engineSelector here
// but i could reference $
ed.display.$engineSelector.val(newEngine);
}
})
// reset button
var $resetButton = $("<button class='reset'>").html("Reset");
$resetButton.click(function() {
ed.set(ed.get('initialOptions'),
{programmatic: true});
ed.trigger('reset');
ed.display.$results.hide().html('');
});
// run button
var $runButton = $("<button class='run'>").html("Run");
$runButton.click(function() {
ed.display.$results.find('*').css('opacity','0.7');
$runButton.attr('disabled','disabled');
// NB: can't write just this.run
// because then "this" becomes window
// and i don't wanna bother with function binding
setTimeout(function() { ed.run() }, 30);
ed.on('run.finish', function() {
$runButton.removeAttr('disabled');
})
});
var $codeControls = $("<div class='code-controls'>");
// HT http://somerandomdude.com/work/open-iconic/#
$codeControls.append(
// $resetButton[0],
// $engineSelector[0],
$runButton
);
var $cogMenu = $("<ul class='code-settings'></ul>");
$cogMenu.append(
"<li class='settings-icon'>☰</li>",
$("<li></li>").append($engineSelector[0]),
$("<li></li>").append($resetButton[0])
);
$(cm.display.wrapper).prepend($cogMenu);
// create wrapper element for editor
var wrapper = document.createElement('div');
$(cm.display.wrapper).attr("id", "ex-" + options.exerciseName);
// add code controls and results divs after codemirror
$(cm.display.wrapper).prepend($codeControls);
$(wrapper)
.append(cm.display.wrapper)
.append($results);
ed.display = {
wrapper: wrapper,
$runButton: $runButton,
$resetButton: $resetButton,
$engineSelector: $engineSelector,
$results: $results
}
},
defaults: {
code: "",
engine: "webchurch"
// ,exerciseName: "" // necessary?
},
run: function() {
var engine = runners[this.get('engine')];
this.set('result', engine( this ));
},
replaceDomEl: function(domEl) {
$(domEl).replaceWith(this.display.wrapper);
// if we place the item ourselves, we have to call refresh() on the
// codemirror instance HT http://stackoverflow.com/q/10575833/351392
this.get('cm').refresh();
}
});
module.exports = {
EditorModel: EditorModel
};