@vidal-community/vidal-web-components
Version:
Vidal Web Components
93 lines • 5.21 kB
JavaScript
import { DecisionTreeWithCaptions } from './decision-tree-with-captions';
export class Reco {
constructor(recoId, decisionTreeWithCaptions, sharedText, styles) {
this.recoId = recoId;
this.decisionTreeWithCaptions = decisionTreeWithCaptions;
this.sharedText = sharedText;
this.styles = styles;
}
static fromHtml(recoHtml, recoId) {
var _a;
const domParser = new DOMParser();
const recoDom = domParser.parseFromString(recoHtml, 'text/html');
const decisionTreeElements = [...recoDom.querySelectorAll('.arbre')];
decisionTreeElements.forEach((d) => d.remove());
const decisionTreeWithCaptions = decisionTreeElements.map(DecisionTreeWithCaptions.fromElement);
Reco.moveContentItemsToSharedText(decisionTreeWithCaptions, recoDom.body);
if (decisionTreeWithCaptions.length > 1) {
Reco.createTabLinksInSharedText(decisionTreeWithCaptions, recoDom.body);
}
Reco.moveTreeCommentsToSharedText(decisionTreeWithCaptions, recoDom);
const styles = (_a = recoDom.documentElement.querySelector('head')) !== null && _a !== void 0 ? _a : document.createElement('head');
Reco.transformAllDrugListsInAccordions(recoDom);
return new Reco(recoId, decisionTreeWithCaptions, recoDom.body, styles);
}
static transformAllDrugListsInAccordions(recoDom) {
recoDom.body.querySelectorAll('.lstdci').forEach((listDci) => {
const arrowElement = document.createElement('span');
const title = listDci === null || listDci === void 0 ? void 0 : listDci.querySelector('.dci');
arrowElement.classList.add('rotating-arrow');
title === null || title === void 0 ? void 0 : title.prepend(arrowElement);
const accordion = document.createElement('vidal-reco-clic-accordion');
const slot = document.createElement('slot');
const dupNode = listDci.cloneNode(true);
slot.appendChild(dupNode);
accordion.appendChild(slot);
listDci.replaceWith(accordion);
});
}
static moveTreeCommentsToSharedText(decisionTreeWithCaptions, recoDom) {
if (decisionTreeWithCaptions.length === 1) {
const tabLinkTreeAttribute = 'arbre1';
decisionTreeWithCaptions[0].captions.setAttribute('tree', tabLinkTreeAttribute);
const pChargeElement = recoDom.body.querySelector('.pcharge');
pChargeElement === null || pChargeElement === void 0 ? void 0 : pChargeElement.insertBefore(decisionTreeWithCaptions[0].captions, pChargeElement.children[1]);
}
else {
decisionTreeWithCaptions.forEach((decisionTreeWithCaption, index) => {
const tabLinkTreeAttribute = `arbre${index + 1}`;
decisionTreeWithCaption.captions.setAttribute('tree', tabLinkTreeAttribute);
[...recoDom.body.querySelectorAll('.tab-link')]
.filter((tabLink) => tabLink.getAttribute('tree') === tabLinkTreeAttribute)
.forEach((tabLink) => {
tabLink.after(decisionTreeWithCaption.captions);
});
});
}
}
static moveContentItemsToSharedText(decisionTreeWithCaptions, recoBody) {
const contentItems = decisionTreeWithCaptions
.map((decisionTreeWithCaption) => decisionTreeWithCaption.decisionTree)
.filter((decisionTree) => decisionTree.querySelector('.arbre >.content-item'))
.flatMap((decisionTree) => [
...decisionTree.querySelectorAll('.arbre > .content-item'),
]);
if (contentItems) {
const pChargeElement = recoBody.querySelector('.pcharge');
contentItems.forEach((contentItem, index) => {
contentItem.classList.add('moved-item');
pChargeElement === null || pChargeElement === void 0 ? void 0 : pChargeElement.insertBefore(contentItem, pChargeElement.children[index + 1]);
});
}
}
static createTabLinksInSharedText(decisionTreeWithCaptions, recoBody) {
const pChargeElement = recoBody.querySelector('.pcharge');
decisionTreeWithCaptions.forEach((decisionTreeWithCaption, index) => {
var _a;
const textContent = (_a = decisionTreeWithCaption.decisionTree.querySelector('.titarbre')) === null || _a === void 0 ? void 0 : _a.textContent;
if (textContent) {
const tabLink = document.createElement('div');
tabLink.classList.add('content-item');
const title = document.createElement('div');
title.textContent = textContent;
title.classList.add('tab-link');
title.classList.add('title');
title.setAttribute('tree', `arbre${index + 1}`);
tabLink.append(title);
pChargeElement === null || pChargeElement === void 0 ? void 0 : pChargeElement.insertBefore(tabLink, pChargeElement.children[index + 1]);
tabLink.setAttribute('tree', `arbre${index + 1}`);
}
});
}
}
//# sourceMappingURL=reco.js.map