-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathiframe.js
236 lines (211 loc) · 6.36 KB
/
iframe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import Mustache from 'mustache';
import stylesTemplate from './templates/styles';
import conditionalStyles from './styles/embeds/conditional';
import {addClassToElement, removeClassFromElement} from './utils/element-class';
const iframeStyles = {
width: '100%',
overflow: 'hidden',
border: 'none',
};
const iframeAttrs = {
horizontalscrolling: 'no',
verticalscrolling: 'no',
allowTransparency: 'true',
frameBorder: '0',
scrolling: 'no',
};
const webfontScript = 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js';
function isPseudoSelector(key) {
return key.charAt(0) === ':';
}
function isMedia(key) {
return key.charAt(0) === '@';
}
function isValue(test) {
return typeof test === 'string' || typeof test === 'number';
}
function ruleDeclarations(rule) {
return Object.keys(rule).filter((key) => {
return isValue(rule[key]);
}).map((key) => ({property: key, value: rule[key]}));
}
function selectorStyleGroup(selector, selectorClass, classes) {
let styleGroup = [];
if (selector && selectorClass) {
let formattedSelector = selectorClass.split(' ').join('.');
if (!isPseudoSelector(formattedSelector)) {
formattedSelector = `.${formattedSelector}`;
}
styleGroup = Object.keys(selector).filter((decKey) => {
return !isValue(selector[decKey]);
}).reduce((acc, decKey) => {
const className = classes[decKey] || decKey;
return acc.concat(selectorStyleGroup(selector[decKey], className, classes).map((group) => {
let groupSelector = '';
if (isPseudoSelector(group.selector)) {
groupSelector = `${formattedSelector}${group.selector}`;
} else if (isMedia(decKey)) {
groupSelector = formattedSelector;
} else {
groupSelector = `${formattedSelector} ${group.selector}`;
}
return {
selector: groupSelector,
declarations: group.declarations,
media: isMedia(decKey) ? decKey : null,
};
}));
}, []);
const declarations = ruleDeclarations(selector);
if (declarations.length) {
styleGroup.push({
selector: `${formattedSelector}`,
declarations,
});
}
}
return styleGroup;
}
export default class iframe {
constructor(node, config) {
this.el = document.createElement('iframe');
this.parent = node;
this.stylesheet = config.stylesheet;
this.customStylesHash = config.customStyles || {};
this.classes = config.classes;
this.browserFeatures = config.browserFeatures;
this.googleFonts = config.googleFonts || [];
this.name = config.name;
if (config.width) {
this.setWidth(config.width);
}
Object.keys(iframeStyles).forEach((key) => {
this.el.style[key] = iframeStyles[key];
});
Object.keys(iframeAttrs).forEach((key) => this.el.setAttribute(key, iframeAttrs[key]));
this.el.setAttribute('name', config.name);
if (config.title) {
this.el.setAttribute('title', config.title);
}
this.styleTag = null;
}
load() {
return new Promise((resolve) => {
this.el.onload = () => {
return this.loadFonts().then(() => {
this.appendStyleTag();
return resolve();
});
};
this.parent.appendChild(this.el);
});
}
loadFonts() {
if (!this.googleFonts || !this.googleFonts.length) {
return Promise.resolve(true);
}
return this.loadFontScript().then(() => {
return new Promise((resolve) => {
if (!window.WebFont) {
return resolve();
}
window.WebFont.load({
google: {
families: this.googleFonts,
},
fontactive: () => {
return resolve();
},
context: this.el.contentWindow || frames[this.name],
});
return window.setTimeout(() => {
return resolve();
}, 1000);
});
});
}
loadFontScript() {
if (window.WebFont) {
return Promise.resolve();
}
const fontScript = document.createElement('script');
return new Promise((resolve) => {
fontScript.onload = () => {
resolve();
};
fontScript.src = webfontScript;
document.head.appendChild(fontScript);
setTimeout(() => {
resolve();
}, 500);
});
}
setWidth(width) {
this.parent.style['max-width'] = width;
}
get width() {
return this.parent.style['max-width'];
}
addClass(className) {
addClassToElement(className, this.parent);
}
removeClass(className) {
removeClassFromElement(className, this.parent);
}
setName(name) {
this.el.setAttribute('name', name);
}
get document() {
let doc;
if (this.el.contentWindow && this.el.contentWindow.document.body) {
doc = this.el.contentWindow.document;
} else if (this.el.document && this.el.document.body) {
doc = this.el.document;
} else if (this.el.contentDocument && this.el.contentDocument.body) {
doc = this.el.contentDocument;
}
return doc;
}
get customStyles() {
let customStyles = [];
Object.keys(this.customStylesHash).forEach((typeKey) => {
if (this.customStylesHash[typeKey]) {
Object.keys(this.customStylesHash[typeKey]).forEach((key) => {
const styleGroup = selectorStyleGroup(this.customStylesHash[typeKey][key], this.classes[typeKey][key], this.classes[typeKey]);
customStyles = customStyles.concat(styleGroup);
});
}
});
return customStyles;
}
get conditionalCSS() {
if (this.browserFeatures.transition && this.browserFeatures.transform && this.browserFeatures.animation) {
return '';
}
return conditionalStyles;
}
get css() {
const compiled = Mustache.render(stylesTemplate, {selectors: this.customStyles});
return `${this.stylesheet} \n ${compiled} \n ${this.conditionalCSS}`;
}
updateStyles(customStyles, googleFonts) {
this.googleFonts = googleFonts;
return this.loadFonts().then(() => {
this.customStylesHash = customStyles;
this.styleTag.innerHTML = this.css;
return;
});
}
appendStyleTag() {
if (!this.document.head) {
return;
}
this.styleTag = this.document.createElement('style');
if (this.styleTag.styleSheet) {
this.styleTag.styleSheet.cssText = this.css;
} else {
this.styleTag.appendChild(this.document.createTextNode(this.css));
}
this.document.head.appendChild(this.styleTag);
}
}