>;
connectedCallback(): void {
this.noShadowDom = "";
super.connectedCallback();
if (this.hasAttribute("data-path")) {
this.statePath = this.getAttribute("data-path");
}
if (this.statePath) {
this.statePublisher = this.publisher;
const split = this.statePath.split(".");
for (const s of split) {
this.statePublisher = this.statePublisher[s];
}
this.statePublisher.onAssign(this.onStateAssign);
}
}
disconnectedCallback(): void {
if (this.statePath) {
this.statePublisher?.offAssign(this.onStateAssign);
}
super.disconnectedCallback();
}
handleStates(state: string) {
if (!this.states) return [];
const templates: DirectiveResult[] = [];
for (const [stateExpression, renderFn] of Object.entries(this.states)) {
if (stateExpression === "fallback" || !renderFn) continue;
const regexp = new RegExp(stateExpression);
if (regexp.test(state)) {
const params = regexp.exec(state) || [stateExpression];
params.shift();
templates.push(renderFn([...params]));
} else {
try {
const pattern = new UrlPattern("(/)*" + stateExpression + "*");
if (pattern.match(state)) {
const params = pattern.match(state) || {};
templates.push(renderFn(params));
}
} catch (e) {
if (
state.indexOf(
stateExpression.replace(document.location.origin, "")
) != -1
) {
templates.push(renderFn({}));
}
}
}
}
//Gestion fallback
if (templates.length == 0) {
if (this.states?.fallback && this.isConnected) {
templates.push(this.states.fallback());
}
}
return templates;
}
render() {
let state = this.state;
if (
(!Array.isArray(state) && Objects.isObject(state)) ||
state === undefined
) {
state = "";
}
// Gestion des états programmatiques
const programmaticTemplates = this.handleStates(state);
if (programmaticTemplates.length > 0) {
return html`${programmaticTemplates}`;
}
const templates = [];
// Gestion des templates HTML existants
for (const t of this.templatePartsList) {
let path = t.getAttribute(this.templateValueAttribute) as string;
let stateToMatch = state;
if (this.inverted) {
stateToMatch = path;
path = state;
}
if (path == "") path = this.inverted ? ".*?" : "^$";
const regexp = new RegExp(path);
if (regexp.test(stateToMatch + "")) {
templates.push(t);
t.removeAttribute("mode");
} else {
const urlPattern = new UrlPattern(path) as UrlPatternExtended;
if (urlPattern.names.length > 0 && urlPattern.match(stateToMatch)) {
t.setAttribute("mode", "patternMatching");
templates.push(t);
}
}
}
return html`${repeat(
templates,
(template, index) => {
template;
return index + new Date().getTime();
},
(template) => {
if (template?.hasAttribute("dataProviderExpression")) {
const dataProviderExpression = template.getAttribute(
"dataProviderExpression"
) as string;
let dataProvider = "";
let stateToMatch = state;
let path: string = template.getAttribute(
this.templateValueAttribute
) as string;
if (this.inverted) {
stateToMatch = path;
path = state;
}
if (path == "") path = this.inverted ? "*" : "^$";
if (template.getAttribute("mode") == "patternMatching") {
const matcher = new UrlPattern(path);
const filler = new UrlPattern(dataProviderExpression);
dataProvider = filler.stringify(matcher.match(stateToMatch));
} else {
const regexp = new RegExp(path);
const match = (stateToMatch + "").match(regexp);
if (match) {
dataProvider = match
.shift()
?.replace(regexp, dataProviderExpression) as string;
}
}
return html`
${templateContent(template)}
`;
}
return templateContent(template);
}
)}`;
}
}