UNPKG

7.4 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ImageCell = undefined;
7
8var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
9
10var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11
12var _react = require('react');
13
14var _react2 = _interopRequireDefault(_react);
15
16var _reactPortal = require('react-portal');
17
18var _reactPortal2 = _interopRequireDefault(_reactPortal);
19
20var _constants = require('./constants');
21
22var Constants = _interopRequireWildcard(_constants);
23
24var _brokenImagePlaceholder = require('../assets/broken-image-placeholder.png');
25
26var _brokenImagePlaceholder2 = _interopRequireDefault(_brokenImagePlaceholder);
27
28var _propTypes = require('prop-types');
29
30var _propTypes2 = _interopRequireDefault(_propTypes);
31
32function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
36function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
37
38function _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; }
39
40function _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; }
41
42var ImageCell = exports.ImageCell = function (_React$Component) {
43 _inherits(ImageCell, _React$Component);
44
45 function ImageCell(props) {
46 _classCallCheck(this, ImageCell);
47
48 var _this = _possibleConstructorReturn(this, (ImageCell.__proto__ || Object.getPrototypeOf(ImageCell)).call(this, props));
49
50 _this.handleMouseEnter = _this.handleMouseEnter.bind(_this);
51 _this.handleMouseLeave = _this.handleMouseLeave.bind(_this);
52
53 _this.state = {
54 showPopover: false,
55 imageErrored: false
56 };
57 return _this;
58 }
59
60 _createClass(ImageCell, [{
61 key: 'handleMouseEnter',
62 value: function handleMouseEnter(e) {
63 var clientRect = e.target.getBoundingClientRect();
64
65 this.setState({
66 showPopover: true,
67 popoverTop: (clientRect.top + clientRect.bottom) / 2,
68 popoverLeft: clientRect.left + this.props.width - 20
69 });
70 }
71 }, {
72 key: 'handleMouseLeave',
73 value: function handleMouseLeave() {
74 this.setState({
75 showPopover: false
76 });
77 }
78 }, {
79 key: 'handleImageLoaded',
80 value: function handleImageLoaded() {
81 // Treat 5x5 pixels as less as visually meaningless
82 // load about:blank to show the alt text instead
83 if (this.refs.img.width * this.refs.img.height <= 25) {
84 this.refs.img.src = 'about:blank';
85 }
86 }
87 }, {
88 key: 'handleImageError',
89 value: function handleImageError() {
90 this.setState({ imageErrored: true });
91 }
92 }, {
93 key: 'render',
94 value: function render() {
95 var cellData = this.props.cellData;
96 var imageUrl = void 0,
97 href = void 0,
98 alt = void 0;
99
100 if (cellData.main && _typeof(cellData.main) === 'object') {
101 imageUrl = cellData.main.src;
102 href = cellData.main.href || imageUrl;
103 alt = cellData.alt || cellData.main.alt;
104 } else {
105 imageUrl = cellData.main;
106 href = cellData.main;
107 alt = cellData.alt || cellData.main;
108 }
109
110 if (!imageUrl) {
111 return _react2.default.createElement('span', null);
112 }
113
114 if (this.props.mixedContentImage) {
115 imageUrl = this.props.mixedContentImage(imageUrl, imageUrl);
116 }
117 return _react2.default.createElement(
118 'a',
119 {
120 href: this.props.disabled ? 'javascript:void(0);' : href,
121 title: alt,
122 target: '_blank',
123 style: {
124 display: 'inline-block'
125 },
126 onMouseEnter: this.handleMouseEnter,
127 onMouseLeave: this.handleMouseLeave
128 },
129 _react2.default.createElement('img', {
130 ref: 'img',
131 className: 'example-image',
132 src: this.state.imageErrored ? _brokenImagePlaceholder2.default : imageUrl,
133 style: {
134 maxHeight: Constants.ROW_HEIGHT - 20,
135 maxWidth: this.props.width - 20,
136 minHeight: 5,
137 minWidth: 5,
138 marginTop: '-4px'
139 },
140 title: alt,
141 alt: alt ? alt : 'Image',
142 onLoad: this.handleImageLoaded.bind(this),
143 onError: this.handleImageError.bind(this)
144 }),
145 _react2.default.createElement(
146 _reactPortal2.default,
147 { isOpened: !!(imageUrl && this.state.showPopover) },
148 _react2.default.createElement(
149 'div',
150 { className: 'popover fade right in', style: {
151 display: 'block',
152 position: 'absolute',
153 top: this.state.popoverTop,
154 left: this.state.popoverLeft,
155 transform: 'translateY(-50%)',
156 backgroundColor: '#fff'
157 } },
158 _react2.default.createElement('div', { className: 'arrow', style: { top: '50%' } }),
159 _react2.default.createElement(
160 'div',
161 { className: 'popover-content' },
162 _react2.default.createElement('img', {
163 style: { width: '100%' },
164 src: this.state.imageErrored ? _brokenImagePlaceholder2.default : imageUrl,
165 title: this.state.imageErrored ? 'Image not found' : alt,
166 alt: this.state.imageErrored ? 'Image not found' : alt
167 }),
168 _react2.default.createElement(
169 'span',
170 null,
171 alt
172 )
173 )
174 )
175 )
176 );
177 }
178 }]);
179
180 return ImageCell;
181}(_react2.default.Component);
182
183ImageCell.propTypes = {
184 cellData: _propTypes2.default.object.isRequired,
185 width: _propTypes2.default.number.isRequired,
186 mixedContentImage: _propTypes2.default.func,
187 disabled: _propTypes2.default.bool
188};
\No newline at end of file