vdesign-ui 0.2.7 → 0.2.9

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.
@@ -2,82 +2,85 @@ import Vue from 'vue';
2
2
  import ToastComponent from './index.vue';
3
3
  import { inBrowser } from '../utils/env';
4
4
 
5
- let timer = null;
6
- let currentToast = null;
5
+ let queueResult = []
6
+
7
+ function getType(value) {
8
+ if (value !== value) return 'NaN';
9
+ return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
10
+ }
7
11
 
8
12
  class VdToast extends Vue.extend(ToastComponent) {
9
13
  constructor(options) {
10
14
  super();
11
-
12
15
  this.vm = this.$mount();
13
16
 
14
- // 支持字符串或对象传入
17
+ this.timer = null
18
+
19
+ // 判断传入的 options 类型,并进行赋值
15
20
  if (typeof options === 'string' || typeof options === 'number') {
16
21
  this.message = options;
17
- } else if (typeof options === 'object') {
22
+ } else if (getType(options) === 'object') {
23
+ this.message = options.message || '';
24
+ this.duration = options.duration || 1500;
18
25
  Object.assign(this, options);
19
26
  }
20
27
 
21
28
  this.init();
22
29
  }
23
30
 
31
+ /**
32
+ * 初始化方法
33
+ */
24
34
  init() {
25
35
  if (inBrowser) {
26
- document.body.appendChild(this.vm.$el);
27
- this.start();
36
+ document.body.appendChild(this.vm.$el)
37
+ this.start()
28
38
  }
29
39
  }
30
40
 
31
41
  start() {
32
- clearTimeout(timer);
33
- timer = setTimeout(() => {
34
- this.isShow = true;
35
-
42
+ clearTimeout(this.timer)
43
+ /**
44
+ * 在挂载真实 DOM 树后显示过渡效果
45
+ */
46
+ setTimeout(() => {
47
+ this.isShow = true
36
48
  if (this.duration > 0) {
37
49
  this.end();
38
50
  }
39
- }, 100);
51
+ }, 0)
40
52
  }
41
53
 
42
54
  end() {
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
+ * 关闭过渡效果之后移除真实 DOM 树节点
57
+ */
58
+ this.timer = setTimeout(() => {
59
+ this.clear()
60
+ }, this.duration)
55
61
  }
56
62
 
57
63
  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);
64
+ this.isShow = false
65
+ if (inBrowser && this.vm.$el) {
66
+ setTimeout(() => {
67
+ this.vm.$el.parentNode && this.vm.$el.parentNode.removeChild(this.vm.$el);
68
+ }, 500)
63
69
  }
64
70
  }
65
71
  }
66
72
 
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
- };
73
+ const Toast = (options) => {
74
+ const NewOption = new VdToast(options)
75
+ queueResult.push(NewOption)
76
+ return NewOption
77
+ }
79
78
 
80
- // Vue 的原型上挂载,方便全局调用
81
- Vue.prototype.$VdToast = showToast;
79
+ Toast.clear = () => {
80
+ queueResult.forEach(toast => {
81
+ toast.clear()
82
+ })
83
+ queueResult = [];
84
+ }
82
85
 
83
- export default showToast;
86
+ export default Toast
@@ -10,8 +10,9 @@
10
10
  box-sizing: border-box;
11
11
  text-align: center;
12
12
  z-index: 1000;
13
- transition: opacity .3s linear;
13
+ transition: all .3s linear;
14
14
  opacity: 0;
15
+ visibility: hidden;
15
16
  // token
16
17
  min-width: var(--width-toast-small);
17
18
  max-width: var(--width-toast-large);
@@ -24,6 +25,7 @@
24
25
 
25
26
  &.vd-toast-show {
26
27
  opacity: 1;
28
+ visibility: visible;
27
29
  }
28
30
 
29
31
  &-icon {
@@ -1,2 +1,21 @@
1
- // env.js
1
+ import Vue from 'vue';
2
+
2
3
  export const inBrowser = typeof window !== 'undefined';
4
+
5
+ export const isServer = Vue.prototype.$isServer;
6
+
7
+ export function isObject(val) {
8
+ return val !== null && typeof val === 'object';
9
+ }
10
+
11
+ export function isInDocument(element) {
12
+ return document.body.contains(element);
13
+ }
14
+
15
+ export function removeNode(el) {
16
+ const parent = el.parentNode;
17
+
18
+ if (parent) {
19
+ parent.removeChild(el);
20
+ }
21
+ }