toast.js 3.7 KB

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