@vidal-community/vidal-web-components
Version:
Vidal Web Components
126 lines • 5.19 kB
JavaScript
import { getEntryValue } from './utils';
import { html } from 'lit';
import { styleMap } from 'lit/directives/style-map.js';
import { checkboxesStyleMap } from './helpers.css';
export class ActiveExcipientHelpers {
constructor() {
this._activeExcipients = [];
this.getActiveExcipientsById = async (id, activeExcipientsApiService, domParser) => {
const activeExcipients = [];
if (id.includes('product')) {
const xml = await (await activeExcipientsApiService('/rest/api/product/' +
id.match(/\d+/)[0] +
'/molecules/active-excipients')).text();
const dom = domParser.parseFromString(xml, 'text/xml');
[...dom.querySelectorAll('entry')].forEach((entry) => {
var _a, _b;
const type = (_a = entry
.getElementsByTagName('vidal:itemType')[0]) === null || _a === void 0 ? void 0 : _a.getAttribute('name');
if (type === 'ACTIVE_EXCIPIENT') {
const id = (_b = getEntryValue(entry, 'id')) === null || _b === void 0 ? void 0 : _b.replace('/ACTIVE_EXCIPIENT', '');
const label = getEntryValue(entry, 'title');
const shortLabel = label.replace(/\s/g, '_');
const active = true;
activeExcipients.push({
id,
label,
shortLabel,
active,
});
}
});
}
return activeExcipients;
};
}
set activeExcipients(value) {
this._activeExcipients = value;
}
get activeExcipients() {
return this._activeExcipients;
}
async getActiveExcipients(activeExcipientsApiService, ids) {
const domParser = new DOMParser();
await Promise.all(ids.map(async (id) => {
const activeExcipientsById = await this.getActiveExcipientsById(id, activeExcipientsApiService, domParser);
this.activeExcipients =
this.activeExcipients.concat(activeExcipientsById);
}));
const uniqueIds = [];
this.activeExcipients = this.activeExcipients
.filter((ae) => {
if (uniqueIds.includes(ae.id)) {
return false;
}
uniqueIds.push(ae.id);
return true;
})
.sort((ae1, ae2) => ae1.shortLabel > ae2.shortLabel
? 1
: ae1.shortLabel === ae2.shortLabel
? 0
: -1);
}
renderActiveExcipients(renderRoot) {
return html ` <div id="active-excipients" class="filters excipients">
${this.activeExcipients.length > 0
? html ` <div id="active-excipient-search-wrapper">
<div id="active-excipient-search" class="filter">
<input
type="text"
name="active-excipient-search"
placeholder="Filtrer par excipient actif"
value=""
checked
="${(e) => {
this.filterActiveExcipientsRender(e.target.value, renderRoot);
}}"
/>
</div>
</div>
<div id="active-excipients-filter-wrapper">
${this.activeExcipients.map((activeExcipient) => html ` <div class="filter flex">
<input
class="cursor-pointer"
style="${styleMap(checkboxesStyleMap(activeExcipient.active))}"
type="checkbox"
?checked="${activeExcipient.active}"
name="${activeExcipient.shortLabel}"
value="${activeExcipient.id}"
="${() => this.handleActiveExcipientClick(this.activeExcipients, activeExcipient.id)}"
/>
<label class="cursor-pointer"
>${activeExcipient.label}</label
>
</div>`)}
</div>`
: null}
</div>`;
}
resetActiveExcipients() {
this.activeExcipients = [];
}
filterActiveExcipientsRender(value, renderRoot) {
const checkboxes = [
...renderRoot.querySelectorAll('.filters.excipients input[type=checkbox]'),
];
checkboxes.forEach((checkbox) => {
if (checkbox.name.includes(value)) {
checkbox.parentElement.style.display = 'block';
}
else {
checkbox.parentElement.style.display = 'none';
}
});
}
handleActiveExcipientClick(activeExcipients, id) {
this.activeExcipients = activeExcipients.map((ae) => {
if (ae.id === id) {
return { ...ae, active: !ae.active };
}
return ae;
});
window.dispatchEvent(new Event('clickOnActiveExcipientCheckbox'));
}
}
//# sourceMappingURL=active-excipient-helpers.js.map