Blame view

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