Reset and execute update display and storage

This commit is contained in:
Berkeley Martinez
2015-11-19 21:51:38 -08:00
parent 4bdf1b2854
commit 74fa49cd75
10 changed files with 142 additions and 85 deletions

View File

@ -7,13 +7,10 @@ window.common = (function({ common = { init: [] }}) {
'(.*\\.should\\..*\\;)/' '(.*\\.should\\..*\\;)/'
); );
common.addTestsToString = function(code) { common.addTestsToString = function({ code, tests = [], ...rest }) {
const userTests = []; const userTests = [];
// insert tests from mongo code = tests.reduce((code, test) => '\n' + code + test, code);
for (var i = 0; i < common.tests.length; i++) {
code += '\n' + common.tests[i];
}
var counter = 0; var counter = 0;
var match = BDDregex.exec(code); var match = BDDregex.exec(code);
@ -34,7 +31,7 @@ window.common = (function({ common = { init: [] }}) {
match = BDDregex.exec(code); match = BDDregex.exec(code);
} }
return { code, userTests }; return { ...rest, code, userTests };
}; };
return common; return common;

View File

@ -1,4 +1,4 @@
window.common = (function({ $, common = { init: [] }}) { window.common = (function({ $, Rx, common = { init: [] }}) {
common.ctrlEnterClickHandler = function ctrlEnterClickHandler(e) { common.ctrlEnterClickHandler = function ctrlEnterClickHandler(e) {
// ctrl + enter or cmd + enter // ctrl + enter or cmd + enter
@ -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($) { common.init.push(function($) {
var $marginFix = $('.innerMarginFix'); var $marginFix = $('.innerMarginFix');
@ -133,20 +127,18 @@ window.common = (function({ $, common = { init: [] }}) {
} }
}); });
$('#submitButton').on('click', function() { common.submitBtn$ = Rx.Observable.fromEvent($('#submitButton'), 'click');
common.executeChallenge(true);
});
if (common.editor) { common.resetBtn$ = Rx.Observable.fromEvent($('#reset-button'), 'click');
$('#reset-button').on('click', common.resetEditor);
}
if (common.challengeName) { if (common.challengeName) {
window.ga('send', 'event', 'Challenge', 'load', common.challengeName); window.ga('send', 'event', 'Challenge', 'load', common.challengeName);
} }
$('#complete-courseware-dialog').on('hidden.bs.modal', function() { $('#complete-courseware-dialog').on('hidden.bs.modal', function() {
if (common.editor.focus) {
common.editor.focus(); common.editor.focus();
}
}); });
$('#trigger-issue-modal').on('click', function() { $('#trigger-issue-modal').on('click', function() {

View File

@ -7,6 +7,15 @@ window.common = (function(global) {
var codeStorage = { var codeStorage = {
getStoredValue(key) { 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'); return '' + localStorage.getItem(key + 'Val');
}, },
@ -20,7 +29,7 @@ window.common = (function(global) {
updateStorage(key, code) { updateStorage(key, code) {
if ( if (
!localStorage || !localStorage ||
typeof localStorage !== 'function' || typeof localStorage.setItem !== 'function' ||
!key || !key ||
typeof key !== 'string' typeof key !== 'string'
) { ) {
@ -28,6 +37,7 @@ window.common = (function(global) {
return code; return code;
} }
localStorage.setItem(key + 'Val', code); localStorage.setItem(key + 'Val', code);
return code;
} }
}; };

View File

@ -10,7 +10,7 @@ window.common = (function(global) {
if ( if (
!CodeMirror || !CodeMirror ||
challengeType === challengeTypes.BONFIRE || challengeType === challengeTypes.BASEJUMP ||
challengeType === challengeTypes.ZIPLINE || challengeType === challengeTypes.ZIPLINE ||
challengeType === challengeTypes.VIDEO || challengeType === challengeTypes.VIDEO ||
challengeType === challengeTypes.STEP || challengeType === challengeTypes.STEP ||

View File

@ -35,7 +35,7 @@ window.common = (function(global) {
this.plugin = new jailed.Plugin(path + 'plugin.js'); this.plugin = new jailed.Plugin(path + 'plugin.js');
this.startTimeout(); this.startTimeout();
this.plugin.whenConnected(() => { this.plugin.whenConnected(() => {
this.endTimeout(); this.cancelTimout();
}); });
}, },
destroyPlugin() { destroyPlugin() {
@ -45,8 +45,8 @@ window.common = (function(global) {
// sends the input to the plugin for evaluation // sends the input to the plugin for evaluation
common.detectLoops = function detectLoops({ code = '', ...rest }) { common.detectLoops$ = function detectLoops$({ code = '', ...rest }) {
return new Observable(function(observer) { return Observable.create(function(observer) {
const sandbox = Object.create(Sandbox); const sandbox = Object.create(Sandbox);
sandbox.createPlugin(); sandbox.createPlugin();

View File

@ -1,21 +1,72 @@
$(document).ready(function() { $(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) { common.init.forEach(function(init) {
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'); var $preview = $('#preview');
if (typeof $preview.html() !== 'undefined') { if ($preview.html()) {
$preview.load(function() { $preview.load(function() {
common.executeChallenge(true); common.executeChallenge()
.subscribe(
({ output = '' }) => {
common.updateOutputDisplay(output);
},
({ err }) => {
common.updateOutputDisplay('' + err);
}
);
}); });
} else if ( } else if (
common.challengeType !== '2' && challengeType !== '2' &&
common.challengeType !== '3' && challengeType !== '3' &&
common.challengeType !== '4' && challengeType !== '4' &&
common.challengeType !== '7' 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);
}
);
} }
}); });

View File

@ -29,25 +29,24 @@ window.common = (function(global) {
// checks if the number of opening comments(/*) matches the number of // checks if the number of opening comments(/*) matches the number of
// closing comments(*/) // closing comments(*/)
Observable.just({ code }) return Observable.just({ code })
.flatMap(code => { .flatMap(({ code }) => {
if ( if (
code.match(/\$\s*?\(\s*?\$\s*?\)/gi) && code.match(/\$\s*?\(\s*?\$\s*?\)/gi) &&
openingComments && openingComments &&
openingComments.length > code.match(/\*\//gi).length openingComments.length > code.match(/\*\//gi).length
) { ) {
return Observable.just({ return Observable.throw({
err: 'SyntaxError: Unfinished multi-line comment', err: 'SyntaxError: Unfinished multi-line comment',
code: code code
}); });
} }
if (code.match(detectUnsafeJQ)) { if (code.match(detectUnsafeJQ)) {
return Observable.just({ return Observable.throw({
err: 'Unsafe $($)', err: 'Unsafe $($)',
output: 'Unsafe $($)', code
code: code
}); });
} }
@ -55,9 +54,9 @@ window.common = (function(global) {
code.match(/function/g) && code.match(/function/g) &&
!code.match(detectFunctionCall) !code.match(detectFunctionCall)
) { ) {
return Observable.just({ return Observable.throw({
err: 'SyntaxError: Unsafe or unfinished function declaration', err: 'SyntaxError: Unsafe or unfinished function declaration',
code: code code
}); });
} }
@ -68,50 +67,54 @@ window.common = (function(global) {
openingComments && openingComments &&
openingComments.length > closingComments.length openingComments.length > closingComments.length
) { ) {
return Observable.just({ return Observable.throw({
err: 'SyntaxError: Unfinished HTML comment', err: 'SyntaxError: Unfinished HTML comment',
code: code code
}); });
} }
} }
if (code.match(detectUnsafeConsoleCall)) { if (code.match(detectUnsafeConsoleCall)) {
return Observable.just({ return Observable.throw({
err: 'Invalid if (null) console.log(1); detected', err: 'Invalid if (null) console.log(1); detected',
code: code code
}); });
} }
// add head and tail and detect loops // add head and tail and detect loops
return Observable.just({ code: head + code + tail }) return Observable.just({ code: head + code + tail, original: code });
.map(code => { })
.map(data => {
if (common.challengeType === common.challengeTypes.HTML) { if (common.challengeType === common.challengeTypes.HTML) {
return common.getScriptCode(code); return common.getScriptCode(data);
} }
return common.addTestsToString( return common.addTestsToString(Object.assign(
common.removeComments(code), data,
common.tests.slice() {
); code: common.removeComments(code),
tests: common.tests.slice()
}
));
}) })
.flatMap(common.detectLoops) .flatMap(common.detectLoops$)
.flatMap(({ err, code, data, userTests }) => { .flatMap(({ err, code, data, userTests, original }) => {
if (err) { if (err) {
return Observable.just({ return Observable.throw({ err });
err,
code,
data
});
} }
return common.runTests$({ return common.runTests$({
output: data.output.replace(/\\\"/gi, ''),
data, data,
code, code,
userTests userTests,
original,
output: data.output.replace(/\\\"/gi, '')
}); });
}); })
.catch(e => {
return e && e.err ?
Observable.throw(e) :
Observable.throw({ err: e });
}); });
}; };

View File

@ -3,9 +3,11 @@ window.common = (function(global) {
// all classes should be stored here // all classes should be stored here
// called at the beginning of dom ready // called at the beginning of dom ready
const { const {
Rx: { config },
common = { init: [] } common = { init: [] }
} = global; } = global;
config.longStackSupport = true;
common.head = common.head || []; common.head = common.head || [];
common.tail = common.tail || []; common.tail = common.tail || [];
common.salt = Math.random(); common.salt = Math.random();
@ -70,7 +72,7 @@ window.common = (function(global) {
}; };
const commentRegex = /(\/\*[^(\*\/)]*\*\/)|([ \n]\/\/[^\n]*)/g; const commentRegex = /(\/\*[^(\*\/)]*\*\/)|([ \n]\/\/[^\n]*)/g;
common.removeLogs = function removeComments(str) { common.removeComments = function removeComments(str) {
return str.replace(commentRegex, ''); return str.replace(commentRegex, '');
}; };

View File

@ -28,23 +28,21 @@ window.common = (function(global) {
} }
); );
codeOutput.setValue(` codeOutput.setValue(`/**
/**
* Your output will go here. * Your output will go here.
* Console.log() -type statements * Console.log() -type statements
* will appear in your browser\'s * will appear in your browser\'s
* DevTools JavaScript console. * DevTools JavaScript console.
*/' */'`);
`);
codeOutput.setSize('100%', '100%'); codeOutput.setSize('100%', '100%');
common.updateOutputDisplay = function updateOutputDisplay(str) { common.updateOutputDisplay = function updateOutputDisplay(str = '') {
codeOutput.setValue(str); codeOutput.setValue(str);
return str; return str;
}; };
common.appendToOutputDisplay = function appendToOutputDisplay(str) { common.appendToOutputDisplay = function appendToOutputDisplay(str = '') {
codeOutput.setValue(codeOutput.getValue() + str); codeOutput.setValue(codeOutput.getValue() + str);
return str; return str;
}; };

View File

@ -108,20 +108,24 @@ var paths = {
commonFramework: [ commonFramework: [
'init', 'init',
'bindings',
'add-test-to-string',
'add-faux-stream',
'code-storage', 'code-storage',
'code-uri', 'code-uri',
'create-editor', 'create-editor',
'detect-loops-stream', 'detect-loops-stream',
'display-test-results', 'display-test-results',
'execute-challenge-stream', 'execute-challenge-stream',
'out-display', 'output-display',
'phone-scroll-lock', 'phone-scroll-lock',
'report-issue', 'report-issue',
'run-tests-stream', 'run-tests-stream',
'show-completion', 'show-completion',
'step-challenge', 'step-challenge',
'test-script-stream', 'test-script-stream',
'update-preview' 'update-preview',
'end'
], ],
less: './client/less/main.less', less: './client/less/main.less',