Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageInfo method to get parent position #262

Merged
merged 2 commits into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ Tell the iFrame to resize itself.

Send data to the containing page, `message` can be any data type that can be serialized into JSON. The `targetOrigin` option is used to restrict where the message is sent to, in case your iFrame navigates away to another domain.

### pageInfo(callback)

Ask the containing page for its positioning coordinates. You need to provide a callback which receives an object with the following properties:

* **clientHeight** The height of the viewport in pixels
* **clientWidth** The width of the viewport in pixels
* **offsetLeft** The number of pixels between the left edge of the containing page and the left edge of the iframe
* **offsetTop** The number of pixels between the top edge of the containing page and the top edge of the iframe
* **scrollLeft** The number of pixels between the left edge of the iframe and the left edge of the iframe viewport
* **scrollTop** The number of pixels between the top edge of the iframe and the top edge of the iframe viewport


## Troubleshooting
Expand Down Expand Up @@ -574,4 +584,3 @@ The parentIFrame methods object in the iFrame is now always available and the `e
## License
Copyright © 2013-15 [David J. Bradshaw](https://github.com/davidjbradshaw).
Licensed under the [MIT License](LICENSE).

18 changes: 17 additions & 1 deletion js/iframeResizer.contentWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
widthCalcMode = widthCalcModeDefault,
win = window,
messageCallback = function(){warn('MessageCallback function not defined');},
readyCallback = function(){};
readyCallback = function(){},
pageInfoCallback = function(){};


function addEventListener(el,evt,func){
Expand Down Expand Up @@ -493,6 +494,11 @@
sendMsg(0,0,'message',JSON.stringify(msg),targetOrigin);
},

getPageInfo: function getPageInfoF(callback){
pageInfoCallback = callback
sendMsg(0,0,'pageInfo');
},

setHeightCalculationMethod: function setHeightCalculationMethodF(heightCalculationMethod){
heightCalcMode = heightCalculationMethod;
checkHeightMode();
Expand Down Expand Up @@ -989,6 +995,13 @@
log(' --');
}

function pageInfoFromParent(){
var msgBody = getData();
log('PageInfoFromParent called from parent: ' + msgBody );
pageInfoCallback(JSON.parse(msgBody));
log(' --');
}

function isInitMsg(){
//Test if this message is from a child below us. This is an ugly test, however, updating
//the message format would break backwards compatibity.
Expand All @@ -1009,6 +1022,9 @@
case 'message':
messageFromParent();
break;
case 'pageInfo':
pageInfoFromParent();
break;
default:
if (!isMiddleTier() && !isInitMsg()){
warn('Unexpected message ('+event.data+')');
Expand Down
2 changes: 1 addition & 1 deletion js/iframeResizer.contentWindow.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/iframeResizer.contentWindow.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions js/iframeResizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@
log(iframeId,'--');
}

function sendPageInfoToIframe(){
var bodyPosition = document.body.getBoundingClientRect();
var iFramePosition = messageData.iframe.getBoundingClientRect();
var position = {
clientHeight: Math.max(document.documentElement.clientHeight, window.innerHeight || 0),
clientWidth: Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
offsetLeft: parseInt(iFramePosition.left - bodyPosition.left),
offsetTop: parseInt(iFramePosition.top - bodyPosition.top),
scrollLeft: window.pageXOffset,
scrollTop: window.pageYOffset
};
trigger('Send Page Info','pageInfo:'+JSON.stringify(position), settings[iframeId].iframe,iframeId);
}

function checkIFrameExists(){
var retBool = true;

Expand Down Expand Up @@ -363,6 +377,9 @@
case 'scrollToOffset':
scrollRequestFromChild(true);
break;
case 'pageInfo':
sendPageInfoToIframe()
break;
case 'inPageLink':
findTarget(getMsgBody(9));
break;
Expand Down
2 changes: 1 addition & 1 deletion js/iframeResizer.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/iframeResizer.min.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion src/iframeResizer.contentWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
widthCalcMode = widthCalcModeDefault,
win = window,
messageCallback = function(){warn('MessageCallback function not defined');},
readyCallback = function(){};
readyCallback = function(){},
pageInfoCallback = function(){};


function addEventListener(el,evt,func){
Expand Down Expand Up @@ -493,6 +494,11 @@
sendMsg(0,0,'message',JSON.stringify(msg),targetOrigin);
},

getPageInfo: function getPageInfoF(callback){
pageInfoCallback = callback
sendMsg(0,0,'pageInfo');
},

setHeightCalculationMethod: function setHeightCalculationMethodF(heightCalculationMethod){
heightCalcMode = heightCalculationMethod;
checkHeightMode();
Expand Down Expand Up @@ -989,6 +995,13 @@
log(' --');
}

function pageInfoFromParent(){
var msgBody = getData();
log('PageInfoFromParent called from parent: ' + msgBody );
pageInfoCallback(JSON.parse(msgBody));
log(' --');
}

function isInitMsg(){
//Test if this message is from a child below us. This is an ugly test, however, updating
//the message format would break backwards compatibity.
Expand All @@ -1009,6 +1022,9 @@
case 'message':
messageFromParent();
break;
case 'pageInfo':
pageInfoFromParent();
break;
default:
if (!isMiddleTier() && !isInitMsg()){
warn('Unexpected message ('+event.data+')');
Expand Down
17 changes: 17 additions & 0 deletions src/iframeResizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@
log(iframeId,'--');
}

function sendPageInfoToIframe(){
var bodyPosition = document.body.getBoundingClientRect();
var iFramePosition = messageData.iframe.getBoundingClientRect();
var position = {
clientHeight: Math.max(document.documentElement.clientHeight, window.innerHeight || 0),
clientWidth: Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
offsetLeft: parseInt(iFramePosition.left - bodyPosition.left),
offsetTop: parseInt(iFramePosition.top - bodyPosition.top),
scrollLeft: window.pageXOffset,
scrollTop: window.pageYOffset
};
trigger('Send Page Info','pageInfo:'+JSON.stringify(position), settings[iframeId].iframe,iframeId);
}

function checkIFrameExists(){
var retBool = true;

Expand Down Expand Up @@ -363,6 +377,9 @@
case 'scrollToOffset':
scrollRequestFromChild(true);
break;
case 'pageInfo':
sendPageInfoToIframe()
break;
case 'inPageLink':
findTarget(getMsgBody(9));
break;
Expand Down