Blame view

src/components/dropdown/dropdown.vue 7 KB
ab8aaf95   梁灏   add Dropdown comp...
1
2
3
  <template>
      <div
          :class="[prefixCls]"
26369639   Graham Fairweather   Update v-click-ou...
4
          v-click-outside="onClickoutside"
ab8aaf95   梁灏   add Dropdown comp...
5
          @mouseenter="handleMouseenter"
407eabd5   梁灏   update Dropdown
6
          @mouseleave="handleMouseleave">
eccaa940   梁灏   Dropdown add new ...
7
          <div :class="relClasses" ref="reference" @click="handleClick" @contextmenu.prevent="handleRightClick"><slot></slot></div>
e09b07b7   huanghong   解决drop弹出动画异常
8
          <transition name="transition-drop">
3e4bab96   梁灏   Dropdown add tran...
9
              <Drop
f1800986   梁灏   update Dropdown cls
10
                  :class="dropdownCls"
3e4bab96   梁灏   Dropdown add tran...
11
12
13
14
15
                  v-show="currentVisible"
                  :placement="placement"
                  ref="drop"
                  @mouseenter.native="handleMouseenter"
                  @mouseleave.native="handleMouseleave"
548eac43   梁灏   fixed #1387 and u...
16
                  :data-transfer="transfer"
7bafe9d9   梁灏   fixes #4453 #4480...
17
                  :transfer="transfer"
548eac43   梁灏   fixed #1387 and u...
18
                  v-transfer-dom><slot name="list"></slot></Drop>
b1c118d8   梁灏   support Dropdown
19
          </transition>
ab8aaf95   梁灏   add Dropdown comp...
20
21
22
23
      </div>
  </template>
  <script>
      import Drop from '../select/dropdown.vue';
26369639   Graham Fairweather   Update v-click-ou...
24
      import {directive as clickOutside} from 'v-click-outside-x';
3e4bab96   梁灏   Dropdown add tran...
25
      import TransferDom from '../../directives/transfer-dom';
cb6418ac   梁灏   fixed #721
26
      import { oneOf, findComponentUpward } from '../../utils/assist';
ab8aaf95   梁灏   add Dropdown comp...
27
28
29
30
  
      const prefixCls = 'ivu-dropdown';
  
      export default {
745bcbc2   梁灏   update Dropdown
31
          name: 'Dropdown',
26369639   Graham Fairweather   Update v-click-ou...
32
          directives: { clickOutside, TransferDom },
ab8aaf95   梁灏   add Dropdown comp...
33
34
35
36
          components: { Drop },
          props: {
              trigger: {
                  validator (value) {
eccaa940   梁灏   Dropdown add new ...
37
                      return oneOf(value, ['click', 'hover', 'custom', 'contextMenu']);
ab8aaf95   梁灏   add Dropdown comp...
38
39
40
                  },
                  default: 'hover'
              },
745bcbc2   梁灏   update Dropdown
41
              placement: {
ab8aaf95   梁灏   add Dropdown comp...
42
                  validator (value) {
745bcbc2   梁灏   update Dropdown
43
                      return oneOf(value, ['top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end']);
ab8aaf95   梁灏   add Dropdown comp...
44
                  },
745bcbc2   梁灏   update Dropdown
45
                  default: 'bottom'
51f9f894   梁灏   Dropdown add cust...
46
47
48
49
              },
              visible: {
                  type: Boolean,
                  default: false
3e4bab96   梁灏   Dropdown add tran...
50
51
52
              },
              transfer: {
                  type: Boolean,
262c8524   梁灏   Dropdown support ...
53
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
54
                      return !this.$IVIEW || this.$IVIEW.transfer === '' ? false : this.$IVIEW.transfer;
262c8524   梁灏   Dropdown support ...
55
                  }
da0f6770   梁灏   Dropdown add prop...
56
57
58
59
              },
              transferClassName: {
                  type: String
              },
c7f705d0   troy351   add stopPropagati...
60
61
62
63
              stopPropagation: {
                  type: Boolean,
                  default: false
              },
ab8aaf95   梁灏   add Dropdown comp...
64
          },
407eabd5   梁灏   update Dropdown
65
66
67
          computed: {
              transition () {
                  return ['bottom-start', 'bottom', 'bottom-end'].indexOf(this.placement) > -1 ? 'slide-up' : 'fade';
f1800986   梁灏   update Dropdown cls
68
69
70
              },
              dropdownCls () {
                  return {
da0f6770   梁灏   Dropdown add prop...
71
72
                      [prefixCls + '-transfer']: this.transfer,
                      [this.transferClassName]: this.transferClassName
f1800986   梁灏   update Dropdown cls
73
                  };
eccaa940   梁灏   Dropdown add new ...
74
75
76
77
78
79
80
81
              },
              relClasses () {
                  return [
                      `${prefixCls}-rel`,
                      {
                          [`${prefixCls}-rel-user-select-none`]: this.trigger === 'contextMenu'
                      }
                  ];
407eabd5   梁灏   update Dropdown
82
83
              }
          },
ab8aaf95   梁灏   add Dropdown comp...
84
85
          data () {
              return {
b1c118d8   梁灏   support Dropdown
86
87
                  prefixCls: prefixCls,
                  currentVisible: this.visible
b0893113   jingsam   :art: add eslint
88
              };
ab8aaf95   梁灏   add Dropdown comp...
89
          },
b1c118d8   梁灏   support Dropdown
90
91
92
93
94
95
96
97
98
99
100
101
102
          watch: {
              visible (val) {
                  this.currentVisible = val;
              },
              currentVisible (val) {
                  if (val) {
                      this.$refs.drop.update();
                  } else {
                      this.$refs.drop.destroy();
                  }
                  this.$emit('on-visible-change', val);
              }
          },
