toast.js
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { isObj } from '../common/utils';
var defaultOptions = {
type: 'text',
mask: false,
message: '',
show: true,
zIndex: 1000,
duration: 3000,
position: 'middle',
forbidClick: false,
loadingType: 'circular',
selector: '#van-toast'
};
var queue = [];
var currentOptions = _extends({}, defaultOptions);
function parseOptions(message) {
return isObj(message) ? message : {
message: message
};
}
function getContext() {
var pages = getCurrentPages();
return pages[pages.length - 1];
}
var Toast = function Toast(options) {
if (options === void 0) {
options = {};
}
options = _extends({}, currentOptions, parseOptions(options));
var context = options.context || getContext();
var toast = context.selectComponent(options.selector);
if (!toast) {
console.warn('未找到 van-toast 节点,请确认 selector 及 context 是否正确');
return;
}
delete options.context;
delete options.selector;
queue.push(toast);
toast.setData(options);
clearTimeout(toast.timer);
if (options.duration > 0) {
toast.timer = setTimeout(function () {
toast.clear();
queue = queue.filter(function (item) {
return item !== toast;
});
}, 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 () {
queue.forEach(function (toast) {
toast.clear();
});
queue = [];
};
Toast.setDefaultOptions = function (options) {
Object.assign(currentOptions, options);
};
Toast.resetDefaultOptions = function () {
currentOptions = _extends({}, defaultOptions);
};
export default Toast;