Blame view

src/components/menu/menu.vue 3.58 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>
67f4d8e7   梁灏   update Menu
5
      import { oneOf, findComponentsDownward } 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
46
47
48
          data () {
              return {
                  currentActiveName: this.activeName
              };
          },
e05d7289   梁灏   update Menu
49
50
          computed: {
              classes () {
fd5cd823   梁灏   publish 0.9.10-rc-1
51
52
53
                  let theme = this.theme;
                  if (this.mode === 'vertical' && this.theme === 'primary') theme = 'light';
  
e05d7289   梁灏   update Menu
54
55
                  return [
                      `${prefixCls}`,
fd5cd823   梁灏   publish 0.9.10-rc-1
56
                      `${prefixCls}-${theme}`,
e05d7289   梁灏   update Menu
57
                      {
fd5cd823   梁灏   publish 0.9.10-rc-1
58
                          [`${prefixCls}-${this.mode}`]: this.mode
e05d7289   梁灏   update Menu
59
                      }
b0893113   jingsam   :art: add eslint
60
                  ];
fc3ffbe0   梁灏   publish 0.9.10-rc-2
61
62
63
64
65
66
67
              },
              styles () {
                  let style = {};
  
                  if (this.mode === 'vertical') style.width = this.width;
  
                  return style;
e05d7289   梁灏   update Menu
68
69
70
              }
          },
          methods: {
fd1582c5   梁灏   support Menu & La...
71
              updateActiveName () {
8dd54687   梁灏   #709
72
                  if (this.currentActiveName === undefined) {
fd1582c5   梁灏   support Menu & La...
73
74
75
76
                      this.currentActiveName = -1;
                  }
                  this.broadcast('Submenu', 'on-update-active-name', false);
                  this.broadcast('MenuItem', 'on-update-active-name', this.currentActiveName);
ab673ebc   梁灏   update Menu
77
              },
fd1582c5   梁灏   support Menu & La...
78
79
              updateOpenKeys (name) {
                  const index = this.openNames.indexOf(name);
ab673ebc   梁灏   update Menu
80
                  if (index > -1) {
fd1582c5   梁灏   support Menu & La...
81
                      this.openNames.splice(index, 1);
ab673ebc   梁灏   update Menu
82
                  } else {
fd1582c5   梁灏   support Menu & La...
83
                      this.openNames.push(name);
a8a03995   Aresn   update #1102
84
85
86
87
                      if (this.accordion) {
                          this.openNames.splice(0, this.openNames.length);
                          this.openNames.push(name);
                      }
ab673ebc   梁灏   update Menu
88
                  }
fc3ffbe0   梁灏   publish 0.9.10-rc-2
89
90
              },
              updateOpened () {
67f4d8e7   梁灏   update Menu
91
92
93
94
                  const items = findComponentsDownward(this, 'Submenu');
  
                  if (items.length) {
                      items.forEach(item => {
fd1582c5   梁灏   support Menu & La...
95
                          if (this.openNames.indexOf(item.name) > -1) item.opened = true;
67f4d8e7   梁灏   update Menu
96
97
                      });
                  }
e05d7289   梁灏   update Menu
98
99
              }
          },
fd1582c5   梁灏   support Menu & La...
100
101
          mounted () {
              this.updateActiveName();
fc3ffbe0   梁灏   publish 0.9.10-rc-2
102
              this.updateOpened();
fd1582c5   梁灏   support Menu & La...
103
104
105
106
              this.$on('on-menu-item-select', (name) => {
                  this.currentActiveName = name;
                  this.$emit('on-select', name);
              });
ab673ebc   梁灏   update Menu
107
108
          },
          watch: {
fd1582c5   梁灏   support Menu & La...
109
110
111
112
113
              openNames () {
                  this.$emit('on-open-change', this.openNames);
              },
              activeName (val) {
                  this.currentActiveName = val;
e069c7ae   梁灏   fixed #230
114
              },
fd1582c5   梁灏   support Menu & La...
115
116
              currentActiveName () {
                  this.updateActiveName();
ab673ebc   梁灏   update Menu
117
              }
e05d7289   梁灏   update Menu
118
          }
b0893113   jingsam   :art: add eslint
119
120
      };
  </script>