运维小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

image-viewer.js 5.9KB

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