freeCodeCamp/client/commonFramework/end.js

95 lines
2.4 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;
const { challengeName, challengeType, challengeTypes } = common;
2015-09-27 22:58:59 +00:00
common.init.forEach(function(init) {
init($);
});
common.editorKeyUp$
.debounce(750)
.map(() => common.editor.getValue())
.doOnNext(() => console.log('updating value'))
.subscribe(
code => {
common.codeStorage.updateStorage(common.challengeName, code);
common.codeUri.querify(code);
},
err => console.error(err)
);
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);
}
);
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...');
return common.executeChallenge$();
})
2015-11-22 03:48:24 +00:00
.map(({ tests, ...rest }) => {
const solved = tests.every(test => !test.err);
return { ...rest, tests, solved };
})
.subscribe(
2015-11-22 03:48:24 +00:00
({ solved, output, tests }) => {
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-08-27 07:15:13 +00:00
var $preview = $('#preview');
if ($preview.html()) {
2015-08-27 07:15:13 +00:00
$preview.load(function() {
common.executeChallenge()
.subscribe(
({ output = '' }) => {
common.updateOutputDisplay(output);
},
({ err }) => {
common.updateOutputDisplay('' + err);
}
);
2015-08-27 07:15:13 +00:00
});
2015-11-09 04:04:43 +00:00
} else if (
challengeType !== '2' &&
challengeType !== '3' &&
challengeType !== '4' &&
challengeType !== '7'
2015-11-09 04:04:43 +00:00
) {
common.executeChallenge$()
.subscribe(
2015-11-22 03:48:24 +00:00
({ original, tests }) => {
common.codeStorage.updateStorage(challengeName, original);
2015-11-22 03:48:24 +00:00
common.displayTestResults(tests);
},
({ err }) => {
if (err.stack) {
console.error(err);
}
common.updateOutputDisplay('' + err);
}
);
2015-08-27 07:15:13 +00:00
}
});