Blame view

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