vant-weapp/dist/toast/index.js
2018-02-07 20:11:31 +08:00

67 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module.exports = {
showZanToast(title, timeout) {
const options = formatParameter(title, timeout);
// 清除上一轮的计时器
const { timer = 0 } = this.data.zanToast || {};
clearTimeout(timer);
// 弹层设置~
const zanToast = {
show: true,
icon: options.icon,
image: options.image,
title: options.title
};
this.setData({
zanToast
});
// 传入的显示时长小于0就认为需要一直显示
if (timeout < 0) {
return;
}
// 下一轮计时器
const nextTimer = setTimeout(() => {
this.clearZanToast();
}, timeout || 3000);
this.setData({
'zanToast.timer': nextTimer
});
},
// 清除所有 toast
clearZanToast() {
const { timer = 0 } = this.data.zanToast || {};
clearTimeout(timer);
this.setData({
'zanToast.show': false
});
},
// 快捷方法,显示 loading
showZanLoading(title) {
const options = formatParameter(title);
this.showZanToast({
...options,
icon: 'loading'
});
}
};
function formatParameter(title, timeout = 0) {
// 如果传入的 title 是对象,那么认为所有的配置属性都在这个对象中了
if (typeof title === 'object') {
return title;
}
return {
title,
timeout
};
}