/** * * Lit Faq Search Element * */ // Import litelement base class, html helper function & typescript decorators import { SkhemataBase, property, html, CSSResult } from '@skhemata/skhemata-base'; import { SkhemataFaqCategoriesStyles } from '../style/SkhemataFaqCategoriesStyle'; import { translationEngDefault } from '../translation/SkhemataFaqCategories/eng'; export class SkhemataFaqCategories extends SkhemataBase { @property({ type: Object, attribute: 'api-wordpress' }) apiWordpress = { url: '' }; @property({ type: String, attribute: 'faq-page-path' }) faqPagePath = ''; @property({ type: Array }) categories = []; @property({ type: Number }) currentCategory?: number; @property({ type: Object }) translationData = { eng: translationEngDefault }; static get styles() { return [ ...super.styles, SkhemataFaqCategoriesStyles ]; } constructor() { super(); let params = new URLSearchParams(window.location.search); let c: string = params.get('c') || ''; if (c !== '') { this.currentCategory = parseInt(c, 10); } window.addEventListener('popstate', () => { params = new URLSearchParams(window.location.search); c = params.get('c') || ''; if (c !== '') { this.currentCategory = parseInt(c, 10); } else { this.currentCategory = undefined; } }); } /** * Implement `render` to define a template for your element. * Use JS template literals */ protected render() { return html`

${this.getStr('SkhemataFaqCategories.categoriesTitle')}

${this.categories.map( (category: any) => html`
` )} `; } async firstUpdated() { await super.firstUpdated(); this.getCategories(); } /** * Get categories */ private async getCategories() { await fetch(`${this.apiWordpress.url}/ufaq-category?parent=0&per_page=100`) .then(response => response.json()) .then(async data => { this.categories = data; }); } filterCategory(id: number) { const params = new URLSearchParams(window.location.search); if (parseInt(params.get('c') || '', 10) === id || id === -1) { this.currentCategory = undefined; params.delete('c'); } else { this.currentCategory = id; params.set('c', id.toString()); } window.history.pushState( {}, '', decodeURIComponent(`/${this.faqPagePath}?${params.toString()}`) ); window.scrollTo({ top: 0 }); window.dispatchEvent(new Event('popstate')); } }