保誠-保戶業務員媒合平台
tomasysh
2025-01-02 4efb98f2b554b76270b12837db7a7f724e2ede89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { isEqual, capitalize } from 'element-ui/src/utils/util';
import { isDef } from 'element-ui/src/utils/shared';
 
let uid = 0;
 
export default class Node {
 
  constructor(data, config, parentNode) {
    this.data = data;
    this.config = config;
    this.parent = parentNode || null;
    this.level = !this.parent ? 1 : this.parent.level + 1;
    this.uid = uid++;
 
    this.initState();
    this.initChildren();
  }
 
  initState() {
    const { value: valueKey, label: labelKey } = this.config;
 
    this.value = this.data[valueKey];
    this.label = this.data[labelKey];
    this.pathNodes = this.calculatePathNodes();
    this.path = this.pathNodes.map(node => node.value);
    this.pathLabels = this.pathNodes.map(node => node.label);
 
    // lazy load
    this.loading = false;
    this.loaded = false;
  }
 
  initChildren() {
    const { config } = this;
    const childrenKey = config.children;
    const childrenData = this.data[childrenKey];
    this.hasChildren = Array.isArray(childrenData);
    this.children = (childrenData || []).map(child => new Node(child, config, this));
  }
 
  get isDisabled() {
    const { data, parent, config } = this;
    const disabledKey = config.disabled;
    const { checkStrictly } = config;
    return data[disabledKey] ||
      !checkStrictly && parent && parent.isDisabled;
  }
 
  get isLeaf() {
    const { data, loaded, hasChildren, children } = this;
    const { lazy, leaf: leafKey } = this.config;
    if (lazy) {
      const isLeaf = isDef(data[leafKey])
        ? data[leafKey]
        : (loaded ? !children.length : false);
      this.hasChildren = !isLeaf;
      return isLeaf;
    }
    return !hasChildren;
  }
 
  calculatePathNodes() {
    const nodes = [this];
    let parent = this.parent;
 
    while (parent) {
      nodes.unshift(parent);
      parent = parent.parent;
    }
 
    return nodes;
  }
 
  getPath() {
    return this.path;
  }
 
  getValue() {
    return this.value;
  }
 
  getValueByOption() {
    return this.config.emitPath
      ? this.getPath()
      : this.getValue();
  }
 
  getText(allLevels, separator) {
    return allLevels ? this.pathLabels.join(separator) : this.label;
  }
 
  isSameNode(checkedValue) {
    const value = this.getValueByOption();
    return this.config.multiple && Array.isArray(checkedValue)
      ? checkedValue.some(val => isEqual(val, value))
      : isEqual(checkedValue, value);
  }
 
  broadcast(event, ...args) {
    const handlerName = `onParent${capitalize(event)}`;
 
    this.children.forEach(child => {
      if (child) {
        // bottom up
        child.broadcast(event, ...args);
        child[handlerName] && child[handlerName](...args);
      }
    });
  }
 
  emit(event, ...args) {
    const { parent } = this;
    const handlerName = `onChild${capitalize(event)}`;
    if (parent) {
      parent[handlerName] && parent[handlerName](...args);
      parent.emit(event, ...args);
    }
  }
 
  onParentCheck(checked) {
    if (!this.isDisabled) {
      this.setCheckState(checked);
    }
  }
 
  onChildCheck() {
    const { children } = this;
    const validChildren = children.filter(child => !child.isDisabled);
    const checked = validChildren.length
      ? validChildren.every(child => child.checked)
      : false;
 
    this.setCheckState(checked);
  }
 
  setCheckState(checked) {
    const totalNum = this.children.length;
    const checkedNum = this.children.reduce((c, p) => {
      const num = p.checked ? 1 : (p.indeterminate ? 0.5 : 0);
      return c + num;
    }, 0);
 
    this.checked = checked;
    this.indeterminate = checkedNum !== totalNum && checkedNum > 0;
  }
 
  syncCheckState(checkedValue) {
    const value = this.getValueByOption();
    const checked = this.isSameNode(checkedValue, value);
 
    this.doCheck(checked);
  }
 
  doCheck(checked) {
    if (this.checked !== checked) {
      if (this.config.checkStrictly) {
        this.checked = checked;
      } else {
        // bottom up to unify the calculation of the indeterminate state
        this.broadcast('check', checked);
        this.setCheckState(checked);
        this.emit('check');
      }
    }
  }
}