-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 1.53 KB
/
index.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
var calculateHeight;
var isIE = !!navigator.userAgent.toLowerCase().match(/trident/);
if (isIE) {
calculateHeight = function() {
if (document.body) {
return document.body.clientHeight + 30;
} else {
return -1;
}
};
} else {
calculateHeight = function() {
if (document.documentElement) {
return document.documentElement.getBoundingClientRect().height;
} else {
return -1;
}
};
}
var ResizableIframe = function(options) {
options = options || {};
this.options = {
nameKey: options.nameKey || 'iframeName',
heightKey: options.heightKey || 'iframeHeight',
intervalDuration: options.intervalDuration || 1000
};
this.knownHeight = 0;
this.start();
};
ResizableIframe.prototype.checkHeight = function() {
var newHeight = calculateHeight();
if (newHeight !== -1 && newHeight !== this.knownHeight) {
var sizeObject = {};
sizeObject[this.options.nameKey] = window.name;
sizeObject[this.options.heightKey] = newHeight;
if (window.parent && parent.postMessage) {
parent.postMessage(JSON.stringify(sizeObject), '*');
}
this.knownHeight = newHeight;
}
};
ResizableIframe.prototype.start = function() {
if ( ! this.interval) {
this.checkHeight();
var self = this;
this.interval = setInterval(function() {
self.checkHeight();
}, this.options.intervalDuration);
}
};
ResizableIframe.prototype.stop = function() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
};
module.exports = ResizableIframe;