Blame view

src/components/poptip/poptip.vue 10.8 KB
9699c270   梁灏   add Poptip component
1
2
3
4
5
  <template>
      <div
          :class="classes"
          @mouseenter="handleMouseenter"
          @mouseleave="handleMouseleave"
26369639   Graham Fairweather   Update v-click-ou...
6
          v-click-outside="handleClose">
9699c270   梁灏   add Poptip component
7
          <div
d6342fe1   jingsam   fixed ie bug
8
              :class="[prefixCls + '-rel']"
79288d43   梁灏   support Poptip & ...
9
              ref="reference"
9699c270   梁灏   add Poptip component
10
              @click="handleClick"
071506fc   梁灏   optimize Poptip f...
11
12
              @mousedown="handleFocus(false)"
              @mouseup="handleBlur(false)">
9699c270   梁灏   add Poptip component
13
14
              <slot></slot>
          </div>
79288d43   梁灏   support Poptip & ...
15
          <transition name="fade">
fcf3cace   梁灏   Poptip & Tooltip ...
16
              <div
1d83a8aa   梁灏   fixed #1612
17
                  :class="popperClasses"
fcf3cace   梁灏   Poptip & Tooltip ...
18
19
20
                  :style="styles"
                  ref="popper"
                  v-show="visible"
b75ad4a1   梁灏   fixed #1700
21
                  @click="handleTransferClick"
fcf3cace   梁灏   Poptip & Tooltip ...
22
23
                  @mouseenter="handleMouseenter"
                  @mouseleave="handleMouseleave"
548eac43   梁灏   fixed #1387 and u...
24
25
                  :data-transfer="transfer"
                  v-transfer-dom>
79288d43   梁灏   support Poptip & ...
26
27
28
29
                  <div :class="[prefixCls + '-content']">
                      <div :class="[prefixCls + '-arrow']"></div>
                      <div :class="[prefixCls + '-inner']" v-if="confirm">
                          <div :class="[prefixCls + '-body']">
e64ee85c   梁灏   update Poptip Icons
30
                              <i class="ivu-icon ivu-icon-ios-help-circle"></i>
79288d43   梁灏   support Poptip & ...
31
32
33
                              <div :class="[prefixCls + '-body-message']"><slot name="title">{{ title }}</slot></div>
                          </div>
                          <div :class="[prefixCls + '-footer']">
e5337c81   梁灏   fixed some compon...
34
35
                              <i-button type="text" size="small" @click.native="cancel">{{ localeCancelText }}</i-button>
                              <i-button type="primary" size="small" @click.native="ok">{{ localeOkText }}</i-button>
79288d43   梁灏   support Poptip & ...
36
                          </div>
9699c270   梁灏   add Poptip component
37
                      </div>
79288d43   梁灏   support Poptip & ...
38
                      <div :class="[prefixCls + '-inner']" v-if="!confirm">
a980832f   梁灏   Poptip add paddin...
39
40
                          <div :class="[prefixCls + '-title']" :style="contentPaddingStyle" v-if="showTitle" ref="title"><slot name="title"><div :class="[prefixCls + '-title-inner']">{{ title }}</div></slot></div>
                          <div :class="[prefixCls + '-body']" :style="contentPaddingStyle">
130ed889   梁灏   Poptip add prop w...
41
                              <div :class="contentClasses"><slot name="content"><div :class="[prefixCls + '-body-content-inner']">{{ content }}</div></slot></div>
79288d43   梁灏   support Poptip & ...
42
                          </div>
613f5243   梁灏   update Poptip
43
                      </div>
9699c270   梁灏   add Poptip component
44
45
                  </div>
              </div>
79288d43   梁灏   support Poptip & ...
46
          </transition>
9699c270   梁灏   add Poptip component
47
48
49
50
      </div>
  </template>
  <script>
      import Popper from '../base/popper';
4b7138b9   梁灏   fixed some bugs
51
      import iButton from '../button/button.vue';
