mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
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
|
|
};
|
|
}
|
|
|
|
var Toast = function Toast(options) {
|
|
if (options === void 0) {
|
|
options = {};
|
|
}
|
|
|
|
options = _extends({}, currentOptions, parseOptions(options));
|
|
var pages = getCurrentPages();
|
|
var ctx = pages[pages.length - 1];
|
|
var toast = ctx.selectComponent(options.selector);
|
|
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; |