| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import Vue from 'vue';
- import VueToast from './Toast';
- import { isObj, isServer, isInDocument } from '../utils';
- var defaultOptions = {
- type: 'text',
- mask: false,
- value: true,
- message: '',
- className: '',
- onClose: null,
- duration: 3000,
- position: 'middle',
- forbidClick: false,
- loadingType: 'circular',
- getContainer: 'body',
- overlayStyle: null
- };
- var parseOptions = function parseOptions(message) {
- return isObj(message) ? message : {
- message: message
- };
- };
- var queue = [];
- var multiple = false;
- var currentOptions = _extends({}, defaultOptions);
- function createInstance() {
- /* istanbul ignore if */
- if (isServer) {
- return {};
- }
- if (!queue.length || multiple || !isInDocument(queue[0].$el)) {
- var toast = new (Vue.extend(VueToast))({
- el: document.createElement('div')
- });
- queue.push(toast);
- }
- return queue[queue.length - 1];
- } // transform toast options to popup props
- function transformer(options) {
- options.overlay = options.mask;
- return options;
- }
- 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 = _extends({}, currentOptions, parseOptions(options), {
- clear: function clear() {
- toast.value = false;
- if (options.onClose) {
- options.onClose();
- }
- if (multiple && !isServer) {
- clearTimeout(toast.timer);
- queue = queue.filter(function (item) {
- return item !== toast;
- });
- var parent = toast.$el.parentNode;
- if (parent) {
- parent.removeChild(toast.$el);
- }
- toast.$destroy();
- }
- }
- });
- _extends(toast, transformer(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 (options) {
- _extends(currentOptions, options);
- };
- Toast.resetDefaultOptions = function () {
- currentOptions = _extends({}, defaultOptions);
- };
- 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;
|