Blame view

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