Blame view

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