ab8aaf95   梁灏   add Dropdown comp...
103
104
          methods: {
              handleClick () {
51f9f894   梁灏   Dropdown add cust...
105
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
106
107
108
                  if (this.trigger !== 'click') {
                      return false;
                  }
b1c118d8   梁灏   support Dropdown
109
                  this.currentVisible = !this.currentVisible;
ab8aaf95   梁灏   add Dropdown comp...
110
              },
eccaa940   梁灏   Dropdown add new ...
111
112
113
114
115
116
117
              handleRightClick () {
                  if (this.trigger === 'custom') return false;
                  if (this.trigger !== 'contextMenu') {
                      return false;
                  }
                  this.currentVisible = !this.currentVisible;
              },
ab8aaf95   梁灏   add Dropdown comp...
118
              handleMouseenter () {
51f9f894   梁灏   Dropdown add cust...
119
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
120
121
122
                  if (this.trigger !== 'hover') {
                      return false;
                  }
3e4bab96   梁灏   Dropdown add tran...
123
                  if (this.timeout) clearTimeout(this.timeout);
ab8aaf95   梁灏   add Dropdown comp...
124
                  this.timeout = setTimeout(() => {
b1c118d8   梁灏   support Dropdown
125
                      this.currentVisible = true;
ab8aaf95   梁灏   add Dropdown comp...
126
127
128
                  }, 250);
              },
              handleMouseleave () {
51f9f894   梁灏   Dropdown add cust...
129
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
130
131
132
                  if (this.trigger !== 'hover') {
                      return false;
                  }
3e4bab96   梁灏   Dropdown add tran...
133
134
135
136
137
138
                  if (this.timeout) {
                      clearTimeout(this.timeout);
                      this.timeout = setTimeout(() => {
                          this.currentVisible = false;
                      }, 150);
                  }
ab8aaf95   梁灏   add Dropdown comp...
139
              },
526a142c   Sergio Crisostomo   add click outside...
140
141
              onClickoutside (e) {
                  this.handleClose();
eccaa940   梁灏   Dropdown add new ...
142
                  this.handleRightClose();
526a142c   Sergio Crisostomo   add click outside...
143
144
                  if (this.currentVisible) this.$emit('on-clickoutside', e);
              },
ab8aaf95   梁灏   add Dropdown comp...
145
              handleClose () {
51f9f894   梁灏   Dropdown add cust...
146
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
147
148
149
                  if (this.trigger !== 'click') {
                      return false;
                  }
b1c118d8   梁灏   support Dropdown
150
                  this.currentVisible = false;
6b71ba94   梁灏   update Dropdown
151
              },
eccaa940   梁灏   Dropdown add new ...
152
153
154
155
156
157
158
              handleRightClose () {
                  if (this.trigger === 'custom') return false;
                  if (this.trigger !== 'contextMenu') {
                      return false;
                  }
                  this.currentVisible = false;
              },
6b71ba94   梁灏   update Dropdown
159
              hasParent () {
cb6418ac   梁灏   fixed #721
160
161
162
  //                const $parent = this.$parent.$parent.$parent;
                  const $parent = findComponentUpward(this, 'Dropdown');
                  if ($parent) {
6b71ba94   梁灏   update Dropdown
163
164
165
166
                      return $parent;
                  } else {
                      return false;
                  }
ab8aaf95   梁灏   add Dropdown comp...
167
168
              }
          },
b1c118d8   梁灏   support Dropdown
169
170
          mounted () {
              this.$on('on-click', (key) => {
c7f705d0   troy351   add stopPropagati...
171
                  if (this.stopPropagation) return;
6b71ba94   梁灏   update Dropdown
172
                  const $parent = this.hasParent();
b1c118d8   梁灏   support Dropdown
173
174
175
                  if ($parent) $parent.$emit('on-click', key);
              });
              this.$on('on-hover-click', () => {
6b71ba94   梁灏   update Dropdown
176
                  const $parent = this.hasParent();
407eabd5   梁灏   update Dropdown
177
178
                  if ($parent) {
                      this.$nextTick(() => {
51f9f894   梁灏   Dropdown add cust...
179
                          if (this.trigger === 'custom') return false;
b1c118d8   梁灏   support Dropdown
180
                          this.currentVisible = false;
407eabd5   梁灏   update Dropdown
181
182
183
184
                      });
                      $parent.$emit('on-hover-click');
                  } else {
                      this.$nextTick(() => {
51f9f894   梁灏   Dropdown add cust...
185
                          if (this.trigger === 'custom') return false;
b1c118d8   梁灏   support Dropdown
186
                          this.currentVisible = false;
407eabd5   梁灏   update Dropdown
187
188
                      });
                  }
b1c118d8   梁灏   support Dropdown
189
190
              });
              this.$on('on-haschild-click', () => {
6b71ba94   梁灏   update Dropdown
191
                  this.$nextTick(() => {
51f9f894   梁灏   Dropdown add cust...
192
                      if (this.trigger === 'custom') return false;
b1c118d8   梁灏   support Dropdown
193
                      this.currentVisible = true;
6b71ba94   梁灏   update Dropdown
194
195
196
                  });
                  const $parent = this.hasParent();
                  if ($parent) $parent.$emit('on-haschild-click');
b1c118d8   梁灏   support Dropdown
197
              });
2d74744d   梁灏   update Dropdown
198
          }
b0893113   jingsam   :art: add eslint
199
200
      };
  </script>