Load iFrame on page load
This commit is contained in:
@ -1,35 +1,22 @@
|
|||||||
window.common = (function(global) {
|
window.common = (function(global) {
|
||||||
const {
|
const {
|
||||||
$,
|
|
||||||
Rx: { Observable, Disposable },
|
|
||||||
common = { init: [] }
|
common = { init: [] }
|
||||||
} = global;
|
} = global;
|
||||||
|
|
||||||
|
|
||||||
function getFaux() {
|
const faux$ = common.getScriptContent$('/js/faux.js').shareReplay();
|
||||||
return new Observable(function(observer) {
|
|
||||||
const jqXHR = $.get('/js/faux.js')
|
|
||||||
.success(data => observer.onNext(data))
|
|
||||||
.fail(e => observer.onError(e))
|
|
||||||
.always(() => observer.onCompleted());
|
|
||||||
|
|
||||||
return new Disposable(() => {
|
common.hasJs = function hasJs(code = '') {
|
||||||
jqXHR.abort();
|
return code.match(/\<\s?script\s?\>/gi) &&
|
||||||
});
|
code.match(/\<\s?\/\s?script\s?\>/gi);
|
||||||
});
|
};
|
||||||
}
|
|
||||||
|
|
||||||
const faux$ = getFaux().shareReplay();
|
|
||||||
|
|
||||||
common.safeHTMLRun = function safeHTMLRun(code) {
|
|
||||||
if (!code.match(/\<script\>/gi)) {
|
|
||||||
return Observable.just(code);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
common.addFaux$ = function addFaux$(code) {
|
||||||
// grab user javaScript
|
// grab user javaScript
|
||||||
var scriptCode = code
|
var scriptCode = code
|
||||||
.split(/\<\s?script\s?\>/gi)[1]
|
.split(/\<\s?script\s?\>/gi)[1]
|
||||||
.split(/\<\s?\/\s?script\s?\>/gi)[0];
|
.split(/\<\s?\/\s?script\s?\>/gi)[0];
|
||||||
|
|
||||||
return faux$.map(faux => faux + scriptCode);
|
return faux$.map(faux => faux + scriptCode);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -71,24 +71,24 @@ $(document).ready(function() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
var $preview = $('#preview');
|
if (challengeType === challengeTypes.HTML) {
|
||||||
if ($preview.html()) {
|
var $preview = $('#preview');
|
||||||
$preview.load(function() {
|
return Observable.fromCallback($preview.ready, $preview)()
|
||||||
common.executeChallenge()
|
.delay(500)
|
||||||
.subscribe(
|
.flatMap(() => common.executeChallenge$())
|
||||||
({ output = '' }) => {
|
.subscribe(
|
||||||
common.updateOutputDisplay(output);
|
({ code, tests }) => {
|
||||||
},
|
common.displayTestResults(tests);
|
||||||
({ err }) => {
|
},
|
||||||
common.updateOutputDisplay('' + err);
|
({ err }) => {
|
||||||
}
|
console.error(err);
|
||||||
);
|
}
|
||||||
});
|
);
|
||||||
} else if (
|
}
|
||||||
challengeType !== '2' &&
|
|
||||||
challengeType !== '3' &&
|
if (
|
||||||
challengeType !== '4' &&
|
challengeType === challengeTypes.BONFIRE &&
|
||||||
challengeType !== '7'
|
challengeType === challengeTypes.JS
|
||||||
) {
|
) {
|
||||||
Observable.just({})
|
Observable.just({})
|
||||||
.delay(500)
|
.delay(500)
|
||||||
|
@ -84,9 +84,28 @@ window.common = (function(global) {
|
|||||||
// add head and tail and detect loops
|
// add head and tail and detect loops
|
||||||
return Observable.just({ code: head + code + tail, original: code });
|
return Observable.just({ code: head + code + tail, original: code });
|
||||||
})
|
})
|
||||||
.map(data => {
|
.flatMap(data => {
|
||||||
if (common.challengeType === common.challengeTypes.HTML) {
|
if (common.challengeType === common.challengeTypes.HTML) {
|
||||||
return common.getScriptCode(data);
|
|
||||||
|
if (common.hasJs(code)) {
|
||||||
|
return common.addFaux$(data)
|
||||||
|
.flatMap(code => common.detectLoops$(code))
|
||||||
|
.flatMap(({ err }) => {
|
||||||
|
if (err) {
|
||||||
|
return Observable.throw({ err });
|
||||||
|
}
|
||||||
|
return common.runPreviewTests$({
|
||||||
|
code: data.code,
|
||||||
|
tests: common.tests.slice()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return common.updatePreview$(data.code)
|
||||||
|
.flatMap(code => common.runPreviewTests$({
|
||||||
|
code,
|
||||||
|
tests: common.tests.slice()
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
return common.addTestsToString(Object.assign(
|
return common.addTestsToString(Object.assign(
|
||||||
@ -95,21 +114,21 @@ window.common = (function(global) {
|
|||||||
code: common.removeComments(code),
|
code: common.removeComments(code),
|
||||||
tests: common.tests.slice()
|
tests: common.tests.slice()
|
||||||
}
|
}
|
||||||
));
|
))
|
||||||
})
|
.flatMap(common.detectLoops$)
|
||||||
.flatMap(common.detectLoops$)
|
.flatMap(({ err, code, data, userTests, original }) => {
|
||||||
.flatMap(({ err, code, data, userTests, original }) => {
|
if (err) {
|
||||||
if (err) {
|
return Observable.throw({ err });
|
||||||
return Observable.throw({ err });
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return common.runTests$({
|
return common.runTests$({
|
||||||
data,
|
data,
|
||||||
code,
|
code,
|
||||||
userTests,
|
userTests,
|
||||||
original,
|
original,
|
||||||
output: data.output.replace(/\\\"/gi, '')
|
output: data.output.replace(/\\\"/gi, '')
|
||||||
});
|
});
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
return e && e.err ?
|
return e && e.err ?
|
||||||
|
@ -3,7 +3,7 @@ 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 },
|
Rx: { Disposable, Observable, config },
|
||||||
common = { init: [] }
|
common = { init: [] }
|
||||||
} = global;
|
} = global;
|
||||||
|
|
||||||
@ -86,5 +86,21 @@ window.common = (function(global) {
|
|||||||
return code.replace(regexp, text);
|
return code.replace(regexp, text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
common.getScriptContent$ = function getScriptContent$(script) {
|
||||||
|
return Observable.create(function(observer) {
|
||||||
|
const jqXHR = $.get(script)
|
||||||
|
.success(data => {
|
||||||
|
observer.onNext(data);
|
||||||
|
observer.onCompleted();
|
||||||
|
})
|
||||||
|
.fail(e => observer.onError(e))
|
||||||
|
.always(() => observer.onCompleted());
|
||||||
|
|
||||||
|
return new Disposable(() => {
|
||||||
|
jqXHR.abort();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return common;
|
return common;
|
||||||
})(window);
|
})(window);
|
||||||
|
@ -5,12 +5,12 @@ window.common = (function(global) {
|
|||||||
common = { init: [] }
|
common = { init: [] }
|
||||||
} = global;
|
} = global;
|
||||||
|
|
||||||
const { challengeType = '0' } = common;
|
const { challengeTypes, challengeType = '0' } = common;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!CodeMirror ||
|
!CodeMirror ||
|
||||||
challengeType === '0' ||
|
challengeType !== challengeTypes.JS ||
|
||||||
challengeType === '7'
|
challengeType !== challengeTypes.BONFIRE
|
||||||
) {
|
) {
|
||||||
common.updateOutputDisplay = () => {};
|
common.updateOutputDisplay = () => {};
|
||||||
common.appendToOutputDisplay = () => {};
|
common.appendToOutputDisplay = () => {};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
window.common = (function({ common = { init: [] } }) {
|
window.common = (function({ Rx: { Observable }, common = { init: [] } }) {
|
||||||
var libraryIncludes = `
|
var libraryIncludes = `
|
||||||
<link
|
<link
|
||||||
rel='stylesheet'
|
rel='stylesheet'
|
||||||
@ -19,21 +19,29 @@ window.common = (function({ common = { init: [] } }) {
|
|||||||
</style>
|
</style>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
var iFrameScript = "<script src='/js/iFrameScripts.js'></script>";
|
const iFrameScript$ =
|
||||||
|
common.getScriptContent$('/js/iFrameScripts.js').shareReplay();
|
||||||
|
|
||||||
|
// runPreviewTests$ should be set up in the preview window
|
||||||
|
common.runPreviewTests$ =
|
||||||
|
() => Observable.throw({ err: new Error('run preview not enabled') });
|
||||||
|
|
||||||
common.updatePreview = function updatePreview(code = '', shouldTest = false) {
|
common.updatePreview$ = function updatePreview$(code = '') {
|
||||||
const previewFrame = document.getElementById('preview');
|
const previewFrame = document.getElementById('preview');
|
||||||
const preview = previewFrame.contentDocument ||
|
const preview = previewFrame.contentDocument ||
|
||||||
previewFrame.contentWindow.document;
|
previewFrame.contentWindow.document;
|
||||||
if (!preview) {
|
if (!preview) {
|
||||||
return code;
|
return Observable.just(code);
|
||||||
}
|
}
|
||||||
preview.open();
|
|
||||||
preview.write(libraryIncludes + code + (shouldTest ? iFrameScript : ''));
|
|
||||||
preview.close();
|
|
||||||
|
|
||||||
return code;
|
return iFrameScript$
|
||||||
|
.map(script => `<script>${script}</script>`)
|
||||||
|
.doOnNext(script => {
|
||||||
|
preview.open();
|
||||||
|
preview.write(libraryIncludes + code + script);
|
||||||
|
preview.close();
|
||||||
|
})
|
||||||
|
.map(() => code);
|
||||||
};
|
};
|
||||||
|
|
||||||
return common;
|
return common;
|
||||||
|
@ -2,27 +2,36 @@
|
|||||||
window.$ = parent.$;
|
window.$ = parent.$;
|
||||||
window.$(function() {
|
window.$(function() {
|
||||||
var _ = parent._;
|
var _ = parent._;
|
||||||
|
var Rx = parent.Rx;
|
||||||
var chai = parent.chai;
|
var chai = parent.chai;
|
||||||
var expect = chai.expect;
|
var assert = chai.assert;
|
||||||
var tests = parent.tests;
|
var tests = parent.tests;
|
||||||
var common = parent.common;
|
var common = parent.common;
|
||||||
var editorValue = common.editor.getValue();
|
var code = common.editor.getValue();
|
||||||
var editor = common.editor;
|
var editor = common.editor;
|
||||||
|
|
||||||
var userTests = common.tests.map(test => {
|
common.runPreviewTests$ =
|
||||||
var userTest = {};
|
function runPreviewTests$({ tests = [], ...rest }) {
|
||||||
try {
|
return Rx.Observable.from(tests)
|
||||||
/* eslint-disable no-eval */
|
.map(test => {
|
||||||
eval(test);
|
const userTest = {};
|
||||||
/* eslint-enable no-eval */
|
try {
|
||||||
} catch (e) {
|
/* eslint-disable no-eval */
|
||||||
userTest.err = e.message.split(':').shift();
|
eval(test);
|
||||||
} finally {
|
/* eslint-enable no-eval */
|
||||||
userTest.text = test
|
} catch (e) {
|
||||||
.split(',')
|
userTest.err = e.message.split(':').shift();
|
||||||
.pop()
|
} finally {
|
||||||
.replace(/\'/g, '')
|
userTest.text = test
|
||||||
.replace(/\)/, '');
|
.split(',')
|
||||||
}
|
.pop()
|
||||||
});
|
.replace(/\'/g, '')
|
||||||
|
.replace(/\)/, '');
|
||||||
|
}
|
||||||
|
return userTest;
|
||||||
|
})
|
||||||
|
.toArray()
|
||||||
|
.map(tests => ({ ...rest, tests }));
|
||||||
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user