Blame view

src/components/dropdown/dropdown.vue 5.33 KB
ab8aaf95   梁灏   add Dropdown comp...
1
2
3
  <template>
      <div
          :class="[prefixCls]"
407eabd5   梁灏   update Dropdown
4
          v-clickoutside="handleClose"
ab8aaf95   梁灏   add Dropdown comp...
5
          @mouseenter="handleMouseenter"
407eabd5   梁灏   update Dropdown
6
          @mouseleave="handleMouseleave">
b1c118d8   梁灏   support Dropdown
7
8
          <div :class="[prefixCls + '-rel']" ref="reference" @click="handleClick"><slot></slot></div>
          <transition :name="transition">
3e4bab96   梁灏   Dropdown add tran...
9
10
11
12
13
14
              <Drop
                  v-show="currentVisible"
                  :placement="placement"
                  ref="drop"
                  @mouseenter.native="handleMouseenter"
                  @mouseleave.native="handleMouseleave"
548eac43   梁灏   fixed #1387 and u...
15
16
                  :data-transfer="transfer"
                  v-transfer-dom><slot name="list"></slot></Drop>
b1c118d8   梁灏   support Dropdown
17
          </transition>
ab8aaf95   梁灏   add Dropdown comp...
18
19
20
21
22
      </div>
  </template>
  <script>
      import Drop from '../select/dropdown.vue';
      import clickoutside from '../../directives/clickoutside';
3e4bab96   梁灏   Dropdown add tran...
23
      import TransferDom from '../../directives/transfer-dom';
cb6418ac   梁灏   fixed #721
24
      import { oneOf, findComponentUpward } from '../../utils/assist';
