327 lines
8.7 KiB
JavaScript
327 lines
8.7 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.browser = browser;
|
|
exports.getOffset = getOffset;
|
|
exports.loopAllChildren = loopAllChildren;
|
|
exports.isInclude = isInclude;
|
|
exports.filterParentPosition = filterParentPosition;
|
|
exports.handleCheckState = handleCheckState;
|
|
exports.getCheck = getCheck;
|
|
exports.getStrictlyValue = getStrictlyValue;
|
|
exports.arraysEqual = arraysEqual;
|
|
|
|
var _react = require('react');
|
|
|
|
var _react2 = _interopRequireDefault(_react);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
|
|
function browser(navigator) {
|
|
var tem = void 0;
|
|
var ua = navigator.userAgent;
|
|
var M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
|
|
if (/trident/i.test(M[1])) {
|
|
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
|
|
return 'IE ' + (tem[1] || '');
|
|
}
|
|
if (M[1] === 'Chrome') {
|
|
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
|
|
if (tem) return tem.slice(1).join(' ').replace('OPR', 'Opera');
|
|
}
|
|
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
|
|
tem = ua.match(/version\/(\d+)/i);
|
|
if (tem) {
|
|
M.splice(1, 1, tem[1]);
|
|
}
|
|
return M.join(' ');
|
|
}
|
|
|
|
// export function getOffset(el) {
|
|
// const obj = el.getBoundingClientRect();
|
|
// return {
|
|
// left: obj.left + document.body.scrollLeft,
|
|
// top: obj.top + document.body.scrollTop,
|
|
// width: obj.width,
|
|
// height: obj.height
|
|
// };
|
|
// }
|
|
|
|
// // iscroll offset
|
|
// offset = function (el) {
|
|
// var left = -el.offsetLeft,
|
|
// top = -el.offsetTop;
|
|
|
|
// // jshint -W084
|
|
// while (el = el.offsetParent) {
|
|
// left -= el.offsetLeft;
|
|
// top -= el.offsetTop;
|
|
// }
|
|
// // jshint +W084
|
|
|
|
// return {
|
|
// left: left,
|
|
// top: top
|
|
// };
|
|
// }
|
|
|
|
/* eslint-disable */
|
|
/* eslint no-loop-func: 0*/
|
|
|
|
function getOffset(ele) {
|
|
var doc = void 0,
|
|
win = void 0,
|
|
docElem = void 0,
|
|
rect = void 0;
|
|
|
|
if (!ele.getClientRects().length) {
|
|
return { top: 0, left: 0 };
|
|
}
|
|
|
|
rect = ele.getBoundingClientRect();
|
|
|
|
if (rect.width || rect.height) {
|
|
doc = ele.ownerDocument;
|
|
win = doc.defaultView;
|
|
docElem = doc.documentElement;
|
|
|
|
return {
|
|
top: rect.top + win.pageYOffset - docElem.clientTop,
|
|
left: rect.left + win.pageXOffset - docElem.clientLeft
|
|
};
|
|
}
|
|
|
|
return rect;
|
|
}
|
|
/* eslint-enable */
|
|
|
|
function getChildrenlength(children) {
|
|
var len = 1;
|
|
if (Array.isArray(children)) {
|
|
len = children.length;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
function getSiblingPosition(index, len, siblingPosition) {
|
|
if (len === 1) {
|
|
siblingPosition.first = true;
|
|
siblingPosition.last = true;
|
|
} else {
|
|
siblingPosition.first = index === 0;
|
|
siblingPosition.last = index === len - 1;
|
|
}
|
|
return siblingPosition;
|
|
}
|
|
|
|
function loopAllChildren(childs, callback, parent) {
|
|
var loop = function loop(children, level, _parent) {
|
|
var len = getChildrenlength(children);
|
|
_react2["default"].Children.forEach(children, function (item, index) {
|
|
var pos = level + '-' + index;
|
|
if (item.props.children && item.type && item.type.isTreeNode) {
|
|
loop(item.props.children, pos, { node: item, pos: pos });
|
|
}
|
|
callback(item, index, pos, item.key || pos, getSiblingPosition(index, len, {}), _parent);
|
|
});
|
|
};
|
|
loop(childs, 0, parent);
|
|
}
|
|
|
|
function isInclude(smallArray, bigArray) {
|
|
return smallArray.every(function (ii, i) {
|
|
return ii === bigArray[i];
|
|
});
|
|
}
|
|
// console.log(isInclude(['0', '1'], ['0', '10', '1']));
|
|
|
|
|
|
// arr.length === 628, use time: ~20ms
|
|
function filterParentPosition(arr) {
|
|
var levelObj = {};
|
|
arr.forEach(function (item) {
|
|
var posLen = item.split('-').length;
|
|
if (!levelObj[posLen]) {
|
|
levelObj[posLen] = [];
|
|
}
|
|
levelObj[posLen].push(item);
|
|
});
|
|
var levelArr = Object.keys(levelObj).sort();
|
|
|
|
var _loop = function _loop(i) {
|
|
if (levelArr[i + 1]) {
|
|
levelObj[levelArr[i]].forEach(function (ii) {
|
|
var _loop2 = function _loop2(j) {
|
|
levelObj[levelArr[j]].forEach(function (_i, index) {
|
|
if (isInclude(ii.split('-'), _i.split('-'))) {
|
|
levelObj[levelArr[j]][index] = null;
|
|
}
|
|
});
|
|
levelObj[levelArr[j]] = levelObj[levelArr[j]].filter(function (p) {
|
|
return p;
|
|
});
|
|
};
|
|
|
|
for (var j = i + 1; j < levelArr.length; j++) {
|
|
_loop2(j);
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
for (var i = 0; i < levelArr.length; i++) {
|
|
_loop(i);
|
|
}
|
|
var nArr = [];
|
|
levelArr.forEach(function (i) {
|
|
nArr = nArr.concat(levelObj[i]);
|
|
});
|
|
return nArr;
|
|
}
|
|
// console.log(filterParentPosition(
|
|
// ['0-2', '0-3-3', '0-10', '0-10-0', '0-0-1', '0-0', '0-1-1', '0-1']
|
|
// ));
|
|
|
|
|
|
function stripTail(str) {
|
|
var arr = str.match(/(.+)(-[^-]+)$/);
|
|
var st = '';
|
|
if (arr && arr.length === 3) {
|
|
st = arr[1];
|
|
}
|
|
return st;
|
|
}
|
|
function splitPosition(pos) {
|
|
return pos.split('-');
|
|
}
|
|
|
|
function handleCheckState(obj, checkedPositionArr, checkIt) {
|
|
// console.log(stripTail('0-101-000'));
|
|
var objKeys = Object.keys(obj);
|
|
// let s = Date.now();
|
|
objKeys.forEach(function (i, index) {
|
|
var iArr = splitPosition(i);
|
|
var saved = false;
|
|
checkedPositionArr.forEach(function (_pos) {
|
|
// 设置子节点,全选或全不选
|
|
var _posArr = splitPosition(_pos);
|
|
if (iArr.length > _posArr.length && isInclude(_posArr, iArr)) {
|
|
obj[i].halfChecked = false;
|
|
obj[i].checked = checkIt;
|
|
objKeys[index] = null;
|
|
}
|
|
if (iArr[0] === _posArr[0] && iArr[1] === _posArr[1]) {
|
|
// 如果
|
|
saved = true;
|
|
}
|
|
});
|
|
if (!saved) {
|
|
objKeys[index] = null;
|
|
}
|
|
});
|
|
// TODO: 循环 2470000 次耗时约 1400 ms。 性能瓶颈!
|
|
// console.log(Date.now()-s, checkedPositionArr.length * objKeys.length);
|
|
objKeys = objKeys.filter(function (i) {
|
|
return i;
|
|
}); // filter non null;
|
|
|
|
var _loop3 = function _loop3(_pIndex) {
|
|
// 循环设置父节点的 选中 或 半选状态
|
|
var loop = function loop(__pos) {
|
|
var _posLen = splitPosition(__pos).length;
|
|
if (_posLen <= 2) {
|
|
// e.g. '0-0', '0-1'
|
|
return;
|
|
}
|
|
var sibling = 0;
|
|
var siblingChecked = 0;
|
|
var parentPosition = stripTail(__pos);
|
|
objKeys.forEach(function (i /* , index*/) {
|
|
var iArr = splitPosition(i);
|
|
if (iArr.length === _posLen && isInclude(splitPosition(parentPosition), iArr)) {
|
|
sibling++;
|
|
if (obj[i].checked) {
|
|
siblingChecked++;
|
|
var _i = checkedPositionArr.indexOf(i);
|
|
if (_i > -1) {
|
|
checkedPositionArr.splice(_i, 1);
|
|
if (_i <= _pIndex) {
|
|
_pIndex--;
|
|
}
|
|
}
|
|
} else if (obj[i].halfChecked) {
|
|
siblingChecked += 0.5;
|
|
}
|
|
// objKeys[index] = null;
|
|
}
|
|
});
|
|
// objKeys = objKeys.filter(i => i); // filter non null;
|
|
var parent = obj[parentPosition];
|
|
// sibling 不会等于0
|
|
// 全不选 - 全选 - 半选
|
|
if (siblingChecked === 0) {
|
|
parent.checked = false;
|
|
parent.halfChecked = false;
|
|
} else if (siblingChecked === sibling) {
|
|
parent.checked = true;
|
|
parent.halfChecked = false;
|
|
} else {
|
|
parent.halfChecked = true;
|
|
parent.checked = false;
|
|
}
|
|
loop(parentPosition);
|
|
};
|
|
loop(checkedPositionArr[_pIndex], _pIndex);
|
|
pIndex = _pIndex;
|
|
};
|
|
|
|
for (var pIndex = 0; pIndex < checkedPositionArr.length; pIndex++) {
|
|
_loop3(pIndex);
|
|
}
|
|
// console.log(Date.now()-s, objKeys.length, checkIt);
|
|
}
|
|
|
|
function getCheck(treeNodesStates) {
|
|
var halfCheckedKeys = [];
|
|
var checkedKeys = [];
|
|
var checkedNodes = [];
|
|
var checkedNodesPositions = [];
|
|
Object.keys(treeNodesStates).forEach(function (item) {
|
|
var itemObj = treeNodesStates[item];
|
|
if (itemObj.checked) {
|
|
checkedKeys.push(itemObj.key);
|
|
checkedNodes.push(itemObj.node);
|
|
checkedNodesPositions.push({ node: itemObj.node, pos: item });
|
|
} else if (itemObj.halfChecked) {
|
|
halfCheckedKeys.push(itemObj.key);
|
|
}
|
|
});
|
|
return {
|
|
halfCheckedKeys: halfCheckedKeys, checkedKeys: checkedKeys, checkedNodes: checkedNodes, checkedNodesPositions: checkedNodesPositions, treeNodesStates: treeNodesStates
|
|
};
|
|
}
|
|
|
|
function getStrictlyValue(checkedKeys, halfChecked) {
|
|
if (halfChecked) {
|
|
return { checked: checkedKeys, halfChecked: halfChecked };
|
|
}
|
|
return checkedKeys;
|
|
}
|
|
|
|
function arraysEqual(a, b) {
|
|
if (a === b) return true;
|
|
if (a === null || typeof a === 'undefined' || b === null || typeof b === 'undefined') {
|
|
return false;
|
|
}
|
|
if (a.length !== b.length) return false;
|
|
|
|
// If you don't care about the order of the elements inside
|
|
// the array, you should sort both arrays here.
|
|
|
|
for (var i = 0; i < a.length; ++i) {
|
|
if (a[i] !== b[i]) return false;
|
|
}
|
|
return true;
|
|
} |