image-viewer.js 5.0 KB

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