vant/packages/toast/src/toast.js
张敏 b4544ef1dc Fix: 修复toast关闭时未移除Dom节点,以及补上单元测试 (#19)
* 文档页样式和打包配置优化

* upload unit test

* intanbul ignore src/index

* unit test

* utils dom unit test

* fix: toast not remove

* remove dom unit test

* remove toast clear unit test

* remove toast import vue
2017-04-26 15:49:44 +08:00

78 lines
1.9 KiB
JavaScript

import Vue from 'vue';
import merge from 'src/utils/merge';
const ToastConstructor = Vue.extend(require('./toast.vue'));
let instance;
const getInstance = () => {
if (instance) instance.clear();
instance = new ToastConstructor({
el: document.createElement('div')
});
return instance;
};
const removeDom = event => {
/* istanbul ignore else */
if (event.target.parentNode) {
event.target.parentNode.removeChild(event.target);
}
};
var Toast = (options = {}) => {
const duration = options.duration || 3000;
const instance = getInstance();
instance.closed = false;
clearTimeout(instance.timer);
instance.type = options.type ? options.type : 'text';
instance.message = typeof options === 'string' ? options : options.message;
instance.forbidClick = options.forbidClick ? options.forbidClick : false;
instance.clear = () => {
if (instance.closed) return;
instance.visible = false;
instance.$el.addEventListener('transitionend', removeDom);
instance.closed = true;
};
document.body.appendChild(instance.$el);
Vue.nextTick(function() {
instance.visible = true;
instance.$el.removeEventListener('transitionend', removeDom);
instance.timer = setTimeout(function() {
instance.clear();
}, duration);
});
return instance;
};
Toast.loading = (options) => {
return new Toast(merge({
type: 'loading'
}, options));
};
Toast.success = (options) => {
const message = typeof options === 'string' ? options : options.message;
return new Toast(merge({
type: 'success',
message: message
}, options));
};
Toast.fail = (options) => {
const message = typeof options === 'string' ? options : options.message;
return new Toast(merge({
type: 'fail',
message: message
}, options));
};
Toast.clear = () => {
/* istanbul ignore else */
if (instance) instance.clear();
};
export default Toast;