annotate iframe-resizer/src/iframeResizer.contentWindow.js @ 0:ac5f9272033b draft

first upload
author saskia-hiltemann
date Tue, 01 Jul 2014 11:42:23 -0400
parents
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: iframeSizer.contentWindow.js
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
3 * Desc: Include this file in any page being loaded into an iframe
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
4 * to force the iframe to resize to the content size.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
5 * Requires: iframeResizer.js on host page.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
6 * Author: David J. Bradshaw - dave@bradshaw.net
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
7 * Contributor: Jure Mav - jure.mav@gmail.com
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
8 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
9
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
10 ;(function() {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
11 'use strict';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
12
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
13 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
14 autoResize = true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
15 base = 10,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
16 bodyBackground = '',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
17 bodyMargin = 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
18 bodyMarginStr = '',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
19 bodyPadding = '',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
20 calculateWidth = false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
21 doubleEventList = {'resize':1,'click':1},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
22 eventCancelTimer = 64,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
23 height = 1,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
24 firstRun = true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
25 heightCalcModeDefault = 'offset',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
26 heightCalcMode = heightCalcModeDefault,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
27 initLock = true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
28 initMsg = '',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
29 interval = 32,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
30 logging = false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
31 msgID = '[iFrameSizer]', //Must match host page msg ID
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
32 msgIdLen = msgID.length,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
33 myID = '',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
34 publicMethods = false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
35 resetRequiredMethods = {max:1,scroll:1,bodyScroll:1,documentElementScroll:1},
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
36 targetOriginDefault = '*',
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
37 target = window.parent,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
38 tolerance = 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
39 triggerLocked = false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
40 triggerLockedTimer = null,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
41 width = 1;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
42
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
43
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
44 function addEventListener(el,evt,func){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
45 if ('addEventListener' in window){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
46 el.addEventListener(evt,func, false);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
47 } else if ('attachEvent' in window){ //IE
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
48 el.attachEvent('on'+evt,func);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
49 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
50 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
51
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
52 function formatLogMsg(msg){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
53 return msgID + '[' + myID + ']' + ' ' + msg;
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 function log(msg){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
57 if (logging && ('object' === typeof window.console)){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 console.log(formatLogMsg(msg));
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 function warn(msg){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63 if ('object' === typeof window.console){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
64 console.warn(formatLogMsg(msg));
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
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
68
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
69 function init(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 log('Initialising iFrame');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71 readData();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72 setMargin();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 setBodyStyle('background',bodyBackground);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74 setBodyStyle('padding',bodyPadding);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 injectClearFixIntoBodyElement();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 checkHeightMode();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77 stopInfiniteResizingOfIFrame();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78 setupPublicMethods();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79 startEventListeners();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 sendSize('init','Init message from host page');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83 function readData(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 var data = initMsg.substr(msgIdLen).split(':');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 function strBool(str){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
88 return 'true' === str ? true : false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
89 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
90
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
91 myID = data[0];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92 bodyMargin = (undefined !== data[1]) ? Number(data[1]) : bodyMargin; //For V1 compatibility
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 calculateWidth = (undefined !== data[2]) ? strBool(data[2]) : calculateWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 logging = (undefined !== data[3]) ? strBool(data[3]) : logging;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95 interval = (undefined !== data[4]) ? Number(data[4]) : interval;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 publicMethods = (undefined !== data[5]) ? strBool(data[5]) : publicMethods;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97 autoResize = (undefined !== data[6]) ? strBool(data[6]) : autoResize;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98 bodyMarginStr = data[7];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99 heightCalcMode = (undefined !== data[8]) ? data[8] : heightCalcMode;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100 bodyBackground = data[9];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101 bodyPadding = data[10];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 tolerance = (undefined !== data[11]) ? Number(data[11]) : tolerance;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105 function chkCSS(attr,value){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 if (-1 !== value.indexOf('-')){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 warn('Negative CSS value ignored for '+attr);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 value='';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110 return value;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
112
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
113 function setBodyStyle(attr,value){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
114 if ((undefined !== value) && ('' !== value) && ('null' !== value)){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 document.body.style[attr] = value;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 log('Body '+attr+' set to "'+value+'"');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 function setMargin(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121 //If called via V1 script, convert bodyMargin from int to str
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122 if (undefined === bodyMarginStr){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123 bodyMarginStr = bodyMargin+'px';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 chkCSS('margin',bodyMarginStr);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
126 setBodyStyle('margin',bodyMarginStr);
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 function stopInfiniteResizingOfIFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
130 document.documentElement.style.height = '';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131 document.body.style.height = '';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 log('HTML & body height set to "auto"');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 function initWindowResizeListener(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136 addEventListener(window,'resize', function(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137 sendSize('resize','Window resized');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141 function initWindowClickListener(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
142 addEventListener(window,'click', function(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
143 sendSize('click','Window clicked');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
144 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
145 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
146
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
147 function checkHeightMode(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
148 if (heightCalcModeDefault !== heightCalcMode){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
149 if (!(heightCalcMode in getHeight)){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
150 warn(heightCalcMode + ' is not a valid option for heightCalculationMethod.');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
151 heightCalcMode='bodyScroll';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
152 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
153 log('Height calculation method set to "'+heightCalcMode+'"');
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
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
157 function startEventListeners(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
158 if ( true === autoResize ) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
159 initWindowResizeListener();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
160 initWindowClickListener();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
161 setupMutationObserver();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
162 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
163 else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
164 log('Auto Resize disabled');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
165 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
166 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
167
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
168 function injectClearFixIntoBodyElement(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
169 var clearFix = document.createElement('div');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
170 clearFix.style.clear = 'both';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
171 clearFix.style.display = 'block'; //Guard against this having been globally redefined in CSS.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
172 document.body.appendChild(clearFix);
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 setupPublicMethods(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
176 if (publicMethods) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
177 log('Enable public methods');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
178
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
179 window.parentIFrame = {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
180 close: function closeF(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
181 sendSize('close','parentIFrame.close()', 0, 0);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
182 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
183 getId: function getIdF(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
184 return myID;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
185 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
186 reset: function resetF(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
187 resetIFrame('parentIFrame.size');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
188 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
189 sendMessage: function sendMessageF(msg,targetOrigin){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
190 sendMsg(0,0,'message',msg,targetOrigin);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
191 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
192 setHeightCalculationMethod: function setHeightCalculationMethodF(heightCalculationMethod){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
193 heightCalcMode = heightCalculationMethod;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
194 checkHeightMode();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
195 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
196 setTargetOrigin: function setTargetOriginF(targetOrigin){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
197 log('Set targetOrigin: '+targetOrigin);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
198 targetOriginDefault = targetOrigin;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
199 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
200 size: function sizeF(customHeight, customWidth){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
201 var valString = ''+(customHeight?customHeight:'')+(customWidth?','+customWidth:'');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
202 lockTrigger();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
203 sendSize('size','parentIFrame.size('+valString+')', customHeight, customWidth);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
204 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
205 };
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
206 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
207 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
208
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
209 function initInterval(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
210 if ( 0 !== interval ){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
211 log('setInterval: '+interval+'ms');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
212 setInterval(function(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
213 sendSize('interval','setInterval: '+interval);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
214 },Math.abs(interval));
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
215 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
216 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
217
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
218 function setupInjectElementLoadListners(mutations){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
219 function addLoadListener(element){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
220 if (element.height === undefined || element.width === undefined || 0 === element.height || 0 === element.width){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
221 log('Attach listerner to '+element.src);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
222 addEventListener(element,'load', function imageLoaded(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
223 sendSize('imageLoad','Image loaded');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
224 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
225 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
226 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
227
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
228 mutations.forEach(function (mutation) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
229 if (mutation.type === 'attributes' && mutation.attributeName === 'src'){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
230 addLoadListener(mutation.target);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
231 } else if (mutation.type === 'childList'){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
232 var images = mutation.target.querySelectorAll('img');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
233 Array.prototype.forEach.call(images,function (image) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
234 addLoadListener(image);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
235 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
236 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
237 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
238 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
239
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
240 function setupMutationObserver(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
241
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
242 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
243
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
244 function createMutationObserver(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
245 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
246 target = document.querySelector('body'),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
247
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
248 config = {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
249 attributes : true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
250 attributeOldValue : false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
251 characterData : true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
252 characterDataOldValue : false,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
253 childList : true,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
254 subtree : true
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
255 },
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
256
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
257 observer = new MutationObserver(function(mutations) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
258 sendSize('mutationObserver','mutationObserver: ' + mutations[0].target + ' ' + mutations[0].type);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
259 setupInjectElementLoadListners(mutations); //Deal with WebKit asyncing image loading when tags are injected into the page
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
260 });
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
261
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
262 log('Enable MutationObserver');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
263 observer.observe(target, config);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
264 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
265
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
266 if (MutationObserver){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
267 if (0 > interval) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
268 initInterval();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
269 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
270 createMutationObserver();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
271 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
272 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
273 else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
274 warn('MutationObserver not supported in this browser!');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
275 initInterval();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
276 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
277 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
278
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
279
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
280 // document.documentElement.offsetHeight is not reliable, so
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
281 // we have to jump through hoops to get a better value.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
282 function getBodyOffsetHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
283 function getComputedBodyStyle(prop) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
284 function convertUnitsToPxForIE8(value) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
285 var PIXEL = /^\d+(px)?$/i;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
286
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
287 if (PIXEL.test(value)) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
288 return parseInt(value,base);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
289 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
290
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
291 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
292 style = el.style.left,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
293 runtimeStyle = el.runtimeStyle.left;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
294
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
295 el.runtimeStyle.left = el.currentStyle.left;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
296 el.style.left = value || 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
297 value = el.style.pixelLeft;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
298 el.style.left = style;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
299 el.runtimeStyle.left = runtimeStyle;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
300
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
301 return value;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
302 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
303
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
304 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
305 el = document.body,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
306 retVal = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
307
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
308 if (('defaultView' in document) && ('getComputedStyle' in document.defaultView)) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
309 retVal = document.defaultView.getComputedStyle(el, null);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
310 retVal = (null !== retVal) ? retVal[prop] : 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
311 } else {//IE8
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
312 retVal = convertUnitsToPxForIE8(el.currentStyle[prop]);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
313 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
314
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
315 return parseInt(retVal,base);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
316 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
317
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
318 return document.body.offsetHeight +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
319 getComputedBodyStyle('marginTop') +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
320 getComputedBodyStyle('marginBottom');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
321 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
322
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
323 function getBodyScrollHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
324 return document.body.scrollHeight;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
325 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
326
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
327 function getDEOffsetHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
328 return document.documentElement.offsetHeight;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
329 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
330
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
331 function getDEScrollHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
332 return document.documentElement.scrollHeight;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
333 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
334
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
335 //From https://github.com/guardian/iframe-messenger
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
336 function getLowestElementHeight() {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
337 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
338 allElements = document.querySelectorAll('body *'),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
339 allElementsLength = allElements.length,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
340 maxBottomVal = 0,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
341 timer = new Date().getTime();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
342
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
343 for (var i = 0; i < allElementsLength; i++) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
344 if (allElements[i].getBoundingClientRect().bottom > maxBottomVal) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
345 maxBottomVal = allElements[i].getBoundingClientRect().bottom;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
346 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
347 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
348
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
349 timer = new Date().getTime() - timer;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
350
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
351 log('Parsed '+allElementsLength+' HTML elements');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
352 log('LowestElement bottom position calculated in ' + timer + 'ms');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
353
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
354 return maxBottomVal;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
355 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
356
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
357 function getAllHeights(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
358 return [
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
359 getBodyOffsetHeight(),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
360 getBodyScrollHeight(),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
361 getDEOffsetHeight(),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
362 getDEScrollHeight()
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
363 ];
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 function getMaxHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
367 return Math.max.apply(null,getAllHeights());
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
368 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
369
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
370 function getMinHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
371 return Math.min.apply(null,getAllHeights());
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
372 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
373
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
374 function getBestHeight(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
375 return Math.max(getBodyOffsetHeight(),getLowestElementHeight());
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
376 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
377
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
378 var getHeight = {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
379 offset : getBodyOffsetHeight, //Backward compatability
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
380 bodyOffset : getBodyOffsetHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
381 bodyScroll : getBodyScrollHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
382 documentElementOffset : getDEOffsetHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
383 scroll : getDEScrollHeight, //Backward compatability
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
384 documentElementScroll : getDEScrollHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
385 max : getMaxHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
386 min : getMinHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
387 grow : getMaxHeight,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
388 lowestElement : getBestHeight,
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 function getWidth(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
392 return Math.max(
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
393 document.documentElement.scrollWidth,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
394 document.body.scrollWidth
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
395 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
396 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
397
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
398 function sendSize(triggerEvent, triggerEventDesc, customHeight, customWidth){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
399
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
400 var currentHeight,currentWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
401
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
402 function recordTrigger(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
403 if (!(triggerEvent in {'reset':1,'resetPage':1,'init':1})){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
404 log( 'Trigger event: ' + triggerEventDesc );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
405 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
406 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
407
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
408 function resizeIFrame(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
409 height = currentHeight;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
410 width = currentWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
411
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
412 sendMsg(height,width,triggerEvent);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
413 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
414
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
415 function isDoubleFiredEvent(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
416 return triggerLocked && (triggerEvent in doubleEventList);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
417 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
418
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
419 function isSizeChangeDetected(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
420 function checkTolarance(a,b){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
421 var retVal = Math.abs(a-b) <= tolerance;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
422 return !retVal;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
423 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
424
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
425 currentHeight = (undefined !== customHeight) ? customHeight : getHeight[heightCalcMode]();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
426 currentWidth = (undefined !== customWidth ) ? customWidth : getWidth();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
427
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
428 return checkTolarance(height,currentHeight) ||
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
429 (calculateWidth && checkTolarance(width,currentWidth));
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
430
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
431 //return (height !== currentHeight) ||
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
432 // (calculateWidth && width !== currentWidth);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
433 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
434
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
435 function isForceResizableEvent(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
436 return !(triggerEvent in {'init':1,'interval':1,'size':1});
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
437 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
438
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
439 function isForceResizableHeightCalcMode(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
440 return (heightCalcMode in resetRequiredMethods);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
441 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
442
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
443 function logIgnored(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
444 log('No change in size detected');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
445 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
446
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
447 function checkDownSizing(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
448 if (isForceResizableEvent() && isForceResizableHeightCalcMode()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
449 resetIFrame(triggerEventDesc);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
450 } else if (!(triggerEvent in {'interval':1})){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
451 recordTrigger();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
452 logIgnored();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
453 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
454 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
455
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
456 if (!isDoubleFiredEvent()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
457 if (isSizeChangeDetected()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
458 recordTrigger();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
459 lockTrigger();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
460 resizeIFrame();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
461 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
462 checkDownSizing();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
463 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
464 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
465 log('Trigger event cancelled: '+triggerEvent);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
466 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
467 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
468
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
469 function lockTrigger(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
470 if (!triggerLocked){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
471 triggerLocked = true;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
472 log('Trigger event lock on');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
473 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
474 clearTimeout(triggerLockedTimer);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
475 triggerLockedTimer = setTimeout(function(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
476 triggerLocked = false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
477 log('Trigger event lock off');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
478 log('--');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
479 },eventCancelTimer);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
480 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
481
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
482 function triggerReset(triggerEvent){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
483 height = getHeight[heightCalcMode]();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
484 width = getWidth();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
485
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
486 sendMsg(height,width,triggerEvent);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
487 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
488
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
489 function resetIFrame(triggerEventDesc){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
490 var hcm = heightCalcMode;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
491 heightCalcMode = heightCalcModeDefault;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
492
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
493 log('Reset trigger event: ' + triggerEventDesc);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
494 lockTrigger();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
495 triggerReset('reset');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
496
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
497 heightCalcMode = hcm;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
498 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
499
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
500 function sendMsg(height,width,triggerEvent,msg,targetOrigin){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
501 function setTargetOrigin(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
502 if (undefined === targetOrigin){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
503 targetOrigin = targetOriginDefault;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
504 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
505 log('Message targetOrigin: '+targetOrigin);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
506 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
507 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
508
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
509 function sendToParent(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
510 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
511 size = height + ':' + width,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
512 message = myID + ':' + size + ':' + triggerEvent + (undefined !== msg ? ':' + msg : '');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
513
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
514 log('Sending message to host page (' + message + ')');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
515 target.postMessage( msgID + message, targetOrigin);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
516 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
517
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
518 setTargetOrigin();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
519 sendToParent();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
520 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
521
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
522 function receiver(event) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
523 function isMessageForUs(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
524 return msgID === (''+event.data).substr(0,msgIdLen); //''+ Protects against non-string messages
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
525 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
526
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
527 function initFromParent(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
528 initMsg = event.data;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
529 init();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
530 firstRun = false;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
531 setTimeout(function(){ initLock = false;},eventCancelTimer);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
532 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
533
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
534 function resetFromParent(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
535 if (!initLock){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
536 log('Page size reset by host page');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
537 triggerReset('resetPage');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
538 } else {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
539 log('Page reset ignored by init');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
540 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
541 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
542
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
543 function getMessageType(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
544 return event.data.split(']')[1];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
545 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
546
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
547 function isMiddleTier(){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
548 return ('iFrameResize' in window);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
549 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
550
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
551 if (isMessageForUs()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
552 if (firstRun){ //Check msg ID
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
553 initFromParent();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
554 } else if ('reset' === getMessageType()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
555 resetFromParent();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
556 } else if (event.data !== initMsg && !isMiddleTier()){
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
557 warn('Unexpected message ('+event.data+')');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
558 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
559 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
560 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
561
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
562 addEventListener(window, 'message', receiver);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
563
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
564 })();