toast.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _utils = require("../common/utils");
  7. var defaultOptions = {
  8. type: "text",
  9. mask: false,
  10. message: "",
  11. show: true,
  12. zIndex: 1000,
  13. duration: 2000,
  14. position: "middle",
  15. forbidClick: false,
  16. loadingType: "circular",
  17. selector: "#van-toast"
  18. };
  19. var queue = [];
  20. var currentOptions = Object.assign({}, defaultOptions);
  21. function parseOptions(message) {
  22. return (0, _utils.isObj)(message) ? message : {
  23. message: message
  24. };
  25. }
  26. function getContext() {
  27. var pages = getCurrentPages();
  28. return pages[pages.length - 1];
  29. }
  30. function Toast(toastOptions) {
  31. var options = Object.assign(Object.assign({}, currentOptions), parseOptions(toastOptions));
  32. var context = options.context || getContext();
  33. var toast = context.selectComponent(options.selector);
  34. if (!toast) {
  35. console.warn("未找到 van-toast 节点,请确认 selector 及 context 是否正确");
  36. return;
  37. }
  38. delete options.context;
  39. delete options.selector;
  40. toast.clear = function () {
  41. toast.setData({
  42. show: false
  43. });
  44. if (options.onClose) {
  45. options.onClose();
  46. }
  47. };
  48. queue.push(toast);
  49. toast.setData(options);
  50. clearTimeout(toast.timer);
  51. if (options.duration != null && options.duration > 0) {
  52. toast.timer = setTimeout(function () {
  53. toast.clear();
  54. queue = queue.filter(function (item) {
  55. return item !== toast;
  56. });
  57. }, options.duration);
  58. }
  59. return toast;
  60. }
  61. var createMethod = function createMethod(type) {
  62. return function (options) {
  63. return Toast(Object.assign({
  64. type: type
  65. }, parseOptions(options)));
  66. };
  67. };
  68. Toast.loading = createMethod("loading");
  69. Toast.success = createMethod("success");
  70. Toast.fail = createMethod("fail");
  71. Toast.clear = function () {
  72. queue.forEach(function (toast) {
  73. toast.clear();
  74. });
  75. queue = [];
  76. };
  77. Toast.setDefaultOptions = function (options) {
  78. Object.assign(currentOptions, options);
  79. };
  80. Toast.resetDefaultOptions = function () {
  81. currentOptions = Object.assign({}, defaultOptions);
  82. };
  83. var _default = Toast;
  84. exports["default"] = _default;