Blame view

src/components/modal/confirm.js 7.89 KB
be966e9f   梁灏   add Modal component
1
2
  import Vue from 'vue';
  import Modal from './modal.vue';
77cf1cd5   Aresn   $Modal support SSR
3
  import Button from '../button/button.vue';
e5337c81   梁灏   fixed some compon...
4
  import Locale from '../../mixins/locale';
be966e9f   梁灏   add Modal component
5
6
7
8
9
10
  
  const prefixCls = 'ivu-modal-confirm';
  
  Modal.newInstance = properties => {
      const _props = properties || {};
  
77cf1cd5   Aresn   $Modal support SSR
11
      const Instance = new Vue({
e5337c81   梁灏   fixed some compon...
12
          mixins: [ Locale ],
77cf1cd5   Aresn   $Modal support SSR
13
          data: Object.assign({}, _props, {
be966e9f   梁灏   add Modal component
14
15
16
17
18
19
              visible: false,
              width: 416,
              title: '',
              body: '',
              iconType: '',
              iconName: '',
e5337c81   梁灏   fixed some compon...
20
21
              okText: undefined,
              cancelText: undefined,
be966e9f   梁灏   add Modal component
22
23
              showCancel: false,
              loading: false,
a87da689   Rijn   added scrollable ...
24
              buttonLoading: false,
34324f28   zhigang.li   add closable prop...
25
              scrollable: false,
504f8c52   梁灏   update Modal
26
              closable: false
be966e9f   梁灏   add Modal component
27
          }),
77cf1cd5   Aresn   $Modal support SSR
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
          render (h) {
              let footerVNodes = [];
              if (this.showCancel) {
                  footerVNodes.push(h(Button, {
                      props: {
                          type: 'text',
                          size: 'large'
                      },
                      on: {
                          click: this.cancel
                      }
                  }, this.localeCancelText));
              }
              footerVNodes.push(h(Button, {
                  props: {
                      type: 'primary',
                      size: 'large',
                      loading: this.buttonLoading
                  },
                  on: {
                      click: this.ok
                  }
              }, this.localeOkText));
  
3ef24d5f   Aresn   $Modal support re...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
              // render content
              let body_render;
              if (this.render) {
                  body_render = h('div', {
                      attrs: {
                          class: `${prefixCls}-body ${prefixCls}-body-render`
                      }
                  }, [this.render(h)]);
              } else {
                  body_render = h('div', {
                      attrs: {
                          class: `${prefixCls}-body`
                      }
                  }, [
                      h('div', {
3ef24d5f   Aresn   $Modal support re...
67
68
69
70
71
72
73
                          domProps: {
                              innerHTML: this.body
                          }
                      })
                  ]);
              }
  
172e4396   梁灏   update $Modal wit...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
              // when render with no title, hide head
              let head_render;
              if (this.title) {
                  head_render = h('div', {
                      attrs: {
                          class: `${prefixCls}-head`
                      }
                  }, [
                      h('div', {
                          class: this.iconTypeCls
                      }, [
                          h('i', {
                              class: this.iconNameCls
                          })
                      ]),
                      h('div', {
                          attrs: {
                              class: `${prefixCls}-head-title`
                          },
                          domProps: {
                              innerHTML: this.title
                          }
                      })
                  ]);
              }
  
77cf1cd5   Aresn   $Modal support SSR
100
101
102
              return h(Modal, {
                  props: Object.assign({}, _props, {
                      width: this.width,
34324f28   zhigang.li   add closable prop...
103
104
                      scrollable: this.scrollable,
                      closable: this.closable
77cf1cd5   Aresn   $Modal support SSR
105
106
107
108
109
110
111
                  }),
                  domProps: {
                      value: this.visible
                  },
                  on: {
                      input: (status) => {
                          this.visible = status;
f0de3cc7   梁灏   ref #5452
112
113
                      },
                      'on-cancel': this.cancel
77cf1cd5   Aresn   $Modal support SSR
114
115
116
117
118
119
120
                  }
              }, [
                  h('div', {
                      attrs: {
                          class: prefixCls
                      }
                  }, [
172e4396   梁灏   update $Modal wit...
121
                      head_render,
3ef24d5f   Aresn   $Modal support re...
122
                      body_render,
77cf1cd5   Aresn   $Modal support SSR
123
124
125
126
127
128
129
130
                      h('div', {
                          attrs: {
                              class: `${prefixCls}-footer`
                          }
                      }, footerVNodes)
                  ])
              ]);
          },
be966e9f   梁灏   add Modal component
131
132
133
          computed: {
              iconTypeCls () {
                  return [
f4273043   梁灏   update $Modal style
134
135
                      `${prefixCls}-head-icon`,
                      `${prefixCls}-head-icon-${this.iconType}`
b0893113   jingsam   :art: add eslint
136
                  ];
be966e9f   梁灏   add Modal component
137
138
139
140
141
              },
              iconNameCls () {
                  return [
                      'ivu-icon',
                      `ivu-icon-${this.iconName}`
b0893113   jingsam   :art: add eslint
142
                  ];
e5337c81   梁灏   fixed some compon...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
              },
              localeOkText () {
                  if (this.okText) {
                      return this.okText;
                  } else {
                      return this.t('i.modal.okText');
                  }
              },
              localeCancelText () {
                  if (this.cancelText) {
                      return this.cancelText;
                  } else {
                      return this.t('i.modal.cancelText');
                  }
be966e9f   梁灏   add Modal component
157
158
159
160
              }
          },
          methods: {
              cancel () {
6259471f   梁灏   support Modal
161
                  this.$children[0].visible = false;
be966e9f   梁灏   add Modal component
162
163
164
165
166
167
168
169
                  this.buttonLoading = false;
                  this.onCancel();
                  this.remove();
              },
              ok () {
                  if (this.loading) {
                      this.buttonLoading = true;
                  } else {
6259471f   梁灏   support Modal
170
                      this.$children[0].visible = false;
be966e9f   梁灏   add Modal component
171
172
173
174
175
176
177
178
179
180
181
                      this.remove();
                  }
                  this.onOk();
              },
              remove () {
                  setTimeout(() => {
                      this.destroy();
                  }, 300);
              },
              destroy () {
                  this.$destroy();
6259471f   梁灏   support Modal
182
                  document.body.removeChild(this.$el);
be966e9f   梁灏   add Modal component
183
184
185
186
187
188
                  this.onRemove();
              },
              onOk () {},
              onCancel () {},
              onRemove () {}
          }
77cf1cd5   Aresn   $Modal support SSR
189
190
191
192
193
      });
  
      const component = Instance.$mount();
      document.body.appendChild(component.$el);
      const modal = Instance.$children[0];
be966e9f   梁灏   add Modal component
194
195
196
197
198
199
200
201
  
      return {
          show (props) {
              modal.$parent.showCancel = props.showCancel;
              modal.$parent.iconType = props.icon;
  
              switch (props.icon) {
                  case 'info':
37665e29   梁灏   update Modal Icons
202
                      modal.$parent.iconName = 'ios-information-circle';
be966e9f   梁灏   add Modal component
203
204
                      break;
                  case 'success':
37665e29   梁灏   update Modal Icons
205
                      modal.$parent.iconName = 'ios-checkmark-circle';
be966e9f   梁灏   add Modal component
206
207
                      break;
                  case 'warning':
37665e29   梁灏   update Modal Icons
208
                      modal.$parent.iconName = 'ios-alert';
be966e9f   梁灏   add Modal component
209
210
                      break;
                  case 'error':
37665e29   梁灏   update Modal Icons
211
                      modal.$parent.iconName = 'ios-close-circle';
be966e9f   梁灏   add Modal component
212
213
                      break;
                  case 'confirm':
37665e29   梁灏   update Modal Icons
214
                      modal.$parent.iconName = 'ios-help-circle';
be966e9f   梁灏   add Modal component
215
216
217
218
219
220
221
                      break;
              }
  
              if ('width' in props) {
                  modal.$parent.width = props.width;
              }
  
34324f28   zhigang.li   add closable prop...
222
223
224
225
              if ('closable' in props) {
                  modal.$parent.closable = props.closable;
              }
  
be966e9f   梁灏   add Modal component
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
              if ('title' in props) {
                  modal.$parent.title = props.title;
              }
  
              if ('content' in props) {
                  modal.$parent.body = props.content;
              }
  
              if ('okText' in props) {
                  modal.$parent.okText = props.okText;
              }
  
              if ('cancelText' in props) {
                  modal.$parent.cancelText = props.cancelText;
              }
  
              if ('onCancel' in props) {
                  modal.$parent.onCancel = props.onCancel;
              }
  
              if ('onOk' in props) {
                  modal.$parent.onOk = props.onOk;
              }
  
              // async for ok
              if ('loading' in props) {
                  modal.$parent.loading = props.loading;
              }
  
a87da689   Rijn   added scrollable ...
255
256
257
258
              if ('scrollable' in props) {
                  modal.$parent.scrollable = props.scrollable;
              }
  
be966e9f   梁灏   add Modal component
259
260
261
262
263
264
265
266
267
268
269
              // notice when component destroy
              modal.$parent.onRemove = props.onRemove;
  
              modal.visible = true;
          },
          remove () {
              modal.visible = false;
              modal.$parent.buttonLoading = false;
              modal.$parent.remove();
          },
          component: modal
b0893113   jingsam   :art: add eslint
270
      };
be966e9f   梁灏   add Modal component
271
272
273
  };
  
  export default Modal;