image-viewer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { styles } from '../common/utils';
  8. import { SuperComponent, wxComponent } from '../common/src/index';
  9. import config from '../common/config';
  10. import props from './props';
  11. const { prefix } = config;
  12. const name = `${prefix}-image-viewer`;
  13. let ImageViewer = class ImageViewer extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`];
  17. this.properties = Object.assign({}, props);
  18. this.data = {
  19. prefix,
  20. classPrefix: name,
  21. currentSwiperIndex: 0,
  22. windowHeight: 0,
  23. windowWidth: 0,
  24. imagesShape: {},
  25. };
  26. this.options = {
  27. multipleSlots: true,
  28. };
  29. this.controlledProps = [
  30. {
  31. key: 'visible',
  32. event: 'close',
  33. },
  34. ];
  35. this.observers = {
  36. visible(value) {
  37. this.setData({
  38. currentSwiperIndex: value ? this.properties.initialIndex : 0,
  39. });
  40. },
  41. };
  42. this.methods = {
  43. saveScreenSize() {
  44. const { windowHeight, windowWidth } = wx.getSystemInfoSync();
  45. this.setData({
  46. windowHeight,
  47. windowWidth,
  48. });
  49. },
  50. calcImageDisplayStyle(imageWidth, imageHeight) {
  51. const { windowWidth, windowHeight } = this.data;
  52. const ratio = imageWidth / imageHeight;
  53. if (imageWidth < windowWidth && imageHeight < windowHeight) {
  54. return {
  55. styleObj: {
  56. width: `${imageWidth * 2}rpx`,
  57. height: `${imageHeight * 2}rpx`,
  58. left: '50%',
  59. transform: 'translate(-50%, -50%)',
  60. },
  61. };
  62. }
  63. if (ratio >= 1) {
  64. return {
  65. styleObj: {
  66. width: '100vw',
  67. height: `${(windowWidth / ratio) * 2}rpx`,
  68. },
  69. };
  70. }
  71. return {
  72. styleObj: {
  73. width: `${ratio * windowHeight * 2}rpx`,
  74. height: '100vh',
  75. left: '50%',
  76. transform: 'translate(-50%, -50%)',
  77. },
  78. };
  79. },
  80. onImageLoadSuccess(e) {
  81. const { detail: { width, height }, currentTarget: { dataset: { index }, }, } = e;
  82. const { mode, styleObj } = this.calcImageDisplayStyle(width, height);
  83. const origin = this.data.imagesShape;
  84. this.setData({
  85. imagesShape: Object.assign(Object.assign({}, origin), { [index]: {
  86. mode,
  87. style: styles(Object.assign({}, styleObj)),
  88. } }),
  89. });
  90. },
  91. onSwiperChange(e) {
  92. const { detail: { current }, } = e;
  93. this.setData({
  94. currentSwiperIndex: current,
  95. });
  96. this._trigger('change', { index: current });
  97. },
  98. onClose(e) {
  99. const { target: { dataset: { source }, }, } = e;
  100. this._trigger('close', { visible: false, trigger: source, index: this.data.currentSwiperIndex });
  101. },
  102. onDelete() {
  103. this._trigger('delete', { index: this.data.currentSwiperIndex });
  104. },
  105. };
  106. }
  107. ready() {
  108. this.saveScreenSize();
  109. }
  110. };
  111. ImageViewer = __decorate([
  112. wxComponent()
  113. ], ImageViewer);
  114. export default ImageViewer;