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) {
|
|
|
|
if (cm.somethingSelected()){
|
|
|
|
cm.indentSelection("add");
|
|
|
|
} else {
|
|
|
|
var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
|
|
|
|
cm.replaceSelection(spaces);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Shift-Tab": function(cm) {
|
|
|
|
if (cm.somethingSelected()){
|
|
|
|
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-06-15 22:47:17 -07:00
|
|
|
/*
|
|
|
|
Local Storage Update System By Andrew Cay(Resto)
|
|
|
|
localBonfire: singleton object that contains properties and methods related to
|
|
|
|
dealing with the localStorage system.
|
|
|
|
The keys work off of the variable challenge_name to make unique identifiers per bonfire
|
|
|
|
|
|
|
|
Two extra functionalities:
|
|
|
|
Added anonymous version checking system incase of future updates to the system
|
|
|
|
Added keyup listener to editor(myCodeMirror) so the last update has been saved to storage
|
|
|
|
*/
|
|
|
|
var localBonfire = {
|
|
|
|
version: 0.01,
|
|
|
|
keyVersion:"saveVersion",
|
|
|
|
keyStamp: challenge_Name + 'Stamp',
|
|
|
|
keyValue: challenge_Name + 'Val',
|
|
|
|
stampExpireTime: (1000 *60) *60 *24,
|
|
|
|
updateWait: 1500,// 1.5 seconds
|
|
|
|
updateTimeoutId: null
|
|
|
|
};
|
|
|
|
localBonfire.getEditorValue = function(){
|
|
|
|
return localStorage.getItem(localBonfire.keyValue);
|
|
|
|
};
|
|
|
|
localBonfire.getStampTime = function(){
|
|
|
|
//localstorage always saves as strings.
|
|
|
|
return Number.parseInt( localStorage.getItem(localBonfire.keyStamp) );
|
|
|
|
};
|
|
|
|
localBonfire.isAlive = function(){// returns true if IDE was edited within expire time
|
|
|
|
return ( Date.now() - localBonfire.getStampTime() < localBonfire.stampExpireTime );
|
|
|
|
};
|
|
|
|
localBonfire.updateStorage = function(){
|
|
|
|
if(typeof(Storage) !== undefined) {
|
|
|
|
var stamp = Date.now(),
|
|
|
|
value = editor.getValue();
|
|
|
|
localStorage.setItem(localBonfire.keyValue, value);
|
|
|
|
localStorage.setItem(localBonfire.keyStamp, stamp);
|
|
|
|
} else {
|
|
|
|
if( debugging ){
|
|
|
|
console.log('no web storage');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
localBonfire.updateTimeoutId = null;
|
|
|
|
console.log('updated!');
|
|
|
|
};
|
|
|
|
// ANONYMOUS 1 TIME UPDATE VERSION
|
|
|
|
(function(){
|
|
|
|
var savedVersion = localStorage.getItem('saveVersion');
|
|
|
|
if( savedVersion === null ){
|
|
|
|
localStorage.setItem(localBonfire.keyVersion, localBonfire.version);//just write current version
|
|
|
|
}else{
|
|
|
|
//do checking if not current version
|
|
|
|
if( savedVersion !== localBonfire.version ){
|
|
|
|
//update version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
editor.on('keyup', function(codMir, event){
|
|
|
|
window.clearTimeout(localBonfire.updateTimeoutId);
|
|
|
|
localBonfire.updateTimeoutId = window.setTimeout(localBonfire.updateStorage, localBonfire.updateWait);
|
|
|
|
});
|
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();
|
|
|
|
var after = editor.charCoords({line: editor.getCursor().line + 1, ch: 0}, "local").top;
|
|
|
|
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-06-15 22:47:17 -07:00
|
|
|
|
2015-02-13 20:55:49 -05:00
|
|
|
var allSeeds = '';
|
|
|
|
(function() {
|
2015-05-19 22:31:01 -04:00
|
|
|
challengeSeed.forEach(function(elem) {
|
2015-06-15 22:47:17 -07:00
|
|
|
allSeeds += elem + '\n';
|
2015-05-19 22:31:01 -04:00
|
|
|
});
|
2015-02-13 20:55:49 -05:00
|
|
|
})();
|
2015-01-22 22:47:15 -05:00
|
|
|
|
2015-06-15 22:47:17 -07:00
|
|
|
editorValue = (localBonfire.isAlive())? localBonfire.getEditorValue() : allSeeds;
|
2015-01-22 22:47:15 -05:00
|
|
|
|
|
|
|
myCodeMirror.setValue(editorValue);
|
|
|
|
|
2015-01-22 00:38:39 -05:00
|
|
|
function doLinting () {
|
2015-05-19 22:31:01 -04:00
|
|
|
editor.operation(function () {
|
|
|
|
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-01-22 22:47:15 -05: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-05-20 21:50:31 -04:00
|
|
|
ga('send', 'event', 'Challenge', 'ran-code', challenge_Name);
|
2015-05-19 22:31:01 -04:00
|
|
|
userTests= null;
|
|
|
|
$('#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;
|
|
|
|
var regex = new RegExp(/(expect(\s+)?\(.*\;)|(assert(\s+)?\(.*\;)|(assert\.\w.*\;)|(.*\.should\..*\;)/);
|
|
|
|
var match = regex.exec(userJavaScript);
|
|
|
|
while (match != null) {
|
|
|
|
var replacement = '//' + counter + testSalt;
|
|
|
|
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) {
|
|
|
|
userTests= [];
|
2015-01-21 23:54:59 -05:00
|
|
|
}
|
2015-05-19 22:31:01 -04:00
|
|
|
userTests.push({"text": match[0], "line": counter, "err": null});
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
for (var i = 0; i < userTests.length;i++) {
|
|
|
|
var test = userTests[i];
|
|
|
|
var testDoc = document.createElement("div");
|
|
|
|
if (test.err != null) {
|
|
|
|
console.log('Should be displaying bad tests');
|
|
|
|
$(testDoc)
|
|
|
|
.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)
|
|
|
|
.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'/>")
|
|
|
|
.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) {
|
|
|
|
userTests= [{text:"Program Execution Failure", err: "No user tests were run."}];
|
|
|
|
createTestDisplay();
|
|
|
|
} else if (userTests) {
|
|
|
|
userTests.push(false);
|
|
|
|
pushed = true;
|
|
|
|
userTests.forEach(function(test, ix, arr){
|
|
|
|
try {
|
|
|
|
if (test) {
|
|
|
|
var output = eval(reassembleTest(test, data));
|
|
|
|
}
|
|
|
|
} catch(error) {
|
|
|
|
allTestsPassed = false;
|
|
|
|
arr[ix].err = error.message;
|
|
|
|
} finally {
|
|
|
|
if (!test) {
|
|
|
|
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-05-20 21:50:31 -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(
|
|
|
|
'/completed-bonfire/',
|
|
|
|
{
|
|
|
|
challengeInfo: {
|
|
|
|
challengeId: challenge_Id,
|
|
|
|
challengeName: challenge_Name,
|
|
|
|
completedWith: didCompleteWith,
|
|
|
|
challengeType: challengeType,
|
|
|
|
solution: bonfireSolution
|
|
|
|
}
|
|
|
|
}, function(res) {
|
|
|
|
if (res) {
|
|
|
|
$('#complete-courseware-dialog').modal('show');
|
|
|
|
$('#complete-courseware-dialog').keydown(function (e) {
|
|
|
|
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
|
|
|
}
|