Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 19x 19x 19x 19x 11x 11x 11x 11x 11x 11x 11x 11x 41x 41x 4x 4x 4x 12x 12x 12x 4x 4x 4x 41x 41x 11x 11x 11x 11x 11x 11x 11x 11x 11x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 6x 1x 1x 6x 4x 4x 4x 4x 4x 4x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 7x 7x 3x 3x 7x 7x 4x 4x 11x 11x 11x 11x 11x 11x 11x 11x 4x 4x 4x 6x 6x 6x 6x 4x 4x 11x | import { DomPatcher } from './domPatch.js';
/**
* Handles efficient rendering of lists by managing DOM fragments and performing keyed diffing.
*/
export class ListManager {
/**
* @param {DynamicEvaluator} evaluator - The expression evaluator.
* @param {TemplateRenderer} renderer - The template renderer.
*/
constructor(evaluator, renderer) {
this.evaluator = evaluator;
this.renderer = renderer;
this.patcher = new DomPatcher();
}
/**
* Processes all template-based lists within a root element.
* @param {Element} root - The root element to search in.
* @param {Object} scope - The evaluation scope.
* @param {Object} state - The component state.
*/
process(root, scope, state) {
const templates = root.querySelectorAll('template[data-ax-for]');
templates.forEach(template => {
let parent = template.parentNode;
let insideSlot = false;
while (parent) {
if (parent.nodeName === 'SLOT' && parent.hasAttribute && parent.hasAttribute('data-avenx-transcluded')) {
insideSlot = true;
break;
}
parent = parent.parentNode;
}
if (!insideSlot) {
this.#updateList(template, scope, state);
}
});
}
/**
* Updates a specific list based on its template and current state.
* @param {HTMLTemplateElement} template - The list template.
* @param {Object} scope - The evaluation scope.
* @param {Object} state - The component state.
* @private
*/
#updateList(template, scope, state) {
const listExpr = template.getAttribute('data-ax-for');
const itemVar = template.getAttribute('data-ax-as');
const keyExpr = template.getAttribute('data-ax-key');
let list;
try {
list = this.evaluator.evaluateExpression(listExpr, scope, state);
} catch (e) {
console.warn(`[ListManager] Failed to evaluate list expression: ${listExpr}`, e);
return;
}
if (!Array.isArray(list)) return;
const currentItems = this.#getCurrentItems(template);
const nextItems = list.map((item, index) => {
const itemScope = { ...scope, [itemVar]: item, index };
let key = index;
if (keyExpr) {
try {
key = this.evaluator.evaluateExpression(keyExpr, itemScope, state);
} catch (e) {
console.warn(`[ListManager] Failed to evaluate key expression: ${keyExpr}`, e);
}
}
return { item, key: String(key), itemScope };
});
// 1. Remove items that are no longer in the list
const nextKeys = new Set(nextItems.map(i => i.key));
for (const [key, element] of currentItems.entries()) {
if (!nextKeys.has(key)) {
element.remove();
}
}
// 2. Add or move items
let lastElement = template;
const itemTemplate = template.innerHTML.replace(/{%/g, '{{').replace(/%}/g, '}}');
nextItems.forEach(({ key, itemScope }) => {
let element = currentItems.get(key);
const html = this.renderer.render(
itemTemplate,
expr => this.evaluator.evaluateExpression(expr, itemScope, state)
).trim();
if (element) {
const temp = document.createElement('div');
temp.innerHTML = html;
const newElement = temp.firstElementChild;
if (newElement) {
newElement.setAttribute('data-ax-list-item', '');
newElement.setAttribute('data-ax-key-val', key);
if (element.outerHTML !== newElement.outerHTML) {
this.patcher.patchElement(element, newElement);
}
}
} else {
// Create new element
const temp = document.createElement('div');
temp.innerHTML = html;
element = temp.firstElementChild;
if (element) {
element.setAttribute('data-ax-list-item', '');
element.setAttribute('data-ax-key-val', key);
}
}
if (element) {
// Ensure correct order
if (element.previousElementSibling !== lastElement) {
lastElement.after(element);
}
lastElement = element;
}
});
}
/**
* Retrieves currently rendered items for a template by scanning subsequent siblings.
* @param {HTMLTemplateElement} template - The template.
* @returns {Map<string, Element>}
* @private
*/
#getCurrentItems(template) {
const items = new Map();
let current = template.nextElementSibling;
while (current && current.hasAttribute('data-ax-list-item')) {
const key = current.getAttribute('data-ax-key-val');
items.set(key, current);
current = current.nextElementSibling;
}
return items;
}
}
|