toast.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { createVNode, render } from 'vue';
  2. import Toast from '../index.vue';
  3. const defaultOptions = {
  4. msg: '',
  5. id: '',
  6. duration: 2000, //显示时间(毫秒)
  7. center: true, // 未实现
  8. type: 'text',
  9. title: '',
  10. customClass: '', // 未实现
  11. bottom: '30px', // 未实现
  12. size: 'base', // 未实现
  13. iconSize: '20',
  14. icon: null, // 未实现
  15. textAlignCenter: true, // 未实现
  16. loadingRotate: true, // 未实现
  17. bgColor: 'rgba(0, 0, 0, .8)',
  18. onClose: null, // 未实现
  19. unmount: null,
  20. cover: false, //透明遮罩层 // 未实现
  21. coverColor: 'rgba(0, 0, 0, 0)', // 未实现
  22. closeOnClickOverlay: false // 未实现
  23. };
  24. let idsMap: string[] = [];
  25. let optsMap: any[] = [];
  26. const clearToast = (id?: string) => {
  27. if (id) {
  28. const container = document.getElementById(id);
  29. optsMap = optsMap.filter((item) => item.id !== id);
  30. idsMap = idsMap.filter((item) => item !== id);
  31. if (container) {
  32. document.body.removeChild(container);
  33. }
  34. } else {
  35. idsMap.forEach((item) => {
  36. const container = document.getElementById(item);
  37. if (container) {
  38. document.body.removeChild(container);
  39. }
  40. });
  41. optsMap = [];
  42. idsMap = [];
  43. }
  44. };
  45. const updateToast = (opts: any) => {
  46. const container = document.getElementById(opts.id);
  47. if (container) {
  48. const currentOpt = optsMap.find((item) => item.id === opts.id);
  49. if (currentOpt) {
  50. opts = { ...defaultOptions, ...currentOpt, ...opts };
  51. } else {
  52. opts = { ...defaultOptions, ...opts };
  53. }
  54. const instance: any = createVNode(Toast, opts);
  55. render(instance, container);
  56. return instance.component.ctx;
  57. }
  58. };
  59. const mountToast = (opts: any) => {
  60. opts.unmount = clearToast;
  61. let _id;
  62. // 如果是更新已有的toast
  63. if (opts.id) {
  64. _id = opts.id;
  65. if (idsMap.find((item) => item === opts.id)) {
  66. return updateToast(opts);
  67. }
  68. } else {
  69. _id = new Date().getTime() + '';
  70. }
  71. opts = { ...defaultOptions, ...opts };
  72. opts.id = _id;
  73. idsMap.push(opts.id);
  74. optsMap.push(opts);
  75. const container = document.createElement('div');
  76. container.id = opts.id;
  77. const instance: any = createVNode(Toast, opts);
  78. render(instance, container);
  79. document.body.appendChild(container);
  80. return instance.component.ctx;
  81. };
  82. const errorMsg = (msg: string) => {
  83. if (!msg) {
  84. console.warn('[NutUI Toast]: msg不能为空');
  85. return;
  86. }
  87. };
  88. export const ToastFunction = {
  89. text(msg: string, opts = {}) {
  90. errorMsg(msg);
  91. return mountToast({ ...opts, type: 'text', msg });
  92. },
  93. success(msg: string, opts = {}) {
  94. errorMsg(msg);
  95. return mountToast({ icon: 'success', ...opts, msg, type: 'success' });
  96. },
  97. fail(msg: string, opts = {}) {
  98. errorMsg(msg);
  99. return mountToast({ icon: 'failure', ...opts, msg, type: 'fail' });
  100. },
  101. warn(msg: string, opts = {}) {
  102. errorMsg(msg);
  103. return mountToast({ icon: 'tips', ...opts, msg, type: 'warn' });
  104. },
  105. loading(msg: string, opts = {}) {
  106. return mountToast({
  107. icon: 'loading',
  108. ...opts,
  109. msg,
  110. type: 'loading'
  111. });
  112. },
  113. hide(id?: string) {
  114. clearToast(id);
  115. },
  116. install(app: any): void {
  117. app.use(Toast);
  118. app.config.globalProperties.$toast = ToastFunction;
  119. }
  120. };
  121. export { Toast };
  122. export default ToastFunction;