cjs • Lines: 216var $ccGUh$react = require("react");
function $parcel$interopDefault(a) {
return a && a.__esModule ? a.default : a;
}
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
$parcel$export(module.exports, "CollectionBuilder", function () { return $6caebf02f33d362d$export$bf788dd355e3a401; });
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
class $6caebf02f33d362d$export$bf788dd355e3a401 {
build(props, context) {
this.context = context;
return $6caebf02f33d362d$var$iterable(()=>this.iterateCollection(props));
}
*iterateCollection(props) {
let { children: children, items: items } = props;
if ((0, ($parcel$interopDefault($ccGUh$react))).isValidElement(children) && children.type === (0, ($parcel$interopDefault($ccGUh$react))).Fragment) yield* this.iterateCollection({
children: children.props.children,
items: items
});
else if (typeof children === 'function') {
if (!items) throw new Error('props.children was a function but props.items is missing');
let index = 0;
for (let item of items){
yield* this.getFullNode({
value: item,
index: index
}, {
renderer: children
});
index++;
}
} else {
let items = [];
(0, ($parcel$interopDefault($ccGUh$react))).Children.forEach(children, (child)=>{
if (child) items.push(child);
});
let index = 0;
for (let item of items){
let nodes = this.getFullNode({
element: item,
index: index
}, {});
for (let node of nodes){
index++;
yield node;
}
}
}
}
getKey(item, partialNode, state, parentKey) {
if (item.key != null) return item.key;
if (partialNode.type === 'cell' && partialNode.key != null) return `${parentKey}${partialNode.key}`;
let v = partialNode.value;
if (v != null) {
let key = v.key ?? v.id;
if (key == null) throw new Error('No key found for item');
return key;
}
return parentKey ? `${parentKey}.${partialNode.index}` : `$.${partialNode.index}`;
}
getChildState(state, partialNode) {
return {
renderer: partialNode.renderer || state.renderer
};
}
*getFullNode(partialNode, state, parentKey, parentNode) {
if ((0, ($parcel$interopDefault($ccGUh$react))).isValidElement(partialNode.element) && partialNode.element.type === (0, ($parcel$interopDefault($ccGUh$react))).Fragment) {
let children = [];
(0, ($parcel$interopDefault($ccGUh$react))).Children.forEach(partialNode.element.props.children, (child)=>{
children.push(child);
});
let index = partialNode.index ?? 0;
for (const child of children)yield* this.getFullNode({
element: child,
index: index++
}, state, parentKey, parentNode);
return;
}
// If there's a value instead of an element on the node, and a parent renderer function is available,
// use it to render an element for the value.
let element = partialNode.element;
if (!element && partialNode.value && state && state.renderer) {
let cached = this.cache.get(partialNode.value);
if (cached && (!cached.shouldInvalidate || !cached.shouldInvalidate(this.context))) {
cached.index = partialNode.index;
cached.parentKey = parentNode ? parentNode.key : null;
yield cached;
return;
}
element = state.renderer(partialNode.value);
}
// If there's an element with a getCollectionNode function on its type, then it's a supported component.
// Call this function to get a partial node, and recursively build a full node from there.
if ((0, ($parcel$interopDefault($ccGUh$react))).isValidElement(element)) {
let type = element.type;
if (typeof type !== 'function' && typeof type.getCollectionNode !== 'function') {
let name = element.type;
throw new Error(`Unknown element <${name}> in collection.`);
}
let childNodes = type.getCollectionNode(element.props, this.context);
let index = partialNode.index ?? 0;
let result = childNodes.next();
while(!result.done && result.value){
let childNode = result.value;
partialNode.index = index;
let nodeKey = childNode.key ?? null;
if (nodeKey == null) nodeKey = childNode.element ? null : this.getKey(element, partialNode, state, parentKey);
let nodes = this.getFullNode({
...childNode,
key: nodeKey,
index: index,
wrapper: $6caebf02f33d362d$var$compose(partialNode.wrapper, childNode.wrapper)
}, this.getChildState(state, childNode), parentKey ? `${parentKey}${element.key}` : element.key, parentNode);
let children = [
...nodes
];
for (let node of children){
// Cache the node based on its value
node.value = childNode.value ?? partialNode.value ?? null;
if (node.value) this.cache.set(node.value, node);
// The partial node may have specified a type for the child in order to specify a constraint.
// Verify that the full node that was built recursively matches this type.
if (partialNode.type && node.type !== partialNode.type) throw new Error(`Unsupported type <${$6caebf02f33d362d$var$capitalize(node.type)}> in <${$6caebf02f33d362d$var$capitalize(parentNode?.type ?? 'unknown parent type')}>. Only <${$6caebf02f33d362d$var$capitalize(partialNode.type)}> is supported.`);
index++;
yield node;
}
result = childNodes.next(children);
}
return;
}
// Ignore invalid elements
if (partialNode.key == null || partialNode.type == null) return;
// Create full node
let builder = this;
let node = {
type: partialNode.type,
props: partialNode.props,
key: partialNode.key,
parentKey: parentNode ? parentNode.key : null,
value: partialNode.value ?? null,
level: (parentNode?.level ?? 0) + (parentNode?.type === 'item' ? 1 : 0),
index: partialNode.index,
rendered: partialNode.rendered,
textValue: partialNode.textValue ?? '',
'aria-label': partialNode['aria-label'],
wrapper: partialNode.wrapper,
shouldInvalidate: partialNode.shouldInvalidate,
hasChildNodes: partialNode.hasChildNodes || false,
childNodes: $6caebf02f33d362d$var$iterable(function*() {
if (!partialNode.hasChildNodes || !partialNode.childNodes) return;
let index = 0;
for (let child of partialNode.childNodes()){
// Ensure child keys are globally unique by prepending the parent node's key
if (child.key != null) // TODO: Remove this line entirely and enforce that users always provide unique keys.
// Currently this line will have issues when a parent has a key `a` and a child with key `bc`
// but another parent has key `ab` and its child has a key `c`. The combined keys would result in both
// children having a key of `abc`.
child.key = `${node.key}${child.key}`;
let nodes = builder.getFullNode({
...child,
index: index
}, builder.getChildState(state, child), node.key, node);
for (let node of nodes){
index++;
yield node;
}
}
})
};
yield node;
}
constructor(){
this.cache = new WeakMap();
}
}
// Wraps an iterator function as an iterable object, and caches the results.
function $6caebf02f33d362d$var$iterable(iterator) {
let cache = [];
let iterable = null;
return {
*[Symbol.iterator] () {
for (let item of cache)yield item;
if (!iterable) iterable = iterator();
for (let item of iterable){
cache.push(item);
yield item;
}
}
};
}
function $6caebf02f33d362d$var$compose(outer, inner) {
if (outer && inner) return (element)=>outer(inner(element));
if (outer) return outer;
if (inner) return inner;
}
function $6caebf02f33d362d$var$capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
//# sourceMappingURL=CollectionBuilder.cjs.map