Blame view

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