toast.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. var __assign =
  3. (this && this.__assign) ||
  4. function () {
  5. __assign =
  6. Object.assign ||
  7. function (t) {
  8. for (var s, i = 1, n = arguments.length; i < n; i++) {
  9. s = arguments[i];
  10. for (var p in s) {
  11. if (Object.prototype.hasOwnProperty.call(s, p)) {
  12. t[p] = s[p];
  13. }
  14. }
  15. }
  16. return t;
  17. };
  18. return __assign.apply(this, arguments);
  19. };
  20. Object.defineProperty(exports, '__esModule', {
  21. value: true
  22. });
  23. var utils_1 = require('../common/utils');
  24. var defaultOptions = {
  25. type: 'text',
  26. mask: false,
  27. message: '',
  28. show: true,
  29. zIndex: 1000,
  30. duration: 2000,
  31. position: 'middle',
  32. forbidClick: false,
  33. loadingType: 'circular',
  34. selector: '#van-toast'
  35. };
  36. var queue = [];
  37. var currentOptions = __assign({}, defaultOptions);
  38. function parseOptions(message) {
  39. return utils_1.isObj(message)
  40. ? message
  41. : {
  42. message: message
  43. };
  44. }
  45. function getContext() {
  46. var pages = getCurrentPages();
  47. return pages[pages.length - 1];
  48. }
  49. function Toast(toastOptions) {
  50. var options = __assign(__assign({}, currentOptions), parseOptions(toastOptions));
  51. var context = options.context || getContext();
  52. var toast = context.zpSelectComponent(options.selector);
  53. if (!toast) {
  54. console.warn('未找到 van-toast 节点,请确认 selector 及 context 是否正确');
  55. return;
  56. }
  57. delete options.context;
  58. delete options.selector;
  59. toast.clear = function () {
  60. toast.setData({
  61. show: false
  62. });
  63. if (options.onClose) {
  64. options.onClose();
  65. }
  66. };
  67. queue.push(toast);
  68. toast.setData(options);
  69. clearTimeout(toast.timer);
  70. if (options.duration > 0) {
  71. toast.timer = setTimeout(function () {
  72. toast.clear();
  73. queue = queue.filter(function (item) {
  74. return item !== toast;
  75. });
  76. }, options.duration);
  77. }
  78. return toast;
  79. }
  80. var createMethod = function (type) {
  81. return function (options) {
  82. return Toast(
  83. __assign(
  84. {
  85. type: type
  86. },
  87. parseOptions(options)
  88. )
  89. );
  90. };
  91. };
  92. Toast.loading = createMethod('loading');
  93. Toast.success = createMethod('success');
  94. Toast.fail = createMethod('fail');
  95. Toast.clear = function () {
  96. queue.forEach(function (toast) {
  97. toast.clear();
  98. });
  99. queue = [];
  100. };
  101. Toast.setDefaultOptions = function (options) {
  102. Object.assign(currentOptions, options);
  103. };
  104. Toast.resetDefaultOptions = function () {
  105. currentOptions = __assign({}, defaultOptions);
  106. };
  107. exports.default = Toast;