Blame view

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