Blame view

src/components/modal/confirm.js 5.7 KB
be966e9f   梁灏   add Modal component
1
2
3
  import Vue from 'vue';
  import Modal from './modal.vue';
  import Icon from '../icon/icon.vue';
4b7138b9   梁灏   fixed some bugs
4
  import iButton from '../button/button.vue';
be966e9f   梁灏   add Modal component
5
  import { camelcaseToHyphen } from '../../utils/assist';
e5337c81   梁灏   fixed some compon...
6
  import Locale from '../../mixins/locale';
be966e9f   梁灏   add Modal component
7
8
9
10
11
12
13
14
15
16
17
18
19
  
  const prefixCls = 'ivu-modal-confirm';
  
  Modal.newInstance = properties => {
      const _props = properties || {};
  
      let props = '';
      Object.keys(_props).forEach(prop => {
          props += ' :' + camelcaseToHyphen(prop) + '=' + prop;
      });
  
      const div = document.createElement('div');
      div.innerHTML = `
6259471f   梁灏   support Modal
20
          <Modal${props} v-model="visible" :width="width" :scrollable="scrollable">
be966e9f   梁灏   add Modal component
21
22
              <div class="${prefixCls}">
                  <div class="${prefixCls}-head">
6259471f   梁灏   support Modal
23
                      <div class="${prefixCls}-head-title" v-html="title"></div>
be966e9f   梁灏   add Modal component
24
25
                  </div>
                  <div class="${prefixCls}-body">
66993732   梁灏   update Modal、Poptip
26
                      <div :class="iconTypeCls"><i :class="iconNameCls"></i></div>
6259471f   梁灏   support Modal
27
                      <div v-html="body"></div>
be966e9f   梁灏   add Modal component
28
29
                  </div>
                  <div class="${prefixCls}-footer">
e5337c81   梁灏   fixed some compon...
30
31
                      <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>
be966e9f   梁灏   add Modal component
32
33
34
35
36
37
38
39
                  </div>
              </div>
          </Modal>
      `;
      document.body.appendChild(div);
  
      const modal = new Vue({
          el: div,
e5337c81   梁灏   fixed some compon...
40
          mixins: [ Locale ],
4b7138b9   梁灏   fixed some bugs
41
          components: { Modal, iButton, Icon },
be966e9f   梁灏   add Modal component
42
43
44
45
46
47
48
          data: Object.assign(_props, {
              visible: false,
              width: 416,
              title: '',
              body: '',
              iconType: '',
              iconName: '',
e5337c81   梁灏   fixed some compon...
49
50
              okText: undefined,
              cancelText: undefined,
be966e9f   梁灏   add Modal component
51
52
              showCancel: false,
              loading: false,
a87da689   Rijn   added scrollable ...
53
54
              buttonLoading: false,
              scrollable: false
be966e9f   梁灏   add Modal component
55
56
57
58
          }),
          computed: {
              iconTypeCls () {
                  return [
66993732   梁灏   update Modal、Poptip
59
60
                      `${prefixCls}-body-icon`,
                      `${prefixCls}-body-icon-${this.iconType}`
b0893113   jingsam   :art: add eslint
61
                  ];
be966e9f   梁灏   add Modal component
62
63
64
65
66
              },
              iconNameCls () {
                  return [
                      'ivu-icon',
                      `ivu-icon-${this.iconName}`
b0893113   jingsam   :art: add eslint
67
                  ];
e5337c81   梁灏   fixed some compon...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
              },
              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
82
83
84
85
              }
          },
          methods: {
              cancel () {
6259471f   梁灏   support Modal
86
                  this.$children[0].visible = false;
be966e9f   梁灏   add Modal component
87
88
89
90
91
92
93
94
                  this.buttonLoading = false;
                  this.onCancel();
                  this.remove();
              },
              ok () {
                  if (this.loading) {
                      this.buttonLoading = true;
                  } else {
6259471f   梁灏   support Modal
95
                      this.$children[0].visible = false;
be966e9f   梁灏   add Modal component
96
97
98
99
100
101
102
103
104
105
106
                      this.remove();
                  }
                  this.onOk();
              },
              remove () {
                  setTimeout(() => {
                      this.destroy();
                  }, 300);
              },
              destroy () {
                  this.$destroy();
6259471f   梁灏   support Modal
107
                  document.body.removeChild(this.$el);
be966e9f   梁灏   add Modal component
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
                  this.onRemove();
              },
              onOk () {},
              onCancel () {},
              onRemove () {}
          }
      }).$children[0];
  
      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 ...
172
173
174
175
              if ('scrollable' in props) {
                  modal.$parent.scrollable = props.scrollable;
              }
  
be966e9f   梁灏   add Modal component
176
177
178
179
180
181
182
183
184
185
186
              // 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
187
      };
be966e9f   梁灏   add Modal component
188
189
190
  };
  
  export default Modal;