Blame view

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