Blame view

components/ly-tree/tool/util_1.js 2.44 KB
290144e9   易尊强   第一次
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
  export const NODE_KEY = '$treeNodeId';
  
  export const markNodeData = function(node, data) {
  	if (!data || data[NODE_KEY]) return;
  	Object.defineProperty(data, NODE_KEY, {
  		value: node.id,
  		enumerable: false,
  		configurable: false,
  		writable: false
  	});
  };
  
  export const getNodeKey = function(key, data) {
  	if (!data) return null;
  	if (!key) return data[NODE_KEY];
  	return data[key];
  };
  
  export const objectAssign = function(target) {
  	for (let i = 1, j = arguments.length; i < j; i++) {
  		let source = arguments[i] || {};
  		for (let prop in source) {
  			if (source.hasOwnProperty(prop)) {
  				let value = source[prop];
  				if (value !== undefined) {
  					target[prop] = value;
  				}
  			}
  		}
  	}
  
  	return target;
  };
  
  // TODO: use native Array.find, Array.findIndex when IE support is dropped
  export const arrayFindIndex = function(arr, pred) {
  	for (let i = 0; i !== arr.length; ++i) {
  		if (pred(arr[i])) {
  			return i;
  		}
  	}
  	return -1;
  };
  
  export const getChildState = function(node) {
  	let all = true;
  	let none = true;
  	let allWithoutDisable = true;
  	for (let i = 0, j = node.length; i < j; i++) {
  		const n = node[i];
  		if (n.checked !== true || n.indeterminate) {
  			all = false;
  			if (!n.disabled) {
  				allWithoutDisable = false;
  			}
  		}
  		if (n.checked !== false || n.indeterminate) {
  			none = false;
  		}
  	}
  
  	return {
  		all,
  		none,
  		allWithoutDisable,
  		half: !all && !none
  	};
  };
  
  export const reInitChecked = function(node) {
  	if (!node || node.childNodesId.length === 0) return;
  
  	let childNodes = node.getChildNodes(node.childNodesId);
  	const {
  		all,
  		none,
  		half
  	} = getChildState(childNodes);
  	if (all) {
  		node.checked = true;
  		node.indeterminate = false;
  	} else if (half) {
  		node.checked = false;
  		node.indeterminate = true;
  	} else if (none) {
  		node.checked = false;
  		node.indeterminate = false;
  	}
  
  	let parent = node.getParent(node.parentId);
  	if (!parent || parent.level === 0) return;
  
  	if (!node.store().checkStrictly) {
  		reInitChecked(parent);
  	}
  };
  
  export const getPropertyFromData = function(node, prop) {
  	const props = node.store().props;
  	const data = node.data || {};
  	const config = props[prop];
  
  	if (typeof config === 'function') {
  		return config(data, node);
  	} else if (typeof config === 'string') {
  		return data[config];
  	} else if (typeof config === 'undefined') {
  		const dataProp = data[prop];
  		return dataProp === undefined ? '' : dataProp;
  	}
  };
  
  export const isNull = function(v) {
  	return v === undefined || v === null || v === '';
  }