Blame view

src/components/cascader/caspanel.vue 5.03 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"
8ac8d1ed   梁灏   修复警告
6
                  :key="item"
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';
f7ffdac5   梁灏   Cascader support ...
18
      import { findComponentUpward } from '../../utils/assist';
c463ab87   梁灏   add Cascader
19
20
21
  
      export default {
          name: 'Caspanel',
47a7f21d   梁灏   support Cascader
22
          mixins: [ Emitter ],
c463ab87   梁灏   add Cascader
23
24
25
26
27
          components: { Casitem },
          props: {
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
28
                      return [];
c463ab87   梁灏   add Cascader
29
30
                  }
              },
c463ab87   梁灏   add Cascader
31
32
              disabled: Boolean,
              changeOnSelect: Boolean,
bd4e3b9b   梁灏   update Cascader
33
              trigger: String,
c463ab87   梁灏   add Cascader
34
35
36
37
38
              prefixCls: String
          },
          data () {
              return {
                  tmpItem: {},
47a7f21d   梁灏   support Cascader
39
40
                  result: [],
                  sublist: []
b0893113   jingsam   :art: add eslint
41
              };
c463ab87   梁灏   add Cascader
42
          },
47a7f21d   梁灏   support Cascader
43
44
45
46
47
          watch: {
              data () {
                  this.sublist = [];
              }
          },
c463ab87   梁灏   add Cascader
48
49
          methods: {
              handleClickItem (item) {
bd4e3b9b   梁灏   update Cascader
50
                  if (this.trigger !== 'click' && item.children) return;
03a90452   梁灏   update Cascader
51
                  this.handleTriggerItem(item, false, true);
c463ab87   梁灏   add Cascader
52
53
              },
              handleHoverItem (item) {
bd4e3b9b   梁灏   update Cascader
54
                  if (this.trigger !== 'hover' || !item.children) return;
03a90452   梁灏   update Cascader
55
                  this.handleTriggerItem(item, false, true);
c463ab87   梁灏   add Cascader
56
              },
03a90452   梁灏   update Cascader
57
              handleTriggerItem (item, fromInit = false, fromUser = false) {
c463ab87   梁灏   add Cascader
58
59
                  if (item.disabled) return;
  
f7ffdac5   梁灏   Cascader support ...
60
61
62
63
                  if (item.loading !== undefined && !item.children.length) {
                      const cascader = findComponentUpward(this, 'Cascader');
                      if (cascader && cascader.loadData) {
                          cascader.loadData(item, () => {
03a90452   梁灏   update Cascader
64
65
66
67
                              // todo
                              if (fromUser) {
                                  cascader.isLoadedChildren = true;
                              }
f7ffdac5   梁灏   Cascader support ...
68
69
70
71
72
73
                              this.handleTriggerItem(item);
                          });
                          return;
                      }
                  }
  
83b73885   梁灏   fixed #718
74
                  // return value back recursion  // 向上递归,设置临时选中值(并非真实选中)
bd4e3b9b   梁灏   update Cascader
75
76
77
                  const backItem = this.getBaseItem(item);
                  this.tmpItem = backItem;
                  this.emitUpdate([backItem]);
c463ab87   梁灏   add Cascader
78
79
                  if (item.children && item.children.length){
                      this.sublist = item.children;
47a7f21d   梁灏   support Cascader
80
81
82
83
84
                      this.dispatch('Cascader', 'on-result-change', {
                          lastValue: false,
                          changeOnSelect: this.changeOnSelect,
                          fromInit: fromInit
                      });
c463ab87   梁灏   add Cascader
85
86
                  } else {
                      this.sublist = [];
47a7f21d   梁灏   support Cascader
87
88
89
90
91
                      this.dispatch('Cascader', 'on-result-change', {
                          lastValue: true,
                          changeOnSelect: this.changeOnSelect,
                          fromInit: fromInit
                      });
c463ab87   梁灏   add Cascader
92
                  }
c463ab87   梁灏   add Cascader
93
94
95
              },
              updateResult (item) {
                  this.result = [this.tmpItem].concat(item);
bd4e3b9b   梁灏   update Cascader
96
                  this.emitUpdate(this.result);
c463ab87   梁灏   add Cascader
97
98
99
100
101
102
103
104
              },
              getBaseItem (item) {
                  let backItem = Object.assign({}, item);
                  if (backItem.children) {
                      delete backItem.children;
                  }
  
                  return backItem;
bd4e3b9b   梁灏   update Cascader
105
106
107
108
109
110
111
              },
              emitUpdate (result) {
                  if (this.$parent.$options.name === 'Caspanel') {
                      this.$parent.updateResult(result);
                  } else {
                      this.$parent.$parent.updateResult(result);
                  }
c463ab87   梁灏   add Cascader
112
113
              }
          },
47a7f21d   梁灏   support Cascader
114
115
116
          mounted () {
              this.$on('on-find-selected', (params) => {
                  const val = params.value;
bd4e3b9b   梁灏   update Cascader
117
118
119
120
121
122
123
                  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
124
125
126
                                  this.broadcast('Caspanel', 'on-find-selected', {
                                      value: value
                                  });
bd4e3b9b   梁灏   update Cascader
127
128
129
130
131
                              });
                              return false;
                          }
                      }
                  }
47a7f21d   梁灏   support Cascader
132
133
              });
              this.$on('on-clear', () => {
165bb7c9   梁灏   update Cascader
134
135
                  this.sublist = [];
                  this.tmpItem = {};
47a7f21d   梁灏   support Cascader
136
              });
c463ab87   梁灏   add Cascader
137
          }
b0893113   jingsam   :art: add eslint
138
139
      };
  </script>