baseui
Version:
A React Component library implementing the Base design language
204 lines (194 loc) • 6.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toggleIsExpanded = exports.matchString = exports.getPrevId = exports.getParentId = exports.getNextId = exports.getFirstChildId = exports.getExpandableSiblings = exports.getEndId = exports.getCharMatchId = exports.defaultGetId = void 0;
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @ts-ignore
const getLastLeafId = (node, getId) => {
if (node.isExpanded && node.children && node.children.length) {
return getLastLeafId(node.children[node.children.length - 1], getId);
}
return getId(node);
};
// @ts-ignore
const getParentId = (nodes, nodeId, parentId, getId) => {
for (let i = 0; i < nodes.length; i++) {
if (getId(nodes[i]) === nodeId) {
return parentId;
}
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
const foundId = getParentId(nodes[i].children, nodeId, getId(nodes[i]), getId);
if (foundId) {
return foundId;
}
}
}
return null;
};
// @ts-ignore
exports.getParentId = getParentId;
const getPrevId = (nodes, nodeId, parentId, getId) => {
for (let i = 0; i < nodes.length; i++) {
if (getId(nodes[i]) === nodeId) {
if (i === 0) {
return parentId;
} else {
return getLastLeafId(nodes[i - 1], getId);
}
}
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
const foundId = getPrevId(nodes[i].children, nodeId, getId(nodes[i]), getId);
if (foundId) {
return foundId;
}
}
}
return null;
};
// @ts-ignore
exports.getPrevId = getPrevId;
const getFirstChildId = (nodes, nodeId, getId) => {
for (let i = 0; i < nodes.length; i++) {
if (getId(nodes[i]) === nodeId) {
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
return getId(nodes[i].children[0]);
}
}
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
const foundId = getFirstChildId(nodes[i].children, nodeId, getId);
if (foundId) {
return foundId;
}
}
}
return null;
};
// @ts-ignore
exports.getFirstChildId = getFirstChildId;
const getNextId = (nodes, nodeId, closestOmmer, getId) => {
for (let i = 0; i < nodes.length; i++) {
if (getId(nodes[i]) === nodeId) {
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
return getId(nodes[i].children[0]);
} else if (nodes[i + 1]) {
return getId(nodes[i + 1]);
} else {
return closestOmmer;
}
}
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
const foundId = getNextId(
// @ts-ignore
nodes[i].children, nodeId, nodes[i + 1] ? getId(nodes[i + 1]) : closestOmmer, getId);
if (foundId) {
return foundId;
}
}
}
return null;
};
// @ts-ignore
exports.getNextId = getNextId;
const getEndId = (nodes, getId) => {
const endNode = nodes[nodes.length - 1];
if (endNode.isExpanded && endNode.children && endNode.children.length) {
return getEndId(endNode.children, getId);
}
return getId(endNode);
};
// @ts-ignore
exports.getEndId = getEndId;
const getExpandableSiblings = (nodes, nodeId, getId) => {
for (let i = 0; i < nodes.length; i++) {
if (getId(nodes[i]) === nodeId) {
const expandableSiblings = [];
for (let j = 0; j < nodes.length; j++) {
// @ts-ignore
if (!nodes[j].isExpanded && nodes[j].children && nodes[j].children.length) {
// @ts-ignore
expandableSiblings.push(nodes[j]);
}
}
return expandableSiblings;
}
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
const result = getExpandableSiblings(nodes[i].children, nodeId, getId);
if (result.length) {
return result;
}
}
}
return [];
};
exports.getExpandableSiblings = getExpandableSiblings;
const toggleIsExpanded = (arr, toggledNode, getId = node => node.id ? node.id : '') => {
return arr.map(node => {
const newNode = {
...node
};
if (getId(newNode) === getId(toggledNode)) {
newNode.isExpanded = !newNode.isExpanded;
}
if (newNode.children && newNode.children.length) {
newNode.children = toggleIsExpanded(newNode.children, toggledNode);
}
return newNode;
});
};
exports.toggleIsExpanded = toggleIsExpanded;
const getCharMatchId = (nodes, nodeId, chars, closestOmmer, getId) => {
var foundid = matchString(nodes, nodeId, chars, closestOmmer, getId, true);
if (foundid) return foundid;
foundid = matchString(nodes, nodeId, chars, closestOmmer, getId, false);
return foundid;
};
// @ts-ignore
exports.getCharMatchId = getCharMatchId;
const matchString = (nodes, nodeId, chars, closestOmmer, getId, matchPrefix) => {
for (let i = 0; i < nodes.length; i++) {
let label = nodes[i].label;
if (label && typeof label === 'string') {
if (matchPrefix && label.toUpperCase().indexOf(chars.toUpperCase()) === 0 || !matchPrefix && label.toUpperCase().indexOf(chars.toUpperCase()) > 0) {
return getId(nodes[i]);
}
}
// @ts-ignore
if (nodes[i].isExpanded && nodes[i].children && nodes[i].children.length) {
// @ts-ignore
const foundId = matchString(
// @ts-ignore
nodes[i].children, nodeId, chars, nodes[i + 1] ? getId(nodes[i + 1]) : closestOmmer, getId, matchPrefix);
if (foundId) {
return foundId;
}
}
}
return null;
};
exports.matchString = matchString;
const defaultGetId = node => {
if (!node.id) {
throw Error('There needs to be an unique node.id. You can implement a custom mapping with getId.');
}
return node.id;
};
exports.defaultGetId = defaultGetId;