yylib-quick-mobile
Version:
yylib-quick-mobile
88 lines (84 loc) • 3.76 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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;
}
var TreeUtils = {
loopAll: function loopAll(childs, callback, parent) {
var loop = function loop(children, level, _parent) {
var len = getChildrenlength(children);
children.forEach(function (item, index) {
var pos = level + "-" + index;
if (item) {
if (item.children && item.children.constructor == Array) {
loop(item.children, pos, { node: item, pos: pos });
}
callback(item, index, pos, item.key || pos, getSiblingPosition(index, len, {}), _parent);
}
});
};
loop(childs, 0, parent);
},
findWithPropName: function findWithPropName(nodes, propName, propValue, loop, only, childPropName) {
var _results = [];
var _nodes = nodes && nodes.constructor == Array ? nodes : [];
var _childPropName = childPropName ? childPropName : 'children';
for (var i = 0; i < _nodes.length; i++) {
var node = _nodes[i];
if (!node || (typeof node === "undefined" ? "undefined" : _typeof(node)) != "object") continue;
if (node[propName] && node[propName] == propValue) {
_results.push(node);
if (only == undefined || only == true) break;
}
if (loop != false && node[_childPropName] && node[_childPropName].length > 0) {
var currResults = this.findWithPropName(node[_childPropName], propName, propValue, loop, only, _childPropName);
if (currResults != null) {
_results = _results.concat(currResults);
}
}
}
return _results;
},
removeWithPropName: function removeWithPropName(tree, propName, propValue, loop) {
var childName = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : "children";
var doLoop = function doLoop(data, propName, propVal, callback) {
data.forEach(function (item, index, arr) {
if (item[propName] === propVal) {
return callback(item, index, arr);
}
if (loop != false && item[childName] && item[childName].constructor == Array) {
return doLoop(item[childName], propName, propVal, callback);
}
});
};
if (tree && propName && propValue) {
doLoop(tree && tree.constructor == Array ? tree : [tree], propName, propValue, function (item, index, arr) {
arr.splice(index, 1);
});
}
return tree;
},
findById: function findById(nodes, idValue) {
var results = this.findWithPropName(nodes, 'id', idValue, true, true);
return results.length > 0 ? results[0] : null;
},
removeById: function removeById(tree, nodeId) {
var tree = this.removeWithPropName(tree, 'id', nodeId, true);
return tree;
}
};
module.exports = TreeUtils;
;