UNPKG

@bigfishtv/cockpit

Version:

105 lines (82 loc) 4.67 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _dec, _class, _class2, _temp; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import plupload from '../../../lib/plupload'; import uploader from '../../decorators/uploader'; import { extractDataUrlFromNative, getImageUrl, getAssetUrl } from '../../utils/fileUtils'; import AssetUploading from '../asset/AssetUploading'; import AssetCell from '../asset/AssetCell'; // eslint-disable-next-line no-unused-vars var QUEUED = plupload.QUEUED, UPLOADING = plupload.UPLOADING, FAILED = plupload.FAILED, DONE = plupload.DONE; // we define this because react-docgen fails when defaultProp directly references an imported component var DefaultAssetCell = function DefaultAssetCell(props) { return React.createElement(AssetCell, props); }; /** * AssetAutoCell is a smart wrapper for AssetCell. * It listens to the global plupload instance to know if its 'asset' prop is currently uploading and reflects that state accordingly. */ var AssetAutoCell = (_dec = uploader(), _dec(_class = (_temp = _class2 = function (_Component) { _inherits(AssetAutoCell, _Component); function AssetAutoCell() { _classCallCheck(this, AssetAutoCell); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } AssetAutoCell.prototype.componentDidMount = function componentDidMount() { this.mounted = true; this.dataUrl = false; this.fetchingDataUrl = false; }; AssetAutoCell.prototype.componentWillUpdate = function componentWillUpdate(nextProps, nextState) { var _this2 = this; var _props = this.props, uploader = _props.uploader, asset = _props.asset; var file = uploader && uploader.getFile(asset.id) || false; if (file && !this.dataUrl && !this.fetchingDataUrl) { this.fetchingDataUrl = true; extractDataUrlFromNative(file.getNative(), function (data) { _this2.dataUrl = data; _this2.fetchingDataUrl = false; if (_this2.mounted) _this2.forceUpdate(); }); } }; AssetAutoCell.prototype.componentWillUnmount = function componentWillUnmount() { this.mounted = false; }; AssetAutoCell.prototype.render = function render() { var _props2 = this.props, AssetCell = _props2.AssetCell, uploader = _props2.uploader, asset = _props2.asset, size = _props2.size, rest = _objectWithoutProperties(_props2, ['AssetCell', 'uploader', 'asset', 'size']); var file = uploader && uploader.getFile(asset.id) || false; if (file && file.status !== DONE) { return React.createElement(AssetUploading, _extends({}, rest, { asset: asset, url: this.dataUrl || asset.url, percent: file.percent })); } else { var url = asset ? asset.kind == 'image' ? getImageUrl(asset, size) : getAssetUrl(asset) : this.dataUrl || 'http://placehold.it/300x200'; return React.createElement(AssetCell, _extends({ asset: asset, size: size }, rest, { url: url })); } }; return AssetAutoCell; }(Component), _class2.propTypes = { /** object containing either a temp id (for uploading file), or all details to display asset */ asset: PropTypes.object.isRequired, /** tank image resize string */ size: PropTypes.string }, _class2.defaultProps = { asset: {}, size: 'cockpit-150', AssetCell: DefaultAssetCell }, _temp)) || _class); export { AssetAutoCell as default };