Skip to content

Commit

Permalink
Reset and execute update display and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Nov 30, 2015
1 parent 4bdf1b2 commit 74fa49c
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 85 deletions.
9 changes: 3 additions & 6 deletions client/commonFramework/add-test-to-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ window.common = (function({ common = { init: [] }}) {
'(.*\\.should\\..*\\;)/'
);

common.addTestsToString = function(code) {
common.addTestsToString = function({ code, tests = [], ...rest }) {
const userTests = [];

// insert tests from mongo
for (var i = 0; i < common.tests.length; i++) {
code += '\n' + common.tests[i];
}
code = tests.reduce((code, test) => '\n' + code + test, code);

var counter = 0;
var match = BDDregex.exec(code);
Expand All @@ -34,7 +31,7 @@ window.common = (function({ common = { init: [] }}) {
match = BDDregex.exec(code);
}

return { code, userTests };
return { ...rest, code, userTests };
};

return common;
Expand Down
20 changes: 6 additions & 14 deletions client/commonFramework/bindings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
window.common = (function({ $, common = { init: [] }}) {
window.common = (function({ $, Rx, common = { init: [] }}) {

common.ctrlEnterClickHandler = function ctrlEnterClickHandler(e) {
// ctrl + enter or cmd + enter
Expand All @@ -15,12 +15,6 @@ window.common = (function({ $, common = { init: [] }}) {
}
};

common.resetEditor = function resetEditor() {
common.editor.setValue(common.replaceSafeTags(common.seed));
common.executeChallenge(true);
common.codeStorage.updateStorage();
};

common.init.push(function($) {

var $marginFix = $('.innerMarginFix');
Expand Down Expand Up @@ -133,20 +127,18 @@ window.common = (function({ $, common = { init: [] }}) {
}
});

$('#submitButton').on('click', function() {
common.executeChallenge(true);
});
common.submitBtn$ = Rx.Observable.fromEvent($('#submitButton'), 'click');

if (common.editor) {
$('#reset-button').on('click', common.resetEditor);
}
common.resetBtn$ = Rx.Observable.fromEvent($('#reset-button'), 'click');

if (common.challengeName) {
window.ga('send', 'event', 'Challenge', 'load', common.challengeName);
}

$('#complete-courseware-dialog').on('hidden.bs.modal', function() {
common.editor.focus();
if (common.editor.focus) {
common.editor.focus();
}
});

$('#trigger-issue-modal').on('click', function() {
Expand Down
12 changes: 11 additions & 1 deletion client/commonFramework/code-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ window.common = (function(global) {

var codeStorage = {
getStoredValue(key) {
if (
!localStorage ||
typeof localStorage.getItem !== 'function' ||
!key ||
typeof key !== 'string'
) {
console.log('unable to save to storage');
return '';
}
return '' + localStorage.getItem(key + 'Val');
},

Expand All @@ -20,14 +29,15 @@ window.common = (function(global) {
updateStorage(key, code) {
if (
!localStorage ||
typeof localStorage !== 'function' ||
typeof localStorage.setItem !== 'function' ||
!key ||
typeof key !== 'string'
) {
console.log('unable to save to storage');
return code;
}
localStorage.setItem(key + 'Val', code);
return code;
}
};

Expand Down
2 changes: 1 addition & 1 deletion client/commonFramework/create-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ window.common = (function(global) {

if (
!CodeMirror ||
challengeType === challengeTypes.BONFIRE ||
challengeType === challengeTypes.BASEJUMP ||
challengeType === challengeTypes.ZIPLINE ||
challengeType === challengeTypes.VIDEO ||
challengeType === challengeTypes.STEP ||
Expand Down
6 changes: 3 additions & 3 deletions client/commonFramework/detect-loops-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ window.common = (function(global) {
this.plugin = new jailed.Plugin(path + 'plugin.js');
this.startTimeout();
this.plugin.whenConnected(() => {
this.endTimeout();
this.cancelTimout();
});
},
destroyPlugin() {
Expand All @@ -45,8 +45,8 @@ window.common = (function(global) {


// sends the input to the plugin for evaluation
common.detectLoops = function detectLoops({ code = '', ...rest }) {
return new Observable(function(observer) {
common.detectLoops$ = function detectLoops$({ code = '', ...rest }) {
return Observable.create(function(observer) {
const sandbox = Object.create(Sandbox);

sandbox.createPlugin();
Expand Down
67 changes: 59 additions & 8 deletions client/commonFramework/end.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
$(document).ready(function() {
var common = window.common;
const common = window.common;
const { Observable } = window.Rx;
const { challengeName, challengeType, challengeTypes } = common;

common.init.forEach(function(init) {
init($);
});


common.resetBtn$
.doOnNext(() => {
common.editor.setValue(common.replaceSafeTags(common.seed));
})
.flatMap(() => {
return common.executeChallenge$();
})
.subscribe(
({ output, original }) => {
common.codeStorage.updateStorage(challengeName, original);
common.updateOutputDisplay('' + output);
},
({ err }) => {
common.updateOutputDisplay('' + err);
}
);

common.submitBtn$
.flatMap(() => {
return common.executeChallenge$();
})
.subscribe(
({ output, original, userTests }) => {
common.updateOutputDisplay(output);
common.codeStorage.updateStorage(challengeName, original);
}
);

var $preview = $('#preview');
if (typeof $preview.html() !== 'undefined') {
if ($preview.html()) {
$preview.load(function() {
common.executeChallenge(true);
common.executeChallenge()
.subscribe(
({ output = '' }) => {
common.updateOutputDisplay(output);
},
({ err }) => {
common.updateOutputDisplay('' + err);
}
);
});
} else if (
common.challengeType !== '2' &&
common.challengeType !== '3' &&
common.challengeType !== '4' &&
common.challengeType !== '7'
challengeType !== '2' &&
challengeType !== '3' &&
challengeType !== '4' &&
challengeType !== '7'
) {
common.executeChallenge(true);
common.executeChallenge$()
.subscribe(
({ original }) => {
// common.updateOutputDisplay('' + output);
common.codeStorage.updateStorage(challengeName, original);
},
({ err }) => {
if (err.stack) {
console.error(err);
}
common.updateOutputDisplay('' + err);
}
);
}
});
81 changes: 42 additions & 39 deletions client/commonFramework/execute-challenge-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,34 @@ window.common = (function(global) {

// checks if the number of opening comments(/*) matches the number of
// closing comments(*/)
Observable.just({ code })
.flatMap(code => {
return Observable.just({ code })
.flatMap(({ code }) => {
if (
code.match(/\$\s*?\(\s*?\$\s*?\)/gi) &&
openingComments &&
openingComments.length > code.match(/\*\//gi).length
) {

return Observable.just({
return Observable.throw({
err: 'SyntaxError: Unfinished multi-line comment',
code: code
code
});
}

if (code.match(detectUnsafeJQ)) {
return Observable.just({
return Observable.throw({
err: 'Unsafe $($)',
output: 'Unsafe $($)',
code: code
code
});
}

if (
code.match(/function/g) &&
!code.match(detectFunctionCall)
) {
return Observable.just({
return Observable.throw({
err: 'SyntaxError: Unsafe or unfinished function declaration',
code: code
code
});
}

Expand All @@ -68,50 +67,54 @@ window.common = (function(global) {
openingComments &&
openingComments.length > closingComments.length
) {
return Observable.just({
return Observable.throw({
err: 'SyntaxError: Unfinished HTML comment',
code: code
code
});
}
}

if (code.match(detectUnsafeConsoleCall)) {
return Observable.just({
return Observable.throw({
err: 'Invalid if (null) console.log(1); detected',
code: code
code
});
}

// add head and tail and detect loops
return Observable.just({ code: head + code + tail })
.map(code => {
if (common.challengeType === common.challengeTypes.HTML) {
return common.getScriptCode(code);
}
return Observable.just({ code: head + code + tail, original: code });
})
.map(data => {
if (common.challengeType === common.challengeTypes.HTML) {
return common.getScriptCode(data);
}

return common.addTestsToString(
common.removeComments(code),
common.tests.slice()
);
})
.flatMap(common.detectLoops)
.flatMap(({ err, code, data, userTests }) => {
if (err) {
return Observable.just({
err,
code,
data
});
}
return common.addTestsToString(Object.assign(
data,
{
code: common.removeComments(code),
tests: common.tests.slice()
}
));
})
.flatMap(common.detectLoops$)
.flatMap(({ err, code, data, userTests, original }) => {
if (err) {
return Observable.throw({ err });
}

return common.runTests$({
output: data.output.replace(/\\\"/gi, ''),
data,
code,
userTests
});
return common.runTests$({
data,
code,
userTests,
original,
output: data.output.replace(/\\\"/gi, '')
});

})
.catch(e => {
return e && e.err ?
Observable.throw(e) :
Observable.throw({ err: e });
});
};

Expand Down
4 changes: 3 additions & 1 deletion client/commonFramework/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ window.common = (function(global) {
// all classes should be stored here
// called at the beginning of dom ready
const {
Rx: { config },
common = { init: [] }
} = global;

config.longStackSupport = true;
common.head = common.head || [];
common.tail = common.tail || [];
common.salt = Math.random();
Expand Down Expand Up @@ -70,7 +72,7 @@ window.common = (function(global) {
};

const commentRegex = /(\/\*[^(\*\/)]*\*\/)|([ \n]\/\/[^\n]*)/g;
common.removeLogs = function removeComments(str) {
common.removeComments = function removeComments(str) {
return str.replace(commentRegex, '');
};

Expand Down
18 changes: 8 additions & 10 deletions client/commonFramework/output-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,21 @@ window.common = (function(global) {
}
);

codeOutput.setValue(`
/**
* Your output will go here.
* Console.log() -type statements
* will appear in your browser\'s
* DevTools JavaScript console.
*/'
`);
codeOutput.setValue(`/**
* Your output will go here.
* Console.log() -type statements
* will appear in your browser\'s
* DevTools JavaScript console.
*/'`);

codeOutput.setSize('100%', '100%');

common.updateOutputDisplay = function updateOutputDisplay(str) {
common.updateOutputDisplay = function updateOutputDisplay(str = '') {
codeOutput.setValue(str);
return str;
};

common.appendToOutputDisplay = function appendToOutputDisplay(str) {
common.appendToOutputDisplay = function appendToOutputDisplay(str = '') {
codeOutput.setValue(codeOutput.getValue() + str);
return str;
};
Expand Down
Loading

0 comments on commit 74fa49c

Please sign in to comment.