Blame view

src/components/menu/submenu.vue 5.54 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
78
79
80
              },
              hasParentSubmenu () {
                  return findComponentUpward(this, 'Submenu');
              },
4bce7645   zhigang.li   make menu support...
81
82
83
84
              titleStyle () {
                  return this.hasParentSubmenu ? {
                      paddingLeft: 43 + (this.parentSubmenuNum - 1) * 24 + 'px'
                  } : {};
e05d7289   梁灏   update Menu
85
86
87
88
              }
          },
          methods: {
              handleMouseenter () {
0acdae19   梁灏   update Menu
89
                  if (this.disabled) return;
e05d7289   梁灏   update Menu
90
                  if (this.mode === 'vertical') return;
0acdae19   梁灏   update Menu
91
  
e05d7289   梁灏   update Menu
92
93
                  clearTimeout(this.timeout);
                  this.timeout = setTimeout(() => {
67f4d8e7   梁灏   update Menu
94
                      this.parent.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
95
96
97
98
                      this.opened = true;
                  }, 250);
              },
              handleMouseleave () {
0acdae19   梁灏   update Menu
99
                  if (this.disabled) return;
e05d7289   梁灏   update Menu
100
                  if (this.mode === 'vertical') return;
0acdae19   梁灏   update Menu
101
  
e05d7289   梁灏   update Menu
102
103
                  clearTimeout(this.timeout);
                  this.timeout = setTimeout(() => {
67f4d8e7   梁灏   update Menu
104
                      this.parent.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
105
106
                      this.opened = false;
                  }, 150);
0acdae19   梁灏   update Menu
107
108
109
110
111
112
              },
              handleClick () {
                  if (this.disabled) return;
                  if (this.mode === 'horizontal') return;
                  const opened = this.opened;
                  if (this.accordion) {
a163daa0   zhigang.li   Add logical treat...
113
                      this.$parent.$children.forEach(item => {
0acdae19   梁灏   update Menu
114
115
116
117
                          if (item.$options.name === 'Submenu') item.opened = false;
                      });
                  }
                  this.opened = !opened;
67f4d8e7   梁灏   update Menu
118
                  this.parent.updateOpenKeys(this.name);
e05d7289   梁灏   update Menu
119
120
121
122
123
124
125
126
127
128
129
              }
          },
          watch: {
              mode (val) {
                  if (val === 'horizontal') {
                      this.$refs.drop.update();
                  }
              },
              opened (val) {
                  if (this.mode === 'vertical') return;
                  if (val) {
fa0241a5   梁灏   fixed #212
130
131
                      // set drop a width to fixed when menu has fixed position
                      this.dropWidth = parseFloat(getStyle(this.$el, 'width'));
e05d7289   梁灏   update Menu
132
133
134
135
136
                      this.$refs.drop.update();
                  } else {
                      this.$refs.drop.destroy();
                  }
              }
8778b343   梁灏   init Menu components
137
          },
fd1582c5   梁灏   support Menu & La...
138
139
          mounted () {
              this.$on('on-menu-item-select', (name) => {
fd5cd823   梁灏   publish 0.9.10-rc-1
140
                  if (this.mode === 'horizontal') this.opened = false;
fd1582c5   梁灏   support Menu & La...
141
                  this.dispatch('Menu', 'on-menu-item-select', name);
e05d7289   梁灏   update Menu
142
                  return true;
fd1582c5   梁灏   support Menu & La...
143
144
              });
              this.$on('on-update-active-name', (status) => {
4bce7645   zhigang.li   make menu support...
145
146
147
148
                  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...
149
150
                  this.active = status;
              });
e05d7289   梁灏   update Menu
151
          }
b0893113   jingsam   :art: add eslint
152
153
      };
  </script>