| 1 |
import * as lpUtils from 'lpAssetsJsPath/utils.js'; |
| 2 |
|
| 3 |
/** |
| 4 |
* LearnPress themes handler. |
| 5 |
* |
| 6 |
* @since 4.4.7 |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
export class LPThemes { |
| 10 |
/** |
| 11 |
* Themes container selectors. |
| 12 |
*/ |
| 13 |
static selectors = { |
| 14 |
container: '.learn-press-themes', |
| 15 |
elListThemes: '.lp-themes-grid', |
| 16 |
filter: '.lp-themes-filter__item', |
| 17 |
count: '.lp-themes-filter__count', |
| 18 |
search: '.lp-themes-search__input', |
| 19 |
card: '.lp-theme-card', |
| 20 |
title: '.lp-theme-card__title', |
| 21 |
description: '.lp-theme-card__description', |
| 22 |
}; |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize the themes handler. |
| 26 |
* |
| 27 |
* @param {HTMLElement} container Themes container. |
| 28 |
*/ |
| 29 |
init( container ) { |
| 30 |
this.container = container; |
| 31 |
this.events(); |
| 32 |
this.countThemes(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Register theme filter and search events. |
| 37 |
*/ |
| 38 |
events() { |
| 39 |
if ( LPThemes._loadedEvents ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
LPThemes._loadedEvents = true; |
| 43 |
|
| 44 |
lpUtils.eventHandlers( 'click', [ |
| 45 |
{ |
| 46 |
selector: LPThemes.selectors.filter, |
| 47 |
callBack: this.filterByCategory.name, |
| 48 |
class: this, |
| 49 |
}, |
| 50 |
] ); |
| 51 |
|
| 52 |
lpUtils.eventHandlers( 'input', [ |
| 53 |
{ |
| 54 |
selector: LPThemes.selectors.search, |
| 55 |
callBack: this.filterThemes.name, |
| 56 |
class: this, |
| 57 |
}, |
| 58 |
] ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Count themes and update each category count. |
| 63 |
*/ |
| 64 |
countThemes() { |
| 65 |
const container = this.container; |
| 66 |
const cards = container.querySelectorAll( LPThemes.selectors.card ); |
| 67 |
const counts = { all: cards.length }; |
| 68 |
|
| 69 |
cards.forEach( ( card ) => { |
| 70 |
const key = card.getAttribute( 'data-category' ); |
| 71 |
counts[ key ] = ( counts[ key ] || 0 ) + 1; |
| 72 |
} ); |
| 73 |
|
| 74 |
container |
| 75 |
.querySelectorAll( LPThemes.selectors.filter ) |
| 76 |
.forEach( ( item ) => { |
| 77 |
const count = item.querySelector( LPThemes.selectors.count ); |
| 78 |
const text = `(${ |
| 79 |
counts[ item.getAttribute( 'data-category' ) ] || 0 |
| 80 |
})`; |
| 81 |
if ( count && count.textContent !== text ) { |
| 82 |
count.textContent = text; |
| 83 |
} |
| 84 |
} ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Activate the selected category and filter themes. |
| 89 |
* |
| 90 |
* @param {Object} args Event arguments. |
| 91 |
* @param {Event} args.e Browser event. |
| 92 |
* @param {Element} args.target Event target. |
| 93 |
*/ |
| 94 |
filterByCategory( { e, target } ) { |
| 95 |
const filter = target.closest( LPThemes.selectors.filter ); |
| 96 |
const container = filter.closest( LPThemes.selectors.container ); |
| 97 |
|
| 98 |
if ( ! container ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
e.preventDefault(); |
| 103 |
container |
| 104 |
.querySelectorAll( LPThemes.selectors.filter ) |
| 105 |
.forEach( ( item ) => { |
| 106 |
const isActive = item === filter; |
| 107 |
item.classList.toggle( 'active', isActive ); |
| 108 |
item.setAttribute( |
| 109 |
'aria-pressed', |
| 110 |
isActive ? 'true' : 'false' |
| 111 |
); |
| 112 |
} ); |
| 113 |
|
| 114 |
this.filterThemes(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Filter themes by the active category and search terms. |
| 119 |
*/ |
| 120 |
filterThemes() { |
| 121 |
const container = this.container; |
| 122 |
const search = container.querySelector( LPThemes.selectors.search ); |
| 123 |
const query = search ? search.value.trim().toLowerCase() : ''; |
| 124 |
const searchTerms = query.split( /\s+/ ).filter( Boolean ); |
| 125 |
const active = container.querySelector( |
| 126 |
`${ LPThemes.selectors.filter }.active` |
| 127 |
); |
| 128 |
const category = active |
| 129 |
? active.getAttribute( 'data-category' ) |
| 130 |
: 'all'; |
| 131 |
const cards = container.querySelectorAll( LPThemes.selectors.card ); |
| 132 |
|
| 133 |
cards.forEach( ( card ) => { |
| 134 |
const title = card.querySelector( LPThemes.selectors.title ); |
| 135 |
const description = card.querySelector( |
| 136 |
LPThemes.selectors.description |
| 137 |
); |
| 138 |
const text = `${ title ? title.textContent : '' } ${ |
| 139 |
description ? description.textContent : '' |
| 140 |
}`.toLowerCase(); |
| 141 |
card.hidden = |
| 142 |
( 'all' !== category && |
| 143 |
category !== card.getAttribute( 'data-category' ) ) || |
| 144 |
! searchTerms.every( ( term ) => text.includes( term ) ); |
| 145 |
} ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
const lpThemes = new LPThemes(); |
| 150 |
lpUtils.lpOnElementReady( LPThemes.selectors.elListThemes, ( elListThemes ) => { |
| 151 |
const container = elListThemes.closest( LPThemes.selectors.container ); |
| 152 |
lpThemes.init( container ); |
| 153 |
} ); |
| 154 |
|