Blame view

src/components/modal/modal.vue 8.01 KB
be966e9f   梁灏   add Modal component
1
  <template>
6259471f   梁灏   support Modal
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
      <span>
          <transition name="fade">
              <div :class="maskClasses" v-show="visible" @click="mask"></div>
          </transition>
          <div :class="wrapClasses" @click="handleWrapClick">
              <transition name="ease">
                  <div :class="classes" :style="mainStyles" v-show="visible">
                      <div :class="[prefixCls + '-content']">
                          <a :class="[prefixCls + '-close']" v-if="closable" @click="close">
                              <slot name="close">
                                  <Icon type="ios-close-empty"></Icon>
                              </slot>
                          </a>
                          <div :class="[prefixCls + '-header']" v-if="showHead"><slot name="header"><div :class="[prefixCls + '-header-inner']">{{ title }}</div></slot></div>
                          <div :class="[prefixCls + '-body']"><slot></slot></div>
                          <div :class="[prefixCls + '-footer']" v-if="!footerHide">
                              <slot name="footer">
                                  <i-button type="text" size="large" @click.native="cancel">{{ cancelText }}</i-button>
                                  <i-button type="primary" size="large" :loading="buttonLoading" @click.native="ok">{{ okText }}</i-button>
                              </slot>
                          </div>
                      </div>
be966e9f   梁灏   add Modal component
24
                  </div>
6259471f   梁灏   support Modal
25
              </transition>
be966e9f   梁灏   add Modal component
26
          </div>
6259471f   梁灏   support Modal
27
      </span>
be966e9f   梁灏   add Modal component
28
29
30
  </template>
  <script>
      import Icon from '../icon';
4b7138b9   梁灏   fixed some bugs
31
      import iButton from '../button/button.vue';
be966e9f   梁灏   add Modal component
32
      import { getScrollBarSize } from '../../utils/assist';
012cbf28   梁灏   update locale
33
      import { t } from '../../locale';
