Blame view

src/components/menu/menu.vue 5.38 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>
2b24fcce   zhigang.li   change the way to...
5
      import { oneOf, findComponentsDownward, findComponentsUpward } from '../../utils/assist';
fd1582c5   梁灏   support Menu & La...
6
      import Emitter from '../../mixins/emitter';
e05d7289   梁灏   update Menu
7
8
9
  
      const prefixCls = 'ivu-menu';
  
8778b343   梁灏   init Menu components
10
      export default {
fd1582c5   梁灏   support Menu & La...
11
12
          name: 'Menu',
          mixins: [ Emitter ],
e05d7289   梁灏   update Menu
13
14
15
16
17
18
19
20
21
22
23
24
25
          props: {
              mode: {
                  validator (value) {
                      return oneOf(value, ['horizontal', 'vertical']);
                  },
                  default: 'vertical'
              },
              theme: {
                  validator (value) {
                      return oneOf(value, ['light', 'dark', 'primary']);
                  },
                  default: 'light'
              },
fd1582c5   梁灏   support Menu & La...
26
              activeName: {
e05d7289   梁灏   update Menu
27
28
                  type: [String, Number]
              },
fd1582c5   梁灏   support Menu & La...
29
              openNames: {
ab673ebc   梁灏   update Menu
30
31
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
32
                      return [];
ab673ebc   梁灏   update Menu
33
                  }
e05d7289   梁灏   update Menu
34
35
36
37
              },
              accordion: {
                  type: Boolean,
                  default: false
fc3ffbe0   梁灏   publish 0.9.10-rc-2
38
39
40
41
              },
              width: {
                  type: String,
                  default: '240px'
e05d7289   梁灏   update Menu
42
43
              }
          },
fd1582c5   梁灏   support Menu & La...
44
45
          data () {
              return {
b3b4134d   zhigang.li   fixed https://git...
46
47
                  currentActiveName: this.activeName,
                  openedNames: []
fd1582c5   梁灏   support Menu & La...
48
49
              };
          },
