forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiframeResizer.js
More file actions
executable file
·442 lines (374 loc) · 11.8 KB
/
Copy pathiframeResizer.js
File metadata and controls
executable file
·442 lines (374 loc) · 11.8 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* File: iframeReizer.js
* Desc: Force iframes to size to content.
* Requires: iframeResizer.contentWindow.js to be loaded into the target frame.
* Author: David J. Bradshaw - dave@bradshaw.net
* Contributor: Jure Mav - jure.mav@gmail.com
*/
;(function() {
'use strict';
var
count = 0,
firstRun = true,
msgHeader = 'message',
msgHeaderLen = msgHeader.length,
msgId = '[iFrameSizer]', //Must match iframe msg ID
msgIdLen = msgId.length,
page = '', //:'+location.href, //Uncoment to debug nested iFrames
pagePosition = null,
requestAnimationFrame = window.requestAnimationFrame,
resetRequiredMethods = {max:1,scroll:1,bodyScroll:1,documentElementScroll:1},
settings = {},
defaults = {
autoResize : true,
bodyBackground : null,
bodyMargin : null,
bodyMarginV1 : 8,
bodyPadding : null,
checkOrigin : true,
enablePublicMethods : false,
heightCalculationMethod : 'offset',
interval : 32,
log : false,
maxHeight : Infinity,
maxWidth : Infinity,
minHeight : 0,
minWidth : 0,
scrolling : false,
sizeHeight : true,
sizeWidth : false,
tolerance : 0,
closedCallback : function(){},
initCallback : function(){},
messageCallback : function(){},
resizedCallback : function(){}
};
function addEventListener(obj,evt,func){
if ('addEventListener' in window){
obj.addEventListener(evt,func, false);
} else if ('attachEvent' in window){//IE
obj.attachEvent('on'+evt,func);
}
}
function setupRequestAnimationFrame(){
var
vendors = ['moz', 'webkit', 'o', 'ms'],
x;
// Remove vendor prefixing if prefixed and break early if not
for (x = 0; x < vendors.length && !requestAnimationFrame; x += 1) {
requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
}
if (!(requestAnimationFrame)){
log(' RequestAnimationFrame not supported');
}
}
function log(msg){
if (settings.log && (typeof console === 'object')){
console.log(msgId + '[Host page'+page+']' + msg);
}
}
function iFrameListener(event){
function resizeIFrame(){
function resize(){
setSize(messageData);
setPagePosition();
settings.resizedCallback(messageData);
}
syncResize(resize,messageData,'resetPage');
}
function closeIFrame(iframe){
var iframeID = iframe.id;
log(' Removing iFrame: '+iframeID);
iframe.parentNode.removeChild(iframe);
settings.closedCallback(iframeID);
log(' --');
}
function processMsg(){
var data = msg.substr(msgIdLen).split(':');
return {
iframe: document.getElementById(data[0]),
id: data[0],
height: data[1],
width: data[2],
type: data[3]
};
}
function ensureInRange(Dimension){
var
max = Number(settings['max'+Dimension]),
min = Number(settings['min'+Dimension]),
dimension = Dimension.toLowerCase(),
size = Number(messageData[dimension]);
if (min>max){
throw new Error('Value for min'+Dimension+' can not be greater than max'+Dimension);
}
log(' Checking '+dimension+' is in range '+min+'-'+max);
if (size<min) {
size=min;
log(' Set '+dimension+' to min value');
}
if (size>max) {
size=max;
log(' Set '+dimension+' to max value');
}
messageData[dimension]=''+size;
}
function isMessageFromIFrame(){
var
origin = event.origin,
remoteHost = messageData.iframe.src.split('/').slice(0,3).join('/');
if (settings.checkOrigin) {
log(' Checking connection is from: '+remoteHost);
if ((''+origin !== 'null') && (origin !== remoteHost)) {
throw new Error(
'Unexpected message received from: ' + origin +
' for ' + messageData.iframe.id +
'. Message was: ' + event.data +
'. This error can be disabled by adding the checkOrigin: false option.'
);
}
}
return true;
}
function isMessageForUs(){
return msgId === ('' + msg).substr(0,msgIdLen); //''+Protects against non-string msg
}
function isMessageFromMetaParent(){
//test if this message is from a parent above us. This is an ugly test, however, updating
//the message format would break backwards compatibity.
var retCode = messageData.type in {'true':1,'false':1};
if (retCode){
log(' Ignoring init message from meta parent page');
}
return retCode;
}
function forwardMsgFromIFrame(){
var msgBody = msg.substr(msg.indexOf(':')+msgHeaderLen+6); //6 === ':0:0:' + ':' (Ideas to name this magic number most welcome)
log(' MessageCallback passed: {iframe: '+ messageData.iframe.id + ', message: ' + msgBody + '}');
settings.messageCallback({
iframe: messageData.iframe,
message: msgBody
});
log(' --');
}
function checkIFrameExists(){
if (null === messageData.iframe) {
throw new Error('iFrame ('+messageData.id+') does not exist on ' + page);
}
return true;
}
function scrollRequestFromChild(){
log(' Reposition requested from iFrame');
pagePosition = {
x: messageData.width,
y: messageData.height
};
setPagePosition();
}
function actionMsg(){
switch(messageData.type){
case 'close':
closeIFrame(messageData.iframe);
settings.resizedCallback(messageData); //To be removed.
break;
case 'message':
forwardMsgFromIFrame();
break;
case 'scrollTo':
scrollRequestFromChild();
break;
case 'reset':
resetIFrame(messageData);
break;
case 'init':
resizeIFrame();
settings.initCallback(messageData.iframe);
break;
default:
resizeIFrame();
}
}
var
msg = event.data,
messageData = {};
if (isMessageForUs()){
log(' Received: '+msg);
messageData = processMsg();
ensureInRange('Height');
ensureInRange('Width');
if ( !isMessageFromMetaParent() && checkIFrameExists() && isMessageFromIFrame() ){
actionMsg();
firstRun = false;
}
}
}
function getPagePosition (){
if(null === pagePosition){
pagePosition = {
x: (window.pageXOffset !== undefined) ? window.pageXOffset : document.documentElement.scrollLeft,
y: (window.pageYOffset !== undefined) ? window.pageYOffset : document.documentElement.scrollTop
};
log(' Get position: '+pagePosition.x+','+pagePosition.y);
}
}
function setPagePosition(){
if(null !== pagePosition){
window.scrollTo(pagePosition.x,pagePosition.y);
log(' Set position: '+pagePosition.x+','+pagePosition.y);
pagePosition = null;
}
}
function resetIFrame(messageData){
function reset(){
setSize(messageData);
trigger('reset','reset',messageData.iframe);
}
log(' Size reset requested by '+('init'===messageData.type?'host page':'iFrame'));
getPagePosition();
syncResize(reset,messageData,'init');
}
function setSize(messageData){
function setDimension(dimension){
messageData.iframe.style[dimension] = messageData[dimension] + 'px';
log(
' IFrame (' + messageData.iframe.id +
') ' + dimension +
' set to ' + messageData[dimension] + 'px'
);
}
if( settings.sizeHeight) { setDimension('height'); }
if( settings.sizeWidth ) { setDimension('width'); }
}
function syncResize(func,messageData,doNotSync){
if(doNotSync!==messageData.type && requestAnimationFrame){
log(' Requesting animation frame');
requestAnimationFrame(func);
} else {
func();
}
}
function trigger(calleeMsg,msg,iframe){
log('[' + calleeMsg + '] Sending msg to iframe ('+msg+')');
iframe.contentWindow.postMessage( msgId + msg, '*' );
}
function setupIFrame(){
function setLimits(){
function addStyle(style){
if ((Infinity !== settings[style]) && (0 !== settings[style])){
iframe.style[style] = settings[style] + 'px';
log(' Set '+style+' = '+settings[style]+'px');
}
}
addStyle('maxHeight');
addStyle('minHeight');
addStyle('maxWidth');
addStyle('minWidth');
}
function ensureHasId(iframeID){
if (''===iframeID){
iframe.id = iframeID = 'iFrameResizer' + count++;
log(' Added missing iframe ID: '+ iframeID +' (' + iframe.src + ')');
}
return iframeID;
}
function setScrolling(){
log(' IFrame scrolling ' + (settings.scrolling ? 'enabled' : 'disabled') + ' for ' + iframeID);
iframe.style.overflow = false === settings.scrolling ? 'hidden' : 'auto';
iframe.scrolling = false === settings.scrolling ? 'no' : 'yes';
}
//The V1 iFrame script expects an int, where as in V2 expects a CSS
//string value such as '1px 3em', so if we have an int for V2, set V1=V2
//and then convert V2 to a string PX value.
function setupBodyMarginValues(){
if (('number'===typeof(settings.bodyMargin)) || ('0'===settings.bodyMargin)){
settings.bodyMarginV1 = settings.bodyMargin;
settings.bodyMargin = '' + settings.bodyMargin + 'px';
}
}
function createOutgoingMsg(){
return iframeID +
':' + settings.bodyMarginV1 +
':' + settings.sizeWidth +
':' + settings.log +
':' + settings.interval +
':' + settings.enablePublicMethods +
':' + settings.autoResize +
':' + settings.bodyMargin +
':' + settings.heightCalculationMethod +
':' + settings.bodyBackground +
':' + settings.bodyPadding +
':' + settings.tolerance;
}
function init(msg){
//We have to call trigger twice, as we can not be sure if all
//iframes have completed loading when this code runs. The
//event listener also catches the page changing in the iFrame.
addEventListener(iframe,'load',function(){
var fr = firstRun; // Reduce scope of var to function, because IE8's JS execution
// context stack is borked and this value gets externally
// changed midway through running this function.
trigger('iFrame.onload',msg,iframe);
if (!fr && settings.heightCalculationMethod in resetRequiredMethods){
resetIFrame({
iframe:iframe,
height:0,
width:0,
type:'init'
});
}
});
trigger('init',msg,iframe);
}
var
/*jshint validthis:true */
iframe = this,
iframeID = ensureHasId(iframe.id);
setScrolling();
setLimits();
setupBodyMarginValues();
init(createOutgoingMsg());
}
function checkOptions(options){
if ('object' !== typeof options){
throw new TypeError('Options is not an object.');
}
}
function createNativePublicFunction(){
function init(element){
if('IFRAME' !== element.tagName.toUpperCase()) {
throw new TypeError('Expected <IFRAME> tag, found <'+element.tagName+'>.');
} else {
setupIFrame.call(element);
}
}
function processOptions(options){
options = options || {};
checkOptions(options);
for (var option in defaults) {
if (defaults.hasOwnProperty(option)){
settings[option] = options.hasOwnProperty(option) ? options[option] : defaults[option];
}
}
}
return function iFrameResizeF(options,selecter){
processOptions(options);
Array.prototype.forEach.call( document.querySelectorAll( selecter || 'iframe' ), init );
};
}
function createJQueryPublicMethod($){
$.fn.iFrameResize = function $iFrameResizeF(options) {
options = options || {};
checkOptions(options);
settings = $.extend( {}, defaults, options );
return this.filter('iframe').each( setupIFrame ).end();
};
}
setupRequestAnimationFrame();
addEventListener(window,'message',iFrameListener);
if (window.jQuery) { createJQueryPublicMethod(jQuery); }
if (typeof define === 'function' && define.amd) {
define(function (){ return createNativePublicFunction(); });
} else {
window.iFrameResize = createNativePublicFunction();
}
})();