Blame view

src/components/poptip/poptip.vue 7.39 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
16
17
18
19
20
21
22
23
24
25
26
27
          <transition name="fade">
              <div :class="[prefixCls + '-popper']" :style="styles" ref="popper" v-show="visible">
                  <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']">
                              <i-button type="text" size="small" @click.native="cancel">{{ cancelText }}</i-button>
                              <i-button type="primary" size="small" @click.native="ok">{{ okText }}</i-button>
                          </div>
9699c270   梁灏   add Poptip component
28
                      </div>
79288d43   梁灏   support Poptip & ...
29
30
31
32
33
                      <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
34
                      </div>
9699c270   梁灏   add Poptip component
35
36
                  </div>
              </div>
79288d43   梁灏   support Poptip & ...
37
          </transition>
9699c270   梁灏   add Poptip component
38
39
40
41
      </div>
  </template>
  <script>
      import Popper from '../base/popper';
4b7138b9   梁灏   fixed some bugs
42
      import iButton from '../button/button.vue';
9699c270   梁灏   add Poptip component
43
44
      import clickoutside from '../../directives/clickoutside';
      import { oneOf } from '../../utils/assist';
012cbf28   梁灏   update locale
45
      import { t } from '../../locale';
9699c270   梁灏   add Poptip component
46
47
48
49
  
      const prefixCls = 'ivu-poptip';
  
      export default {
79288d43   梁灏   support Poptip & ...
50
          name: 'Poptip',
9699c270   梁灏   add Poptip component
51
52
          mixins: [Popper],
          directives: { clickoutside },
4b7138b9   梁灏   fixed some bugs
53
          components: { iButton },
9699c270   梁灏   add Poptip component
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
          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: {
                  type: String,
012cbf28   梁灏   update locale
83
84
85
                  default () {
                      return t('i.poptip.okText');
                  }
9699c270   梁灏   add Poptip component
86
87
88
              },
              cancelText: {
                  type: String,
012cbf28   梁灏   update locale
89
90
91
                  default () {
                      return t('i.poptip.cancelText');
                  }
9699c270   梁灏   add Poptip component
92
93
94
95
              }
          },
          data () {
              return {
88bb7c92   梁灏   fix bug of Poptip
96
                  prefixCls: prefixCls,
071506fc   梁灏   optimize Poptip f...
97
98
                  showTitle: true,
                  isInput: false
b0893113   jingsam   :art: add eslint
99
              };
9699c270   梁灏   add Poptip component
100
101
102
103
          },
          computed: {
              classes () {
                  return [
4b7138b9   梁灏   fixed some bugs
104
                      `${prefixCls}`,
9699c270   梁灏   add Poptip component
105
                      {
4b7138b9   梁灏   fixed some bugs
106
                          [`${prefixCls}-confirm`]: this.confirm
9699c270   梁灏   add Poptip component
107
                      }
b0893113   jingsam   :art: add eslint
108
                  ];
9699c270   梁灏   add Poptip component
109
110
111
112
              },
              styles () {
                  let style = {};
  
b0893113   jingsam   :art: add eslint
113
                  if (this.width) {
4b7138b9   梁灏   fixed some bugs
114
                      style.width = `${this.width}px`;
9699c270   梁灏   add Poptip component
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
                  }
                  return style;
              }
          },
          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...
140
141
              handleFocus (fromInput = true) {
                  if (this.trigger !== 'focus' || this.confirm || (this.isInput && !fromInput)) {
9699c270   梁灏   add Poptip component
142
143
144
145
                      return false;
                  }
                  this.visible = true;
              },
071506fc   梁灏   optimize Poptip f...
146
147
              handleBlur (fromInput = true) {
                  if (this.trigger !== 'focus' || this.confirm || (this.isInput && !fromInput)) {
9699c270   梁灏   add Poptip component
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
                      return false;
                  }
                  this.visible = false;
              },
              handleMouseenter () {
                  if (this.trigger !== 'hover' || this.confirm) {
                      return false;
                  }
                  this.visible = true;
              },
              handleMouseleave () {
                  if (this.trigger !== 'hover' || this.confirm) {
                      return false;
                  }
                  this.visible = false;
              },
              cancel () {
                  this.visible = false;
                  this.$emit('on-cancel');
              },
              ok () {
                  this.visible = false;
                  this.$emit('on-ok');
071506fc   梁灏   optimize Poptip f...
171
172
              },
              getInputChildren () {
79288d43   梁灏   support Poptip & ...
173
174
                  const $input = this.$refs.reference.querySelectorAll('input');
                  const $textarea = this.$refs.reference.querySelectorAll('textarea');
071506fc   梁灏   optimize Poptip f...
175
176
177
178
179
180
181
182
183
                  let $children = null;
  
                  if ($input.length) {
                      $children = $input[0];
                  } else if ($textarea.length) {
                      $children = $textarea[0];
                  }
  
                  return $children;
9699c270   梁灏   add Poptip component
184
              }
88bb7c92   梁灏   fix bug of Poptip
185
          },
79288d43   梁灏   support Poptip & ...
186
          mounted () {
88bb7c92   梁灏   fix bug of Poptip
187
              if (!this.confirm) {
79288d43   梁灏   support Poptip & ...
188
189
  //                this.showTitle = this.$refs.title.innerHTML != `<div class="${prefixCls}-title-inner"></div>`;
                  this.showTitle = this.$slots.title !== undefined;
88bb7c92   梁灏   fix bug of Poptip
190
              }
071506fc   梁灏   optimize Poptip f...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
              // if trigger and children is input or textarea,listen focus & blur event
              if (this.trigger === 'focus') {
                  const $children = this.getInputChildren();
                  if ($children) {
                      $children.addEventListener('focus', this.handleFocus, false);
                      $children.addEventListener('blur', this.handleBlur, false);
                      this.isInput = true;
                  }
              }
          },
          beforeDestroy () {
              const $children = this.getInputChildren();
              if ($children) {
                  $children.removeEventListener('focus', this.handleFocus, false);
                  $children.removeEventListener('blur', this.handleBlur, false);
              }
9699c270   梁灏   add Poptip component
207
          }
b0893113   jingsam   :art: add eslint
208
      };
d6342fe1   jingsam   fixed ie bug
209
  </script>