image-viewer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, calcIcon } 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. closeBtn(v) {
  42. this.setData({
  43. _closeBtn: calcIcon(v, 'close'),
  44. });
  45. },
  46. deleteBtn(v) {
  47. this.setData({
  48. _deleteBtn: calcIcon(v, 'delete'),
  49. });
  50. },
  51. };
  52. this.methods = {
  53. saveScreenSize() {
  54. const { windowHeight, windowWidth } = wx.getSystemInfoSync();
  55. this.setData({
  56. windowHeight,
  57. windowWidth,
  58. });
  59. },
  60. calcImageDisplayStyle(imageWidth, imageHeight) {
  61. const { windowWidth, windowHeight } = this.data;
  62. const ratio = imageWidth / imageHeight;
  63. if (imageWidth < windowWidth && imageHeight < windowHeight) {
  64. return {
  65. styleObj: {
  66. width: `${imageWidth * 2}rpx`,
  67. height: `${imageHeight * 2}rpx`,
  68. left: '50%',
  69. transform: 'translate(-50%, -50%)',
  70. },
  71. };
  72. }
  73. if (ratio >= 1) {
  74. return {
  75. styleObj: {
  76. width: '100vw',
  77. height: `${(windowWidth / ratio) * 2}rpx`,
  78. },
  79. };
  80. }
  81. return {
  82. styleObj: {
  83. width: `${ratio * windowHeight * 2}rpx`,
  84. height: '100vh',
  85. left: '50%',
  86. transform: 'translate(-50%, -50%)',
  87. },
  88. };
  89. },
  90. onImageLoadSuccess(e) {
  91. const { detail: { width, height }, currentTarget: { dataset: { index }, }, } = e;
  92. const { mode, styleObj } = this.calcImageDisplayStyle(width, height);
  93. const origin = this.data.imagesShape;
  94. this.setData({
  95. imagesShape: Object.assign(Object.assign({}, origin), { [index]: {
  96. mode,
  97. style: styles(Object.assign({}, styleObj)),
  98. } }),
  99. });
  100. },
  101. onSwiperChange(e) {
  102. const { detail: { current }, } = e;
  103. this.setData({
  104. currentSwiperIndex: current,
  105. });
  106. this._trigger('change', { index: current });
  107. },
  108. onClose(e) {
  109. const { source } = e.currentTarget.dataset;
  110. this._trigger('close', { visible: false, trigger: source || 'button', index: this.data.currentSwiperIndex });
  111. },
  112. onDelete() {
  113. this._trigger('delete', { index: this.data.currentSwiperIndex });
  114. },
  115. };
  116. }
  117. ready() {
  118. this.saveScreenSize();
  119. }
  120. };
  121. ImageViewer = __decorate([
  122. wxComponent()
  123. ], ImageViewer);
  124. export default ImageViewer;