UNPKG

@bigfishtv/cockpit

Version:

145 lines (110 loc) 5.09 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _class, _class2, _temp; var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _throttle = require('lodash/throttle'); var _throttle2 = _interopRequireDefault(_throttle); var _axios = require('axios'); var _encodeFormData = require('../../utils/encodeFormData'); var _encodeFormData2 = _interopRequireDefault(_encodeFormData); var _windowVisible = require('../../decorators/windowVisible'); var _windowVisible2 = _interopRequireDefault(_windowVisible); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // try and grab cspNonce from script in current document var cspNonce = document.scripts && document.scripts[0].nonce; var PreviewFrame = (0, _windowVisible2.default)(_class = (_temp = _class2 = function (_Component) { _inherits(PreviewFrame, _Component); function PreviewFrame() { _classCallCheck(this, PreviewFrame); var _this = _possibleConstructorReturn(this, _Component.call(this)); _this.updatePreview = (0, _throttle2.default)(_this.updatePreview, 1000); _this.state = { dirty: true }; return _this; } PreviewFrame.prototype.componentWillUnmount = function componentWillUnmount() { this.updatePreview.cancel(); }; PreviewFrame.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { var dataChanged = nextProps.data !== this.props.data || nextProps.url !== this.props.url || nextProps.date !== this.props.date; if (dataChanged && !this.state.dirty) { this.setState({ dirty: true }); } }; PreviewFrame.prototype.componentDidUpdate = function componentDidUpdate() { if (this.props.windowVisible && this.state.dirty && this.props.enabled && (this.props.url || this.props.data)) { this.updatePreview(); } }; PreviewFrame.prototype.updatePreview = function updatePreview() { var _this2 = this; this.setState({ dirty: false }); this.saveScrollPosition(); if (typeof this.props.data == 'string') { this.updateIframe(this.props.data); } else { this.requestHtml().then(function (res) { return _this2.updateIframe(res.data); }); } }; PreviewFrame.prototype.saveScrollPosition = function saveScrollPosition() { if (this.refs.frame.contentDocument) { this.scrollTop = this.refs.frame.contentDocument.defaultView.scrollY; this.scrollLeft = this.refs.frame.contentDocument.defaultView.scrollX; } else { console.warn("Couldn't save scroll position", this.refs.frame); } }; PreviewFrame.prototype.requestHtml = function requestHtml() { var data = { data: JSON.stringify(this.props.data), preview: 1, date: this.props.date, cspNonce: cspNonce }; return (0, _axios.post)(this.props.url, data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, responseType: 'text', transformRequest: [_encodeFormData2.default] }); }; PreviewFrame.prototype.updateIframe = function updateIframe(html) { html += '<script>window.scrollTo(' + this.scrollLeft + ', ' + this.scrollTop + ');</script>'; this.refs.frame.contentDocument.open(); this.refs.frame.contentDocument.write(html); this.refs.frame.contentDocument.close(); var title = html.match(/<title>(.+?)<\/title>/); if (title) { this.props.onTitleChange(title[1]); } }; PreviewFrame.prototype.render = function render() { return _react2.default.createElement( 'div', { className: 'preview-frame' }, _react2.default.createElement( 'div', { className: 'preview-frame-inner', style: { maxWidth: this.props.deviceWidth, flexBasis: this.props.deviceWidth } }, _react2.default.createElement('iframe', { className: 'preview-frame-iframe', ref: 'frame' }) ) ); }; return PreviewFrame; }(_react.Component), _class2.propTypes = { data: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]), enabled: _propTypes2.default.bool, url: _propTypes2.default.string, date: _propTypes2.default.string }, _temp)) || _class; exports.default = PreviewFrame;