123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import Vue from 'vue';
- import VueToast from './Toast';
- import { isObject, isServer } from '../utils';
- import { removeNode } from '../utils/dom/node';
- var defaultOptions = {
- icon: '',
- type: 'text',
- // @deprecated
- mask: false,
- value: true,
- message: '',
- className: '',
- overlay: false,
- onClose: null,
- onOpened: null,
- duration: 2000,
- iconPrefix: undefined,
- position: 'middle',
- transition: 'van-fade',
- forbidClick: false,
- loadingType: undefined,
- getContainer: 'body',
- overlayStyle: null,
- closeOnClick: false,
- closeOnClickOverlay: false
- }; // default options of specific type
- var defaultOptionsMap = {};
- var queue = [];
- var multiple = false;
- var currentOptions = _extends({}, defaultOptions);
- function parseOptions(message) {
- if (isObject(message)) {
- return message;
- }
- return {
- message: message
- };
- }
- function isInDocument(element) {
- return document.body.contains(element);
- }
- function createInstance() {
- /* istanbul ignore if */
- if (isServer) {
- return {};
- }
- queue = queue.filter(function (item) {
- return !item.$el.parentNode || isInDocument(item.$el);
- });
- if (!queue.length || multiple) {
- var toast = new (Vue.extend(VueToast))({
- el: document.createElement('div')
- });
- toast.$on('input', function (value) {
- toast.value = value;
- });
- queue.push(toast);
- }
- return queue[queue.length - 1];
- } // transform toast options to popup props
- function transformOptions(options) {
- return _extends({}, options, {
- overlay: options.mask || options.overlay,
- mask: undefined,
- duration: undefined
- });
- }
- function Toast(options) {
- if (options === void 0) {
- options = {};
- }
- var toast = createInstance(); // should add z-index if previous toast has not disappeared
- if (toast.value) {
- toast.updateZIndex();
- }
- options = parseOptions(options);
- options = _extends({}, currentOptions, defaultOptionsMap[options.type || currentOptions.type], options);
- if (process.env.NODE_ENV === 'development' && options.mask) {
- console.warn('[Vant] Toast: "mask" option is deprecated, use "overlay" option instead.');
- }
- options.clear = function () {
- toast.value = false;
- if (options.onClose) {
- options.onClose();
- options.onClose = null;
- }
- if (multiple && !isServer) {
- toast.$on('closed', function () {
- clearTimeout(toast.timer);
- queue = queue.filter(function (item) {
- return item !== toast;
- });
- removeNode(toast.$el);
- toast.$destroy();
- });
- }
- };
- _extends(toast, transformOptions(options));
- clearTimeout(toast.timer);
- if (options.duration > 0) {
- toast.timer = setTimeout(function () {
- toast.clear();
- }, options.duration);
- }
- return toast;
- }
- var createMethod = function createMethod(type) {
- return function (options) {
- return Toast(_extends({
- type: type
- }, parseOptions(options)));
- };
- };
- ['loading', 'success', 'fail'].forEach(function (method) {
- Toast[method] = createMethod(method);
- });
- Toast.clear = function (all) {
- if (queue.length) {
- if (all) {
- queue.forEach(function (toast) {
- toast.clear();
- });
- queue = [];
- } else if (!multiple) {
- queue[0].clear();
- } else {
- queue.shift().clear();
- }
- }
- };
- Toast.setDefaultOptions = function (type, options) {
- if (typeof type === 'string') {
- defaultOptionsMap[type] = options;
- } else {
- _extends(currentOptions, type);
- }
- };
- Toast.resetDefaultOptions = function (type) {
- if (typeof type === 'string') {
- defaultOptionsMap[type] = null;
- } else {
- currentOptions = _extends({}, defaultOptions);
- defaultOptionsMap = {};
- }
- };
- Toast.allowMultiple = function (value) {
- if (value === void 0) {
- value = true;
- }
- multiple = value;
- };
- Toast.install = function () {
- Vue.use(VueToast);
- };
- Vue.prototype.$toast = Toast;
- export default Toast;
|