Blame view

src/components/cascader/caspanel.vue 6.35 KB
c463ab87   梁灏   add Cascader
1
  <template>
47a7f21d   梁灏   support Cascader
2
3
4
5
      <span>
          <ul v-if="data && data.length" :class="[prefixCls + '-menu']">
              <Casitem
                  v-for="item in data"
e6e97035   梁灏   fixed bug under V...
6
                  :key="getKey()"
47a7f21d   梁灏   support Cascader
7
8
9
10
11
12
13
                  :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
14
15
16
  </template>
  <script>
      import Casitem from './casitem.vue';
47a7f21d   梁灏   support Cascader
17
      import Emitter from '../../mixins/emitter';
daa9827c   梁灏   fixed #1553
18
      import { findComponentUpward, findComponentDownward } from '../../utils/assist';
c463ab87   梁灏   add Cascader
19
  
e6e97035   梁灏   fixed bug under V...
20
21
      let key = 1;
  
c463ab87   梁灏   add Cascader
22
23
      export default {
          name: 'Caspanel',
47a7f21d   梁灏   support Cascader
24
          mixins: [ Emitter ],
c463ab87   梁灏   add Cascader
25
26
27
28
29
          components: { Casitem },
          props: {
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
30
                      return [];
c463ab87   梁灏   add Cascader
31
32
                  }
              },
c463ab87   梁灏   add Cascader
33
34
              disabled: Boolean,
              changeOnSelect: Boolean,
bd4e3b9b   梁灏   update Cascader
35
              trigger: String,
c463ab87   梁灏   add Cascader
36
37
38
39
40
              prefixCls: String
          },
          data () {
              return {
                  tmpItem: {},
47a7f21d   梁灏   support Cascader
41
42
                  result: [],
                  sublist: []
b0893113   jingsam   :art: add eslint
43
              };
c463ab87   梁灏   add Cascader
44
          },
47a7f21d   梁灏   support Cascader
45
46
47
48
49
          watch: {
              data () {
                  this.sublist = [];
              }
          },
