freeCodeCamp/client/commonFramework/end.js

172 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-08-27 07:02:07 +00:00
$(document).ready(function() {
const common = window.common;
const { Observable } = window.Rx;
2015-12-02 22:56:06 +00:00
const {
addLoopProtect,
challengeName,
challengeType,
challengeTypes
} = common;
2015-09-27 22:58:59 +00:00
common.init.forEach(function(init) {
init($);
});
2015-11-24 23:12:54 +00:00
// only run if editor present
if (common.editor.getValue) {
const code$ = common.editorKeyUp$
.debounce(750)
.map(() => common.editor.getValue())
.distinctUntilChanged()
.shareReplay();
// update storage
code$.subscribe(
code => {
common.codeStorage.updateStorage(common.challengeName, code);
common.codeUri.querify(code);
},
err => console.error(err)
);
code$
2015-11-24 23:12:54 +00:00
// only run for HTML
.filter(() => common.challengeType === challengeTypes.HTML)
.flatMap(code => {
2015-11-30 22:27:39 +00:00
return common.detectUnsafeCode$(code)
2015-12-02 22:56:06 +00:00
.map(() => {
const combinedCode = common.head + code + common.tail;
2015-12-02 22:56:06 +00:00
return addLoopProtect(combinedCode);
})
2015-11-25 00:03:11 +00:00
.flatMap(code => common.updatePreview$(code))
2015-12-02 22:56:06 +00:00
.flatMap(() => common.checkPreview$({ code }))
2015-11-25 00:03:11 +00:00
.catch(err => Observable.just({ err }));
})
.subscribe(
({ err }) => {
if (err) {
2015-11-25 00:03:11 +00:00
console.error(err);
return common.updatePreview$(`
<h1>${err}</h1>
`).subscribe(() => {});
}
},
err => console.error(err)
);
2015-11-24 23:12:54 +00:00
}
common.resetBtn$
.doOnNext(() => {
common.editor.setValue(common.replaceSafeTags(common.seed));
})
.flatMap(() => {
2015-11-25 00:03:11 +00:00
return common.executeChallenge$()
.catch(err => Observable.just({ err }));
})
.subscribe(
2015-11-30 22:27:39 +00:00
({ err, output, originalCode }) => {
2015-11-25 00:03:11 +00:00
if (err) {
console.error(err);
2016-01-12 21:55:23 +00:00
return common.updateOutputDisplay(err);
2015-11-25 00:03:11 +00:00
}
2015-11-30 22:27:39 +00:00
common.codeStorage.updateStorage(challengeName, originalCode);
common.codeUri.querify(originalCode);
2016-01-12 21:55:23 +00:00
common.updateOutputDisplay(output);
},
2015-11-25 00:03:11 +00:00
(err) => {
if (err) {
2015-11-22 04:56:05 +00:00
console.error(err);
}
2016-01-12 21:55:23 +00:00
common.updateOutputDisplay(err);
}
);
2015-11-22 03:48:24 +00:00
Observable.merge(
common.editorExecute$,
common.submitBtn$
)
.flatMap(() => {
2015-11-22 03:48:24 +00:00
common.appendToOutputDisplay('\n// testing challenge...');
2015-11-22 04:56:05 +00:00
return common.executeChallenge$()
.map(({ tests, ...rest }) => {
const solved = tests.every(test => !test.err);
return { ...rest, tests, solved };
})
2015-11-25 00:03:11 +00:00
.catch(err => Observable.just({ err }));
2015-11-22 03:48:24 +00:00
})
.subscribe(
2015-11-22 04:56:05 +00:00
({ err, solved, output, tests }) => {
if (err) {
console.error(err);
2015-11-24 01:50:09 +00:00
if (common.challengeType === common.challengeTypes.HTML) {
return common.updatePreview$(`
<h1>${err}</h1>
2015-11-30 22:27:39 +00:00
`).first().subscribe(() => {});
2015-11-24 01:50:09 +00:00
}
2016-01-12 21:55:23 +00:00
return common.updateOutputDisplay(err);
2015-11-22 04:56:05 +00:00
}
2016-01-12 21:55:23 +00:00
common.updateOutputDisplay(output);
2015-11-21 22:44:33 +00:00
common.displayTestResults(tests);
2015-11-22 03:48:24 +00:00
if (solved) {
common.showCompletion();
}
2015-11-22 04:56:05 +00:00
},
2015-11-24 01:50:09 +00:00
({ err }) => {
2015-11-22 04:56:05 +00:00
console.error(err);
2016-01-12 21:55:23 +00:00
common.updateOutputDisplay(err);
}
);
2015-11-24 21:48:14 +00:00
// initial challenge run to populate tests
2015-11-23 03:42:53 +00:00
if (challengeType === challengeTypes.HTML) {
var $preview = $('#preview');
return Observable.fromCallback($preview.ready, $preview)()
.delay(500)
.flatMap(() => common.executeChallenge$())
2015-11-24 21:48:14 +00:00
.catch(err => Observable.just({ err }))
2015-11-23 03:42:53 +00:00
.subscribe(
2015-11-24 01:50:09 +00:00
({ err, tests }) => {
if (err) {
console.error(err);
if (common.challengeType === common.challengeTypes.HTML) {
return common.updatePreview$(`
<h1>${err}</h1>
`).subscribe(() => {});
}
2016-01-12 21:55:23 +00:00
return common.updateOutputDisplay(err);
2015-11-24 01:50:09 +00:00
}
2015-11-23 03:42:53 +00:00
common.displayTestResults(tests);
},
({ err }) => {
console.error(err);
}
);
}
if (
2015-11-24 23:12:54 +00:00
challengeType === challengeTypes.BONFIRE ||
2015-11-23 03:42:53 +00:00
challengeType === challengeTypes.JS
2015-11-09 04:04:43 +00:00
) {
2015-11-22 05:17:39 +00:00
Observable.just({})
.delay(500)
.flatMap(() => common.executeChallenge$())
2015-11-24 23:12:54 +00:00
.catch(err => Observable.just({ err }))
.subscribe(
2015-11-30 22:27:39 +00:00
({ err, originalCode, tests }) => {
2015-11-24 23:12:54 +00:00
if (err) {
console.error(err);
2016-01-12 21:55:23 +00:00
return common.updateOutputDisplay(err);
2015-11-24 23:12:54 +00:00
}
2015-11-30 22:27:39 +00:00
common.codeStorage.updateStorage(challengeName, originalCode);
2015-11-22 03:48:24 +00:00
common.displayTestResults(tests);
},
2015-11-24 23:12:54 +00:00
(err) => {
console.error(err);
2016-01-12 21:55:23 +00:00
common.updateOutputDisplay(err);
}
);
2015-08-27 07:15:13 +00:00
}
});