-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.js
200 lines (183 loc) · 5.26 KB
/
play.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
function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
function validateCode(mode, codeMirrors) {
$('#theoutput').prop("class", "alert alert-info");
$('#theoutput').text("compiling...");
var katydidcode = codeMirrors["katydid"].getValue();
var inputcode = codeMirrors[mode].getValue();
var validateFunc = gofunctions["RelapsePlayground"];
var res = validateFunc(mode, katydidcode, inputcode);
if (res.indexOf("Error: ") === 0) {
res = res.replace("Error: ", "");
$('#theoutput').prop("class", "alert alert-danger");
$('#theoutput').text(res);
} else {
if (res == "true") {
$('#theoutput').prop("class", "alert alert-success");
$('#theoutput').text("The input on the right satisfies the validation expression on the left.");
} else if (res == "false") {
$('#theoutput').prop("class", "alert alert-warning");
$('#theoutput').text("The input on the right does not satisfy the validation expression on the left.");
} else {
$('#theoutput').prop("class", "alert alert-danger");
$('#theoutput').text(res);
}
}
}
function reportError(err) {
$('#theoutput').prop("class", "alert alert-danger");
$('#theoutput').text(err);
}
function saveCode(mode, codeMirrors) {
var saving = {
"description": "saved katydid src",
"public": true,
"files": {}
}
for (key in codeMirrors) {
saving["files"][key + "src"] = {}
saving["files"][key + "src"]["content"] = codeMirrors[key].getValue();
}
var github = new Github({});
var gist = github.getGist();
gist.create(saving, function (err, rest) {
if (err == undefined) {
window.location.assign(window.location.pathname + "?gist=" + rest.id + "&share=true&mode=" + mode);
} else {
reportError(err);
}
});
}
function loadCode(codeMirrors) {
var github = new Github({});
var gist = github.getGist(gistText);
gist.read(function (err, content) {
if (err == undefined) {
for (key in codeMirrors) {
codeMirrors[key].setValue(content.files[key + "src"].content);
}
} else {
reportError(err);
}
});
}
function displayShareBox() {
var linkText = window.location.href.replace("&share=true", "");
$("#thelink").val(linkText);
$("#thelink").prop("type", "text");
var theLinkBox = document.getElementById("thelink");
theLinkBox.onfocus = function () {
theLinkBox.select();
// Work around Chrome's little problem
theLinkBox.onmouseup = function () {
// Prevent further mouseup intervention
theLinkBox.onmouseup = null;
return false;
};
};
theLinkBox.focus();
}
CodeMirror.defineSimpleMode("katydidmode", {
start: [
{ regex: /"(?:[^\\]|\\.)*?"/, token: "string" },
{
regex: /(?:\<emptyset\>|\<empty\>|true|false)/,
token: "keyword"
},
{ regex: /\/\/.*/, token: "comment" },
{ regex: /\/\*/, token: "comment", next: "comment" }
],
comment: [
{ regex: /.*?\*\//, token: "comment", next: "start" },
{ regex: /.*/, token: "comment" }
]
});
function setHeightAuto() {
var codeMirrors = $(".CodeMirror");
for (var i = 0; i < codeMirrors.length; i++) {
codeMirrors[i].style.height = "auto";
}
}
function setHeightDefault() {
var codeMirrors = $(".CodeMirror");
for (var i = 0; i < codeMirrors.length; i++) {
codeMirrors[i].style.height = "25em";
}
}
function setDefaults(mode, codeMirrors) {
codeMirrors["katydid"].setValue(defaults[mode]["katydid"]);
codeMirrors[mode].setValue(defaults[mode]["input"]);
}
function init() {
var mode = getUrlParameter("mode");
gistText = getUrlParameter("gist");
share = getUrlParameter("share");
var katydidCodeMirror = CodeMirror(document.getElementById("lefttextarea"), {
mode: "katydidmode",
value: 'loading...',
viewportMargin: Infinity
});
var codeMirrors = { "katydid": katydidCodeMirror };
if (mode == undefined) {
var mode = "json";
}
if (mode == "json") {
var inputCodeMirror = CodeMirror(document.getElementById("righttextarea"), {
mode: { name: "javascript", json: true },
value: 'loading...',
viewportMargin: Infinity
});
codeMirrors[mode] = inputCodeMirror;
} else {
if (mode == "xml") {
var inputCodeMirror = CodeMirror(document.getElementById("righttextarea"), {
mode: "xml",
value: 'loading...',
viewportMargin: Infinity
});
codeMirrors[mode] = inputCodeMirror;
}
}
$("#mode" + mode).addClass("active");
$("#inputheading").text(mode + " input");
if (gistText == undefined) {
setDefaults(mode, codeMirrors);
} else {
loadCode(codeMirrors);
if (!(share == undefined)) {
displayShareBox();
}
}
$("#saveButton").click(function (ev) {
saveCode(mode, codeMirrors);
});
$("#validateButton").click(function (ev) {
ev.preventDefault();
validateCode(mode, codeMirrors);
});
setHeightDefault();
$("#autosizeButton").click(function (ev) {
ev.preventDefault();
wasChecked = $("#autosizeButton").hasClass("active");
if (wasChecked) {
$("#autosizeButton").removeClass("active");
setHeightDefault();
} else {
$("#autosizeButton").addClass("active");
setHeightAuto();
}
});
for (var key in codeMirrors) {
codeMirrors[key].on('keyup', function (instance, event) {
validateCode(mode, codeMirrors);
});
}
}