Blame view

src/components/poptip/poptip.vue 8.53 KB
9699c270   梁灏   add Poptip component
1
2
3
4
5
6
7
  <template>
      <div
          :class="classes"
          @mouseenter="handleMouseenter"
          @mouseleave="handleMouseleave"
          v-clickoutside="handleClose">
          <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
17
18
19
20
21
22
              <div
                  :class="[prefixCls + '-popper']"
                  :style="styles"
                  ref="popper"
                  v-show="visible"
                  @mouseenter="handleMouseenter"
                  @mouseleave="handleMouseleave"
548eac43   梁灏   fixed #1387 and u...
23
24
                  :data-transfer="transfer"
                  v-transfer-dom>
79288d43   梁灏   support Poptip & ...
25
26
27
28
29
30
31
32
                  <div :class="[prefixCls + '-content']">
                      <div :class="[prefixCls + '-arrow']"></div>
                      <div :class="[prefixCls + '-inner']" v-if="confirm">
                          <div :class="[prefixCls + '-body']">
                              <i class="ivu-icon ivu-icon-help-circled"></i>
                              <div :class="[prefixCls + '-body-message']"><slot name="title">{{ title }}</slot></div>
                          </div>
                          <div :class="[prefixCls + '-footer']">
e5337c81   梁灏   fixed some compon...
33
34
                              <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 & ...
35
                          </div>
9699c270   梁灏   add Poptip component
36
                      </div>
79288d43   梁灏   support Poptip & ...
37
38
39
40
41
                      <div :class="[prefixCls + '-inner']" v-if="!confirm">
                          <div :class="[prefixCls + '-title']" v-if="showTitle" ref="title"><slot name="title"><div :class="[prefixCls + '-title-inner']">{{ title }}</div></slot></div>
                          <div :class="[prefixCls + '-body']">
                              <div :class="[prefixCls + '-body-content']"><slot name="content"><div :class="[prefixCls + '-body-content-inner']">{{ content }}</div></slot></div>
                          </div>
613f5243   梁灏   update Poptip
42
                      </div>
9699c270   梁灏   add Poptip component
43
44
                  </div>
              </div>
79288d43   梁灏   support Poptip & ...
45
          </transition>
9699c270   梁灏   add Poptip component
46
47
48
49
      </div>
  </template>
  <script>
      import Popper from '../base/popper';
4b7138b9   梁灏   fixed some bugs
50
      import iButton from '../button/button.vue';
9699c270   梁灏   add Poptip component
51
      import clickoutside from '../../directives/clickoutside';
fcf3cace   梁灏   Poptip & Tooltip ...
52
      import TransferDom from '../../directives/transfer-dom';
9699c270   梁灏   add Poptip component
53
      import { oneOf } from '../../utils/assist';
e5337c81   梁灏   fixed some compon...
54
      import Locale from '../../mixins/locale';
9699c270   梁灏   add Poptip component
55
56
57
58
  
      const prefixCls = 'ivu-poptip';
  
      export default {
79288d43   梁灏   support Poptip & ...
59
          name: 'Poptip',
e5337c81   梁灏   fixed some compon...
60
          mixins: [ Popper, Locale ],
fcf3cace   梁灏   Poptip & Tooltip ...
61
          directives: { clickoutside, TransferDom },
4b7138b9   梁灏   fixed some bugs
62
          components: { iButton },
9699c270   梁灏   add Poptip component
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
          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...
91
                  type: String
9699c270   梁灏   add Poptip component
92
93
              },
              cancelText: {
e5337c81   梁灏   fixed some compon...
94
                  type: String
fcf3cace   梁灏   Poptip & Tooltip ...
95
96
97
98
              },
              transfer: {
                  type: Boolean,
                  default: false
9699c270   梁灏   add Poptip component
99
100
101
102
              }
          },
          data () {
              return {
88bb7c92   梁灏   fix bug of Poptip
103
                  prefixCls: prefixCls,
071506fc   梁灏   optimize Poptip f...
104
105
                  showTitle: true,
                  isInput: false
b0893113   jingsam   :art: add eslint
106
              };
9699c270   梁灏   add Poptip component
107
108
109
110
          },
          computed: {
              classes () {
                  return [
4b7138b9   梁灏   fixed some bugs
111
                      `${prefixCls}`,
9699c270   梁灏   add Poptip component
112
                      {
4b7138b9   梁灏   fixed some bugs
113
                          [`${prefixCls}-confirm`]: this.confirm
9699c270   梁灏   add Poptip component
114
                      }
b0893113   jingsam   :art: add eslint
115
                  ];
9699c270   梁灏   add Poptip component
116
117
118
119
              },
              styles () {
                  let style = {};
  
b0893113   jingsam   :art: add eslint
120
                  if (this.width) {
4b7138b9   梁灏   fixed some bugs
121
                      style.width = `${this.width}px`;
9699c270   梁灏   add Poptip component
122
123
                  }
                  return style;
e5337c81   梁灏   fixed some compon...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
              },
              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;
                  }
