2015-11-23 21:58:51 +00:00
|
|
|
window.common = (function(global) {
|
|
|
|
const {
|
2015-12-01 16:01:51 +00:00
|
|
|
Rx: { BehaviorSubject, Observable },
|
2015-11-23 21:58:51 +00:00
|
|
|
common = { init: [] }
|
|
|
|
} = global;
|
|
|
|
|
2015-11-30 22:27:39 +00:00
|
|
|
// the first script tag here is to proxy jQuery
|
|
|
|
// We use the same jQuery on the main window but we change the
|
|
|
|
// context to that of the iframe.
|
2015-11-18 05:25:16 +00:00
|
|
|
var libraryIncludes = `
|
2015-11-30 22:27:39 +00:00
|
|
|
<script>
|
2015-12-02 01:24:50 +00:00
|
|
|
window.loopProtect = parent.loopProtect;
|
2015-12-02 01:38:48 +00:00
|
|
|
window.__err = null;
|
2015-12-02 22:56:06 +00:00
|
|
|
window.loopProtect.hit = function(line) {
|
|
|
|
window.__err = new Error(
|
2015-12-19 22:30:56 +00:00
|
|
|
'Potential infinite loop at line ' +
|
|
|
|
line +
|
2016-01-03 04:39:47 +00:00
|
|
|
'. To disable loop protection, write:' +
|
|
|
|
' \\n\\/\\/ noprotect\\nas the first' +
|
2015-12-19 22:30:56 +00:00
|
|
|
' line. Beware that if you do have an infinite loop in your code' +
|
|
|
|
' this will crash your browser.'
|
2015-12-02 22:56:06 +00:00
|
|
|
);
|
|
|
|
};
|
2015-11-30 22:27:39 +00:00
|
|
|
</script>
|
2015-11-18 05:25:16 +00:00
|
|
|
<link
|
|
|
|
rel='stylesheet'
|
|
|
|
href='//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.min.css'
|
|
|
|
/>
|
|
|
|
<link
|
|
|
|
rel='stylesheet'
|
|
|
|
href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'
|
|
|
|
/>
|
|
|
|
|
|
|
|
<link
|
|
|
|
rel='stylesheet'
|
|
|
|
href='//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css'
|
|
|
|
/>
|
|
|
|
<style>
|
|
|
|
body { padding: 0px 3px 0px 3px; }
|
|
|
|
</style>
|
|
|
|
`;
|
2015-12-03 23:09:52 +00:00
|
|
|
const codeDisabledError = `
|
|
|
|
<script>
|
|
|
|
window.__err = new Error('code has been disabled');
|
|
|
|
</script>
|
|
|
|
`;
|
2015-11-18 05:25:16 +00:00
|
|
|
|
2015-11-23 03:42:53 +00:00
|
|
|
const iFrameScript$ =
|
|
|
|
common.getScriptContent$('/js/iFrameScripts.js').shareReplay();
|
2015-12-03 20:13:25 +00:00
|
|
|
const jQueryScript$ = common.getScriptContent$(
|
|
|
|
'/bower_components/jquery/dist/jquery.js'
|
|
|
|
).shareReplay();
|
2015-11-18 05:25:16 +00:00
|
|
|
|
2015-12-01 16:01:51 +00:00
|
|
|
// behavior subject allways remembers the last value
|
|
|
|
// we use this to determine if runPreviewTest$ is defined
|
|
|
|
// and prime it with false
|
|
|
|
common.previewReady$ = new BehaviorSubject(false);
|
|
|
|
|
2015-12-02 22:56:06 +00:00
|
|
|
// These should be set up in the preview window
|
|
|
|
// if this error is seen it is because the function tried to run
|
|
|
|
// before the iframe has completely loaded
|
2015-11-23 03:42:53 +00:00
|
|
|
common.runPreviewTests$ =
|
2015-12-02 22:56:06 +00:00
|
|
|
common.checkPreview$ =
|
|
|
|
() => Observable.throw(new Error('Preview not fully loaded'));
|
|
|
|
|
2015-11-18 05:25:16 +00:00
|
|
|
|
2015-11-23 03:42:53 +00:00
|
|
|
common.updatePreview$ = function updatePreview$(code = '') {
|
2015-11-30 22:27:39 +00:00
|
|
|
const preview = common.getIframe('preview');
|
2015-11-18 05:25:16 +00:00
|
|
|
|
2015-12-03 20:13:25 +00:00
|
|
|
return Observable.combineLatest(
|
|
|
|
iFrameScript$,
|
|
|
|
jQueryScript$,
|
|
|
|
(iframe, jQuery) => ({
|
|
|
|
iframeScript: `<script>${iframe}</script>`,
|
|
|
|
jQuery: `<script>${jQuery}</script>`
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
.flatMap(({ iframeScript, jQuery }) => {
|
2015-12-01 16:01:51 +00:00
|
|
|
// we make sure to override the last value in the
|
|
|
|
// subject to false here.
|
|
|
|
common.previewReady$.onNext(false);
|
2015-11-23 03:42:53 +00:00
|
|
|
preview.open();
|
2015-12-03 20:13:25 +00:00
|
|
|
preview.write(
|
|
|
|
libraryIncludes +
|
|
|
|
jQuery +
|
2015-12-03 23:09:52 +00:00
|
|
|
(common.shouldRun() ? code : codeDisabledError) +
|
2015-12-03 20:13:25 +00:00
|
|
|
'<!-- -->' +
|
|
|
|
iframeScript
|
|
|
|
);
|
2015-11-23 03:42:53 +00:00
|
|
|
preview.close();
|
2015-12-01 16:01:51 +00:00
|
|
|
// now we filter false values and wait for the first true
|
|
|
|
return common.previewReady$
|
|
|
|
.filter(ready => ready)
|
2015-11-30 22:27:39 +00:00
|
|
|
.first()
|
|
|
|
// the delay here is to give code within the iframe
|
|
|
|
// control to run
|
2015-12-02 19:19:15 +00:00
|
|
|
.delay(400);
|
2015-11-23 03:42:53 +00:00
|
|
|
})
|
|
|
|
.map(() => code);
|
2015-11-18 05:25:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return common;
|
|
|
|
}(window));
|