c463ab87   梁灏   add Cascader
50
51
          methods: {
              handleClickItem (item) {
a5f4a1d4   梁灏   fixed #1922
52
                  if (this.trigger !== 'click' && item.children && item.children.length) return;  // #1922
03a90452   梁灏   update Cascader
53
                  this.handleTriggerItem(item, false, true);
c463ab87   梁灏   add Cascader
54
55
              },
              handleHoverItem (item) {
a5f4a1d4   梁灏   fixed #1922
56
                  if (this.trigger !== 'hover' || !item.children || !item.children.length) return;  // #1922
03a90452   梁灏   update Cascader
57
                  this.handleTriggerItem(item, false, true);
c463ab87   梁灏   add Cascader
58
              },
03a90452   梁灏   update Cascader
59
              handleTriggerItem (item, fromInit = false, fromUser = false) {
c463ab87   梁灏   add Cascader
60
61
                  if (item.disabled) return;
  
c736b0f1   梁灏   fix #4189
62
                  const cascader = findComponentUpward(this, 'Cascader');
f7ffdac5   梁灏   Cascader support ...
63
                  if (item.loading !== undefined && !item.children.length) {
f7ffdac5   梁灏   Cascader support ...
64
65
                      if (cascader && cascader.loadData) {
                          cascader.loadData(item, () => {
03a90452   梁灏   update Cascader
66
67
68
69
                              // todo
                              if (fromUser) {
                                  cascader.isLoadedChildren = true;
                              }
53a6915a   梁灏   fixed Cascader bug
70
71
72
                              if (item.children.length) {
                                  this.handleTriggerItem(item);
                              }
f7ffdac5   梁灏   Cascader support ...
73
74
75
76
77
                          });
                          return;
                      }
                  }
  
83b73885   梁灏   fixed #718
78
                  // return value back recursion  // 向上递归,设置临时选中值(并非真实选中)
bd4e3b9b   梁灏   update Cascader
79
                  const backItem = this.getBaseItem(item);
55649319   梁灏   fix #4998 , ref #...
80
81
82
83
84
85
                  // #5021 for this.changeOnSelect,加 if 是因为 #4472
                  if (
                      this.changeOnSelect ||
                      (backItem.label !== this.tmpItem.label || backItem.value !== this.tmpItem.value) ||
                      (backItem.label === this.tmpItem.label && backItem.value === this.tmpItem.value)
                  ) {
1a6249ad   liyunhang.fe   fix: cascader lim...
86
87
88
                      this.tmpItem = backItem;
                      this.emitUpdate([backItem]);
                  }
55649319   梁灏   fix #4998 , ref #...
89
  
c463ab87   梁灏   add Cascader
90
91
                  if (item.children && item.children.length){
                      this.sublist = item.children;
47a7f21d   梁灏   support Cascader
92
93
94
95
96
                      this.dispatch('Cascader', 'on-result-change', {
                          lastValue: false,
                          changeOnSelect: this.changeOnSelect,
                          fromInit: fromInit
                      });
daa9827c   梁灏   fixed #1553
97
98
99
100
101
102
103
104
  
                      // #1553
                      if (this.changeOnSelect) {
                          const Caspanel = findComponentDownward(this, 'Caspanel');
                          if (Caspanel) {
                              Caspanel.$emit('on-clear', true);
                          }
                      }
c463ab87   梁灏   add Cascader
105
106
                  } else {
                      this.sublist = [];
47a7f21d   梁灏   support Cascader
107
108
109
110
111
                      this.dispatch('Cascader', 'on-result-change', {
                          lastValue: true,
                          changeOnSelect: this.changeOnSelect,
                          fromInit: fromInit
                      });
c463ab87   梁灏   add Cascader
112
                  }
c736b0f1   梁灏   fix #4189
113
114
115
116
  
                  if (cascader) {
                      cascader.$refs.drop.update();
                  }
c463ab87   梁灏   add Cascader
117
118
119
              },
              updateResult (item) {
                  this.result = [this.tmpItem].concat(item);
bd4e3b9b   梁灏   update Cascader
120
                  this.emitUpdate(this.result);
c463ab87   梁灏   add Cascader
121
122
123
124
125
126
127
128
              },
              getBaseItem (item) {
                  let backItem = Object.assign({}, item);
                  if (backItem.children) {
                      delete backItem.children;
                  }
  
                  return backItem;
bd4e3b9b   梁灏   update Cascader
129
130
131
132
133
134
135
              },
              emitUpdate (result) {
                  if (this.$parent.$options.name === 'Caspanel') {
                      this.$parent.updateResult(result);
                  } else {
                      this.$parent.$parent.updateResult(result);
                  }
e6e97035   梁灏   fixed bug under V...
136
137
138
              },
              getKey () {
                  return key++;
c463ab87   梁灏   add Cascader
139
140
              }
          },
47a7f21d   梁灏   support Cascader
141
142
143
          mounted () {
              this.$on('on-find-selected', (params) => {
                  const val = params.value;
bd4e3b9b   梁灏   update Cascader
144
145
146
147
148
149
150
                  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
151
152
153
                                  this.broadcast('Caspanel', 'on-find-selected', {
                                      value: value
                                  });
bd4e3b9b   梁灏   update Cascader
154
155
156
157
158
                              });
                              return false;
                          }
                      }
                  }
47a7f21d   梁灏   support Cascader
159
              });
daa9827c   梁灏   fixed #1553
160
161
              // deep for #1553
              this.$on('on-clear', (deep = false) => {
165bb7c9   梁灏   update Cascader
162
163
                  this.sublist = [];
                  this.tmpItem = {};
daa9827c   梁灏   fixed #1553
164
165
166
167
168
169
                  if (deep) {
                      const Caspanel = findComponentDownward(this, 'Caspanel');
                      if (Caspanel) {
                          Caspanel.$emit('on-clear', true);
                      }
                  }
47a7f21d   梁灏   support Cascader
170
              });
c463ab87   梁灏   add Cascader
171
          }
b0893113   jingsam   :art: add eslint
172
173
      };
  </script>