Blame view

src/components/menu/submenu.vue 5.29 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,
b2d676bd   zhigang.li   fixed the bug abo...
49
                  dropWidth: parseFloat(getStyle(this.$el, 'width'))
b0893113   jingsam   :art: add eslint
50
              };
e05d7289   梁灏   update Menu
51
52
53
54
55
56
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}-submenu`,
                      {
4bce7645   zhigang.li   make menu support...
57
                          [`${prefixCls}-item-active`]: this.active && !this.hasParentSubmenu,
0acdae19   梁灏   update Menu
58
                          [`${prefixCls}-opened`]: this.opened,
4bce7645   zhigang.li   make menu support...
59
60
61
                          [`${prefixCls}-submenu-disabled`]: this.disabled,
                          [`${prefixCls}-submenu-has-parent-submenu`]: this.hasParentSubmenu,
                          [`${prefixCls}-child-item-active`]: this.active
e05d7289   梁灏   update Menu
62
                      }
b0893113   jingsam   :art: add eslint
63
                  ];
e05d7289   梁灏   update Menu
64
              },
0acdae19   梁灏   update Menu
65
              accordion () {
b2d676bd   zhigang.li   fixed the bug abo...
66
                  return this.menu.accordion;
fa0241a5   梁灏   fixed #212
67
68
69
70
71
72
              },
              dropStyle () {
                  let style = {};
  
                  if (this.dropWidth) style.minWidth = `${this.dropWidth}px`;
                  return style;
4bce7645   zhigang.li   make menu support...
73
              },
4bce7645   zhigang.li   make menu support...
74
75
76
77
              titleStyle () {
                  return this.hasParentSubmenu ? {
                      paddingLeft: 43 + (this.parentSubmenuNum - 1) * 24 + 'px'
                  } : {};
e05d7289   梁灏   update Menu
78
79
80
81
              }
          },
          methods: {
              handleMouseenter () {
0acdae19   梁灏   update Menu
82
                  if (this.disabled) return;
e05d7289   梁灏   update Menu
83
                  if (this.mode === 'vertical') return;
0acdae19   梁灏   update Menu
84
  
e05d7289   梁灏   update Menu
85
86
                  clearTimeout(this.timeout);
                  this.timeout = setTimeout(() => {
b2d676bd   zhigang.li   fixed the bug abo...
87
                      this.menu.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
88
89
90
91
                      this.opened = true;
                  }, 250);
              },
              handleMouseleave () {
0acdae19   梁灏   update Menu
92
                  if (this.disabled) return;
e05d7289   梁灏   update Menu
93
                  if (this.mode === 'vertical') return;
0acdae19   梁灏   update Menu
94
  
e05d7289   梁灏   update Menu
95
96
                  clearTimeout(this.timeout);
                  this.timeout = setTimeout(() => {
b2d676bd   zhigang.li   fixed the bug abo...
97
                      this.menu.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
98
99
                      this.opened = false;
                  }, 150);
0acdae19   梁灏   update Menu
100
101
102
103
104
105
              },
              handleClick () {
                  if (this.disabled) return;
                  if (this.mode === 'horizontal') return;
                  const opened = this.opened;
                  if (this.accordion) {
a163daa0   zhigang.li   Add logical treat...
106
                      this.$parent.$children.forEach(item => {
0acdae19   梁灏   update Menu
107
108
109
110
                          if (item.$options.name === 'Submenu') item.opened = false;
                      });
                  }
                  this.opened = !opened;
b2d676bd   zhigang.li   fixed the bug abo...
111
                  this.menu.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
112
113
114
115
116
117
118
119
120
121
122
              }
          },
          watch: {
              mode (val) {
                  if (val === 'horizontal') {
                      this.$refs.drop.update();
                  }
              },
              opened (val) {
                  if (this.mode === 'vertical') return;
                  if (val) {
fa0241a5   梁灏   fixed #212
123
124
                      // set drop a width to fixed when menu has fixed position
                      this.dropWidth = parseFloat(getStyle(this.$el, 'width'));
e05d7289   梁灏   update Menu
125
126
127
128
129
                      this.$refs.drop.update();
                  } else {
                      this.$refs.drop.destroy();
                  }
              }
8778b343   梁灏   init Menu components
130
          },
fd1582c5   梁灏   support Menu & La...
131
132
          mounted () {
              this.$on('on-menu-item-select', (name) => {
fd5cd823   梁灏   publish 0.9.10-rc-1
133
                  if (this.mode === 'horizontal') this.opened = false;
fd1582c5   梁灏   support Menu & La...
134
                  this.dispatch('Menu', 'on-menu-item-select', name);
e05d7289   梁灏   update Menu
135
                  return true;
fd1582c5   梁灏   support Menu & La...
136
137
              });
              this.$on('on-update-active-name', (status) => {
4bce7645   zhigang.li   make menu support...
138
139
140
141
                  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...
142
143
                  this.active = status;
              });
e05d7289   梁灏   update Menu
144
          }
b0893113   jingsam   :art: add eslint
145
146
      };
  </script>