@mui/x-tree-view
Version:
The community edition of the MUI X Tree View components.
122 lines (119 loc) • 4.34 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TREE_VIEW_ROOT_PARENT_ID = void 0;
exports.buildItemsLookups = buildItemsLookups;
exports.isItemDisabled = exports.buildSiblingIndexes = void 0;
var _formatErrorMessage2 = _interopRequireDefault(require("@mui/x-internals/formatErrorMessage"));
const TREE_VIEW_ROOT_PARENT_ID = exports.TREE_VIEW_ROOT_PARENT_ID = '__TREE_VIEW_ROOT_PARENT_ID__';
const buildSiblingIndexes = siblings => {
const siblingsIndexLookup = {};
siblings.forEach((childId, index) => {
siblingsIndexLookup[childId] = index;
});
return siblingsIndexLookup;
};
/**
* Check if an item is disabled.
* This method should only be used in selectors that are checking if several items are disabled.
* Otherwise, use the `itemsSelector.isItemDisabled` selector.
* @returns
*/
exports.buildSiblingIndexes = buildSiblingIndexes;
const isItemDisabled = (itemMetaLookup, itemId) => {
if (itemId == null) {
return false;
}
let itemMeta = itemMetaLookup[itemId];
// This can be called before the item has been added to the item map.
if (!itemMeta) {
return false;
}
if (itemMeta.disabled) {
return true;
}
while (itemMeta.parentId != null) {
itemMeta = itemMetaLookup[itemMeta.parentId];
if (!itemMeta) {
return false;
}
if (itemMeta.disabled) {
return true;
}
}
return false;
};
exports.isItemDisabled = isItemDisabled;
function buildItemsLookups(parameters) {
const {
storeParameters,
items,
parentId,
depth,
isItemExpandable,
otherItemsMetaLookup
} = parameters;
const metaLookup = {};
const modelLookup = {};
const orderedChildrenIds = [];
const itemsChildren = [];
const processItem = item => {
const id = storeParameters.getItemId ? storeParameters.getItemId(item) : item.id;
checkId({
id,
parentId,
item,
itemMetaLookup: otherItemsMetaLookup,
siblingsMetaLookup: metaLookup
});
const label = storeParameters.getItemLabel ? storeParameters.getItemLabel(item) : item.label;
if (label == null) {
throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Tree View: All items must have a `label` property. ' + 'You can use the `getItemLabel` prop to specify a custom label for each item. ' + `An item was provided without a label: ${JSON.stringify(item)}` : (0, _formatErrorMessage2.default)(192, JSON.stringify(item)));
}
const children = (storeParameters.getItemChildren ? storeParameters.getItemChildren(item) : item.children) || [];
itemsChildren.push({
id,
children
});
modelLookup[id] = item;
metaLookup[id] = {
id,
label,
parentId,
idAttribute: undefined,
expandable: isItemExpandable(item, children),
disabled: storeParameters.isItemDisabled ? storeParameters.isItemDisabled(item) : false,
selectable: storeParameters.isItemSelectionDisabled ? !storeParameters.isItemSelectionDisabled(item) : true,
depth
};
orderedChildrenIds.push(id);
};
for (const item of items) {
processItem(item);
}
return {
metaLookup,
modelLookup,
orderedChildrenIds,
childrenIndexes: buildSiblingIndexes(orderedChildrenIds),
itemsChildren
};
}
function checkId({
id,
parentId,
item,
itemMetaLookup,
siblingsMetaLookup
}) {
if (id == null) {
throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Tree View: All items must have a unique `id` property. ' + 'You can use the `getItemId` prop to specify a custom id for each item. ' + `An item was provided without an id: ${JSON.stringify(item)}` : (0, _formatErrorMessage2.default)(193, JSON.stringify(item)));
}
if (siblingsMetaLookup[id] != null ||
// Ignore items with the same parent id, because it's the same item from the previous generation.
itemMetaLookup[id] != null && itemMetaLookup[id].parentId !== parentId) {
throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Tree View: All items must have a unique \`id\` property. ` + `The id "${id}" is used by multiple items. ` + 'Use the `getItemId` prop to specify a custom id for each item if needed.' : (0, _formatErrorMessage2.default)(194, id));
}
}