-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopeniframe.es5.js
More file actions
170 lines (151 loc) · 6.43 KB
/
openiframe.es5.js
File metadata and controls
170 lines (151 loc) · 6.43 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
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
"use strict";
function _instanceof(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _classCallCheck(instance, Constructor) {
if (!_instanceof(instance, Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
/**
* Create a new OpenIframe element
* @class OpenIframe
*/
var OpenIframe =
/*#__PURE__*/
function () {
/**
* 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
*/
function OpenIframe(options) {
var _this = this;
_classCallCheck(this, OpenIframe);
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(function () {
_this.createIframeElement();
});
}
_createClass(OpenIframe, [{
key: "runPolyfills",
value: function runPolyfills() {
(function (arr) {
arr.forEach(function (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
var argArr = Array.prototype.slice.call(arguments);
var docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = _instanceof(argItem, Node);
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.appendChild(docFrag);
}
});
});
arr.forEach(function (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
var argArr = Array.prototype.slice.call(arguments);
var docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = _instanceof(argItem, Node);
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.insertBefore(docFrag, this.firstChild);
}
});
});
})([Element.prototype, Document.prototype, DocumentFragment.prototype]);
}
}, {
key: "validationContainer",
value: function validationContainer(callback) {
var valid = true;
if (typeof this.container === 'string') {
try {
var 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 (_instanceof(!this.container, HTMLElement)) valid = false;
if (valid) {
callback();
}
}
}, {
key: "createIframeElement",
value: function createIframeElement() {
var iframe = document.createElement('iframe');
iframe.src = "".concat(this.proxyUrl, "?proxyHost=").concat(this.src);
iframe.width = this.iframeWidth;
iframe.height = this.iframeHeight;
if (this.append) {
return this.container.append(iframe);
}
return this.container.prepend(iframe);
}
}]);
return OpenIframe;
}();