Blame view

src/components/cascader/caspanel.vue 3.83 KB
c463ab87   梁灏   add Cascader
1
2
3
4
5
6
7
8
9
  <template>
      <ul v-if="data && data.length" :class="[prefixCls + '-menu']">
          <Casitem
              v-for="item in data"
              :prefix-cls="prefixCls"
              :data.sync="item"
              :tmp-item="tmpItem"
              @click.stop="handleClickItem(item)"
              @mouseenter.stop="handleHoverItem(item)"></Casitem>
bd4e3b9b   梁灏   update Cascader
10
      </ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data.sync="sublist" :disabled="disabled" :trigger="trigger" :change-on-select="changeOnSelect"></Caspanel>
c463ab87   梁灏   add Cascader
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  </template>
  <script>
      import Casitem from './casitem.vue';
      import { oneOf } from '../../utils/assist';
  
      export default {
          name: 'Caspanel',
          components: { Casitem },
          props: {
              data: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              sublist: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              disabled: Boolean,
              changeOnSelect: Boolean,
bd4e3b9b   梁灏   update Cascader
34
              trigger: String,
c463ab87   梁灏   add Cascader
35
36
37
38
39
40
41
42
43
44
              prefixCls: String
          },
          data () {
              return {
                  tmpItem: {},
                  result: []
              }
          },
          methods: {
              handleClickItem (item) {
bd4e3b9b   梁灏   update Cascader
45
                  if (this.trigger !== 'click' && item.children) return;
c463ab87   梁灏   add Cascader
46
47
48
                  this.handleTriggerItem(item);
              },
              handleHoverItem (item) {
bd4e3b9b   梁灏   update Cascader
49
                  if (this.trigger !== 'hover' || !item.children) return;
c463ab87   梁灏   add Cascader
50
51
                  this.handleTriggerItem(item);
              },
bd4e3b9b   梁灏   update Cascader
52
              handleTriggerItem (item, fromInit = false) {
c463ab87   梁灏   add Cascader
53
54
                  if (item.disabled) return;
  
bd4e3b9b   梁灏   update Cascader
55
56
57
58
59
                  // return value back recursion
                  const backItem = this.getBaseItem(item);
                  this.tmpItem = backItem;
                  this.emitUpdate([backItem]);
  
c463ab87   梁灏   add Cascader
60
61
                  if (item.children && item.children.length){
                      this.sublist = item.children;
bd4e3b9b   梁灏   update Cascader
62
                      this.$dispatch('on-result-change', false, this.changeOnSelect, fromInit);
c463ab87   梁灏   add Cascader
63
64
                  } else {
                      this.sublist = [];
bd4e3b9b   梁灏   update Cascader
65
                      this.$dispatch('on-result-change', true, this.changeOnSelect, fromInit);
c463ab87   梁灏   add Cascader
66
                  }
c463ab87   梁灏   add Cascader
67
68
69
              },
              updateResult (item) {
                  this.result = [this.tmpItem].concat(item);
bd4e3b9b   梁灏   update Cascader
70
                  this.emitUpdate(this.result);
c463ab87   梁灏   add Cascader
71
72
73
74
75
76
77
78
              },
              getBaseItem (item) {
                  let backItem = Object.assign({}, item);
                  if (backItem.children) {
                      delete backItem.children;
                  }
  
                  return backItem;
bd4e3b9b   梁灏   update Cascader
79
80
81
82
83
84
85
              },
              emitUpdate (result) {
                  if (this.$parent.$options.name === 'Caspanel') {
                      this.$parent.updateResult(result);
                  } else {
                      this.$parent.$parent.updateResult(result);
                  }
c463ab87   梁灏   add Cascader
86
87
88
89
90
91
92
              }
          },
          watch: {
              data () {
                  this.sublist = [];
              }
          },
bd4e3b9b   梁灏   update Cascader
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
          events: {
              'on-find-selected' (val) {
                  let value = [...val];
                  for (let i = 0; i < value.length; i++) {
                      for (let j = 0; j < this.data.length; j++) {
                          if (value[i] === this.data[j].value) {
                              this.handleTriggerItem(this.data[j], true);
                              value.splice(0, 1);
                              this.$nextTick(() => {
                                  this.$broadcast('on-find-selected', value);
                              });
                              return false;
                          }
                      }
                  }
165bb7c9   梁灏   update Cascader
108
109
110
111
              },
              'on-clear' () {
                  this.sublist = [];
                  this.tmpItem = {};
bd4e3b9b   梁灏   update Cascader
112
              }
c463ab87   梁灏   add Cascader
113
114
115
          }
      }
  </script>