ldx-widgets
Version:
widgets
115 lines (88 loc) • 3.32 kB
text/coffeescript
React = require 'react'
{iframe} = React.DOM
###
- OPTIONAL - String
file type contained within the iframe
- REQUIRED - String
the source of the document to be loaded in the iframe
- OPTIONAL - String
the id attribute used on the iframe
- OPTIONAL - String
the name attribute used on the string. Also used when accessing frames[]
- OPTIONAL - String
the CSS class set to the iframe
- OPTIONAL - String | Number
a fixed height for the container
- OPTIONAL - String | Number
a fixed width for the container
- OPTIONAL - Object
query params passed with iframe request
- OPTIONAL - Function
fired when the iframe finishes loading. This can be used to set a fixed height on a parent container in order to match the height of the embedded content. This is useful because iframes will not take the height of their content by default
- REQUIRED - Function
required function that contains the server request class used for making service calls
###
IframeEmbedder = React.createClass
displayName: 'IframeEmbedder'
propTypes:
type: React.PropTypes.string
src: React.PropTypes.string.isRequired
id: React.PropTypes.string
name: React.PropTypes.string
className: React.PropTypes.string
height: React.PropTypes.oneOfType [
React.PropTypes.string
React.PropTypes.number
]
width: React.PropTypes.oneOfType [
React.PropTypes.string
React.PropTypes.number
]
resizeContentAfterLoad: React.PropTypes.func
queryParams: React.PropTypes.object
requestClass: React.PropTypes.func.isRequired
getDefaultProps: ->
height: '100%'
width: '100%'
className: 'iframe-embedded'
type: ''
id: ''
name: ''
render: ->
{width, height, className, id, name} =
iframe {
className: className
src: 'about:blank'
width: width
height: height
id: id
name: name
ref: 'embeddedFrame'
}
componentWillReceiveProps: (nextProps) ->
if .src isnt nextProps.src
getDocument: (props) ->
{src, queryParams, type, requestClass} = props
new requestClass
url: src
data: queryParams
.done (res) =>
if src and res?
# Wrap plain text
if type?.search('text/') > -1 then res = "<pre>#{res}</pre>"
if .embeddedFrame.contentDocument?
.embeddedFrame.contentDocument.open()
.embeddedFrame.contentDocument.writeln(res)
.embeddedFrame.contentDocument.close()
componentDidMount: ->
resizeContentAfterLoad: ->
# What this does is it will size the iframe container to be the height of its content, removing the need for its own scrollbar
# Added an extra 40px to accommodate IE scrollbar
docHeight = .embeddedFrame.contentDocument.getElementsByTagName('html')[0].scrollHeight
.resizeContentAfterLoad?(docHeight + 40)
shouldComponentUpdate: (nextProps) ->
.src isnt nextProps.src or .height isnt nextProps.height or .width isnt nextProps.width
module.exports = IframeEmbedder