Blame view

src/components/menu/submenu.vue 4.47 KB
8778b343   梁灏   init Menu components
1
  <template>
e05d7289   梁灏   update Menu
2
      <li :class="classes" @mouseenter="handleMouseenter" @mouseleave="handleMouseleave">
fd1582c5   梁灏   support Menu & La...
3
          <div :class="[prefixCls + '-submenu-title']" ref="reference" @click="handleClick">
0acdae19   梁灏   update Menu
4
5
              <slot name="title"></slot>
              <Icon type="ios-arrow-down" :class="[prefixCls + '-submenu-title-icon']"></Icon>
e05d7289   梁灏   update Menu
6
          </div>
0acdae19   梁灏   update Menu
7
          <ul :class="[prefixCls]" v-if="mode === 'vertical'" v-show="opened"><slot></slot></ul>
fd1582c5   梁灏   support Menu & La...
8
9
10
11
12
13
14
          <transition name="slide-up" v-else>
              <Drop
                  v-show="opened"
                  placement="bottom"
                  ref="drop"
                  :style="dropStyle"><slot></slot></Drop>
          </transition>
e05d7289   梁灏   update Menu
15
      </li>
8778b343   梁灏   init Menu components
16
17
  </template>
  <script>
e05d7289   梁灏   update Menu
18
19
      import Drop from '../select/dropdown.vue';
      import Icon from '../icon/icon.vue';
67f4d8e7   梁灏   update Menu
20
      import { getStyle, findComponentUpward } from '../../utils/assist';
fd1582c5   梁灏   support Menu & La...
21
      import Emitter from '../../mixins/emitter';
fa0241a5   梁灏   fixed #212
22
  
e05d7289   梁灏   update Menu
23
24
      const prefixCls = 'ivu-menu';
  
8778b343   梁灏   init Menu components
25
      export default {
e05d7289   梁灏   update Menu
26
          name: 'Submenu',
fd1582c5   梁灏   support Menu & La...
27
          mixins: [ Emitter ],
e05d7289   梁灏   update Menu
28
29
          components: { Icon, Drop },
          props: {
fd1582c5   梁灏   support Menu & La...
30
              name: {
e05d7289   梁灏   update Menu
31
32
                  type: [String, Number],
                  required: true
0acdae19   梁灏   update Menu
33
34
35
36
              },
              disabled: {
                  type: Boolean,
                  default: false
e05d7289   梁灏   update Menu
37
38
              }
          },
8778b343   梁灏   init Menu components
39
          data () {
e05d7289   梁灏   update Menu
40
41
42
              return {
                  prefixCls: prefixCls,
                  active: false,
fa0241a5   梁灏   fixed #212
43
                  opened: false,
67f4d8e7   梁灏   update Menu
44
45
                  dropWidth: parseFloat(getStyle(this.$el, 'width')),
                  parent: findComponentUpward(this, 'Menu')
b0893113   jingsam   :art: add eslint
46
              };
e05d7289   梁灏   update Menu
47
48
49
50
51
52
53
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}-submenu`,
                      {
                          [`${prefixCls}-item-active`]: this.active,
0acdae19   梁灏   update Menu
54
55
                          [`${prefixCls}-opened`]: this.opened,
                          [`${prefixCls}-submenu-disabled`]: this.disabled
e05d7289   梁灏   update Menu
56
                      }
b0893113   jingsam   :art: add eslint
57
                  ];
e05d7289   梁灏   update Menu
58
59
              },
              mode () {
67f4d8e7   梁灏   update Menu
60
                  return this.parent.mode;
0acdae19   梁灏   update Menu
61
62
              },
              accordion () {
67f4d8e7   梁灏   update Menu
63
                  return this.parent.accordion;
fa0241a5   梁灏   fixed #212
64
65
66
67
68
69
              },
              dropStyle () {
                  let style = {};
  
                  if (this.dropWidth) style.minWidth = `${this.dropWidth}px`;
                  return style;
e05d7289   梁灏   update Menu
70
71
72
73
              }
          },
          methods: {
              handleMouseenter () {
0acdae19   梁灏   update Menu
74
                  if (this.disabled) return;
e05d7289   梁灏   update Menu
75
                  if (this.mode === 'vertical') return;
0acdae19   梁灏   update Menu
76
  
e05d7289   梁灏   update Menu
77
78
                  clearTimeout(this.timeout);
                  this.timeout = setTimeout(() => {
67f4d8e7   梁灏   update Menu
79
                      this.parent.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
80
81
82
83
                      this.opened = true;
                  }, 250);
              },
              handleMouseleave () {
0acdae19   梁灏   update Menu
84
                  if (this.disabled) return;
e05d7289   梁灏   update Menu
85
                  if (this.mode === 'vertical') return;
0acdae19   梁灏   update Menu
86
  
e05d7289   梁灏   update Menu
87
88
                  clearTimeout(this.timeout);
                  this.timeout = setTimeout(() => {
67f4d8e7   梁灏   update Menu
89
                      this.parent.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
90
91
                      this.opened = false;
                  }, 150);
0acdae19   梁灏   update Menu
92
93
94
95
96
97
              },
              handleClick () {
                  if (this.disabled) return;
                  if (this.mode === 'horizontal') return;
                  const opened = this.opened;
                  if (this.accordion) {
67f4d8e7   梁灏   update Menu
98
                      this.parent.$children.forEach(item => {
0acdae19   梁灏   update Menu
99
100
101
102
                          if (item.$options.name === 'Submenu') item.opened = false;
                      });
                  }
                  this.opened = !opened;
67f4d8e7   梁灏   update Menu
103
                  this.parent.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
104
105
106
107
108
109
110
111
112
113
114
              }
          },
          watch: {
              mode (val) {
                  if (val === 'horizontal') {
                      this.$refs.drop.update();
                  }
              },
              opened (val) {
                  if (this.mode === 'vertical') return;
                  if (val) {
fa0241a5   梁灏   fixed #212
115
116
                      // set drop a width to fixed when menu has fixed position
                      this.dropWidth = parseFloat(getStyle(this.$el, 'width'));
e05d7289   梁灏   update Menu
117
118
119
120
121
                      this.$refs.drop.update();
                  } else {
                      this.$refs.drop.destroy();
                  }
              }
8778b343   梁灏   init Menu components
122
          },
fd1582c5   梁灏   support Menu & La...
123
124
          mounted () {
              this.$on('on-menu-item-select', (name) => {
fd5cd823   梁灏   publish 0.9.10-rc-1
125
                  if (this.mode === 'horizontal') this.opened = false;
fd1582c5   梁灏   support Menu & La...
126
                  this.dispatch('Menu', 'on-menu-item-select', name);
e05d7289   梁灏   update Menu
127
                  return true;
fd1582c5   梁灏   support Menu & La...
128
129
130
131
              });
              this.$on('on-update-active-name', (status) => {
                  this.active = status;
              });
e05d7289   梁灏   update Menu
132
          }
b0893113   jingsam   :art: add eslint
133
134
      };
  </script>