Blame view

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