通用评论

ImagePreviewer.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _spin = require('antd/es/spin');
  6. var _spin2 = _interopRequireDefault(_spin);
  7. var _icon = require('antd/es/icon');
  8. var _icon2 = _interopRequireDefault(_icon);
  9. var _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; }; }();
  10. require('antd/es/spin/style/css');
  11. require('antd/es/icon/style/css');
  12. var _react = require('react');
  13. var _react2 = _interopRequireDefault(_react);
  14. var _classnames = require('classnames');
  15. var _classnames2 = _interopRequireDefault(_classnames);
  16. require('./ImagePreviewer.css');
  17. var _helper = require('../../helper');
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  20. 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; }
  21. 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; }
  22. var ImagePreviewer = function (_React$Component) {
  23. _inherits(ImagePreviewer, _React$Component);
  24. function ImagePreviewer(props) {
  25. _classCallCheck(this, ImagePreviewer);
  26. var _this = _possibleConstructorReturn(this, (ImagePreviewer.__proto__ || Object.getPrototypeOf(ImagePreviewer)).call(this, props));
  27. _this.setIndex = _this.setIndex.bind(_this);
  28. _this.onPrev = _this.onPrev.bind(_this);
  29. _this.onNext = _this.onNext.bind(_this);
  30. _this.onOrigin = _this.onOrigin.bind(_this);
  31. _this.rotateLeft = _this.rotateLeft.bind(_this);
  32. _this.rotateRight = _this.rotateRight.bind(_this);
  33. _this.onImgLoad = _this.onImgLoad.bind(_this);
  34. _this.onImageError = _this.onImageError.bind(_this);
  35. _this.state = {
  36. cIndex: props.index,
  37. direction: 0, // 0- 0deg 1- 90deg 2- 180deg 3- 270deg
  38. loading: false
  39. };
  40. return _this;
  41. }
  42. _createClass(ImagePreviewer, [{
  43. key: 'componentWillReceiveProps',
  44. value: function componentWillReceiveProps(nextProps) {
  45. if (nextProps.index !== this.props.index) {
  46. this.setIndex(nextProps.index);
  47. }
  48. }
  49. }, {
  50. key: 'onPrev',
  51. value: function onPrev() {
  52. var prev = this.state.cIndex === 0 ? this.props.list.length - 1 : this.state.cIndex - 1;
  53. this.setIndex(prev);
  54. }
  55. }, {
  56. key: 'onNext',
  57. value: function onNext() {
  58. var next = this.state.cIndex === this.props.list.length - 1 ? 0 : this.state.cIndex + 1;
  59. this.setIndex(next);
  60. }
  61. }, {
  62. key: 'onOrigin',
  63. value: function onOrigin() {
  64. window.open(this.props.list[this.state.cIndex]);
  65. }
  66. }, {
  67. key: 'onImgLoad',
  68. value: function onImgLoad() {
  69. this.setState({
  70. loading: false
  71. });
  72. }
  73. }, {
  74. key: 'onImageError',
  75. value: function onImageError() {
  76. this.setState({
  77. loading: false
  78. });
  79. }
  80. }, {
  81. key: 'setIndex',
  82. value: function setIndex(index) {
  83. this.setState({
  84. cIndex: index,
  85. direction: 0,
  86. loading: true
  87. });
  88. }
  89. }, {
  90. key: 'rotateLeft',
  91. value: function rotateLeft() {
  92. var direction = this.state.direction - 1;
  93. if (direction === -1) {
  94. direction = 3;
  95. }
  96. this.setState({
  97. direction: direction
  98. });
  99. }
  100. }, {
  101. key: 'rotateRight',
  102. value: function rotateRight() {
  103. var direction = this.state.direction + 1;
  104. if (direction === 4) {
  105. direction = 0;
  106. }
  107. this.setState({
  108. direction: direction
  109. });
  110. }
  111. // calcHeight(node) {
  112. // const { naturalHeight, naturalWidth } = node;
  113. // }
  114. }, {
  115. key: 'render',
  116. value: function render() {
  117. var _this2 = this;
  118. var _props = this.props,
  119. list = _props.list,
  120. onFold = _props.onFold;
  121. var cIndex = this.state.cIndex;
  122. return _react2.default.createElement(
  123. 'div',
  124. { className: 'container' },
  125. _react2.default.createElement(
  126. 'div',
  127. { className: 'toolbar' },
  128. _react2.default.createElement(
  129. 'span',
  130. { className: 'button', onClick: onFold },
  131. _react2.default.createElement(_icon2.default, { type: 'to-top' }),
  132. '\u6536\u8D77'
  133. ),
  134. _react2.default.createElement(
  135. 'span',
  136. { className: 'button', onClick: this.onOrigin },
  137. _react2.default.createElement(_icon2.default, { type: 'search' }),
  138. ' \u67E5\u770B\u539F\u56FE'
  139. )
  140. ),
  141. _react2.default.createElement(
  142. 'div',
  143. { className: 'pictureWrapper' },
  144. _react2.default.createElement(
  145. _spin2.default,
  146. { spinning: this.state.loading },
  147. _react2.default.createElement('img', {
  148. // ref={node => {
  149. // if (node) {
  150. // this.calcHeight(node);
  151. // }
  152. // }}
  153. className: 'picture',
  154. src: (0, _helper.addImageProcess)(list[cIndex], { large: true }),
  155. alt: list[cIndex],
  156. style: {
  157. transform: 'rotate(' + this.state.direction * 90 + 'deg)',
  158. opacity: this.state.loading ? 0 : 1,
  159. transition: '0.3s opacity'
  160. },
  161. onLoad: this.onImgLoad,
  162. onError: this.onImageError
  163. })
  164. ),
  165. _react2.default.createElement(
  166. 'div',
  167. { className: 'tools' },
  168. list.length > 1 && _react2.default.createElement('div', {
  169. onClick: this.onPrev,
  170. className: (0, _classnames2.default)("item", "left")
  171. }),
  172. _react2.default.createElement('div', { onClick: onFold, className: (0, _classnames2.default)("item", "shrink") }),
  173. list.length > 1 && _react2.default.createElement('div', {
  174. onClick: this.onNext,
  175. className: (0, _classnames2.default)("item", "right")
  176. })
  177. )
  178. ),
  179. list.length > 1 && _react2.default.createElement(
  180. 'div',
  181. { className: 'list' },
  182. list.map(function (item, index) {
  183. return _react2.default.createElement(
  184. 'div',
  185. {
  186. className: (0, _classnames2.default)({
  187. "wrapper": true,
  188. "active": index === cIndex
  189. }),
  190. key: item
  191. },
  192. _react2.default.createElement('div', {
  193. className: (0, _classnames2.default)({
  194. "thumbnail": true
  195. }),
  196. style: {
  197. backgroundImage: 'url(' + (0, _helper.addImageProcess)(item, {
  198. small: true
  199. }) + ')'
  200. },
  201. onClick: function onClick() {
  202. _this2.setIndex(index);
  203. }
  204. })
  205. );
  206. })
  207. )
  208. );
  209. }
  210. }]);
  211. return ImagePreviewer;
  212. }(_react2.default.Component);
  213. exports.default = ImagePreviewer;
  214. //# sourceMappingURL=ImagePreviewer.js.map