toast.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 config from '../common/config';
  9. import props from './props';
  10. import transition from '../mixins/transition';
  11. import { calcIcon } from '../common/utils';
  12. const { prefix } = config;
  13. const name = `${prefix}-toast`;
  14. let Toast = class Toast extends SuperComponent {
  15. constructor() {
  16. super(...arguments);
  17. this.externalClasses = [`${prefix}-class`];
  18. this.options = {
  19. multipleSlots: true,
  20. };
  21. this.behaviors = [transition()];
  22. this.hideTimer = null;
  23. this.data = {
  24. prefix,
  25. classPrefix: name,
  26. typeMapIcon: '',
  27. };
  28. this.properties = props;
  29. this.methods = {
  30. show(options) {
  31. if (this.hideTimer)
  32. clearTimeout(this.hideTimer);
  33. const iconMap = {
  34. loading: 'loading',
  35. success: 'check-circle',
  36. warning: 'error-circle',
  37. fail: 'close-circle',
  38. };
  39. const typeMapIcon = iconMap[options === null || options === void 0 ? void 0 : options.theme];
  40. const defaultOptions = {
  41. direction: props.direction.value,
  42. duration: props.duration.value,
  43. icon: props.icon.value,
  44. message: props.message.value,
  45. placement: props.placement.value,
  46. preventScrollThrough: props.preventScrollThrough.value,
  47. theme: props.theme.value,
  48. };
  49. const data = Object.assign(Object.assign(Object.assign({}, defaultOptions), options), { visible: true, isLoading: (options === null || options === void 0 ? void 0 : options.theme) === 'loading', _icon: calcIcon(typeMapIcon !== null && typeMapIcon !== void 0 ? typeMapIcon : options.icon) });
  50. const { duration } = data;
  51. this.setData(data);
  52. if (duration > 0) {
  53. this.hideTimer = setTimeout(() => {
  54. this.hide();
  55. }, duration);
  56. }
  57. },
  58. hide() {
  59. var _a, _b;
  60. this.setData({ visible: false });
  61. (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.close) === null || _b === void 0 ? void 0 : _b.call(_a);
  62. this.triggerEvent('close');
  63. },
  64. destroyed() {
  65. if (this.hideTimer) {
  66. clearTimeout(this.hideTimer);
  67. this.hideTimer = null;
  68. }
  69. this.triggerEvent('destory');
  70. },
  71. loop() { },
  72. };
  73. }
  74. detached() {
  75. this.destroyed();
  76. }
  77. };
  78. Toast = __decorate([
  79. wxComponent()
  80. ], Toast);
  81. export default Toast;