-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
59 lines (47 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
var React = require('react');
var PropTypes = require('react/lib/ReactPropTypes');
// https://developer.zendesk.com/blog/rendering-to-iframes-in-react
function adjustHeightWhenComplete(myFrame, myDoc) {
if(myDoc.readyState === 'complete') {
var content_height = myFrame.contentWindow.document.documentElement.scrollHeight;
myFrame.style.height = content_height + 'px';
} else {
// This will be continiously called until the iFrame is ready
setTimeout(function(){adjustHeightWhenComplete(myFrame, myDoc)});
}
};
// https://gist.github.com/jeremiahlee/1748966
var GistEmbed = React.createClass({
displayName: 'GistEmbed',
propTypes: {
gistId: PropTypes.number.isRequired
},
componentDidMount: function() {
// Create an iframe, append it to this document where specified
var gistFrame = document.createElement("iframe");
gistFrame.setAttribute("width", "100%");
gistFrame.id = "gistFrame" + this.props.gistId;
var zone = document.getElementById("gistZone" + this.props.gistId);
zone.innerHTML = "";
zone.appendChild(gistFrame);
// Create the iframe's document
var url = "https://gist.github.com/" + this.props.gistId + ".js";
var gistFrameHTML = '<html><body><script type="text/javascript" src=' + url + '></script></body></html>';
// Set iframe's document with a trigger for this document to adjust the height
var gistFrameDoc = gistFrame.document;
if (gistFrame.contentDocument) {
gistFrameDoc = gistFrame.contentDocument;
} else if (gistFrame.contentWindow) {
gistFrameDoc = gistFrame.contentWindow.document;
}
gistFrameDoc.open();
gistFrameDoc.writeln(gistFrameHTML);
gistFrameDoc.close();
adjustHeightWhenComplete(gistFrame, gistFrameDoc);
},
render: function() {
return React.DOM.div({id: 'gistZone' + this.props.gistId});
}
});
module.exports = GistEmbed;