2015-01-21 23:54:59 -05:00
|
|
|
var widgets = [];
|
|
|
|
var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
|
2015-05-19 22:31:01 -04:00
|
|
|
lineNumbers: true,
|
|
|
|
mode: "javascript",
|
|
|
|
theme: 'monokai',
|
|
|
|
runnable: true,
|
|
|
|
lint: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
autoCloseBrackets: true,
|
|
|
|
scrollbarStyle: 'null',
|
|
|
|
lineWrapping: true,
|
|
|
|
gutters: ["CodeMirror-lint-markers"],
|
|
|
|
onKeyEvent: doLinting
|
2015-01-21 23:54:59 -05:00
|
|
|
});
|
|
|
|
var editor = myCodeMirror;
|
2015-01-25 23:56:04 -05:00
|
|
|
editor.setSize("100%", "auto");
|
2015-01-22 15:49:16 -05:00
|
|
|
|
2015-02-06 12:36:55 -05:00
|
|
|
// Hijack tab key to enter two spaces intead
|
|
|
|
editor.setOption("extraKeys", {
|
2015-05-19 22:31:01 -04:00
|
|
|
Tab: function(cm) {
|
2015-06-18 17:08:21 -04:00
|
|
|
if (cm.somethingSelected()) {
|
2015-05-19 22:31:01 -04:00
|
|
|
cm.indentSelection("add");
|
|
|
|
} else {
|
|
|
|
var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
|
|
|
|
cm.replaceSelection(spaces);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Shift-Tab": function(cm) {
|
2015-06-18 17:08:21 -04:00
|
|
|
if (cm.somethingSelected()) {
|
2015-05-19 22:31:01 -04:00
|
|
|
cm.indentSelection("subtract");
|
|
|
|
} else {
|
|
|
|
var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
|
|
|
|
cm.replaceSelection(spaces);
|
2015-02-06 12:36:55 -05:00
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
},
|
|
|
|
"Ctrl-Enter": function() {
|
|
|
|
bonfireExecute();
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-06 12:36:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-01-22 15:49:16 -05:00
|
|
|
|
2015-02-03 01:57:35 -05:00
|
|
|
var attempts = 0;
|
|
|
|
if (attempts) {
|
2015-05-19 22:31:01 -04:00
|
|
|
attempts = 0;
|
2015-02-03 01:57:35 -05:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
|
|
|
|
var codeOutput = CodeMirror.fromTextArea(document.getElementById("codeOutput"), {
|
2015-05-19 22:31:01 -04:00
|
|
|
lineNumbers: false,
|
|
|
|
mode: "text",
|
|
|
|
theme: 'monokai',
|
|
|
|
readOnly: 'nocursor',
|
|
|
|
lineWrapping: true
|
2015-01-21 23:54:59 -05:00
|
|
|
});
|
2015-02-06 16:55:48 -08:00
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
codeOutput.setValue('/**\n' +
|
2015-05-19 22:31:01 -04:00
|
|
|
' * Your output will go here.\n' + ' * Console.log() -type statements\n' +
|
|
|
|
' * will appear in your browser\'s\n' + ' * DevTools JavaScript console.\n' +
|
|
|
|
' */');
|
2015-01-21 23:54:59 -05:00
|
|
|
codeOutput.setSize("100%", "100%");
|
|
|
|
var info = editor.getScrollInfo();
|
2015-06-18 17:08:21 -04:00
|
|
|
var after = editor.charCoords({
|
|
|
|
line: editor.getCursor().line + 1,
|
|
|
|
ch: 0
|
|
|
|
}, "local").top;
|
2015-01-21 23:54:59 -05:00
|
|
|
if (info.top + info.clientHeight < after)
|
2015-05-19 22:31:01 -04:00
|
|
|
editor.scrollTo(null, after - info.clientHeight + 3);
|
2015-01-22 22:47:15 -05:00
|
|
|
|
|
|
|
var editorValue;
|
|
|
|
|
|
|
|
|
|
|
|
var challengeSeed = challengeSeed || null;
|
2015-01-24 00:44:08 -05:00
|
|
|
var tests = tests || [];
|
2015-01-22 22:47:15 -05:00
|
|
|
|
2015-02-13 20:55:49 -05:00
|
|
|
var allSeeds = '';
|
|
|
|
(function() {
|
2015-05-19 22:31:01 -04:00
|
|
|
challengeSeed.forEach(function(elem) {
|
|
|
|
allSeeds += elem + '\n';
|
|
|
|
});
|
2015-02-13 20:55:49 -05:00
|
|
|
})();
|
2015-01-22 22:47:15 -05:00
|
|
|
|
2015-02-13 20:55:49 -05:00
|
|
|
editorValue = allSeeds;
|
2015-01-22 22:47:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
myCodeMirror.setValue(editorValue);
|
|
|
|
|
2015-06-18 17:08:21 -04:00
|
|
|
function doLinting() {
|
|
|
|
editor.operation(function() {
|
2015-05-19 22:31:01 -04:00
|
|
|
for (var i = 0; i < widgets.length; ++i)
|
|
|
|
editor.removeLineWidget(widgets[i]);
|
|
|
|
widgets.length = 0;
|
|
|
|
JSHINT(editor.getValue());
|
|
|
|
for (var i = 0; i < JSHINT.errors.length; ++i) {
|
|
|
|
var err = JSHINT.errors[i];
|
|
|
|
if (!err) continue;
|
|
|
|
var msg = document.createElement("div");
|
|
|
|
var icon = msg.appendChild(document.createElement("span"));
|
|
|
|
icon.innerHTML = "!!";
|
|
|
|
icon.className = "lint-error-icon";
|
|
|
|
msg.appendChild(document.createTextNode(err.reason));
|
|
|
|
msg.className = "lint-error";
|
|
|
|
widgets.push(editor.addLineWidget(err.line - 1, msg, {
|
|
|
|
coverGutter: false,
|
|
|
|
noHScroll: true
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2015-01-21 23:54:59 -05:00
|
|
|
};
|
|
|
|
|
2015-06-18 17:08:21 -04:00
|
|
|
$('#submitButton').on('click', function() {
|
2015-05-19 22:31:01 -04:00
|
|
|
bonfireExecute();
|
2015-01-22 22:47:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
function bonfireExecute() {
|
2015-05-19 22:31:01 -04:00
|
|
|
attempts++;
|
2015-06-18 17:08:21 -04:00
|
|
|
ga('send', 'event', 'Challenge', 'ran-code', challenge_Name);
|
|
|
|
userTests = null;
|
2015-05-19 22:31:01 -04:00
|
|
|
$('#codeOutput').empty();
|
|
|
|
var userJavaScript = myCodeMirror.getValue();
|
|
|
|
userJavaScript = removeComments(userJavaScript);
|
|
|
|
userJavaScript = scrapeTests(userJavaScript);
|
|
|
|
// simple fix in case the user forgets to invoke their function
|
|
|
|
|
|
|
|
submit(userJavaScript, function(cls, message) {
|
|
|
|
if (cls) {
|
|
|
|
codeOutput.setValue(message.error);
|
|
|
|
runTests('Error', null);
|
|
|
|
} else {
|
|
|
|
codeOutput.setValue(message.output);
|
|
|
|
message.input = removeLogs(message.input);
|
|
|
|
runTests(null, message);
|
|
|
|
}
|
|
|
|
});
|
2015-01-22 22:47:15 -05:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
|
2015-01-24 00:44:08 -05:00
|
|
|
var userTests;
|
2015-01-21 23:54:59 -05:00
|
|
|
var testSalt = Math.random();
|
|
|
|
|
2015-01-22 18:44:25 -05:00
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
var scrapeTests = function(userJavaScript) {
|
2015-01-22 15:49:16 -05:00
|
|
|
|
2015-05-19 22:31:01 -04:00
|
|
|
// insert tests from mongo
|
|
|
|
for (var i = 0; i < tests.length; i++) {
|
|
|
|
userJavaScript += '\n' + tests[i];
|
|
|
|
}
|
2015-01-22 18:44:25 -05:00
|
|
|
|
2015-05-19 22:31:01 -04:00
|
|
|
var counter = 0;
|
2015-06-18 17:08:21 -04:00
|
|
|
var regex = new RegExp(
|
|
|
|
/(expect(\s+)?\(.*\;)|(assert(\s+)?\(.*\;)|(assert\.\w.*\;)|(.*\.should\..*\;)/
|
|
|
|
);
|
2015-05-19 22:31:01 -04:00
|
|
|
var match = regex.exec(userJavaScript);
|
|
|
|
while (match != null) {
|
|
|
|
var replacement = '//' + counter + testSalt;
|
2015-06-18 17:08:21 -04:00
|
|
|
userJavaScript = userJavaScript.substring(0, match.index) + replacement +
|
|
|
|
userJavaScript.substring(match.index + match[0].length);
|
2015-01-21 23:54:59 -05:00
|
|
|
|
2015-05-19 22:31:01 -04:00
|
|
|
if (!userTests) {
|
2015-06-18 17:08:21 -04:00
|
|
|
userTests = [];
|
2015-01-21 23:54:59 -05:00
|
|
|
}
|
2015-06-18 17:08:21 -04:00
|
|
|
userTests.push({
|
|
|
|
"text": match[0],
|
|
|
|
"line": counter,
|
|
|
|
"err": null
|
|
|
|
});
|
2015-05-19 22:31:01 -04:00
|
|
|
counter++;
|
|
|
|
match = regex.exec(userJavaScript);
|
|
|
|
}
|
2015-01-22 15:49:16 -05:00
|
|
|
|
2015-05-19 22:31:01 -04:00
|
|
|
return userJavaScript;
|
2015-01-21 23:54:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
function removeComments(userJavaScript) {
|
2015-05-19 22:31:01 -04:00
|
|
|
var regex = new RegExp(/(\/\*[^(\*\/)]*\*\/)|\/\/[^\n]*/g);
|
|
|
|
return userJavaScript.replace(regex, '');
|
2015-01-21 23:54:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeLogs(userJavaScript) {
|
2015-05-19 22:31:01 -04:00
|
|
|
return userJavaScript.replace(/(console\.[\w]+\s*\(.*\;)/g, '');
|
2015-01-21 23:54:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var pushed = false;
|
|
|
|
var createTestDisplay = function() {
|
2015-05-19 22:31:01 -04:00
|
|
|
if (pushed) {
|
|
|
|
userTests.pop();
|
|
|
|
}
|
2015-06-18 17:08:21 -04:00
|
|
|
for (var i = 0; i < userTests.length; i++) {
|
2015-05-19 22:31:01 -04:00
|
|
|
var test = userTests[i];
|
|
|
|
var testDoc = document.createElement("div");
|
|
|
|
if (test.err != null) {
|
|
|
|
console.log('Should be displaying bad tests');
|
|
|
|
$(testDoc)
|
2015-06-18 17:08:21 -04:00
|
|
|
.html(
|
|
|
|
"<div class='row'><div class='col-xs-2 text-center'><i class='ion-close-circled big-error-icon'></i></div><div class='col-xs-10 test-output wrappable test-vertical-center grayed-out-test-output'>" +
|
|
|
|
test.text + "</div><div class='col-xs-10 test-output wrappable'>" +
|
|
|
|
test.err + "</div></div><div class='ten-pixel-break'/>")
|
2015-06-04 22:35:42 -04:00
|
|
|
.appendTo($('#testSuite'));
|
2015-05-19 22:31:01 -04:00
|
|
|
} else {
|
|
|
|
$(testDoc)
|
2015-06-18 17:08:21 -04:00
|
|
|
.html(
|
|
|
|
"<div class='row'><div class='col-xs-2 text-center'><i class='ion-checkmark-circled big-success-icon'></i></div><div class='col-xs-10 test-output test-vertical-center wrappable grayed-out-test-output'>" +
|
|
|
|
test.text + "</div></div><div class='ten-pixel-break'/>")
|
2015-05-19 22:31:01 -04:00
|
|
|
.appendTo($('#testSuite'));
|
2015-01-21 23:54:59 -05:00
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
};
|
2015-01-21 23:54:59 -05:00
|
|
|
};
|
2015-02-13 20:55:49 -05:00
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
var expect = chai.expect;
|
2015-05-20 21:50:31 -04:00
|
|
|
var assert = chai.assert;
|
2015-05-26 10:41:50 -04:00
|
|
|
var should = chai.should();
|
2015-02-13 20:55:49 -05:00
|
|
|
|
2015-01-22 18:44:25 -05:00
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
var reassembleTest = function(test, data) {
|
2015-05-19 22:31:01 -04:00
|
|
|
var lineNum = test.line;
|
|
|
|
var regexp = new RegExp("\/\/" + lineNum + testSalt);
|
|
|
|
return data.input.replace(regexp, test.text);
|
2015-01-21 23:54:59 -05:00
|
|
|
};
|
2015-01-24 00:44:08 -05:00
|
|
|
|
2015-01-21 23:54:59 -05:00
|
|
|
var runTests = function(err, data) {
|
2015-05-19 22:31:01 -04:00
|
|
|
var allTestsPassed = true;
|
|
|
|
pushed = false;
|
|
|
|
$('#testSuite').children().remove();
|
|
|
|
if (err && userTests.length > 0) {
|
2015-06-18 17:08:21 -04:00
|
|
|
userTests = [{
|
|
|
|
text: "Program Execution Failure",
|
|
|
|
err: "No user tests were run."
|
|
|
|
}];
|
2015-05-19 22:31:01 -04:00
|
|
|
createTestDisplay();
|
|
|
|
} else if (userTests) {
|
|
|
|
userTests.push(false);
|
|
|
|
pushed = true;
|
2015-06-18 17:08:21 -04:00
|
|
|
userTests.forEach(function(chaiTestFromJSON, indexOfTestArray,
|
|
|
|
__testArray) {
|
2015-05-19 22:31:01 -04:00
|
|
|
try {
|
2015-06-09 16:46:41 -04:00
|
|
|
if (chaiTestFromJSON) {
|
|
|
|
var output = eval(reassembleTest(chaiTestFromJSON, data));
|
2015-05-19 22:31:01 -04:00
|
|
|
}
|
2015-06-18 17:08:21 -04:00
|
|
|
} catch (error) {
|
2015-05-19 22:31:01 -04:00
|
|
|
allTestsPassed = false;
|
2015-06-09 16:46:41 -04:00
|
|
|
__testArray[indexOfTestArray].err = error.message;
|
2015-05-19 22:31:01 -04:00
|
|
|
} finally {
|
2015-06-09 16:46:41 -04:00
|
|
|
if (!chaiTestFromJSON) {
|
2015-05-19 22:31:01 -04:00
|
|
|
createTestDisplay();
|
2015-01-28 17:32:21 -05:00
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
}
|
|
|
|
});
|
2015-01-28 17:32:21 -05:00
|
|
|
|
2015-05-19 22:31:01 -04:00
|
|
|
if (allTestsPassed) {
|
|
|
|
allTestsPassed = false;
|
|
|
|
showCompletion();
|
2015-01-24 00:44:08 -05:00
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
|
|
|
|
}
|
2015-01-24 00:44:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
function showCompletion() {
|
2015-05-19 22:31:01 -04:00
|
|
|
var time = Math.floor(Date.now()) - started;
|
2015-06-18 17:08:21 -04:00
|
|
|
ga('send', 'event', 'Challenge', 'solved', challenge_Name + ', Time: ' + time +
|
|
|
|
', Attempts: ' + attempts);
|
2015-05-25 17:27:27 -04:00
|
|
|
var bonfireSolution = myCodeMirror.getValue();
|
|
|
|
var didCompleteWith = $('#completed-with').val() || null;
|
|
|
|
$.post(
|
2015-06-18 17:08:21 -04:00
|
|
|
'/completed-bonfire/', {
|
2015-05-25 17:27:27 -04:00
|
|
|
challengeInfo: {
|
|
|
|
challengeId: challenge_Id,
|
|
|
|
challengeName: challenge_Name,
|
|
|
|
completedWith: didCompleteWith,
|
|
|
|
challengeType: challengeType,
|
|
|
|
solution: bonfireSolution
|
|
|
|
}
|
2015-06-18 17:08:21 -04:00
|
|
|
},
|
|
|
|
function(res) {
|
2015-05-25 17:27:27 -04:00
|
|
|
if (res) {
|
|
|
|
$('#complete-courseware-dialog').modal('show');
|
2015-06-18 17:08:21 -04:00
|
|
|
$('#complete-courseware-dialog').keydown(function(e) {
|
2015-05-25 17:27:27 -04:00
|
|
|
if (e.ctrlKey && e.keyCode == 13) {
|
|
|
|
$('#next-courseware-button').click();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
}
|
2015-05-25 17:27:27 -04:00
|
|
|
);
|
|
|
|
|
2015-05-10 21:59:55 -07:00
|
|
|
}
|