26369639   Graham Fairweather   Update v-click-ou...
52
      import {directive as clickOutside} from 'v-click-outside-x';
fcf3cace   梁灏   Poptip & Tooltip ...
53
      import TransferDom from '../../directives/transfer-dom';
9699c270   梁灏   add Poptip component
54
      import { oneOf } from '../../utils/assist';
7bafe9d9   梁灏   fixes #4453 #4480...
55
      import { transferIndex, transferIncrease } from '../../utils/transfer-queue';
e5337c81   梁灏   fixed some compon...
56
      import Locale from '../../mixins/locale';
9699c270   梁灏   add Poptip component
57
58
59
60
  
      const prefixCls = 'ivu-poptip';
  
      export default {
79288d43   梁灏   support Poptip & ...
61
          name: 'Poptip',
e5337c81   梁灏   fixed some compon...
62
          mixins: [ Popper, Locale ],
26369639   Graham Fairweather   Update v-click-ou...
63
          directives: { clickOutside, TransferDom },
4b7138b9   梁灏   fixed some bugs
64
          components: { iButton },
9699c270   梁灏   add Poptip component
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
          props: {
              trigger: {
                  validator (value) {
                      return oneOf(value, ['click', 'focus', 'hover']);
                  },
                  default: 'click'
              },
              placement: {
                  validator (value) {
                      return oneOf(value, ['top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end']);
                  },
                  default: 'top'
              },
              title: {
                  type: [String, Number]
              },
              content: {
                  type: [String, Number],
                  default: ''
              },
              width: {
                  type: [String, Number]
              },
              confirm: {
                  type: Boolean,
                  default: false
              },
              okText: {
e5337c81   梁灏   fixed some compon...
93
                  type: String
9699c270   梁灏   add Poptip component
94
95
              },
              cancelText: {
e5337c81   梁灏   fixed some compon...
96
                  type: String
fcf3cace   梁灏   Poptip & Tooltip ...
97
98
99
              },
              transfer: {
                  type: Boolean,
d54c9795   梁灏   Poptip support gl...
100
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
101
                      return !this.$IVIEW || this.$IVIEW.transfer === '' ? false : this.$IVIEW.transfer;
d54c9795   梁灏   Poptip support gl...
102
                  }
19c208d3   梁灏   Poptip add prop `...
103
104
105
              },
              popperClass: {
                  type: String
130ed889   梁灏   Poptip add prop w...
106
107
108
109
              },
              wordWrap: {
                  type: Boolean,
                  default: false
a980832f   梁灏   Poptip add paddin...
110
111
112
113
              },
              // default by css: 8px 16px
              padding: {
                  type: String
6a971099   梁灏   Poptip add disabl...
114
115
116
117
118
              },
              // 3.4.0
              disabled: {
                  type: Boolean,
                  default: false
9699c270   梁灏   add Poptip component
119
120
121
122
              }
          },
          data () {
              return {
88bb7c92   梁灏   fix bug of Poptip
123
                  prefixCls: prefixCls,
071506fc   梁灏   optimize Poptip f...
124
                  showTitle: true,
b75ad4a1   梁灏   fixed #1700
125
126
                  isInput: false,
                  disableCloseUnderTransfer: false,  // transfer 模式下,点击 slot 也会触发关闭
7bafe9d9   梁灏   fixes #4453 #4480...
127
                  tIndex: this.handleGetIndex()
b0893113   jingsam   :art: add eslint
128
              };
9699c270   梁灏   add Poptip component
129
130
131
132
          },
          computed: {
              classes () {
                  return [
4b7138b9   梁灏   fixed some bugs
133
                      `${prefixCls}`,
9699c270   梁灏   add Poptip component
134
                      {
4b7138b9   梁灏   fixed some bugs
135
                          [`${prefixCls}-confirm`]: this.confirm
9699c270   梁灏   add Poptip component
136
                      }
b0893113   jingsam   :art: add eslint
137
                  ];
9699c270   梁灏   add Poptip component
138
              },
1d83a8aa   梁灏   fixed #1612
139
140
141
142
              popperClasses () {
                  return [
                      `${prefixCls}-popper`,
                      {
19c208d3   梁灏   Poptip add prop `...
143
144
                          [`${prefixCls}-confirm`]: this.transfer && this.confirm,
                          [`${this.popperClass}`]: !!this.popperClass
1d83a8aa   梁灏   fixed #1612
145
146
147
                      }
                  ];
              },
