Blame view

src/components/modal/confirm.js 8.3 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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
      // let props = '';
      // Object.keys(_props).forEach(prop => {
      //     props += ' :' + camelcaseToHyphen(prop) + '=' + prop;
      // });
      //
      // const div = document.createElement('div');
      // div.innerHTML = `
      //     <Modal${props} v-model="visible" :width="width" :scrollable="scrollable">
      //         <div class="${prefixCls}">
      //             <div class="${prefixCls}-head">
      //                 <div class="${prefixCls}-head-title" v-html="title"></div>
      //             </div>
      //             <div class="${prefixCls}-body">
      //                 <div :class="iconTypeCls"><i :class="iconNameCls"></i></div>
      //                 <div v-html="body"></div>
      //             </div>
      //             <div class="${prefixCls}-footer">
      //                 <i-button type="text" size="large" v-if="showCancel" @click.native="cancel">{{ localeCancelText }}</i-button>
      //                 <i-button type="primary" size="large" :loading="buttonLoading" @click.native="ok">{{ localeOkText }}</i-button>
      //             </div>
      //         </div>
      //     </Modal>
      // `;
      // document.body.appendChild(div);
be966e9f   梁灏   add Modal component
35
  
77cf1cd5   Aresn   $Modal support SSR
36
      const Instance = new Vue({
e5337c81   梁灏   fixed some compon...
37
          mixins: [ Locale ],
77cf1cd5   Aresn   $Modal support SSR
38
          data: Object.assign({}, _props, {
be966e9f   梁灏   add Modal component
39
40
41
42
43
44
              visible: false,
              width: 416,
              title: '',
              body: '',
              iconType: '',
              iconName: '',
e5337c81   梁灏   fixed some compon...
45
46
              okText: undefined,
              cancelText: undefined,
be966e9f   梁灏   add Modal component
47
48
              showCancel: false,
              loading: false,
a87da689   Rijn   added scrollable ...
49
50
              buttonLoading: false,
              scrollable: false
be966e9f   梁灏   add Modal component
51
          }),
77cf1cd5   Aresn   $Modal support SSR
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
          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));
  
              return h(Modal, {
                  props: Object.assign({}, _props, {
                      width: this.width,
                      scrollable: this.scrollable
                  }),
                  domProps: {
                      value: this.visible
                  },
                  on: {
                      input: (status) => {
                          this.visible = status;
                      }
                  }
              }, [
                  h('div', {
                      attrs: {
                          class: prefixCls
                      }
                  }, [
                      h('div', {
                          attrs: {
                              class: `${prefixCls}-head`
                          }
                      }, h('div', {
                          attrs: {
                              class: `${prefixCls}-head-title`
                          },
                          domProps: {
                              innerHTML: this.title
                          }
                      })),
                      h('div', {
                          attrs: {
                              class: `${prefixCls}-body`
                          }
                      }, [
                          h('div', {
                              class: this.iconTypeCls
                          }, [
                              h('i', {
                                  class: this.iconNameCls
                              })
                          ]),
                          h('div', {
                              domProps: {
                                  innerHTML: this.body
                              }
                          })
                      ]),
                      h('div', {
                          attrs: {
                              class: `${prefixCls}-footer`
                          }
                      }, footerVNodes)
                  ])
              ]);
          },
be966e9f   梁灏   add Modal component
133
134
135
          computed: {
              iconTypeCls () {
                  return [
66993732   梁灏   update Modal、Poptip
136
137
                      `${prefixCls}-body-icon`,
                      `${prefixCls}-body-icon-${this.iconType}`
b0893113   jingsam   :art: add eslint
138
                  ];
be966e9f   梁灏   add Modal component
139
140
141
142
143
              },
              iconNameCls () {
                  return [
                      'ivu-icon',
                      `ivu-icon-${this.iconName}`
b0893113   jingsam   :art: add eslint
144
                  ];
e5337c81   梁灏   fixed some compon...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
              },
              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
159
160
161
162
              }
          },
          methods: {
              cancel () {
6259471f   梁灏   support Modal
163
                  this.$children[0].visible = false;
be966e9f   梁灏   add Modal component
164
165
166
167
168
169
170
171
                  this.buttonLoading = false;
                  this.onCancel();
                  this.remove();
              },
              ok () {
                  if (this.loading) {
                      this.buttonLoading = true;
                  } else {
6259471f   梁灏   support Modal
172
                      this.$children[0].visible = false;
be966e9f   梁灏   add Modal component
173
174
175
176
177
178
179
180
181
182
183
                      this.remove();
                  }
                  this.onOk();
              },
              remove () {
                  setTimeout(() => {
                      this.destroy();
                  }, 300);
              },
              destroy () {
                  this.$destroy();
6259471f   梁灏   support Modal
184
                  document.body.removeChild(this.$el);
be966e9f   梁灏   add Modal component
185
186
187
188
189
190
                  this.onRemove();
              },
              onOk () {},
              onCancel () {},
              onRemove () {}
          }
77cf1cd5   Aresn   $Modal support SSR
191
192
193
194
195
      });
  
      const component = Instance.$mount();
      document.body.appendChild(component.$el);
      const modal = Instance.$children[0];
be966e9f   梁灏   add Modal component
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
  
      return {
          show (props) {
              modal.$parent.showCancel = props.showCancel;
              modal.$parent.iconType = props.icon;
  
              switch (props.icon) {
                  case 'info':
                      modal.$parent.iconName = 'information-circled';
                      break;
                  case 'success':
                      modal.$parent.iconName = 'checkmark-circled';
                      break;
                  case 'warning':
                      modal.$parent.iconName = 'android-alert';
                      break;
                  case 'error':
                      modal.$parent.iconName = 'close-circled';
                      break;
                  case 'confirm':
                      modal.$parent.iconName = 'help-circled';
                      break;
              }
  
              if ('width' in props) {
                  modal.$parent.width = props.width;
              }
  
              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 ...
253
254
255
256
              if ('scrollable' in props) {
                  modal.$parent.scrollable = props.scrollable;
              }
  
be966e9f   梁灏   add Modal component
257
258
259
260
261
262
263
264
265
266
267
              // 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
268
      };
be966e9f   梁灏   add Modal component
269
270
271
  };
  
  export default Modal;