储能智慧云小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

image.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { SuperComponent, wxComponent } from '../common/src/index';
  8. import ImageProps from './props';
  9. import config from '../common/config';
  10. import { addUnit, getRect } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-image`;
  13. let Image = class Image extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`, `${prefix}-class-load`];
  17. this.options = {
  18. multipleSlots: true,
  19. };
  20. this.properties = ImageProps;
  21. this.data = {
  22. prefix,
  23. isLoading: true,
  24. isFailed: false,
  25. innerStyle: '',
  26. classPrefix: name,
  27. };
  28. this.preSrc = '';
  29. this.observers = {
  30. src() {
  31. if (this.preSrc === this.properties.src)
  32. return;
  33. this.update();
  34. },
  35. 'width, height'(width, height) {
  36. this.calcSize(width, height);
  37. },
  38. };
  39. this.methods = {
  40. onLoaded(e) {
  41. const sdkVersion = wx.getSystemInfoSync().SDKVersion;
  42. const versionArray = sdkVersion.split('.').map((v) => parseInt(v, 10));
  43. const { mode, tId } = this.properties;
  44. const isInCompatible = versionArray[0] < 2 ||
  45. (versionArray[0] === 2 && versionArray[1] < 10) ||
  46. (versionArray[0] === 2 && versionArray[1] === 10 && versionArray[2] < 3);
  47. if (mode === 'heightFix' && isInCompatible) {
  48. const { height: picHeight, width: picWidth } = e.detail;
  49. getRect(this, `#${tId !== null && tId !== void 0 ? tId : 'image'}`).then((rect) => {
  50. const { height } = rect;
  51. const resultWidth = ((height / picHeight) * picWidth).toFixed(2);
  52. this.setData({ innerStyle: `height: ${addUnit(height)}; width: ${resultWidth}px;` });
  53. });
  54. }
  55. this.setData({
  56. isLoading: false,
  57. isFailed: false,
  58. });
  59. this.triggerEvent('load', e.detail);
  60. },
  61. onLoadError(e) {
  62. this.setData({
  63. isLoading: false,
  64. isFailed: true,
  65. });
  66. this.triggerEvent('error', e.detail);
  67. },
  68. calcSize(width, height) {
  69. let innerStyle = '';
  70. if (width) {
  71. innerStyle += `width: ${addUnit(width)};`;
  72. }
  73. if (height) {
  74. innerStyle += `height: ${addUnit(height)};`;
  75. }
  76. this.setData({
  77. innerStyle,
  78. });
  79. },
  80. update() {
  81. const { src } = this.properties;
  82. this.preSrc = src;
  83. if (!src) {
  84. this.onLoadError({ errMsg: '图片链接为空' });
  85. }
  86. else {
  87. this.setData({
  88. isLoading: true,
  89. isFailed: false,
  90. });
  91. }
  92. },
  93. };
  94. }
  95. };
  96. Image = __decorate([
  97. wxComponent()
  98. ], Image);
  99. export default Image;