9699c270   梁灏   add Poptip component
148
149
150
              styles () {
                  let style = {};
  
b0893113   jingsam   :art: add eslint
151
                  if (this.width) {
4b7138b9   梁灏   fixed some bugs
152
                      style.width = `${this.width}px`;
9699c270   梁灏   add Poptip component
153
                  }
7bafe9d9   梁灏   fixes #4453 #4480...
154
155
156
  
                  if (this.transfer) style['z-index'] = 1060 + this.tIndex;
  
9699c270   梁灏   add Poptip component
157
                  return style;
e5337c81   梁灏   fixed some compon...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
              },
              localeOkText () {
                  if (this.okText === undefined) {
                      return this.t('i.poptip.okText');
                  } else {
                      return this.okText;
                  }
              },
              localeCancelText () {
                  if (this.cancelText === undefined) {
                      return this.t('i.poptip.cancelText');
                  } else {
                      return this.cancelText;
                  }
130ed889   梁灏   Poptip add prop w...
172
173
174
175
176
177
178
179
              },
              contentClasses () {
                  return [
                      `${prefixCls}-body-content`,
                      {
                          [`${prefixCls}-body-content-word-wrap`]: this.wordWrap
                      }
                  ];
a980832f   梁灏   Poptip add paddin...
180
181
182
              },
              contentPaddingStyle () {
                  const styles = {};
855d4940   梁灏   Tag add more colo...
183
                  if (this.padding !== '') styles['padding'] = this.padding;
a980832f   梁灏   Poptip add paddin...
184
                  return styles;
7bafe9d9   梁灏   fixes #4453 #4480...
185
              },
