-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopeniframe.es6.js
More file actions
130 lines (112 loc) · 4.31 KB
/
openiframe.es6.js
File metadata and controls
130 lines (112 loc) · 4.31 KB
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
/**
* Create a new OpenIframe element
* @class OpenIframe
*/
class OpenIframe {
/**
* Creates an instance of OpenIframe.
* @param {Object} options
* @param {DOM|string} options.container - css selector or DOM element
* @param {Number} options.width - iframe width
* @param {Number} options.height - iframe height
* @param {Boolean} options.append - append iframe to container, prepend by default
* @param {String} options.src - iframe src
* @example
* new OpenIframe({
container: '#header',
height: 600,
width: 800
src: 'https://example.com',
append: true
})
* @memberof OpenIframe
* @returns iframe Element
*/
constructor(options) {
this.container = options.container;
this.iframeWidth = parseFloat(options.width) || 800;
this.iframeHeight = parseFloat(options.height) || 600;
this.append = options.append || false;
this.src = options.src || '#';
this.proxyUrl = 'https://serene-hamlet-82201.herokuapp.com/'
this.runPolyfills()
this.validationContainer(() => {
this.createIframeElement();
});
}
runPolyfills() {
((arr) => {
arr.forEach((item) => {
if (item.hasOwnProperty('append')) {
return;
}
Object.defineProperty(item, 'append', {
configurable: true,
enumerable: true,
writable: true,
value: function append() {
// eslint-disable-next-line prefer-rest-params
const argArr = Array.prototype.slice.call(arguments);
const docFrag = document.createDocumentFragment();
argArr.forEach((argItem) => {
const isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.appendChild(docFrag);
}
});
});
arr.forEach((item) => {
if (item.hasOwnProperty('prepend')) {
return;
}
Object.defineProperty(item, 'prepend', {
configurable: true,
enumerable: true,
writable: true,
value: function prepend() {
// eslint-disable-next-line prefer-rest-params
const argArr = Array.prototype.slice.call(arguments);
const docFrag = document.createDocumentFragment();
argArr.forEach((argItem) => {
const isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.insertBefore(docFrag, this.firstChild);
}
});
});
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
}
validationContainer(callback) {
let valid = true;
if (typeof this.container === 'string') {
try {
const container = document.querySelector(this.container);
if (container === null) {
valid = false;
console.error('OpenIframe: is not a valid selector or element not found')
} else {
this.container = document.querySelector(this.container);
}
} catch (error) {
valid = false;
console.error('OpenIframe: is not a valid selector.')
}
}
if (!this.container instanceof HTMLElement) valid = false;
if (valid) {
callback()
}
}
createIframeElement() {
const iframe = document.createElement('iframe');
iframe.src = `${this.proxyUrl}?proxyHost=${this.src}`;
iframe.width = this.iframeWidth;
iframe.height = this.iframeHeight;
if (this.append) {
return this.container.append(iframe)
}
return this.container.prepend(iframe);
}
}