amos-tool
Version:
amos ui tool
232 lines (216 loc) • 6.84 kB
JavaScript
;
var _utils = require("./utils"), _trim = require("./_trim"), _deepCopy = require("./_deepCopy");
function getChildrenlength(e) {
var t = 1;
return _utils.isArray(e) && (t = e.length), t;
}
function flatToHierarchy(e) {
if (!e.length) return e;
var t = [], n = {};
e.forEach(function(e) {
if (e.pos) {
var t = e.pos.split("-").length;
n[t] || (n[t] = []), n[t].push(e);
}
});
var r = Object.keys(n).sort(function(e, t) {
return t - e;
});
return _utils.reduce(r, function(e, r) {
return r && r !== e && n[e].forEach(function(e) {
var l = !1;
n[r].forEach(function(t) {
isInclude(t.pos.split("-"), e.pos.split("-")) && (l = !0, t.children || (t.children = []),
t.children.push(e));
}), l || t.push(e);
}), r;
}), n[r[r.length - 1]].concat(t);
}
function filterParentPosition(e) {
var t = {};
e.forEach(function(e) {
var n = e.split("-").length;
t[n] || (t[n] = []), t[n].push(e);
});
for (var n = Object.keys(t).sort(), r = 0; r < n.length; r++) n[r + 1] && t[n[r]].forEach(function(e) {
for (var l = r + 1; l < n.length; l++) t[n[l]].forEach(function(r, c) {
isInclude(e.split("-"), r.split("-")) && (t[n[l]][c] = null);
}), t[n[l]] = t[n[l]].filter(function(e) {
return e;
});
});
var l = [];
return n.forEach(function(e) {
l = l.concat(t[e]);
}), l;
}
function isInclude(e, t) {
return _utils.every(e, function(e, n) {
return e === t[n];
});
}
function filterAllCheckedData(e, t) {
if (!e.length) return e;
var n = recursiveGen(t), r = [];
return function t(n) {
n.forEach(function(n) {
if (!n.__checked) {
var l = e.indexOf(n.value), c = n.children;
l > -1 ? (n.__checked = !0, r.push({
node: n,
pos: n._pos
}), e.splice(l, 1), c && recursive(c, function(e) {
e.__checked = !0, r.push({
node: e,
pos: e._pos
});
})) : c && t(c);
}
});
}(n), function e(t, n) {
n || (n = {
root: !0
});
var l = 0;
t.forEach(function(t) {
var n = t.children;
if (!n || t.__checked || t.__halfChecked) t.__checked ? l++ : t.__halfChecked && (l += .5); else {
var r = e(n, t);
r.__checked ? l++ : r.__halfChecked && (l += .5);
}
});
var c = t.length;
return l === c ? (n.__checked = !0, r.push({
node: n,
pos: n._pos
})) : l < c && l > 0 && (n.__halfChecked = !0), n.root ? t : n;
}(n), r.forEach(function(e, t) {
delete r[t].node.__checked, delete r[t].node._pos, r[t].node.props = {
title: r[t].node.title,
label: r[t].node.label || r[t].node.title,
value: r[t].node.value
}, r[t].node.children && (r[t].node.props.children = r[t].node.children), delete r[t].node.title,
delete r[t].node.label, delete r[t].node.value, delete r[t].node.children;
}), r;
}
function recursiveGen(e, t) {
return t || (t = 0), e.map(function(e, n) {
var r = t + "-" + n, l = {
title: e.title,
label: e.label || e.title,
value: e.value,
key: e.key,
_pos: r
};
return e.children && (l.children = recursiveGen(e.children, r)), l;
});
}
function recursive(e, t) {
e.forEach(function(e) {
t(e), e.children && recursive(e.children, t);
});
}
function flatCustomArr(e, t) {
if (e = _trim(e), _utils.isEmpty(e)) return t;
if (!t.length) return t;
var n = [], r = {};
!function t(n) {
n.forEach(function(n) {
n.code && (0 == n.level || "0" == n.level || n.level) && (n.children && n.children.length > 0 ? (r[n.level] || (r[n.level] = []),
r[n.level].push(n), t(n.children), n.children = []) : (r[n.level] || (r[n.level] = []),
n.label.indexOf(e) > -1 && r[n.level].push(n)));
});
}(t);
var l = Object.keys(r).sort(function(e, t) {
return t - e;
});
return _utils.reduce(l, function(e, t) {
return t && t !== e && r[e].forEach(function(e, l) {
var c = !1;
r[t].forEach(function(t) {
isInclude(t.pos.split("-"), e.pos.split("-")) && (c = !0, t.children || (t.children = []),
t.children.push(e));
}), c || n.push(e);
}), t;
}), r[l[l.length - 1]].concat(n);
}
function recombineFlatData(e, t) {
return flatCustomArr(e, t);
}
function flatMatchedData2Hierarchy(e) {
if (!e.length) return e;
var t = [], n = {};
e.forEach(function(e) {
if (e.code) {
var t = e.code.split("_").length;
n[t] || (n[t] = []), n[t].push(e);
}
});
var r = Object.keys(n).sort(function(e, t) {
return t - e;
});
return _utils.reduce(r, function(e, r) {
return r && r !== e && n[e].forEach(function(e) {
var l = !1;
n[r].forEach(function(t) {
isInclude(t.code.split("_"), e.code.split("_")) && (l = !0, t.children || (t.children = []),
t.children.push(e));
}), l || t.push(e);
}), r;
}), n[r[r.length - 1]].concat(t);
}
function fileterAllMatchedData(e, t) {
var n = [];
return function t(r) {
r.forEach(function(r) {
if (!r.__matched) {
r.__pos = r.code;
var l = r.label.indexOf(e), c = r.children;
l > -1 ? (r.__matched = !0, n.push(r), c && t(c)) : c && t(c);
}
});
}(t), function e(t, r) {
r || (r = {
root: !0
});
var l = !1;
return t.forEach(function(t) {
var n = t.children;
!n || t.__matched || t.__halfMatched ? t.__matched && (l = !0) : e(n, t).__matched && (l = !0);
}), l && (r.__matched = !0, r.children = [], r.root || n.push(r)), r.root ? t : r;
}(t), n.forEach(function(e, t) {
delete n[t].__matched, delete n[t].__halfMatched, delete n[t].__pos, delete n[t].children;
}), n;
}
function recombineData(e, t) {
return e = _trim(e), _utils.isEmpty(e) ? t : t.length ? flatMatchedData2Hierarchy(fileterAllMatchedData(e, t = _deepCopy(t))) : t;
}
function filterChilds(e, t) {
var n = [];
return function t(r) {
r.forEach(function(r, l) {
r.children && r.children.length > 0 ? t(r.children) : r.label.indexOf(e) > -1 && n.push(r);
});
}(t), n;
}
function filterTreeLevel(e, t, n) {
var r = (n = n || {}).levelKey || "level", l = n.childrenKey || "children";
return function e(t) {
t.forEach(function(t, n) {
t[l] && t[l].length > 0 ? e(t[l]) : 3 == t[r] && delete t[l];
});
}(e = e || []), e;
}
module.exports = {
getChildrenlength: getChildrenlength,
flatToHierarchy: flatToHierarchy,
filterParentPosition: filterParentPosition,
isInclude: isInclude,
filterAllCheckedData: filterAllCheckedData,
recombineFlatData: recombineFlatData,
recombineData: recombineData,
filterChilds: filterChilds,
fileterAllMatchedData: fileterAllMatchedData,
recursive: recursive,
filterTreeLevel: filterTreeLevel
};