9699c270   梁灏   add Poptip component
186
187
188
          },
          methods: {
              handleClick () {
6a971099   梁灏   Poptip add disabl...
189
190
                  if (this.disabled) return;
  
9699c270   梁灏   add Poptip component
191
192
193
194
195
196
197
198
199
                  if (this.confirm) {
                      this.visible = !this.visible;
                      return true;
                  }
                  if (this.trigger !== 'click') {
                      return false;
                  }
                  this.visible = !this.visible;
              },
b75ad4a1   梁灏   fixed #1700
200
201
202
              handleTransferClick () {
                  if (this.transfer) this.disableCloseUnderTransfer = true;
              },
9699c270   梁灏   add Poptip component
203
              handleClose () {
b75ad4a1   梁灏   fixed #1700
204
205
206
207
                  if (this.disableCloseUnderTransfer) {
                      this.disableCloseUnderTransfer = false;
                      return false;
                  }
9699c270   梁灏   add Poptip component
208
209
210
211
212
213
214
215
216
                  if (this.confirm) {
                      this.visible = false;
                      return true;
                  }
                  if (this.trigger !== 'click') {
                      return false;
                  }
                  this.visible = false;
              },
071506fc   梁灏   optimize Poptip f...
217
              handleFocus (fromInput = true) {
6a971099   梁灏   Poptip add disabl...
218
219
                  if (this.disabled) return;
  
071506fc   梁灏   optimize Poptip f...
220
                  if (this.trigger !== 'focus' || this.confirm || (this.isInput && !fromInput)) {
9699c270   梁灏   add Poptip component
221
222
223
224
                      return false;
                  }
                  this.visible = true;
              },
071506fc   梁灏   optimize Poptip f...
225
226
              handleBlur (fromInput = true) {
                  if (this.trigger !== 'focus' || this.confirm || (this.isInput && !fromInput)) {
9699c270   梁灏   add Poptip component
227
228
229
230
231
                      return false;
                  }
                  this.visible = false;
              },
              handleMouseenter () {
6a971099   梁灏   Poptip add disabl...
232
233
                  if (this.disabled) return;
                  
9699c270   梁灏   add Poptip component
234
235
236
                  if (this.trigger !== 'hover' || this.confirm) {
                      return false;
                  }
fcf3cace   梁灏   Poptip & Tooltip ...
237
238
239
240
                  if (this.enterTimer) clearTimeout(this.enterTimer);
                  this.enterTimer = setTimeout(() => {
                      this.visible = true;
                  }, 100);
9699c270   梁灏   add Poptip component
241
242
243
244
245
              },
              handleMouseleave () {
                  if (this.trigger !== 'hover' || this.confirm) {
                      return false;
                  }
fcf3cace   梁灏   Poptip & Tooltip ...
246
247
248
249
250
251
                  if (this.enterTimer) {
                      clearTimeout(this.enterTimer);
                      this.enterTimer = setTimeout(() => {
                          this.visible = false;
                      }, 100);
                  }
9699c270   梁灏   add Poptip component
252
253
254
255
256
257
258
259
              },
              cancel () {
                  this.visible = false;
                  this.$emit('on-cancel');
              },
              ok () {
                  this.visible = false;
                  this.$emit('on-ok');
071506fc   梁灏   optimize Poptip f...
260
261
              },
              getInputChildren () {
79288d43   梁灏   support Poptip & ...
262
263
                  const $input = this.$refs.reference.querySelectorAll('input');
                  const $textarea = this.$refs.reference.querySelectorAll('textarea');
071506fc   梁灏   optimize Poptip f...
264
265
266
267
268
269
270
271
272
                  let $children = null;
  
                  if ($input.length) {
                      $children = $input[0];
                  } else if ($textarea.length) {
                      $children = $textarea[0];
                  }
  
                  return $children;
7bafe9d9   梁灏   fixes #4453 #4480...
273
274
275
276
277
278
279
              },
              handleGetIndex () {
                  transferIncrease();
                  return transferIndex;
              },
              handleIndexIncrease () {
                  this.tIndex = this.handleGetIndex();
9699c270   梁灏   add Poptip component
280
              }
88bb7c92   梁灏   fix bug of Poptip
281
          },
79288d43   梁灏   support Poptip & ...
282
          mounted () {
88bb7c92   梁灏   fix bug of Poptip
283
              if (!this.confirm) {
79288d43   梁灏   support Poptip & ...
284
  //                this.showTitle = this.$refs.title.innerHTML != `<div class="${prefixCls}-title-inner"></div>`;
8e7ac210   Aresn   fixed #1222
285
                  this.showTitle = (this.$slots.title !== undefined) || this.title;
88bb7c92   梁灏   fix bug of Poptip
286
              }
071506fc   梁灏   optimize Poptip f...
287
288
              // if trigger and children is input or textarea,listen focus & blur event
              if (this.trigger === 'focus') {
b8adf5cf   Aresn   fixed #1101
289
290
291
                  this.$nextTick(() => {
                      const $children = this.getInputChildren();
                      if ($children) {
8e7ac210   Aresn   fixed #1222
292
                          this.isInput = true;
b8adf5cf   Aresn   fixed #1101
293
294
295
296
                          $children.addEventListener('focus', this.handleFocus, false);
                          $children.addEventListener('blur', this.handleBlur, false);
                      }
                  });
071506fc   梁灏   optimize Poptip f...
297
298
299
300
301
302
303
304
              }
          },
          beforeDestroy () {
              const $children = this.getInputChildren();
              if ($children) {
                  $children.removeEventListener('focus', this.handleFocus, false);
                  $children.removeEventListener('blur', this.handleBlur, false);
              }
9699c270   梁灏   add Poptip component
305
          }
b0893113   jingsam   :art: add eslint
306
      };
d6342fe1   jingsam   fixed ie bug
307
  </script>