UNPKG

ldx-widgets

Version:

widgets

115 lines (88 loc) 3.32 kB
React = require 'react' {iframe} = React.DOM ### @type - OPTIONAL - String file type contained within the iframe @src - REQUIRED - String the source of the document to be loaded in the iframe @id - OPTIONAL - String the id attribute used on the iframe @name - OPTIONAL - String the name attribute used on the string. Also used when accessing frames[] @className - OPTIONAL - String the CSS class set to the iframe @height - OPTIONAL - String | Number a fixed height for the container @width - OPTIONAL - String | Number a fixed width for the container @queryParams - OPTIONAL - Object query params passed with iframe request @resizeContentAfterLoad - 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 @requestClass - 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} = @props iframe { className: className src: 'about:blank' width: width height: height id: id name: name ref: 'embeddedFrame' } componentWillReceiveProps: (nextProps) -> if @props.src isnt nextProps.src @getDocument(nextProps) 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 @refs.embeddedFrame.contentDocument? @refs.embeddedFrame.contentDocument.open() @refs.embeddedFrame.contentDocument.writeln(res) @refs.embeddedFrame.contentDocument.close() @resizeContentAfterLoad() componentDidMount: -> @getDocument(@props) 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 = @refs.embeddedFrame.contentDocument.getElementsByTagName('html')[0].scrollHeight @props.resizeContentAfterLoad?(docHeight + 40) shouldComponentUpdate: (nextProps) -> @props.src isnt nextProps.src or @props.height isnt nextProps.height or @props.width isnt nextProps.width module.exports = IframeEmbedder