e05d7289   梁灏   update Menu
50
51
          computed: {
              classes () {
fd5cd823   梁灏   publish 0.9.10-rc-1
52
53
54
                  let theme = this.theme;
                  if (this.mode === 'vertical' && this.theme === 'primary') theme = 'light';
  
e05d7289   梁灏   update Menu
55
56
                  return [
                      `${prefixCls}`,
fd5cd823   梁灏   publish 0.9.10-rc-1
57
                      `${prefixCls}-${theme}`,
e05d7289   梁灏   update Menu
58
                      {
fd5cd823   梁灏   publish 0.9.10-rc-1
59
                          [`${prefixCls}-${this.mode}`]: this.mode
e05d7289   梁灏   update Menu
60
                      }
b0893113   jingsam   :art: add eslint
61
                  ];
fc3ffbe0   梁灏   publish 0.9.10-rc-2
62
63
64
65
66
67
68
              },
              styles () {
                  let style = {};
  
                  if (this.mode === 'vertical') style.width = this.width;
  
                  return style;
e05d7289   梁灏   update Menu
69
70
71
              }
          },
          methods: {
fd1582c5   梁灏   support Menu & La...
72
              updateActiveName () {
8dd54687   梁灏   #709
73
                  if (this.currentActiveName === undefined) {
fd1582c5   梁灏   support Menu & La...
74
75
76
77
                      this.currentActiveName = -1;
                  }
                  this.broadcast('Submenu', 'on-update-active-name', false);
                  this.broadcast('MenuItem', 'on-update-active-name', this.currentActiveName);
ab673ebc   梁灏   update Menu
78
              },
fd1582c5   梁灏   support Menu & La...
79
              updateOpenKeys (name) {
b3b4134d   zhigang.li   fixed https://git...
80
81
                  let names = [...this.openedNames];
                  const index = names.indexOf(name);
1f7905d4   zhigang.li   fixed bug of menu...
82
83
84
                  if (this.accordion) findComponentsDownward(this, 'Submenu').forEach(item => {
                      item.opened = false;
                  });
b3b4134d   zhigang.li   fixed https://git...
85
                  if (index >= 0) {
e549c92b   zhigang.li   fixed https://git...
86
87
88
89
90
91
92
                      let currentSubmenu = null;
                      findComponentsDownward(this, 'Submenu').forEach(item => {
                          if (item.name === name) {
                              currentSubmenu = item;
                              item.opened = false;
                          }
                      });
1f7905d4   zhigang.li   fixed bug of menu...
93
94
95
                      findComponentsUpward(currentSubmenu, 'Submenu').forEach(item => {
                          item.opened = true;
                      });
e549c92b   zhigang.li   fixed https://git...
96
97
98
                      findComponentsDownward(currentSubmenu, 'Submenu').forEach(item => {
                          item.opened = false;
                      });
ab673ebc   梁灏   update Menu
99
                  } else {
1f7905d4   zhigang.li   fixed bug of menu...
100
                      if (this.accordion) {
b3b4134d   zhigang.li   fixed https://git...
101
                          let currentSubmenu = null;
a163daa0   zhigang.li   Add logical treat...
102
                          findComponentsDownward(this, 'Submenu').forEach(item => {
e549c92b   zhigang.li   fixed https://git...
103
104
105
106
                              if (item.name === name) {
                                  currentSubmenu = item;
                                  item.opened = true;
                              }
a163daa0   zhigang.li   Add logical treat...
107
                          });
2b24fcce   zhigang.li   change the way to...
108
                          findComponentsUpward(currentSubmenu, 'Submenu').forEach(item => {
e549c92b   zhigang.li   fixed https://git...
109
110
111
112
113
                              item.opened = true;
                          });
                      } else {
                          findComponentsDownward(this, 'Submenu').forEach(item => {
                              if (item.name === name) item.opened = true;
a163daa0   zhigang.li   Add logical treat...
114
                          });
a8a03995   Aresn   update #1102
115
                      }
ab673ebc   梁灏   update Menu
116
                  }
e549c92b   zhigang.li   fixed https://git...
117
118
119
                  let openedNames = findComponentsDownward(this, 'Submenu').filter(item => item.opened).map(item => item.name);
                  this.openedNames = [...openedNames];
                  this.$emit('on-open-change', openedNames);
fc3ffbe0   梁灏   publish 0.9.10-rc-2
120
121
              },
              updateOpened () {
67f4d8e7   梁灏   update Menu
122
123
124
125
                  const items = findComponentsDownward(this, 'Submenu');
  
                  if (items.length) {
                      items.forEach(item => {
b3b4134d   zhigang.li   fixed https://git...
126
127
                          if (this.openedNames.indexOf(item.name) > -1) item.opened = true;
                          else item.opened = false;
67f4d8e7   梁灏   update Menu
128
129
                      });
                  }
164f7bcb   梁灏   update Menu
130
131
132
              },
              handleEmitSelectEvent (name) {
                  this.$emit('on-select', name);
e05d7289   梁灏   update Menu
133
134
              }
          },
fd1582c5   梁灏   support Menu & La...
135
          mounted () {
b3b4134d   zhigang.li   fixed https://git...
136
              this.openedNames = [...this.openNames];
fc3ffbe0   梁灏   publish 0.9.10-rc-2
137
              this.updateOpened();
e098ce3f   梁灏   Optimize Menu ini...
138
              this.$nextTick(() => this.updateActiveName());
fd1582c5   梁灏   support Menu & La...
139
140
141
142
              this.$on('on-menu-item-select', (name) => {
                  this.currentActiveName = name;
                  this.$emit('on-select', name);
              });
ab673ebc   梁灏   update Menu
143
144
          },
          watch: {
b3b4134d   zhigang.li   fixed https://git...
145
146
              openNames (names) {
                  this.openedNames = names;
fd1582c5   梁灏   support Menu & La...
147
148
149
              },
              activeName (val) {
                  this.currentActiveName = val;
e069c7ae   梁灏   fixed #230
150
              },
fd1582c5   梁灏   support Menu & La...
151
152
              currentActiveName () {
                  this.updateActiveName();
ab673ebc   梁灏   update Menu
153
              }
e05d7289   梁灏   update Menu
154
          }
b0893113   jingsam   :art: add eslint
155
156
      };
  </script>