js • Lines: 214import $cbGKE$react from "react";
/*
* 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 $43bac1ab0e94ec67$export$bf788dd355e3a401 {
build(props, context) {
this.context = context;
return $43bac1ab0e94ec67$var$iterable(()=>this.iterateCollection(props));
}
*iterateCollection(props) {
let { children: children, items: items } = props;
if ((0, $cbGKE$react).isValidElement(children) && children.type === (0, $cbGKE$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, $cbGKE$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) {
var _v_key;
let key = (_v_key = v.key) !== null && _v_key !== void 0 ? _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, $cbGKE$react).isValidElement(partialNode.element) && partialNode.element.type === (0, $cbGKE$react).Fragment) {
let children = [];
(0, $cbGKE$react).Children.forEach(partialNode.element.props.children, (child)=>{
children.push(child);
});
var _partialNode_index;
let index = (_partialNode_index = partialNode.index) !== null && _partialNode_index !== void 0 ? _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, $cbGKE$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);
var _partialNode_index1;
let index = (_partialNode_index1 = partialNode.index) !== null && _partialNode_index1 !== void 0 ? _partialNode_index1 : 0;
let result = childNodes.next();
while(!result.done && result.value){
let childNode = result.value;
partialNode.index = index;
var _childNode_key;
let nodeKey = (_childNode_key = childNode.key) !== null && _childNode_key !== void 0 ? _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: $43bac1ab0e94ec67$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){
var _childNode_value, _ref;
// Cache the node based on its value
node.value = (_ref = (_childNode_value = childNode.value) !== null && _childNode_value !== void 0 ? _childNode_value : partialNode.value) !== null && _ref !== void 0 ? _ref : null;
if (node.value) this.cache.set(node.value, node);
var _parentNode_type;
// 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 <${$43bac1ab0e94ec67$var$capitalize(node.type)}> in <${$43bac1ab0e94ec67$var$capitalize((_parentNode_type = parentNode === null || parentNode === void 0 ? void 0 : parentNode.type) !== null && _parentNode_type !== void 0 ? _parentNode_type : 'unknown parent type')}>. Only <${$43bac1ab0e94ec67$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;
var _partialNode_value, _parentNode_level, _partialNode_textValue;
let node = {
type: partialNode.type,
props: partialNode.props,
key: partialNode.key,
parentKey: parentNode ? parentNode.key : null,
value: (_partialNode_value = partialNode.value) !== null && _partialNode_value !== void 0 ? _partialNode_value : null,
level: ((_parentNode_level = parentNode === null || parentNode === void 0 ? void 0 : parentNode.level) !== null && _parentNode_level !== void 0 ? _parentNode_level : 0) + ((parentNode === null || parentNode === void 0 ? void 0 : parentNode.type) === 'item' ? 1 : 0),
index: partialNode.index,
rendered: partialNode.rendered,
textValue: (_partialNode_textValue = partialNode.textValue) !== null && _partialNode_textValue !== void 0 ? _partialNode_textValue : '',
'aria-label': partialNode['aria-label'],
wrapper: partialNode.wrapper,
shouldInvalidate: partialNode.shouldInvalidate,
hasChildNodes: partialNode.hasChildNodes || false,
childNodes: $43bac1ab0e94ec67$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 $43bac1ab0e94ec67$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 $43bac1ab0e94ec67$var$compose(outer, inner) {
if (outer && inner) return (element)=>outer(inner(element));
if (outer) return outer;
if (inner) return inner;
}
function $43bac1ab0e94ec67$var$capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}
export {$43bac1ab0e94ec67$export$bf788dd355e3a401 as CollectionBuilder};
//# sourceMappingURL=CollectionBuilder.js.map