@patternfly/pfe-core
Advanced tools
Comparing version 2.4.0 to 2.4.1
@@ -29,12 +29,7 @@ import type { ReactiveController, ReactiveElement } from 'lit'; | ||
export declare class SlotController implements ReactiveController { | ||
#private; | ||
host: ReactiveElement; | ||
static anonymous: symbol; | ||
private nodes; | ||
private logger; | ||
private firstUpdated; | ||
private mo; | ||
private slotNames; | ||
private deprecations; | ||
constructor(host: ReactiveElement, ...config: ([SlotsConfig] | (string | null)[])); | ||
hostConnected(): void; | ||
hostConnected(): Promise<void>; | ||
hostUpdated(): void; | ||
@@ -69,11 +64,3 @@ hostDisconnected(): void; | ||
getSlotted<T extends Element = Element>(...slotNames: string[]): T[]; | ||
private onSlotChange; | ||
private onMutation; | ||
private getChildrenForSlot; | ||
private initSlot; | ||
/** | ||
* Maps the defined slots into an object that is easier to query | ||
*/ | ||
private init; | ||
} | ||
export {}; |
@@ -1,3 +0,1 @@ | ||
import { __decorate } from "tslib"; | ||
import { bound } from '../decorators/bound.js'; | ||
import { Logger } from './logger.js'; | ||
@@ -15,37 +13,47 @@ function isObjectConfigSpread(config) { | ||
static { this.anonymous = Symbol('anonymous slot'); } | ||
#nodes = new Map(); | ||
#logger; | ||
#firstUpdated = false; | ||
#mo = new MutationObserver(records => this.#onMutation(records)); | ||
#slotNames; | ||
#deprecations = {}; | ||
constructor(host, ...config) { | ||
this.host = host; | ||
this.nodes = new Map(); | ||
this.firstUpdated = false; | ||
this.mo = new MutationObserver(this.onMutation); | ||
this.deprecations = {}; | ||
this.logger = new Logger(this.host); | ||
this.#logger = new Logger(this.host); | ||
if (isObjectConfigSpread(config)) { | ||
const [{ slots, deprecations }] = config; | ||
this.slotNames = slots; | ||
this.deprecations = deprecations ?? {}; | ||
this.#slotNames = slots; | ||
this.#deprecations = deprecations ?? {}; | ||
} | ||
else if (config.length >= 1) { | ||
this.slotNames = config; | ||
this.deprecations = {}; | ||
this.#slotNames = config; | ||
this.#deprecations = {}; | ||
} | ||
else { | ||
this.slotNames = [null]; | ||
this.#slotNames = [null]; | ||
} | ||
host.addController(this); | ||
} | ||
hostConnected() { | ||
this.host.addEventListener('slotchange', this.onSlotChange); | ||
this.firstUpdated = false; | ||
this.mo.observe(this.host, { childList: true }); | ||
this.init(); | ||
async hostConnected() { | ||
this.host.addEventListener('slotchange', this.#onSlotChange); | ||
this.#firstUpdated = false; | ||
this.#mo.observe(this.host, { childList: true }); | ||
// Map the defined slots into an object that is easier to query | ||
this.#nodes.clear(); | ||
// Loop over the properties provided by the schema | ||
this.#slotNames.forEach(this.#initSlot); | ||
Object.values(this.#deprecations).forEach(this.#initSlot); | ||
this.host.requestUpdate(); | ||
// insurance for framework integrations | ||
await this.host.updateComplete; | ||
this.host.requestUpdate(); | ||
} | ||
hostUpdated() { | ||
if (!this.firstUpdated) { | ||
this.slotNames.forEach(this.initSlot); | ||
this.firstUpdated = true; | ||
if (!this.#firstUpdated) { | ||
this.#slotNames.forEach(this.#initSlot); | ||
this.#firstUpdated = true; | ||
} | ||
} | ||
hostDisconnected() { | ||
this.mo.disconnect(); | ||
this.#mo.disconnect(); | ||
} | ||
@@ -60,7 +68,7 @@ /** | ||
if (!names.length) { | ||
this.logger.warn(`Please provide at least one slot name for which to search.`); | ||
this.#logger.warn(`Please provide at least one slot name for which to search.`); | ||
return false; | ||
} | ||
else { | ||
return names.some(x => this.nodes.get(x)?.hasContent ?? false); | ||
return names.some(x => this.#nodes.get(x)?.hasContent ?? false); | ||
} | ||
@@ -89,14 +97,14 @@ } | ||
if (!slotNames.length) { | ||
return (this.nodes.get(SlotController.anonymous)?.elements ?? []); | ||
return (this.#nodes.get(SlotController.anonymous)?.elements ?? []); | ||
} | ||
else { | ||
return slotNames.flatMap(slotName => this.nodes.get(slotName)?.elements ?? []); | ||
return slotNames.flatMap(slotName => this.#nodes.get(slotName)?.elements ?? []); | ||
} | ||
} | ||
onSlotChange(event) { | ||
#onSlotChange = (event) => { | ||
const slotName = event.target.name; | ||
this.initSlot(slotName); | ||
this.#initSlot(slotName); | ||
this.host.requestUpdate(); | ||
} | ||
async onMutation(records) { | ||
}; | ||
#onMutation = async (records) => { | ||
const changed = []; | ||
@@ -106,3 +114,3 @@ for (const { addedNodes, removedNodes } of records) { | ||
if (node instanceof HTMLElement && node.slot) { | ||
this.initSlot(node.slot); | ||
this.#initSlot(node.slot); | ||
changed.push(node.slot); | ||
@@ -112,43 +120,19 @@ } | ||
} | ||
if (changed.length) { | ||
this.host.requestUpdate(); | ||
} | ||
} | ||
getChildrenForSlot(name) { | ||
this.host.requestUpdate(); | ||
}; | ||
#getChildrenForSlot(name) { | ||
const children = Array.from(this.host.children); | ||
return children.filter(isSlot(name)); | ||
} | ||
initSlot(slotName) { | ||
#initSlot = (slotName) => { | ||
const name = slotName || SlotController.anonymous; | ||
const elements = this.nodes.get(name)?.slot?.assignedElements?.() ?? this.getChildrenForSlot(name); | ||
const elements = this.#nodes.get(name)?.slot?.assignedElements?.() ?? this.#getChildrenForSlot(name); | ||
const selector = slotName ? `slot[name="${slotName}"]` : 'slot:not([name])'; | ||
const slot = this.host.shadowRoot?.querySelector?.(selector) ?? null; | ||
const hasContent = !!elements.length; | ||
this.nodes.set(name, { elements, name: slotName ?? '', hasContent, slot }); | ||
this.logger.log(slotName, hasContent); | ||
} | ||
/** | ||
* Maps the defined slots into an object that is easier to query | ||
*/ | ||
init() { | ||
this.nodes.clear(); | ||
// Loop over the properties provided by the schema | ||
this.slotNames.forEach(this.initSlot); | ||
Object.values(this.deprecations).forEach(this.initSlot); | ||
this.host.requestUpdate(); | ||
} | ||
this.#nodes.set(name, { elements, name: slotName ?? '', hasContent, slot }); | ||
this.#logger.log(slotName, hasContent); | ||
}; | ||
} | ||
__decorate([ | ||
bound | ||
], SlotController.prototype, "onSlotChange", null); | ||
__decorate([ | ||
bound | ||
], SlotController.prototype, "onMutation", null); | ||
__decorate([ | ||
bound | ||
], SlotController.prototype, "initSlot", null); | ||
__decorate([ | ||
bound | ||
], SlotController.prototype, "init", null); | ||
export { SlotController }; | ||
//# sourceMappingURL=slot-controller.js.map |
{ | ||
"name": "@patternfly/pfe-core", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "PatternFly Elements Core Library", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
395042
9334