ab8aaf95   梁灏   add Dropdown comp...
25
26
27
28
  
      const prefixCls = 'ivu-dropdown';
  
      export default {
745bcbc2   梁灏   update Dropdown
29
          name: 'Dropdown',
3e4bab96   梁灏   Dropdown add tran...
30
          directives: { clickoutside, TransferDom },
ab8aaf95   梁灏   add Dropdown comp...
31
32
33
34
          components: { Drop },
          props: {
              trigger: {
                  validator (value) {
51f9f894   梁灏   Dropdown add cust...
35
                      return oneOf(value, ['click', 'hover', 'custom']);
ab8aaf95   梁灏   add Dropdown comp...
36
37
38
                  },
                  default: 'hover'
              },
745bcbc2   梁灏   update Dropdown
39
              placement: {
ab8aaf95   梁灏   add Dropdown comp...
40
                  validator (value) {
745bcbc2   梁灏   update Dropdown
41
                      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...
42
                  },
745bcbc2   梁灏   update Dropdown
43
                  default: 'bottom'
51f9f894   梁灏   Dropdown add cust...
44
45
46
47
              },
              visible: {
                  type: Boolean,
                  default: false
3e4bab96   梁灏   Dropdown add tran...
48
49
50
51
              },
              transfer: {
                  type: Boolean,
                  default: false
ab8aaf95   梁灏   add Dropdown comp...
52
53
              }
          },
407eabd5   梁灏   update Dropdown
54
55
56
57
58
          computed: {
              transition () {
                  return ['bottom-start', 'bottom', 'bottom-end'].indexOf(this.placement) > -1 ? 'slide-up' : 'fade';
              }
          },
ab8aaf95   梁灏   add Dropdown comp...
59
60
          data () {
              return {
b1c118d8   梁灏   support Dropdown
61
62
                  prefixCls: prefixCls,
                  currentVisible: this.visible
b0893113   jingsam   :art: add eslint
63
              };
ab8aaf95   梁灏   add Dropdown comp...
64
          },
b1c118d8   梁灏   support Dropdown
65
66
67
68
69
70
71
72
73
74
75
76
77
          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...
78
79
          methods: {
              handleClick () {
51f9f894   梁灏   Dropdown add cust...
80
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
81
82
83
                  if (this.trigger !== 'click') {
                      return false;
                  }
b1c118d8   梁灏   support Dropdown
84
                  this.currentVisible = !this.currentVisible;
ab8aaf95   梁灏   add Dropdown comp...
85
86
              },
              handleMouseenter () {
51f9f894   梁灏   Dropdown add cust...
87
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
88
89
90
                  if (this.trigger !== 'hover') {
                      return false;
                  }
3e4bab96   梁灏   Dropdown add tran...
91
                  if (this.timeout) clearTimeout(this.timeout);
ab8aaf95   梁灏   add Dropdown comp...
92
                  this.timeout = setTimeout(() => {
b1c118d8   梁灏   support Dropdown
93
                      this.currentVisible = true;
ab8aaf95   梁灏   add Dropdown comp...
94
95
96
                  }, 250);
              },
              handleMouseleave () {
51f9f894   梁灏   Dropdown add cust...
97
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
98
99
100
                  if (this.trigger !== 'hover') {
                      return false;
                  }
3e4bab96   梁灏   Dropdown add tran...
101
102
103
104
105
106
                  if (this.timeout) {
                      clearTimeout(this.timeout);
                      this.timeout = setTimeout(() => {
                          this.currentVisible = false;
                      }, 150);
                  }
ab8aaf95   梁灏   add Dropdown comp...
107
108
              },
              handleClose () {
51f9f894   梁灏   Dropdown add cust...
109
                  if (this.trigger === 'custom') return false;
ab8aaf95   梁灏   add Dropdown comp...
110
111
112
                  if (this.trigger !== 'click') {
                      return false;
                  }
b1c118d8   梁灏   support Dropdown
113
                  this.currentVisible = false;
6b71ba94   梁灏   update Dropdown
114
115
              },
              hasParent () {
cb6418ac   梁灏   fixed #721
116
117
118
  //                const $parent = this.$parent.$parent.$parent;
                  const $parent = findComponentUpward(this, 'Dropdown');
                  if ($parent) {
6b71ba94   梁灏   update Dropdown
119
120
121
122
                      return $parent;
                  } else {
                      return false;
                  }
ab8aaf95   梁灏   add Dropdown comp...
123
124
              }
          },
b1c118d8   梁灏   support Dropdown
125
126
          mounted () {
              this.$on('on-click', (key) => {
6b71ba94   梁灏   update Dropdown
127
                  const $parent = this.hasParent();
b1c118d8   梁灏   support Dropdown
128
129
130
                  if ($parent) $parent.$emit('on-click', key);
              });
              this.$on('on-hover-click', () => {
6b71ba94   梁灏   update Dropdown
131
                  const $parent = this.hasParent();
407eabd5   梁灏   update Dropdown
132
133
                  if ($parent) {
                      this.$nextTick(() => {
51f9f894   梁灏   Dropdown add cust...
134
                          if (this.trigger === 'custom') return false;
b1c118d8   梁灏   support Dropdown
135
                          this.currentVisible = false;
407eabd5   梁灏   update Dropdown
136
137
138
139
                      });
                      $parent.$emit('on-hover-click');
                  } else {
                      this.$nextTick(() => {
51f9f894   梁灏   Dropdown add cust...
140
                          if (this.trigger === 'custom') return false;
b1c118d8   梁灏   support Dropdown
141
                          this.currentVisible = false;
407eabd5   梁灏   update Dropdown
142
143
                      });
                  }
b1c118d8   梁灏   support Dropdown
144
145
              });
              this.$on('on-haschild-click', () => {
6b71ba94   梁灏   update Dropdown
146
                  this.$nextTick(() => {
51f9f894   梁灏   Dropdown add cust...
147
                      if (this.trigger === 'custom') return false;
b1c118d8   梁灏   support Dropdown
148
                      this.currentVisible = true;
6b71ba94   梁灏   update Dropdown
149
150
151
                  });
                  const $parent = this.hasParent();
                  if ($parent) $parent.$emit('on-haschild-click');
b1c118d8   梁灏   support Dropdown
152
              });
2d74744d   梁灏   update Dropdown
153
          }
b0893113   jingsam   :art: add eslint
154
155
      };
  </script>