annotate iframe-resizer/src/iframeResizer.js @ 9:7300ed4c1481 draft default tip

Uploaded
author saskia-hiltemann
date Mon, 04 Sep 2017 10:49:00 -0400
parents ac5f9272033b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
1 /*
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
2 * File: iframeReizer.js
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
3 * Desc: Force iframes to size to content.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
4 * Requires: iframeResizer.contentWindow.js to be loaded into the target frame.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
5 * Author: David J. Bradshaw - dave@bradshaw.net
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
6 * Contributor: Jure Mav - jure.mav@gmail.com
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
7 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
8 ;( function() {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
9 'use strict';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
10
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
11 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
12 count = 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
13 firstRun = true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
14 msgHeader = 'message',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
15 msgHeaderLen = msgHeader.length,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
16 msgId = '[iFrameSizer]', //Must match iframe msg ID
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
17 msgIdLen = msgId.length,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
18 page = '', //:'+location.href, //Uncoment to debug nested iFrames
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
19 pagePosition = null,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
20 requestAnimationFrame = window.requestAnimationFrame,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
21 resetRequiredMethods = {max:1,scroll:1,bodyScroll:1,documentElementScroll:1},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
22 settings = {},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
23
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
24 defaults = {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
25 autoResize : true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
26 bodyBackground : null,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
27 bodyMargin : null,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
28 bodyMarginV1 : 8,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
29 bodyPadding : null,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
30 checkOrigin : true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
31 enablePublicMethods : false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
32 heightCalculationMethod : 'offset',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
33 interval : 32,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
34 log : false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
35 maxHeight : Infinity,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
36 maxWidth : Infinity,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
37 minHeight : 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
38 minWidth : 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
39 scrolling : false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
40 sizeHeight : true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
41 sizeWidth : false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
42 tolerance : 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
43 closedCallback : function(){},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
44 initCallback : function(){},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
45 messageCallback : function(){},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
46 resizedCallback : function(){}
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
47 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
48
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
49 function addEventListener(obj,evt,func){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
50 if ('addEventListener' in window){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
51 obj.addEventListener(evt,func, false);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
52 } else if ('attachEvent' in window){//IE
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
53 obj.attachEvent('on'+evt,func);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
54 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
55 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
56
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
57 function setupRequestAnimationFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 vendors = ['moz', 'webkit', 'o', 'ms'],
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 x;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 // Remove vendor prefixing if prefixed and break early if not
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63 for (x = 0; x < vendors.length && !requestAnimationFrame; x += 1) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
64 requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
65 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
66
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
67 if (!(requestAnimationFrame)){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
68 log(' RequestAnimationFrame not supported');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
69 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72 function log(msg){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 if (settings.log && (typeof console === 'object')){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74 console.log(msgId + '[Host page'+page+']' + msg);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79 function iFrameListener(event){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 function resizeIFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 function resize(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82 setSize(messageData);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83 setPagePosition();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84 settings.resizedCallback(messageData);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 syncResize(resize,messageData,'resetPage');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
88 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
89
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
90 function closeIFrame(iframe){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
91 var iframeID = iframe.id;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 log(' Removing iFrame: '+iframeID);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 iframe.parentNode.removeChild(iframe);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95 settings.closedCallback(iframeID);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 log(' --');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99 function processMsg(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100 var data = msg.substr(msgIdLen).split(':');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 return {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103 iframe: document.getElementById(data[0]),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104 id: data[0],
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105 height: data[1],
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 width: data[2],
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 type: data[3]
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 function ensureInRange(Dimension){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
112 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
113 max = Number(settings['max'+Dimension]),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
114 min = Number(settings['min'+Dimension]),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 dimension = Dimension.toLowerCase(),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 size = Number(messageData[dimension]);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 if (min>max){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119 throw new Error('Value for min'+Dimension+' can not be greater than max'+Dimension);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122 log(' Checking '+dimension+' is in range '+min+'-'+max);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124 if (size<min) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 size=min;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
126 log(' Set '+dimension+' to min value');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
127 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
128
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
129 if (size>max) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
130 size=max;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131 log(' Set '+dimension+' to max value');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134 messageData[dimension]=''+size;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137 function isMessageFromIFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140 origin = event.origin,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141 remoteHost = messageData.iframe.src.split('/').slice(0,3).join('/');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
142
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
143 if (settings.checkOrigin) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
144 log(' Checking connection is from: '+remoteHost);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
145
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
146 if ((''+origin !== 'null') && (origin !== remoteHost)) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
147 throw new Error(
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
148 'Unexpected message received from: ' + origin +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
149 ' for ' + messageData.iframe.id +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
150 '. Message was: ' + event.data +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
151 '. This error can be disabled by adding the checkOrigin: false option.'
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
152 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
153 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
154 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
155
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
156 return true;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
157 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
158
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
159 function isMessageForUs(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
160 return msgId === ('' + msg).substr(0,msgIdLen); //''+Protects against non-string msg
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
161 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
162
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
163 function isMessageFromMetaParent(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
164 //test if this message is from a parent above us. This is an ugly test, however, updating
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
165 //the message format would break backwards compatibity.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
166 var retCode = messageData.type in {'true':1,'false':1};
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
167
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
168 if (retCode){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
169 log(' Ignoring init message from meta parent page');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
170 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
171
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
172 return retCode;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
173 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
174
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
175 function forwardMsgFromIFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
176 var msgBody = msg.substr(msg.indexOf(':')+msgHeaderLen+6); //6 === ':0:0:' + ':' (Ideas to name this magic number most welcome)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
177
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
178 log(' MessageCallback passed: {iframe: '+ messageData.iframe.id + ', message: ' + msgBody + '}');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
179 settings.messageCallback({
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
180 iframe: messageData.iframe,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
181 message: msgBody
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
182 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
183 log(' --');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
184 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
185
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
186 function checkIFrameExists(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
187 if (null === messageData.iframe) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
188 throw new Error('iFrame ('+messageData.id+') does not exist on ' + page);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
189 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
190 return true;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
191 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
192
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
193 function actionMsg(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
194 switch(messageData.type){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
195 case 'close':
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
196 closeIFrame(messageData.iframe);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
197 settings.resizedCallback(messageData); //To be removed.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
198 break;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
199 case 'message':
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
200 forwardMsgFromIFrame();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
201 break;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
202 case 'reset':
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
203 resetIFrame(messageData);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
204 break;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
205 case 'init':
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
206 resizeIFrame();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
207 settings.initCallback(messageData.iframe);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
208 break;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
209 default:
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
210 resizeIFrame();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
211 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
212 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
213
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
214 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
215 msg = event.data,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
216 messageData = {};
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
217
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
218 if (isMessageForUs()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
219 log(' Received: '+msg);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
220 messageData = processMsg();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
221 ensureInRange('Height');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
222 ensureInRange('Width');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
223
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
224 if ( !isMessageFromMetaParent() && checkIFrameExists() && isMessageFromIFrame() ){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
225 actionMsg();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
226 firstRun = false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
227 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
228 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
229 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
230
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
231
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
232 function getPagePosition (){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
233 if(null === pagePosition){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
234 pagePosition = {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
235 x: (window.pageXOffset !== undefined) ? window.pageXOffset : document.documentElement.scrollLeft,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
236 y: (window.pageYOffset !== undefined) ? window.pageYOffset : document.documentElement.scrollTop
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
237 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
238 log(' Get position: '+pagePosition.x+','+pagePosition.y);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
239 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
240 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
241
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
242 function setPagePosition(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
243 if(null !== pagePosition){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
244 window.scrollTo(pagePosition.x,pagePosition.y);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
245 log(' Set position: '+pagePosition.x+','+pagePosition.y);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
246 pagePosition = null;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
247 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
248 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
249
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
250 function resetIFrame(messageData){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
251 function reset(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
252 setSize(messageData);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
253 trigger('reset','reset',messageData.iframe);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
254 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
255
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
256 log(' Size reset requested by '+('init'===messageData.type?'host page':'iFrame'));
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
257 getPagePosition();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
258 syncResize(reset,messageData,'init');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
259 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
260
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
261 function setSize(messageData){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
262 function setDimension(dimension,min,max){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
263 messageData.iframe.style[dimension] = messageData[dimension] + 'px';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
264 log(
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
265 ' IFrame (' + messageData.iframe.id +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
266 ') ' + dimension +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
267 ' set to ' + messageData[dimension] + 'px'
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
268 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
269 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
270
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
271 if( settings.sizeHeight) { setDimension('height'); }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
272 if( settings.sizeWidth ) { setDimension('width'); }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
273 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
274
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
275 function syncResize(func,messageData,doNotSync){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
276 if(doNotSync!==messageData.type && requestAnimationFrame){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
277 log(' Requesting animation frame');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
278 requestAnimationFrame(func);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
279 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
280 func();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
281 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
282 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
283
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
284 function trigger(calleeMsg,msg,iframe){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
285 log('[' + calleeMsg + '] Sending msg to iframe ('+msg+')');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
286 iframe.contentWindow.postMessage( msgId + msg, '*' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
287 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
288
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
289
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
290 function setupIFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
291 function setLimits(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
292 function addStyle(style){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
293 if ((Infinity !== settings[style]) && (0 !== settings[style])){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
294 iframe.style[style] = settings[style] + 'px';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
295 log(' Set '+style+' = '+settings[style]+'px');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
296 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
297 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
298
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
299 addStyle('maxHeight');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
300 addStyle('minHeight');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
301 addStyle('maxWidth');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
302 addStyle('minWidth');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
303 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
304
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
305 function ensureHasId(iframeID){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
306 if (''===iframeID){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
307 iframe.id = iframeID = 'iFrameResizer' + count++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
308 log(' Added missing iframe ID: '+ iframeID);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
309 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
310
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
311 return iframeID;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
312 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
313
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
314 function setScrolling(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
315 log(' IFrame scrolling ' + (settings.scrolling ? 'enabled' : 'disabled') + ' for ' + iframeID);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
316 iframe.style.overflow = false === settings.scrolling ? 'hidden' : 'auto';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
317 iframe.scrolling = false === settings.scrolling ? 'no' : 'yes';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
318 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
319
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
320 //The V1 iFrame script expects an int, where as in V2 expects a CSS
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
321 //string value such as '1px 3em', so if we have an int for V2, set V1=V2
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
322 //and then convert V2 to a string PX value.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
323 function setupBodyMarginValues(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
324 if (('number'===typeof(settings.bodyMargin)) || ('0'===settings.bodyMargin)){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
325 settings.bodyMarginV1 = settings.bodyMargin;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
326 settings.bodyMargin = '' + settings.bodyMargin + 'px';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
327 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
328 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
329
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
330 function createOutgoingMsg(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
331 return iframeID +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
332 ':' + settings.bodyMarginV1 +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
333 ':' + settings.sizeWidth +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
334 ':' + settings.log +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
335 ':' + settings.interval +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
336 ':' + settings.enablePublicMethods +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
337 ':' + settings.autoResize +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
338 ':' + settings.bodyMargin +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
339 ':' + settings.heightCalculationMethod +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
340 ':' + settings.bodyBackground +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
341 ':' + settings.bodyPadding +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
342 ':' + settings.tolerance;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
343 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
344
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
345 function init(msg){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
346 //We have to call trigger twice, as we can not be sure if all
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
347 //iframes have completed loading when this code runs. The
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
348 //event listener also catches the page changing in the iFrame.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
349 addEventListener(iframe,'load',function(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
350 var fr = firstRun; // Reduce scope of var to function, because IE8's JS execution
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
351 // context stack is borked and this value gets externally
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
352 // changed midway through running this function.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
353 trigger('iFrame.onload',msg,iframe);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
354 if (!fr && settings.heightCalculationMethod in resetRequiredMethods){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
355 resetIFrame({
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
356 iframe:iframe,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
357 height:0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
358 width:0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
359 type:'init'
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
360 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
361 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
362 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
363 trigger('init',msg,iframe);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
364 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
365
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
366 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
367 /*jshint validthis:true */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
368 iframe = this,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
369 iframeID = ensureHasId(iframe.id);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
370
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
371 setScrolling();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
372 setLimits();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
373 setupBodyMarginValues();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
374 init(createOutgoingMsg());
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
375 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
376
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
377 function checkOptions(options){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
378 if ('object' !== typeof options){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
379 throw new TypeError('Options is not an object.');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
380 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
381 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
382
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
383 function createNativePublicFunction(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
384 function init(element){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
385 if('IFRAME' !== element.tagName) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
386 throw new TypeError('Expected <IFRAME> tag, found <'+element.tagName+'>.');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
387 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
388 setupIFrame.call(element);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
389 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
390 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
391
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
392 function processOptions(options){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
393 options = options || {};
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
394
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
395 checkOptions(options);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
396
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
397 for (var option in defaults) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
398 if (defaults.hasOwnProperty(option)){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
399 settings[option] = options.hasOwnProperty(option) ? options[option] : defaults[option];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
400 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
401 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
402 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
403
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
404 return function iFrameResizeF(options,selecter){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
405 processOptions(options);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
406 Array.prototype.forEach.call( document.querySelectorAll( selecter || 'iframe' ), init );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
407 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
408 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
409
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
410 function createJQueryPublicMethod($){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
411 $.fn.iFrameResize = function $iFrameResizeF(options) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
412 checkOptions(options);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
413 settings = $.extend( {}, defaults, options );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
414 return this.filter('iframe').each( setupIFrame ).end();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
415 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
416 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
417
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
418 setupRequestAnimationFrame();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
419 addEventListener(window,'message',iFrameListener);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
420
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
421 if ('jQuery' in window) { createJQueryPublicMethod(jQuery); }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
422
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
423 if (typeof define === 'function' && define.amd) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
424 define(function (){ return createNativePublicFunction(); });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
425 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
426 window.iFrameResize = createNativePublicFunction();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
427 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
428
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
429 })();