2015-08-27 00:02:07 -07:00
|
|
|
$(document).ready(function() {
|
2015-11-19 21:51:38 -08:00
|
|
|
const common = window.common;
|
|
|
|
const { Observable } = window.Rx;
|
|
|
|
const { challengeName, challengeType, challengeTypes } = common;
|
2015-09-09 18:59:56 -07:00
|
|
|
|
2015-09-27 15:58:59 -07:00
|
|
|
common.init.forEach(function(init) {
|
|
|
|
init($);
|
|
|
|
});
|
|
|
|
|
2015-11-21 20:30:30 -08:00
|
|
|
common.editorKeyUp$
|
|
|
|
.debounce(750)
|
|
|
|
.map(() => common.editor.getValue())
|
2015-11-21 20:56:05 -08:00
|
|
|
.distinctUntilChanged()
|
2015-11-21 20:30:30 -08:00
|
|
|
.doOnNext(() => console.log('updating value'))
|
|
|
|
.subscribe(
|
|
|
|
code => {
|
|
|
|
common.codeStorage.updateStorage(common.challengeName, code);
|
|
|
|
common.codeUri.querify(code);
|
|
|
|
},
|
|
|
|
err => console.error(err)
|
|
|
|
);
|
2015-11-19 21:51:38 -08:00
|
|
|
|
|
|
|
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 }) => {
|
2015-11-21 20:56:05 -08:00
|
|
|
if (err.stack) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2015-11-19 21:51:38 -08:00
|
|
|
common.updateOutputDisplay('' + err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-11-21 19:48:24 -08:00
|
|
|
Observable.merge(
|
|
|
|
common.editorExecute$,
|
|
|
|
common.submitBtn$
|
|
|
|
)
|
2015-11-19 21:51:38 -08:00
|
|
|
.flatMap(() => {
|
2015-11-21 19:48:24 -08:00
|
|
|
common.appendToOutputDisplay('\n// testing challenge...');
|
2015-11-21 20:56:05 -08:00
|
|
|
return common.executeChallenge$()
|
|
|
|
.map(({ tests, ...rest }) => {
|
|
|
|
const solved = tests.every(test => !test.err);
|
|
|
|
return { ...rest, tests, solved };
|
|
|
|
})
|
|
|
|
.catch(err => Observable.just(err));
|
2015-11-21 19:48:24 -08:00
|
|
|
})
|
2015-11-19 21:51:38 -08:00
|
|
|
.subscribe(
|
2015-11-21 20:56:05 -08:00
|
|
|
({ err, solved, output, tests }) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return common.updateOutputDisplay('' + err);
|
|
|
|
}
|
2015-11-19 21:51:38 -08:00
|
|
|
common.updateOutputDisplay(output);
|
2015-11-21 14:44:33 -08:00
|
|
|
common.displayTestResults(tests);
|
2015-11-21 19:48:24 -08:00
|
|
|
if (solved) {
|
|
|
|
common.showCompletion();
|
|
|
|
}
|
2015-11-21 20:56:05 -08:00
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
console.error(err);
|
|
|
|
common.updateOutputDisplay('' + err);
|
2015-11-19 21:51:38 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
var $preview = $('#preview');
|
2015-11-19 21:51:38 -08:00
|
|
|
if ($preview.html()) {
|
2015-08-27 00:15:13 -07:00
|
|
|
$preview.load(function() {
|
2015-11-19 21:51:38 -08:00
|
|
|
common.executeChallenge()
|
|
|
|
.subscribe(
|
|
|
|
({ output = '' }) => {
|
|
|
|
common.updateOutputDisplay(output);
|
|
|
|
},
|
|
|
|
({ err }) => {
|
|
|
|
common.updateOutputDisplay('' + err);
|
|
|
|
}
|
|
|
|
);
|
2015-08-27 00:15:13 -07:00
|
|
|
});
|
2015-11-08 20:04:43 -08:00
|
|
|
} else if (
|
2015-11-19 21:51:38 -08:00
|
|
|
challengeType !== '2' &&
|
|
|
|
challengeType !== '3' &&
|
|
|
|
challengeType !== '4' &&
|
|
|
|
challengeType !== '7'
|
2015-11-08 20:04:43 -08:00
|
|
|
) {
|
2015-11-21 21:17:39 -08:00
|
|
|
Observable.just({})
|
|
|
|
.delay(500)
|
|
|
|
.flatMap(() => common.executeChallenge$())
|
2015-11-19 21:51:38 -08:00
|
|
|
.subscribe(
|
2015-11-21 19:48:24 -08:00
|
|
|
({ original, tests }) => {
|
2015-11-19 21:51:38 -08:00
|
|
|
common.codeStorage.updateStorage(challengeName, original);
|
2015-11-21 19:48:24 -08:00
|
|
|
common.displayTestResults(tests);
|
2015-11-19 21:51:38 -08:00
|
|
|
},
|
|
|
|
({ err }) => {
|
|
|
|
if (err.stack) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
common.updateOutputDisplay('' + err);
|
|
|
|
}
|
|
|
|
);
|
2015-08-27 00:15:13 -07:00
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
});
|