@vidal-community/vidal-web-components
Version:
Vidal Web Components
128 lines (126 loc) • 5.01 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { css, html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
class DomainWithRecos {
constructor(domain, recos) {
this.domain = domain;
this.recos = recos;
}
}
export class RecoWithDomains {
constructor(recoId, recoName, domainNames) {
this.recoId = recoId;
this.recoName = recoName;
this.domainNames = domainNames;
}
}
let VidalRecoClicTree = class VidalRecoClicTree extends LitElement {
get currentReco() {
return this._currentReco;
}
set currentReco(currentReco) {
this._currentReco = currentReco;
this.fetchData();
}
get dataFetchingSupplier() {
return this._dataFetchingSupplier;
}
set dataFetchingSupplier(dataFetchingSupplier) {
this._dataFetchingSupplier = dataFetchingSupplier;
this.fetchData();
}
async fetchData() {
if (this.currentReco && this.dataFetchingSupplier) {
this.currentRecoWithDomains = await this.getRecoWithDomains(this.currentReco.recoId);
}
}
getRecoHtml(reco) {
return html ` <div class="reco-name">${reco}</div>`;
}
render() {
var _a, _b, _c;
const categoriesWithRecos = (_c = (_b = (_a = this.currentRecoWithDomains) === null || _a === void 0 ? void 0 : _a.domainNames) === null || _b === void 0 ? void 0 : _b.map((domain) => {
var _a;
const recos = ((_a = this.currentRecoWithDomains) === null || _a === void 0 ? void 0 : _a.recoName)
? [this.currentRecoWithDomains.recoName]
: [];
return new DomainWithRecos(domain, recos);
})) !== null && _c !== void 0 ? _c : [];
const categoriesAsHtml = categoriesWithRecos.map((categoryWithRecos) => {
const recosAsHtml = html `${categoryWithRecos.recos.map((reco) => this.getRecoHtml(reco))}`;
return html ` <div class="domain-tree-container">
<div class="domain-title">${categoryWithRecos.domain}</div>
<div>${recosAsHtml}</div>
</div>`;
});
return html `
<div class="reco-tree">
<div class="component-title">Domaine thérapeutique</div>
${categoriesAsHtml}
</div>
`;
}
async getRecoWithDomains(recoId) {
const domainsXml = await this.fetchAllRecoDomainsXml();
const recoWithDomains = this.extractRecoDomainsFromXmlFeed(domainsXml);
const recoWithDomainsFiltered = recoWithDomains.filter((reco) => reco.recoId === recoId);
return Promise.resolve(recoWithDomainsFiltered[0]);
}
async fetchAllRecoDomainsXml() {
if (this.dataFetchingSupplier) {
const response = await this.dataFetchingSupplier('/rest/api/recos/all');
return response.text();
}
return Promise.resolve('');
}
extractRecoDomainsFromXmlFeed(allRecosXml) {
const recoDomains = [];
const domParser = new DOMParser();
const document = domParser.parseFromString(allRecosXml, 'application/xml');
const entries = [...document.querySelectorAll('entry')];
entries.forEach((entry) => {
const recoName = entry.getElementsByTagName('vidal:name')[0]
.textContent;
const recoId = Number(entry.getElementsByTagName('vidal:id')[0].textContent);
const domains = [...entry.getElementsByTagName('vidal:domain')].map((item) => item.textContent);
recoDomains.push(new RecoWithDomains(recoId, recoName, domains));
});
return recoDomains;
}
};
VidalRecoClicTree.styles = css `
:host {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
padding: 12px;
width: 90%;
}
.component-title {
font-weight: bold;
margin-bottom: 8px;
}
.reco-name {
margin-left: 12px;
}
`;
__decorate([
state()
], VidalRecoClicTree.prototype, "currentRecoWithDomains", void 0);
__decorate([
property({ attribute: false })
], VidalRecoClicTree.prototype, "currentReco", null);
__decorate([
property({ attribute: false })
], VidalRecoClicTree.prototype, "dataFetchingSupplier", null);
VidalRecoClicTree = __decorate([
customElement('vidal-reco-clic-tree')
], VidalRecoClicTree);
export { VidalRecoClicTree };
//# sourceMappingURL=vidal-reco-clic-tree.js.map