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>
|
|
|
|
window.$ = parent.$.proxy(parent.$.fn.find, parent.$(document));
|
2015-12-02 01:24:50 +00:00
|
|
|
window.loopProtect = parent.loopProtect;
|
2015-12-02 01:38:48 +00:00
|
|
|
window.__err = null;
|
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-11-23 03:42:53 +00:00
|
|
|
const iFrameScript$ =
|
|
|
|
common.getScriptContent$('/js/iFrameScripts.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-11-23 03:42:53 +00:00
|
|
|
// runPreviewTests$ should be set up in the preview window
|
|
|
|
common.runPreviewTests$ =
|
2015-11-30 22:27:39 +00:00
|
|
|
() => Observable.throw(new Error('run preview not enabled'));
|
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-11-23 03:42:53 +00:00
|
|
|
return iFrameScript$
|
|
|
|
.map(script => `<script>${script}</script>`)
|
2015-11-23 21:58:51 +00:00
|
|
|
.flatMap(script => {
|
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-11-24 07:47:14 +00:00
|
|
|
preview.write(libraryIncludes + code + '<!-- -->' + script);
|
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
|
|
|
|
.delay(100);
|
2015-11-23 03:42:53 +00:00
|
|
|
})
|
|
|
|
.map(() => code);
|
2015-11-18 05:25:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return common;
|
|
|
|
}(window));
|