9699c270   梁灏   add Poptip component
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
              }
          },
          methods: {
              handleClick () {
                  if (this.confirm) {
                      this.visible = !this.visible;
                      return true;
                  }
                  if (this.trigger !== 'click') {
                      return false;
                  }
                  this.visible = !this.visible;
              },
              handleClose () {
                  if (this.confirm) {
                      this.visible = false;
                      return true;
                  }
                  if (this.trigger !== 'click') {
                      return false;
                  }
                  this.visible = false;
              },
071506fc   梁灏   optimize Poptip f...
161
162
              handleFocus (fromInput = true) {
                  if (this.trigger !== 'focus' || this.confirm || (this.isInput && !fromInput)) {
9699c270   梁灏   add Poptip component
163
164
165
166
                      return false;
                  }
                  this.visible = true;
              },
071506fc   梁灏   optimize Poptip f...
167
168
              handleBlur (fromInput = true) {
                  if (this.trigger !== 'focus' || this.confirm || (this.isInput && !fromInput)) {
9699c270   梁灏   add Poptip component
169
170
171
172
173
174
175
176
                      return false;
                  }
                  this.visible = false;
              },
              handleMouseenter () {
                  if (this.trigger !== 'hover' || this.confirm) {
                      return false;
                  }
fcf3cace   梁灏   Poptip & Tooltip ...
177
178
179
180
                  if (this.enterTimer) clearTimeout(this.enterTimer);
                  this.enterTimer = setTimeout(() => {
                      this.visible = true;
                  }, 100);
9699c270   梁灏   add Poptip component
181
182
183
184
185
              },
              handleMouseleave () {
                  if (this.trigger !== 'hover' || this.confirm) {
                      return false;
                  }
fcf3cace   梁灏   Poptip & Tooltip ...
186
187
188
189
190
191
                  if (this.enterTimer) {
                      clearTimeout(this.enterTimer);
                      this.enterTimer = setTimeout(() => {
                          this.visible = false;
                      }, 100);
                  }
9699c270   梁灏   add Poptip component
192
193
194
195
196
197
198
199
              },
              cancel () {
                  this.visible = false;
                  this.$emit('on-cancel');
              },
              ok () {
                  this.visible = false;
                  this.$emit('on-ok');
071506fc   梁灏   optimize Poptip f...
200
201
              },
              getInputChildren () {
79288d43   梁灏   support Poptip & ...
202
203
                  const $input = this.$refs.reference.querySelectorAll('input');
                  const $textarea = this.$refs.reference.querySelectorAll('textarea');
071506fc   梁灏   optimize Poptip f...
204
205
206
207
208
209
210
211
212
                  let $children = null;
  
                  if ($input.length) {
                      $children = $input[0];
                  } else if ($textarea.length) {
                      $children = $textarea[0];
                  }
  
                  return $children;
9699c270   梁灏   add Poptip component
213
              }
88bb7c92   梁灏   fix bug of Poptip
214
          },
79288d43   梁灏   support Poptip & ...
215
          mounted () {
88bb7c92   梁灏   fix bug of Poptip
216
              if (!this.confirm) {
79288d43   梁灏   support Poptip & ...
217
  //                this.showTitle = this.$refs.title.innerHTML != `<div class="${prefixCls}-title-inner"></div>`;
8e7ac210   Aresn   fixed #1222
218
                  this.showTitle = (this.$slots.title !== undefined) || this.title;
88bb7c92   梁灏   fix bug of Poptip
219
              }
071506fc   梁灏   optimize Poptip f...
220
221
              // if trigger and children is input or textarea,listen focus & blur event
              if (this.trigger === 'focus') {
b8adf5cf   Aresn   fixed #1101
222
223
224
                  this.$nextTick(() => {
                      const $children = this.getInputChildren();
                      if ($children) {
8e7ac210   Aresn   fixed #1222
225
                          this.isInput = true;
b8adf5cf   Aresn   fixed #1101
226
227
228
229
                          $children.addEventListener('focus', this.handleFocus, false);
                          $children.addEventListener('blur', this.handleBlur, false);
                      }
                  });
071506fc   梁灏   optimize Poptip f...
230
231
232
233
234
235
236
237
              }
          },
          beforeDestroy () {
              const $children = this.getInputChildren();
              if ($children) {
                  $children.removeEventListener('focus', this.handleFocus, false);
                  $children.removeEventListener('blur', this.handleBlur, false);
              }
9699c270   梁灏   add Poptip component
238
          }
b0893113   jingsam   :art: add eslint
239
      };
d6342fe1   jingsam   fixed ie bug
240
  </script>