vdesign-ui 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,94 +1,83 @@
1
- import Vue from 'vue'
2
- import VdToast from './index.vue'
1
+ import Vue from 'vue';
2
+ import ToastComponent from './index.vue';
3
3
  import { inBrowser } from '../utils/env';
4
4
 
5
- let timer = null
5
+ let timer = null;
6
+ let currentToast = null;
6
7
 
7
- /**
8
- * 使用新类继承 VUE 扩展组件
9
- */
10
- class Toast extends Vue.extend(VdToast) {
8
+ class VdToast extends Vue.extend(ToastComponent) {
11
9
  constructor(options) {
12
- super()
13
- const type = Object.prototype.toString.call(options)
14
- /**
15
- * 赋值虚拟节点
16
- */
17
- this.vm = this.$mount()
18
- /**
19
- * 判断 options 类型
20
- */
21
- if (type === '[object String]' || type === '[object Number]') {
22
- this.message = options
23
- } else if (type === '[object Object]') {
24
- /**
25
- * 循环遍历,设置该类属性
26
- */
27
- Object.keys(options).forEach(elem => {
28
- this[elem] = options[elem]
29
- })
30
- } else {
31
- /**
32
- * 抛出错误
33
- */
34
- throw new Error(`${JSON.stringify(options)} is not Number, String or Object`)
35
- }
10
+ super();
11
+
12
+ this.vm = this.$mount();
36
13
 
37
- // // 设置具名插槽内容
38
- // if (options.slotContent) {
39
- // const slotContent = options.slotContent;
40
- // this.$slots.icon = slotContent.icon ? [this.$createElement('div', { domProps: { innerHTML: slotContent.icon } })] : [];
41
- // this.$slots.content = slotContent.content ? [this.$createElement('div', { domProps: { innerHTML: slotContent.content } })] : [];
42
- // }
14
+ // 支持字符串或对象传入
15
+ if (typeof options === 'string' || typeof options === 'number') {
16
+ this.message = options;
17
+ } else if (typeof options === 'object') {
18
+ Object.assign(this, options);
19
+ }
43
20
 
44
- this.init()
21
+ this.init();
45
22
  }
46
23
 
47
- /**
48
- * 初始化方法
49
- */
50
24
  init() {
51
- /**
52
- * 把虚拟 DOM 插入到真实 DOM 树中
53
- */
54
25
  if (inBrowser) {
55
- document.body.appendChild(this.vm.$el) // 将虚拟 DOM 插入真实 DOM
56
- this.start()
26
+ document.body.appendChild(this.vm.$el);
27
+ this.start();
57
28
  }
58
29
  }
59
30
 
60
- /**
61
- * 开始此组件
62
- */
63
31
  start() {
64
- clearTimeout(timer)
65
- /**
66
- * 在挂载真实 DOM 树后显示过渡效果
67
- */
32
+ clearTimeout(timer);
68
33
  timer = setTimeout(() => {
69
- this.isShow = true
70
- this.end()
71
- }, 100)
34
+ this.isShow = true;
35
+
36
+ if (this.duration > 0) {
37
+ this.end();
38
+ }
39
+ }, 100);
72
40
  }
73
41
 
74
- /**
75
- * 结束此组件
76
- */
77
42
  end() {
78
- /**
79
- * 关闭过渡效果之后移除真实 DOM 树节点
80
- */
81
- setTimeout(() => {
82
- this.isShow = false
83
- setTimeout(() => {
84
- if (inBrowser) {
85
- document.body.removeChild(this.vm.$el)
86
- }
87
- }, 500)
88
- }, this.duration)
43
+ clearTimeout(timer);
44
+ if (this.duration > 0) {
45
+ timer = setTimeout(() => {
46
+ this.isShow = false;
47
+ setTimeout(() => {
48
+ // 检查元素是否仍在 DOM 中
49
+ if (inBrowser && document.body.contains(this.vm.$el)) {
50
+ document.body.removeChild(this.vm.$el);
51
+ }
52
+ }, 500);
53
+ }, this.duration);
54
+ }
55
+ }
56
+
57
+ clear() {
58
+ this.isShow = false;
59
+ clearTimeout(timer);
60
+ // 检查元素是否仍在 DOM 中
61
+ if (inBrowser && document.body.contains(this.vm.$el)) {
62
+ document.body.removeChild(this.vm.$el);
63
+ }
89
64
  }
90
65
  }
91
66
 
92
- const GetToast = options => new Toast(options)
67
+ // 创建静态方法以支持直接调用
68
+ const showToast = options => {
69
+ currentToast && currentToast.clear(); // 清除现有的 toast
70
+ currentToast = new VdToast(options); // 创建新的 toast 实例
71
+ return currentToast;
72
+ };
73
+
74
+ // 添加静态方法,支持手动清除
75
+ showToast.clear = () => {
76
+ currentToast && currentToast.clear();
77
+ currentToast = null;
78
+ };
79
+
80
+ // 在 Vue 的原型上挂载,方便全局调用
81
+ Vue.prototype.$VdToast = showToast;
93
82
 
94
- export default GetToast
83
+ export default showToast;