index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  6. var _vue = _interopRequireDefault(require("vue"));
  7. var _Toast = _interopRequireDefault(require("./Toast"));
  8. var _utils = require("../utils");
  9. var _node = require("../utils/dom/node");
  10. var defaultOptions = {
  11. icon: '',
  12. type: 'text',
  13. // @deprecated
  14. mask: false,
  15. value: true,
  16. message: '',
  17. className: '',
  18. overlay: false,
  19. onClose: null,
  20. onOpened: null,
  21. duration: 2000,
  22. iconPrefix: undefined,
  23. position: 'middle',
  24. transition: 'van-fade',
  25. forbidClick: false,
  26. loadingType: undefined,
  27. getContainer: 'body',
  28. overlayStyle: null,
  29. closeOnClick: false,
  30. closeOnClickOverlay: false
  31. }; // default options of specific type
  32. var defaultOptionsMap = {};
  33. var queue = [];
  34. var multiple = false;
  35. var currentOptions = (0, _extends2.default)({}, defaultOptions);
  36. function parseOptions(message) {
  37. if ((0, _utils.isObject)(message)) {
  38. return message;
  39. }
  40. return {
  41. message: message
  42. };
  43. }
  44. function isInDocument(element) {
  45. return document.body.contains(element);
  46. }
  47. function createInstance() {
  48. /* istanbul ignore if */
  49. if (_utils.isServer) {
  50. return {};
  51. }
  52. queue = queue.filter(function (item) {
  53. return !item.$el.parentNode || isInDocument(item.$el);
  54. });
  55. if (!queue.length || multiple) {
  56. var toast = new (_vue.default.extend(_Toast.default))({
  57. el: document.createElement('div')
  58. });
  59. toast.$on('input', function (value) {
  60. toast.value = value;
  61. });
  62. queue.push(toast);
  63. }
  64. return queue[queue.length - 1];
  65. } // transform toast options to popup props
  66. function transformOptions(options) {
  67. return (0, _extends2.default)({}, options, {
  68. overlay: options.mask || options.overlay,
  69. mask: undefined,
  70. duration: undefined
  71. });
  72. }
  73. function Toast(options) {
  74. if (options === void 0) {
  75. options = {};
  76. }
  77. var toast = createInstance(); // should add z-index if previous toast has not disappeared
  78. if (toast.value) {
  79. toast.updateZIndex();
  80. }
  81. options = parseOptions(options);
  82. options = (0, _extends2.default)({}, currentOptions, defaultOptionsMap[options.type || currentOptions.type], options);
  83. if (process.env.NODE_ENV === 'development' && options.mask) {
  84. console.warn('[Vant] Toast: "mask" option is deprecated, use "overlay" option instead.');
  85. }
  86. options.clear = function () {
  87. toast.value = false;
  88. if (options.onClose) {
  89. options.onClose();
  90. options.onClose = null;
  91. }
  92. if (multiple && !_utils.isServer) {
  93. toast.$on('closed', function () {
  94. clearTimeout(toast.timer);
  95. queue = queue.filter(function (item) {
  96. return item !== toast;
  97. });
  98. (0, _node.removeNode)(toast.$el);
  99. toast.$destroy();
  100. });
  101. }
  102. };
  103. (0, _extends2.default)(toast, transformOptions(options));
  104. clearTimeout(toast.timer);
  105. if (options.duration > 0) {
  106. toast.timer = setTimeout(function () {
  107. toast.clear();
  108. }, options.duration);
  109. }
  110. return toast;
  111. }
  112. var createMethod = function createMethod(type) {
  113. return function (options) {
  114. return Toast((0, _extends2.default)({
  115. type: type
  116. }, parseOptions(options)));
  117. };
  118. };
  119. ['loading', 'success', 'fail'].forEach(function (method) {
  120. Toast[method] = createMethod(method);
  121. });
  122. Toast.clear = function (all) {
  123. if (queue.length) {
  124. if (all) {
  125. queue.forEach(function (toast) {
  126. toast.clear();
  127. });
  128. queue = [];
  129. } else if (!multiple) {
  130. queue[0].clear();
  131. } else {
  132. queue.shift().clear();
  133. }
  134. }
  135. };
  136. Toast.setDefaultOptions = function (type, options) {
  137. if (typeof type === 'string') {
  138. defaultOptionsMap[type] = options;
  139. } else {
  140. (0, _extends2.default)(currentOptions, type);
  141. }
  142. };
  143. Toast.resetDefaultOptions = function (type) {
  144. if (typeof type === 'string') {
  145. defaultOptionsMap[type] = null;
  146. } else {
  147. currentOptions = (0, _extends2.default)({}, defaultOptions);
  148. defaultOptionsMap = {};
  149. }
  150. };
  151. Toast.allowMultiple = function (value) {
  152. if (value === void 0) {
  153. value = true;
  154. }
  155. multiple = value;
  156. };
  157. Toast.install = function () {
  158. _vue.default.use(_Toast.default);
  159. };
  160. _vue.default.prototype.$toast = Toast;
  161. var _default = Toast;
  162. exports.default = _default;