/
foreclojure.js
285 lines (253 loc) · 8.92 KB
/
foreclojure.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
$(document).ready(function() {
configureDataTables();
configureCodeBox();
configureGolf();
$("form#run-code button#approve-button").live("click", function(e) {
e.preventDefault();
if(confirm("Are you sure you want to mark this problem as approved?"))
$(this).parents("form").attr("action", "/problem/approve").submit();
});
$("form#run-code button#reject-button").live("click", function(e) {
e.preventDefault();
if(confirm("Are you sure you want to reject this problem? It will be permanently deleted.")) {
$(this).parents("form").attr("action", "/problem/reject").submit();
}
});
$("form#run-code button#edit-button").live("click", function(e) {
e.preventDefault();
$(this).parents("form").attr("action", "/problem/edit").submit();
});
$("button.user-follow-button").live("click", function(e) {
e.preventDefault();
var $form = $(this).parents("form");
var $button = $(this);
$.ajax({type: "POST",
url: "/rest" + $form.attr("action"),
dataType: "json",
success: function(data) {
if (data) {
$button.text(data["next-label"]);
$form.attr("action", data["next-action"]);
}
},
});
return false;
});
$("#user-table").addClass("js-enabled");
$("#user-table input.following").live("click", function(e) {
e.preventDefault();
var $checkbox = $(this)
var $form = $checkbox.parents("form")
$.ajax({type: "POST",
url: "/rest" + $form.attr("action"),
dataType: "json",
success: function(data) {
if (data) {
$checkbox.attr("checked", data["following"]);
$form.attr("action", data["next-action"]);
}
},
});
return false;
});
});
var difficulty = {
"Elementary": 0,
"Easy": 1,
"Medium": 2,
"Hard": 3,
"": 4
};
// dataTable will call this function in preparation for sorting a column.
// We're responsible for giving it the "real" data to sort on, for all the
// rows at once
jQuery.fn.dataTableExt.afnSortData['difficulty'] = function(oSettings, iColumn)
{
var aData = [];
// fnGetTrNodes returns a context we can use in jquery to iterate over
// only the <td> elements for this column. General approach taken from
// http://datatables.net/plug-ins/sorting#functions_data_source
$('td:eq('+iColumn+')', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
aData.push(difficulty[$(this).text()]);
});
return aData;
}
// See comments for above function to make sense of this mess
jQuery.fn.dataTableExt.afnSortData['user-name'] = function(oSettings, iColumn)
{
var aData = [];
$('td:eq('+iColumn+') a.user-profile-link', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
aData.push($(this).text());
});
return aData;
}
function configureDataTables(){
$('#problem-table').dataTable( {
"iDisplayLength": 100,
"aaSorting": [[5, "desc"], [1, "asc"], [4, "desc"]],
"aoColumns": [
{"sType": "string"},
{"sSortDataType": "difficulty", "sType": "numeric"},
{"sType": "string"},
{"sType": "string"},
{"sType": "numeric"},
{"sType": "string"}
]
} );
$('#unapproved-problems').dataTable( {
"iDisplayLength": 25,
"aaSorting": [[3, "desc"]],
"aoColumns": [
{"sType": "string"},
{"sType": "string"},
{"sType": "string"},
{"sType": "string"}
]
} );
$('#user-table').dataTable( {
"iDisplayLength":100,
"aaSorting": [[0, "asc"]],
"aoColumns": [
{"sType": "numeric"},
{"sSortDataType": "user-name"},
{"sType": "numeric"},
{"sType": "string"}
]
} );
$('#server-user-table').dataTable( {
"iDisplayLength":10,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/datatable/users"
} );
}
function setIconColor(element, color, timeOut) {
timeOut = (typeof timeOut == "undefined") ? 0 : timeOut
setTimeout (function() {
element.src = element.src.replace(new RegExp("(.*/images/).*(light.png)"), "$1" + color + "$2");
}, timeOut);
}
function changeToCodeView() {
$('#code-div').show('fast');
$('#golfgraph').hide('fast');
$('#graph-link').html("View Chart");
}
function configureCodeBox(){
//For no javascript version we have the code-box text area
//If we have javascript on then we remove it and replace it with
//the proper div
var oldBox = $('#code-box');
var disableJavaScriptCodeBox = $('#disable-javascript-codebox');
if (disableJavaScriptCodeBox.length){
return;
}
var hiddenCodeInput = "<input type=\"hidden\" value=\"blank\" name=\"code\" id=\"code\">";
oldBox.replaceWith("<div id=\"code-div\"> <pre id=\"editor\">" + oldBox.val() + "</pre></div>"+hiddenCodeInput);
if ($("#run-button").length || $("#submission-button").length){
var editor = ace.edit("editor");
editor.setTheme("ace/theme/textmate");
var ClojureMode = require("ace/mode/clojure").Mode;
var session = editor.getSession();
var clickHandler = function() {
var text = session.getValue(),
id = $('#id').attr("value"),
images = $(".testcases").find("img"),
cont = true,
high = false,
animationTime = 800,
waitTimePerItem = 500,
waitTime = waitTimePerItem,
beforeSendCallback = function(data) {
var anim = function() {
if(cont) {
images.animate({
opacity: high ? 1.0 : 0.1,
}, animationTime);
high = !high;
setTimeout(anim,animationTime);
}
};
$("#message-text").text("Executing unit tests...");
$("#error-message-text").text("");
images.each( function(index, element) {
setIconColor(element, "blue");
});
setTimeout(changeToCodeView,0);
setTimeout(anim,0);
},
successCallback = function(data) {
var failingTest = data.failingTest,
getColorFor = function(index) {
return index === failingTest ? "red" : "green";
},
testWasExecuted = function(index) {
return index <= failingTest;
},
setColor = function(index,element) {
var color = getColorFor(index);
waitTime = waitTimePerItem * (index+1);
setIconColor(element, color, waitTime);
},
setMessages = function() {
$("#message-text").html(data.message);
$("#error-message-text").html(data.error);
$("#golfgraph").html(data.golfChart);
$("#golfscore").html(data.golfScore);
configureGolf();
},
stopAnimation = function() {
cont = false;
images.stop(true);
images.css({ opacity: 1.0, });
};
setTimeout(stopAnimation, waitTime);
images.filter( testWasExecuted ).
each(setColor);
setTimeout (setMessages, waitTime);
};
$.ajax({type: "POST",
url: "/rest/problem/"+id,
dataType: "json",
data: { id: id, code: text, },
timout: 20000, // default clojail timeout is 10000
beforeSend: beforeSendCallback,
success: successCallback,
error: function(data, str, error) {
$("#message-text").text("An Error occured: "+error);
},
});
return false;
};
$("#run-button").click(clickHandler);
session.setMode(new ClojureMode());
session.setUseSoftTabs(true);
session.setTabSize(2);
document.getElementById('editor').style.fontSize='13px';
$("#run-button").click(function(){
var text = editor.getSession().getValue();
$('#code').val(text);
});
$("#submission-button").click(function(){
var text = editor.getSession().getValue();
$('#code').val(text);
});
}
}
function configureGolf(){
$('#graph-link').show();
$('#golfgraph').hide();
$('#graph-link').click(function() {
$('#code-div').toggle('fast', function() {
// Animation complete.
});
$('#golfgraph').toggle('fast', function() {
// Animation complete.
});
var text = $('#graph-link').html();
if (text && text == 'View Chart'){
$('#graph-link').html("View Code");
} else {
$('#graph-link').html("View Chart");
}
});
}