2013-12-31 14:37:43 -05:00
|
|
|
/* global discourseUrl */
|
2014-04-24 12:48:45 -04:00
|
|
|
/* global discourseUserName */
|
2013-12-31 14:37:43 -05:00
|
|
|
/* global discourseEmbedUrl */
|
|
|
|
(function() {
|
|
|
|
var comments = document.getElementById('discourse-comments'),
|
2014-04-24 12:48:45 -04:00
|
|
|
iframe = document.createElement('iframe');
|
|
|
|
if (typeof discourseUserName === 'undefined') {
|
|
|
|
iframe.src =
|
|
|
|
[ discourseUrl,
|
|
|
|
'embed/comments?embed_url=',
|
|
|
|
encodeURIComponent(discourseEmbedUrl)
|
|
|
|
].join('');
|
|
|
|
} else {
|
|
|
|
iframe.src =
|
|
|
|
[ discourseUrl,
|
|
|
|
'embed/comments?embed_url=',
|
|
|
|
encodeURIComponent(discourseEmbedUrl),
|
|
|
|
'&discourse_username=',
|
|
|
|
discourseUserName
|
|
|
|
].join('');
|
|
|
|
}
|
2013-12-31 14:37:43 -05:00
|
|
|
iframe.id = 'discourse-embed-frame';
|
|
|
|
iframe.width = "100%";
|
|
|
|
iframe.frameBorder = "0";
|
|
|
|
iframe.scrolling = "no";
|
|
|
|
comments.appendChild(iframe);
|
|
|
|
|
2014-01-03 14:45:22 -05:00
|
|
|
// Thanks http://amendsoft-javascript.blogspot.ca/2010/04/find-x-and-y-coordinate-of-html-control.html
|
|
|
|
function findPosY(obj)
|
|
|
|
{
|
|
|
|
var top = 0;
|
|
|
|
if(obj.offsetParent)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
top += obj.offsetTop;
|
|
|
|
if(!obj.offsetParent)
|
|
|
|
break;
|
|
|
|
obj = obj.offsetParent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(obj.y)
|
|
|
|
{
|
|
|
|
top += obj.y;
|
|
|
|
}
|
|
|
|
return top;
|
|
|
|
}
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
function postMessageReceived(e) {
|
|
|
|
if (!e) { return; }
|
|
|
|
if (discourseUrl.indexOf(e.origin) === -1) { return; }
|
|
|
|
|
|
|
|
if (e.data) {
|
|
|
|
if (e.data.type === 'discourse-resize' && e.data.height) {
|
|
|
|
iframe.height = e.data.height + "px";
|
|
|
|
}
|
2014-01-03 14:45:22 -05:00
|
|
|
|
|
|
|
if (e.data.type === 'discourse-scroll' && e.data.top) {
|
|
|
|
// find iframe offset
|
|
|
|
var destY = findPosY(iframe) + e.data.top;
|
|
|
|
window.scrollTo(0, destY);
|
|
|
|
}
|
2013-12-31 14:37:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener('message', postMessageReceived, false);
|
|
|
|
|
|
|
|
})();
|