be966e9f   梁灏   add Modal component
34
35
36
37
  
      const prefixCls = 'ivu-modal';
  
      export default {
4b7138b9   梁灏   fixed some bugs
38
          components: { Icon, iButton },
be966e9f   梁灏   add Modal component
39
          props: {
6259471f   梁灏   support Modal
40
              value: {
be966e9f   梁灏   add Modal component
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
                  type: Boolean,
                  default: false
              },
              closable: {
                  type: Boolean,
                  default: true
              },
              maskClosable: {
                  type: Boolean,
                  default: true
              },
              title: {
                  type: String
              },
              width: {
                  type: [Number, String],
                  default: 520
              },
              okText: {
                  type: String,
012cbf28   梁灏   update locale
61
62
63
                  default () {
                      return t('i.modal.okText');
                  }
be966e9f   梁灏   add Modal component
64
65
66
              },
              cancelText: {
                  type: String,
012cbf28   梁灏   update locale
67
68
69
                  default () {
                      return t('i.modal.cancelText');
                  }
be966e9f   梁灏   add Modal component
70
71
72
73
74
              },
              loading: {
                  type: Boolean,
                  default: false
              },
6259471f   梁灏   support Modal
75
              styles: {
be966e9f   梁灏   add Modal component
76
77
78
79
80
81
82
83
84
                  type: Object
              },
              className: {
                  type: String
              },
              // for instance
              footerHide: {
                  type: Boolean,
                  default: false
f9a2e611   Rijn   added scrolling p...
85
              },
5d0b89ce   Rijn   change scrolling ...
86
              scrollable: {
f9a2e611   Rijn   added scrolling p...
87
88
                  type: Boolean,
                  default: false
be966e9f   梁灏   add Modal component
89
90
91
92
93
94
95
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  wrapShow: false,
                  showHead: true,
6259471f   梁灏   support Modal
96
97
                  buttonLoading: false,
                  visible: this.value
b0893113   jingsam   :art: add eslint
98
              };
be966e9f   梁灏   add Modal component
99
100
101
102
103
104
105
106
107
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrap`,
                      {
                          [`${prefixCls}-hidden`]: !this.wrapShow,
                          [`${this.className}`]: !!this.className
                      }
b0893113   jingsam   :art: add eslint
108
                  ];
be966e9f   梁灏   add Modal component
109
110
111
112
113
114
115
              },
              maskClasses () {
                  return `${prefixCls}-mask`;
              },
              classes () {
                  return `${prefixCls}`;
              },
6259471f   梁灏   support Modal
116
              mainStyles () {
be966e9f   梁灏   add Modal component
117
118
119
120
121
122
                  let style = {};
  
                  const styleWidth = {
                      width: `${this.width}px`
                  };
  
6259471f   梁灏   support Modal
123
                  const customStyle = this.styles ? this.styles : {};
be966e9f   梁灏   add Modal component
124
125
126
127
128
129
130
131
132
  
                  Object.assign(style, styleWidth, customStyle);
  
                  return style;
              }
          },
          methods: {
              close () {
                  this.visible = false;
6259471f   梁灏   support Modal
133
                  this.$emit('input', false);
be966e9f   梁灏   add Modal component
134
135
136
137
138
139
140
                  this.$emit('on-cancel');
              },
              mask () {
                  if (this.maskClosable) {
                      this.close();
                  }
              },
09bce8de   梁灏   update Modal
141
142
              handleWrapClick (event) {
                  // use indexOf,do not use === ,because ivu-modal-wrap can have other custom className
48dd8ebf   梁灏   update Modal
143
144
                  const className = event.target.getAttribute('class');
                  if (className && className.indexOf(`${prefixCls}-wrap`) > -1) this.mask();
09bce8de   梁灏   update Modal
145
              },
be966e9f   梁灏   add Modal component
146
147
148
149
150
151
152
153
              cancel () {
                  this.close();
              },
              ok () {
                  if (this.loading) {
                      this.buttonLoading = true;
                  } else {
                      this.visible = false;
6259471f   梁灏   support Modal
154
                      this.$emit('input', false);
be966e9f   梁灏   add Modal component
155
156
157
158
159
160
                  }
                  this.$emit('on-ok');
              },
              EscClose (e) {
                  if (this.visible && this.closable) {
                      if (e.keyCode === 27) {
b0893113   jingsam   :art: add eslint
161
                          this.close();
be966e9f   梁灏   add Modal component
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
                      }
                  }
              },
              checkScrollBar () {
                  let fullWindowWidth = window.innerWidth;
                  if (!fullWindowWidth) { // workaround for missing window.innerWidth in IE8
                      const documentElementRect = document.documentElement.getBoundingClientRect();
                      fullWindowWidth = documentElementRect.right - Math.abs(documentElementRect.left);
                  }
                  this.bodyIsOverflowing = document.body.clientWidth < fullWindowWidth;
                  if (this.bodyIsOverflowing) {
                      this.scrollBarWidth = getScrollBarSize();
                  }
              },
              setScrollBar () {
                  if (this.bodyIsOverflowing && this.scrollBarWidth !== undefined) {
                      document.body.style.paddingRight = `${this.scrollBarWidth}px`;
                  }
              },
              resetScrollBar () {
                  document.body.style.paddingRight = '';
              },
              addScrollEffect () {
                  this.checkScrollBar();
                  this.setScrollBar();
                  document.body.style.overflow = 'hidden';
              },
              removeScrollEffect() {
                  document.body.style.overflow = '';
                  this.resetScrollBar();
              }
          },
6259471f   梁灏   support Modal
194
          mounted () {
be966e9f   梁灏   add Modal component
195
196
197
198
199
200
              if (this.visible) {
                  this.wrapShow = true;
              }
  
              let showHead = true;
  
2ac208b9   梁灏   fixed #407
201
              if (this.$slots.header === undefined && !this.title) {
be966e9f   梁灏   add Modal component
202
203
204
205
206
207
208
209
210
211
                  showHead = false;
              }
  
              this.showHead = showHead;
  
              // ESC close
              document.addEventListener('keydown', this.EscClose);
          },
          beforeDestroy () {
              document.removeEventListener('keydown', this.EscClose);
727b795c   梁灏   reset body scroll...
212
              this.removeScrollEffect();
be966e9f   梁灏   add Modal component
213
214
          },
          watch: {
6259471f   梁灏   support Modal
215
216
              value (val) {
                  this.visible = val;
6259471f   梁灏   support Modal
217
              },
be966e9f   梁灏   add Modal component
218
219
220
              visible (val) {
                  if (val === false) {
                      this.buttonLoading = false;
e011898c   梁灏   fixed #197
221
                      this.timer = setTimeout(() => {
be966e9f   梁灏   add Modal component
222
                          this.wrapShow = false;
9084eb18   梁灏   fixed #92
223
                          this.removeScrollEffect();
be966e9f   梁灏   add Modal component
224
                      }, 300);
be966e9f   梁灏   add Modal component
225
                  } else {
e011898c   梁灏   fixed #197
226
                      if (this.timer) clearTimeout(this.timer);
be966e9f   梁灏   add Modal component
227
                      this.wrapShow = true;
5d0b89ce   Rijn   change scrolling ...
228
                      if (!this.scrollable) {
f9a2e611   Rijn   added scrolling p...
229
230
                          this.addScrollEffect();
                      }
be966e9f   梁灏   add Modal component
231
                  }
3c01d81a   梁灏   fixed Modal bug,w...
232
233
234
235
236
              },
              loading (val) {
                  if (!val) {
                      this.buttonLoading = false;
                  }
f9a2e611   Rijn   added scrolling p...
237
              },
5d0b89ce   Rijn   change scrolling ...
238
              scrollable (val) {
f346ce4b   Rijn   lint fix
239
                  if (!val) {
f9a2e611   Rijn   added scrolling p...
240
241
242
243
                      this.addScrollEffect();
                  } else {
                      this.removeScrollEffect();
                  }
be966e9f   梁灏   add Modal component
244
245
              }
          }
b0893113   jingsam   :art: add eslint
246
      };
d6342fe1   jingsam   fixed ie bug
247
  </script>