image-viewer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, systemInfo } 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. swiperStyle: {},
  25. imagesStyle: {},
  26. maskTop: 0,
  27. };
  28. this.options = {
  29. multipleSlots: true,
  30. };
  31. this.controlledProps = [
  32. {
  33. key: 'visible',
  34. event: 'close',
  35. },
  36. ];
  37. this.observers = {
  38. 'visible,initialIndex,images'(visible, initialIndex, images) {
  39. if (visible && (images === null || images === void 0 ? void 0 : images.length)) {
  40. this.setData({
  41. currentSwiperIndex: initialIndex >= images.length ? images.length - 1 : initialIndex,
  42. });
  43. }
  44. },
  45. closeBtn(v) {
  46. this.setData({
  47. _closeBtn: calcIcon(v, 'close'),
  48. });
  49. },
  50. deleteBtn(v) {
  51. this.setData({
  52. _deleteBtn: calcIcon(v, 'delete'),
  53. });
  54. },
  55. };
  56. this.methods = {
  57. calcMaskTop() {
  58. if (this.data.usingCustomNavbar) {
  59. const rect = (wx === null || wx === void 0 ? void 0 : wx.getMenuButtonBoundingClientRect()) || null;
  60. const { statusBarHeight } = systemInfo;
  61. if (rect && statusBarHeight) {
  62. this.setData({
  63. maskTop: rect.top - statusBarHeight + rect.bottom,
  64. });
  65. }
  66. }
  67. },
  68. saveScreenSize() {
  69. const { windowHeight, windowWidth } = systemInfo;
  70. this.setData({
  71. windowHeight,
  72. windowWidth,
  73. });
  74. },
  75. calcImageDisplayStyle(imageWidth, imageHeight) {
  76. const { windowWidth, windowHeight } = this.data;
  77. const ratio = imageWidth / imageHeight;
  78. if (imageWidth < windowWidth && imageHeight < windowHeight) {
  79. return {
  80. styleObj: {
  81. width: `${imageWidth * 2}rpx`,
  82. height: `${imageHeight * 2}rpx`,
  83. left: '50%',
  84. transform: 'translate(-50%, -50%)',
  85. },
  86. };
  87. }
  88. if (ratio >= 1) {
  89. return {
  90. styleObj: {
  91. width: '100vw',
  92. height: `${(windowWidth / ratio) * 2}rpx`,
  93. },
  94. };
  95. }
  96. const scaledHeight = ratio * windowHeight * 2;
  97. if (scaledHeight < windowWidth) {
  98. return {
  99. styleObj: {
  100. width: `${scaledHeight}rpx`,
  101. height: '100vh',
  102. left: '50%',
  103. transform: 'translate(-50%, -50%)',
  104. },
  105. };
  106. }
  107. return {
  108. styleObj: {
  109. width: '100vw',
  110. height: `${(windowWidth / imageWidth) * imageHeight * 2}rpx`,
  111. },
  112. };
  113. },
  114. onImageLoadSuccess(e) {
  115. const { detail: { width, height }, currentTarget: { dataset: { index }, }, } = e;
  116. const { mode, styleObj } = this.calcImageDisplayStyle(width, height);
  117. const originImagesStyle = this.data.imagesStyle;
  118. const originSwiperStyle = this.data.swiperStyle;
  119. this.setData({
  120. swiperStyle: Object.assign(Object.assign({}, originSwiperStyle), { [index]: {
  121. style: `height: ${styleObj.height}`,
  122. } }),
  123. imagesStyle: Object.assign(Object.assign({}, originImagesStyle), { [index]: {
  124. mode,
  125. style: styles(Object.assign({}, styleObj)),
  126. } }),
  127. });
  128. },
  129. onSwiperChange(e) {
  130. const { detail: { current }, } = e;
  131. this.setData({
  132. currentSwiperIndex: current,
  133. });
  134. this._trigger('change', { index: current });
  135. },
  136. onClose(e) {
  137. const { source } = e.currentTarget.dataset;
  138. this._trigger('close', { visible: false, trigger: source || 'button', index: this.data.currentSwiperIndex });
  139. },
  140. onDelete() {
  141. this._trigger('delete', { index: this.data.currentSwiperIndex });
  142. },
  143. };
  144. }
  145. ready() {
  146. this.saveScreenSize();
  147. this.calcMaskTop();
  148. }
  149. };
  150. ImageViewer = __decorate([
  151. wxComponent()
  152. ], ImageViewer);
  153. export default ImageViewer;