index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { addUnit, isDef } from '../common/utils';
  2. import { VantComponent } from '../common/component';
  3. import { button } from '../mixins/button';
  4. import { openType } from '../mixins/open-type';
  5. const FIT_MODE_MAP = {
  6. none: 'center',
  7. fill: 'scaleToFill',
  8. cover: 'aspectFill',
  9. contain: 'aspectFit'
  10. };
  11. VantComponent({
  12. mixins: [button, openType],
  13. classes: ['custom-class', 'loading-class', 'error-class', 'image-class'],
  14. props: {
  15. src: String,
  16. round: Boolean,
  17. width: {
  18. type: null,
  19. observer: 'setStyle'
  20. },
  21. height: {
  22. type: null,
  23. observer: 'setStyle'
  24. },
  25. radius: null,
  26. lazyLoad: Boolean,
  27. useErrorSlot: Boolean,
  28. useLoadingSlot: Boolean,
  29. showMenuByLongpress: Boolean,
  30. fit: {
  31. type: String,
  32. value: 'fill',
  33. observer: 'setMode'
  34. },
  35. showError: {
  36. type: Boolean,
  37. value: true
  38. },
  39. showLoading: {
  40. type: Boolean,
  41. value: true
  42. }
  43. },
  44. data: {
  45. error: false,
  46. loading: true
  47. },
  48. watch: {
  49. src() {
  50. this.setData({
  51. error: false,
  52. loading: true
  53. });
  54. }
  55. },
  56. mounted() {
  57. this.setMode();
  58. this.setStyle();
  59. },
  60. methods: {
  61. setMode() {
  62. this.setData({
  63. mode: FIT_MODE_MAP[this.data.fit],
  64. });
  65. },
  66. setStyle() {
  67. const { width, height, radius } = this.data;
  68. let style = '';
  69. if (isDef(width)) {
  70. style += `width: ${addUnit(width)};`;
  71. }
  72. if (isDef(height)) {
  73. style += `height: ${addUnit(height)};`;
  74. }
  75. if (isDef(radius)) {
  76. style += 'overflow: hidden;';
  77. style += `border-radius: ${addUnit(radius)};`;
  78. }
  79. this.setData({ style });
  80. },
  81. onLoad(event) {
  82. this.setData({
  83. loading: false
  84. });
  85. this.$emit('load', event.detail);
  86. },
  87. onError(event) {
  88. this.setData({
  89. loading: false,
  90. error: true
  91. });
  92. this.$emit('error', event.detail);
  93. },
  94. onClick(event) {
  95. this.$emit('click', event.detail);
  96. }
  97. }
  98. });