index.js 3.9 KB

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