UNPKG

999 BJavaScriptView Raw
1import bus from '@/js/eventbus.js';
2class Iframe {
3 constructor({el, sandboxAttrs = []}) {
4 if(!el) throw new Error('Expect "el" to mount iframe to!');
5 this.$el = el;
6 this.sandboxAttrs = sandboxAttrs;
7 }
8
9 createIframe() {
10 const iframe = document.createElement('iframe');
11 iframe.setAttribute('sandbox', this.sandboxAttrs.join(' '));
12 iframe.setAttribute('scrolling', 'yes');
13 iframe.style.width = '100%';
14 iframe.style.height = '100%';
15 iframe.style.border = 0;
16 return iframe;
17 }
18
19 setContent({head = "", body = ""}) {
20 const content = `<!DOCTYPE html><html><head>${head}</head><body>${body}</body></html>`
21 const iframe = this.createIframe();
22 this.$el.parentNode.replaceChild(iframe, this.$el);
23 iframe.onload = function() {
24 this.contentWindow.document.open();
25 this.contentWindow.document.write(content);
26 this.contentWindow.document.close();
27 }
28 this.$el = iframe;
29 }
30
31}
32
33export default (...args) => new Iframe(...args);
\No newline at end of file