-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelasticframe.js
More file actions
167 lines (152 loc) · 5.07 KB
/
elasticframe.js
File metadata and controls
167 lines (152 loc) · 5.07 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
'use strict';
/*!
* elasticframe
* https://github.com/kwizzn/elasticframe
* Copyright (c) 2015 Chris Neuhäuser, TYPE10 Media GmbH
* Licensed under the MIT License
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory();
} else {
window.ElasticFrame = factory(); // jshint ignore:line
}
}(function() {
// https://github.com/Modernizr/Modernizr/issues/1894#issuecomment-200691178
var supportsEventOptions = false;
try {
document.addEventListener('test', null, {
get passive() {
supportsEventOptions = true;
}
});
} catch(e) {}
function cancelAnimationFrame(id) {
if (window.cancelAnimationFrame) {
window.cancelAnimationFrame(id);
} else {
clearTimeout(id);
}
}
function requestAnimationFrame(callback) {
if (window.requestAnimationFrame) {
return window.requestAnimationFrame(callback);
} else {
return setTimeout(callback, 16.66); // 60fps
}
}
function getData(json) {
var data;
try {
data = JSON.parse(json);
} catch(e) {}
if (data !== null && typeof data === 'object' && data.type === 'elasticframe') {
return data;
}
return {};
}
function listen(event, handler, options) {
if (window.addEventListener) {
if (!supportsEventOptions && options !== null && typeof options === 'object') {
options = options.capture;
}
window.addEventListener(event, handler, options);
} else {
window.attachEvent('on' + event, handler);
}
}
function sendHeight() {
var de = document.documentElement;
var bd = document.body;
var height = Math.max(
bd.scrollHeight, de.scrollHeight,
bd.offsetHeight, de.offsetHeight,
bd.clientHeight, de.clientHeight
);
window.parent.postMessage(JSON.stringify({ type: 'elasticframe', code: 'height', height: height }), '*');
}
function sendHeightRequest(iframe) {
iframe.contentWindow.postMessage(JSON.stringify({ type: 'elasticframe', code: 'height-request' }), '*');
}
function sendResetRequest() {
window.parent.postMessage(JSON.stringify({ type: 'elasticframe', code: 'reset-request' }), '*');
}
function setHeight(iframe, height) {
if (!isNaN(height)) {
iframe.style.height = height + 'px';
} else {
// This should never happen
iframe.style.height = 'auto';
}
}
function resetHeight(iframe) {
iframe.style.height = '0px';
}
function initParent(iframe) {
var heightDelay,resetDelay,resizeDelay;
if (!iframe.contentWindow.postMessage) {
return;
}
function resizeIframe() {
cancelAnimationFrame(heightDelay);
cancelAnimationFrame(resetDelay);
cancelAnimationFrame(resizeDelay);
resizeDelay = requestAnimationFrame(function() {
resetHeight(iframe);
sendHeightRequest(iframe);
});
}
listen('message', function(event) {
var data;
if (event.source === iframe.contentWindow) {
data = getData(event.data);
switch (data.code) {
case 'height': {
cancelAnimationFrame(heightDelay);
heightDelay = requestAnimationFrame(function() {
setHeight(iframe, parseInt(data.height));
});
break;
}
case 'reset-request': {
cancelAnimationFrame(resetDelay);
resetDelay = requestAnimationFrame(function() {
resetHeight(iframe);
sendHeightRequest(iframe);
});
break;
}
}
}
}, { passive: true });
listen('resize', function() {
resizeIframe();
}, { passive: true });
return { resize: resizeIframe };
}
function initIframe() {
if (!window.parent || !window.parent.postMessage) {
return;
}
listen('message', function(event) {
var data = getData(event.data);
if (data.code === 'height-request') {
if (document.readyState === 'complete') {
sendHeight();
} else {
listen('load', function() {
sendHeight();
}, { once: true, passive: true });
}
}
}, { passive: true });
sendResetRequest();
return { resize: sendResetRequest };
}
return {
initParent: initParent,
initIframe: initIframe
};
}));