Blame view

src/components/dialog/confirm.js 4.96 KB
c571d9ec   梁灏   add a dialog to f...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
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
  import Vue from 'vue';
  import Modal from './dialog.vue';
  import Icon from '../icon/icon.vue';
  import iButton from '../button/button.vue';
  import { camelcaseToHyphen } from '../../utils/assist';
  
  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 = `
          <Modal${props} :visible.sync="visible" :width="width">
              <div class="${prefixCls}">
                  <div class="${prefixCls}-head">
                      <div :class="iconTypeCls"><i :class="iconNameCls"></i></div>
                      <div class="${prefixCls}-head-title">{{{ title }}}</div>
                  </div>
                  <div class="${prefixCls}-body">
                      {{{ body }}}
                  </div>
                  <div class="${prefixCls}-footer">
                      <i-button type="ghost" size="large" v-if="showCancel" @click="cancel">{{ cancelText }}</i-button>
                      <i-button type="primary" size="large" :loading="buttonLoading" @click="ok">{{ okText }}</i-button>
                  </div>
              </div>
          </Modal>
      `;
      document.body.appendChild(div);
  
      const modal = new Vue({
          el: div,
          components: { Modal, iButton, Icon },
          data: Object.assign(_props, {
              visible: false,
              width: 416,
              title: '',
              body: '',
              iconType: '',
              iconName: '',
              okText: '确定',
              cancelText: '取消',
              showCancel: false,
              loading: false,
              buttonLoading: false
          }),
          computed: {
              iconTypeCls () {
                  return [
                      `${prefixCls}-head-icon`,
                      `${prefixCls}-head-icon-${this.iconType}`
                  ]
              },
              iconNameCls () {
                  return [
                      'ivu-icon',
                      `ivu-icon-${this.iconName}`
                  ]
              }
          },
          methods: {
              cancel () {
                  this.visible = false;
                  this.buttonLoading = false;
                  this.onCancel();
                  this.remove();
              },
              ok () {
                  if (this.loading) {
                      this.buttonLoading = true;
                  } else {
                      this.visible = false;
                      this.remove();
                  }
                  this.onOk();
              },
              remove () {
                  setTimeout(() => {
                      this.destroy();
                  }, 300);
              },
              destroy () {
                  this.$destroy();
                  document.body.removeChild(div);
                  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;
              }
  
              // 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
      }
  };
  
  export default Modal;