Blame view

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