Blame view

src/components/menu/menu.vue 4.47 KB
8778b343   梁灏   init Menu components
1
  <template>
fc3ffbe0   梁灏   publish 0.9.10-rc-2
2
      <ul :class="classes" :style="styles"><slot></slot></ul>
8778b343   梁灏   init Menu components
3
4
  </template>
  <script>
e05d7289   梁灏   update Menu
5
6
7
8
      import { oneOf } from '../../utils/assist';
  
      const prefixCls = 'ivu-menu';
  
8778b343   梁灏   init Menu components
9
      export default {
e05d7289   梁灏   update Menu
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
          props: {
              mode: {
                  validator (value) {
                      return oneOf(value, ['horizontal', 'vertical']);
                  },
                  default: 'vertical'
              },
              theme: {
                  validator (value) {
                      return oneOf(value, ['light', 'dark', 'primary']);
                  },
                  default: 'light'
              },
              activeKey: {
                  type: [String, Number]
              },
              openKeys: {
ab673ebc   梁灏   update Menu
27
28
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
29
                      return [];
ab673ebc   梁灏   update Menu
30
                  }
e05d7289   梁灏   update Menu
31
32
33
34
              },
              accordion: {
                  type: Boolean,
                  default: false
fc3ffbe0   梁灏   publish 0.9.10-rc-2
35
36
37
38
              },
              width: {
                  type: String,
                  default: '240px'
e05d7289   梁灏   update Menu
39
40
41
42
              }
          },
          computed: {
              classes () {
fd5cd823   梁灏   publish 0.9.10-rc-1
43
44
45
                  let theme = this.theme;
                  if (this.mode === 'vertical' && this.theme === 'primary') theme = 'light';
  
e05d7289   梁灏   update Menu
46
47
                  return [
                      `${prefixCls}`,
fd5cd823   梁灏   publish 0.9.10-rc-1
48
                      `${prefixCls}-${theme}`,
e05d7289   梁灏   update Menu
49
                      {
fd5cd823   梁灏   publish 0.9.10-rc-1
50
                          [`${prefixCls}-${this.mode}`]: this.mode
e05d7289   梁灏   update Menu
51
                      }
b0893113   jingsam   :art: add eslint
52
                  ];
fc3ffbe0   梁灏   publish 0.9.10-rc-2
53
54
55
56
57
58
59
              },
              styles () {
                  let style = {};
  
                  if (this.mode === 'vertical') style.width = this.width;
  
                  return style;
e05d7289   梁灏   update Menu
60
61
62
63
64
              }
          },
          methods: {
              updateActiveKey () {
                  this.$children.forEach((item, index) => {
fc3ffbe0   梁灏   publish 0.9.10-rc-2
65
66
67
                      if (!this.activeKey && index === 0) {
                          this.activeKey = -1;
                      }
e05d7289   梁灏   update Menu
68
69
70
71
72
73
74
75
76
77
78
79
  
                      if (item.$options.name === 'Submenu') {
                          item.active = false;
                          item.$children.forEach(subitem => {
                              if (subitem.$options.name === 'MenuGroup') {
                                  subitem.$children.forEach(groupItem => {
                                      if (groupItem.key === this.activeKey) {
                                          groupItem.active = true;
                                          groupItem.$parent.$parent.active = true;
                                      } else {
                                          groupItem.active = false;
                                      }
b0893113   jingsam   :art: add eslint
80
                                  });
f9d87766   梁灏   update Menu
81
                              } else if (subitem.$options.name === 'MenuItem') {
e05d7289   梁灏   update Menu
82
83
84
85
86
87
88
                                  if (subitem.key === this.activeKey) {
                                      subitem.active = true;
                                      subitem.$parent.active = true;
                                  } else {
                                      subitem.active = false;
                                  }
                              }
b0893113   jingsam   :art: add eslint
89
                          });
0acdae19   梁灏   update Menu
90
91
92
                      } else if (item.$options.name === 'MenuGroup') {
                          item.$children.forEach(groupItem => {
                              groupItem.active = groupItem.key === this.activeKey;
b0893113   jingsam   :art: add eslint
93
                          });
f9d87766   梁灏   update Menu
94
                      } else if (item.$options.name === 'MenuItem') {
e05d7289   梁灏   update Menu
95
96
                          item.active = item.key === this.activeKey;
                      }
b0893113   jingsam   :art: add eslint
97
                  });
ab673ebc   梁灏   update Menu
98
99
100
101
102
103
104
105
              },
              updateOpenKeys (key) {
                  const index = this.openKeys.indexOf(key);
                  if (index > -1) {
                      this.openKeys.splice(index, 1);
                  } else {
                      this.openKeys.push(key);
                  }
fc3ffbe0   梁灏   publish 0.9.10-rc-2
106
107
108
109
110
111
              },
              updateOpened () {
                  this.$children.forEach(item => {
                      if (item.$options.name === 'Submenu') {
                          if (this.openKeys.indexOf(item.key) > -1) item.opened = true;
                      }
b0893113   jingsam   :art: add eslint
112
                  });
e05d7289   梁灏   update Menu
113
114
115
116
              }
          },
          compiled () {
              this.updateActiveKey();
fc3ffbe0   梁灏   publish 0.9.10-rc-2
117
              this.updateOpened();
8778b343   梁灏   init Menu components
118
          },
e05d7289   梁灏   update Menu
119
120
121
          events: {
              'on-menu-item-select' (key) {
                  this.activeKey = key;
e05d7289   梁灏   update Menu
122
123
                  this.$emit('on-select', key);
              }
ab673ebc   梁灏   update Menu
124
125
126
127
          },
          watch: {
              openKeys () {
                  this.$emit('on-open-change', this.openKeys);
e069c7ae   梁灏   fixed #230
128
129
130
              },
              activeKey () {
                  this.updateActiveKey();
ab673ebc   梁灏   update Menu
131
              }
e05d7289   梁灏   update Menu
132
          }
b0893113   jingsam   :art: add eslint
133
134
      };
  </script>