Blame view

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