Blame view

src/components/base/notification/notice.vue 4.22 KB
7c15ac9e   梁灏   add Message compo...
1
  <template>
36a91579   梁灏   fixed #1881
2
      <transition :name="transitionName" @enter="handleEnter" @leave="handleLeave">
6cadeba4   梁灏   support Message
3
          <div :class="classes" :style="styles">
e0bd31a6   Aresn   update Message
4
5
6
7
8
9
10
11
12
13
14
15
16
17
              <template v-if="type === 'notice'">
                  <div :class="[baseClass + '-content']" ref="content" v-html="content"></div>
                  <a :class="[baseClass + '-close']" @click="close" v-if="closable">
                      <i class="ivu-icon ivu-icon-ios-close-empty"></i>
                  </a>
              </template>
              <template v-if="type === 'message'">
                  <div :class="[baseClass + '-content']" ref="content">
                      <div :class="[baseClass + '-content-text']" v-html="content"></div>
                      <a :class="[baseClass + '-close']" @click="close" v-if="closable">
                          <i class="ivu-icon ivu-icon-ios-close-empty"></i>
                      </a>
                  </div>
              </template>
6cadeba4   梁灏   support Message
18
19
          </div>
      </transition>
7c15ac9e   梁灏   add Message compo...
20
21
22
23
24
25
26
27
28
29
30
31
  </template>
  <script>
      export default {
          props: {
              prefixCls: {
                  type: String,
                  default: ''
              },
              duration: {
                  type: Number,
                  default: 1.5
              },
e0bd31a6   Aresn   update Message
32
33
34
              type: {
                  type: String
              },
7c15ac9e   梁灏   add Message compo...
35
36
37
38
              content: {
                  type: String,
                  default: ''
              },
6cadeba4   梁灏   support Message
39
              styles: {
7c15ac9e   梁灏   add Message compo...
40
41
42
43
                  type: Object,
                  default: function() {
                      return {
                          right: '50%'
b0893113   jingsam   :art: add eslint
44
                      };
7c15ac9e   梁灏   add Message compo...
45
46
47
48
49
50
51
52
53
                  }
              },
              closable: {
                  type: Boolean,
                  default: false
              },
              className: {
                  type: String
              },
6cadeba4   梁灏   support Message
54
              name: {
7c15ac9e   梁灏   add Message compo...
55
56
57
58
59
                  type: String,
                  required: true
              },
              onClose: {
                  type: Function
40f8606f   梁灏   add Notice component
60
61
62
              },
              transitionName: {
                  type: String
7c15ac9e   梁灏   add Message compo...
63
64
              }
          },
03441255   梁灏   optimize Notice s...
65
66
67
          data () {
              return {
                  withDesc: false
b0893113   jingsam   :art: add eslint
68
              };
03441255   梁灏   optimize Notice s...
69
          },
7c15ac9e   梁灏   add Message compo...
70
71
72
73
74
75
76
77
78
          computed: {
              baseClass () {
                  return `${this.prefixCls}-notice`;
              },
              classes () {
                  return [
                      this.baseClass,
                      {
                          [`${this.className}`]: !!this.className,
03441255   梁灏   optimize Notice s...
79
80
                          [`${this.baseClass}-closable`]: this.closable,
                          [`${this.baseClass}-with-desc`]: this.withDesc
7c15ac9e   梁灏   add Message compo...
81
                      }
b0893113   jingsam   :art: add eslint
82
                  ];
7c15ac9e   梁灏   add Message compo...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
              },
              contentClasses () {
                  return `${this.baseClass}-content`;
              }
          },
          methods: {
              clearCloseTimer () {
                  if (this.closeTimer) {
                      clearTimeout(this.closeTimer);
                      this.closeTimer = null;
                  }
              },
              close () {
                  this.clearCloseTimer();
                  this.onClose();
6cadeba4   梁灏   support Message
98
                  this.$parent.close(this.name);
36a91579   梁灏   fixed #1881
99
100
101
102
103
104
105
106
              },
              handleEnter (el) {
                  if (this.type === 'message') {
                      el.style.height = el.scrollHeight + 'px';
                  }
              },
              handleLeave (el) {
                  if (this.type === 'message') {
e7ad4154   梁灏   update Message st...
107
108
109
110
111
112
                      // 优化一下,如果当前只有一个 Message,则不使用 js 过渡动画,这样更优美
                      if (document.getElementsByClassName('ivu-message-notice').length !== 1) {
                          el.style.height = 0;
                          el.style.paddingTop = 0;
                          el.style.paddingBottom = 0;
                      }
36a91579   梁灏   fixed #1881
113
                  }
7c15ac9e   梁灏   add Message compo...
114
115
              }
          },
833501a4   梁灏   support Notice
116
          mounted () {
7c15ac9e   梁灏   add Message compo...
117
118
119
120
121
              this.clearCloseTimer();
  
              if (this.duration !== 0) {
                  this.closeTimer = setTimeout(() => {
                      this.close();
b0893113   jingsam   :art: add eslint
122
                  }, this.duration * 1000);
7c15ac9e   梁灏   add Message compo...
123
              }
03441255   梁灏   optimize Notice s...
124
125
126
  
              // check if with desc in Notice component
              if (this.prefixCls === 'ivu-notice') {
6cadeba4   梁灏   support Message
127
                  this.withDesc = this.$refs.content.querySelectorAll(`.${this.prefixCls}-desc`)[0].innerHTML !== '';
03441255   梁灏   optimize Notice s...
128
              }
7c15ac9e   梁灏   add Message compo...
129
130
131
132
          },
          beforeDestroy () {
              this.clearCloseTimer();
          }
b0893113   jingsam   :art: add eslint
133
      };
d6342fe1   jingsam   fixed ie bug
134
  </script>