@@ -7,8 +7,8 @@ class CMSFilter {
77 this . filterForm = document . querySelector ( '[wt-cmsfilter-element="filter-form"]' ) ;
88 this . listElement = document . querySelector ( '[wt-cmsfilter-element="list"]' ) ;
99 this . filterElements = this . filterForm . querySelectorAll ( '[wt-cmsfilter-category]' ) ;
10- this . currentPage = 1 ; // default value
11- this . itemsPerPage = 0 ; // gets updated during init
10+ this . currentPage = 1 ;
11+ this . itemsPerPage = 0 ;
1212
1313 //TAG elements
1414 this . tagTemplate = document . querySelector ( '[wt-cmsfilter-element="tag-template"]' ) ;
@@ -18,31 +18,26 @@ class CMSFilter {
1818 //Pagination wrapper is a MUST for the full functionality of the filter to work properly,
1919 //if not added the filter will only work with whatever is loaded by default.
2020 this . paginationWrapper = document . querySelector ( '[wt-cmsfilter-element="pagination-wrapper"]' ) || null ;
21- this . loadMode = this . listElement . getAttribute ( 'wt-cmsfilter-loadmode' ) || 'load-all' ; //Currently only paginate and load-all
21+ this . loadMode = this . listElement . getAttribute ( 'wt-cmsfilter-loadmode' ) || 'load-all' ;
2222 this . previousButton = document . querySelector ( '[wt-cmsfilter-pagination="prev"]' ) ;
2323 this . nextButton = document . querySelector ( '[wt-cmsfilter-pagination="next"]' ) ;
2424 this . customNextButton = document . querySelector ( '[wt-cmsfilter-element="custom-next"]' ) ;
2525 this . customPrevButton = document . querySelector ( '[wt-cmsfilter-element="custom-prev"]' ) ;
2626
27- //pagination opt
2827 this . paginationcounter = document . querySelector ( '[wt-cmsfilter-element="page-count"]' ) ;
29-
30- //OPT
3128 this . activeFilterClass = this . filterForm . getAttribute ( 'wt-cmsfilter-class' ) ;
3229 this . clearAll = document . querySelector ( '[wt-cmsfilter-element="clear-all"]' ) ;
3330 this . sortOptions = document . querySelector ( '[wt-cmsfilter-element="sort-options"]' ) ;
3431 this . resultCount = document . querySelector ( '[wt-cmsfilter-element="results-count"]' ) ;
3532 this . emptyElement = document . querySelector ( '[wt-cmsfilter-element="empty"]' ) ;
3633 this . resetIx2 = this . listElement . getAttribute ( 'wt-cmsfilter-resetix2' ) || false ;
3734
38- //Data Tracking Values
3935 this . allItems = [ ] ;
4036 this . filteredItems = [ ] ;
4137 this . totalPages = 1 ;
4238 this . activeFilters = { } ;
4339 this . availableFilters = { } ;
4440
45- //Script Init
4641 this . init ( ) ;
4742 }
4843
@@ -51,6 +46,11 @@ class CMSFilter {
5146 this . itemsPerPage = this . allItems . length ;
5247 if ( this . paginationWrapper ) {
5348 await this . LoadAllItems ( ) ;
49+ if ( this . paginationcounter && this . paginationcounter != this . paginationWrapper . querySelector ( '.w-page-count' ) ) {
50+ this . paginationWrapper . querySelector ( '.w-page-count' ) . remove ( ) ;
51+ } else {
52+ this . paginationcounter = this . paginationWrapper . querySelector ( '.w-page-count' ) ;
53+ }
5454 }
5555 this . SetupEventListeners ( ) ;
5656 this . RenderItems ( ) ;
@@ -213,6 +213,7 @@ class CMSFilter {
213213 } else {
214214 this . filteredItems . forEach ( item => {
215215 this . listElement . appendChild ( item ) ;
216+ if ( this . resetIx2 ) this . ResetInteraction ( item ) ;
216217 } ) ;
217218 }
218219
@@ -221,52 +222,63 @@ class CMSFilter {
221222 }
222223
223224 SortItems ( ) {
224- if ( ! this . sortOptions ) return ;
225+ if ( ! this . sortOptions ) return ;
225226
226227 let [ key , order ] = this . sortOptions . value . split ( '-' ) ;
228+ this . filteredItems = this . filteredItems . filter ( item => ! item . hasAttribute ( 'wt-renderstatic-element' ) ) ;
227229 this . filteredItems . sort ( ( a , b ) => {
228230 let aValue = a . dataset [ key ] ;
229231 let bValue = b . dataset [ key ] ;
230232
233+ // Handle null or undefined values
234+ if ( aValue === undefined || aValue === null ) aValue = '' ;
235+ if ( bValue === undefined || bValue === null ) bValue = '' ;
236+
237+ // Handle numeric values
231238 if ( ! isNaN ( aValue ) && ! isNaN ( bValue ) ) {
232239 aValue = parseFloat ( aValue ) ;
233240 bValue = parseFloat ( bValue ) ;
234241 }
235- else if ( Date . parse ( aValue ) && Date . parse ( bValue ) ) {
242+ // Handle date values
243+ else if ( ! isNaN ( Date . parse ( aValue ) ) && ! isNaN ( Date . parse ( bValue ) ) ) {
236244 aValue = new Date ( aValue ) ;
237245 bValue = new Date ( bValue ) ;
238246 }
247+ // Handle text values
248+ else {
249+ aValue = aValue . toString ( ) . toLowerCase ( ) ;
250+ bValue = bValue . toString ( ) . toLowerCase ( ) ;
251+ }
239252
240253 if ( order === 'asc' ) {
241- return aValue > bValue ? 1 : - 1 ;
254+ return aValue > bValue ? 1 : aValue < bValue ? - 1 : 0 ;
242255 } else {
243- return aValue < bValue ? 1 : - 1 ;
256+ return aValue < bValue ? 1 : aValue > bValue ? - 1 : 0 ;
244257 }
245258 } ) ;
246- }
259+ }
247260
248261 ApplyFilters ( ) {
249262 const filters = this . GetFilters ( ) ;
250- this . currentPage = 1 ; //Reset pagination to first page
263+ this . currentPage = 1 ; // Reset pagination to first page
251264 this . filteredItems = this . allItems . filter ( item => {
252265 return Object . keys ( filters ) . every ( category => {
253266 const values = [ ...filters [ category ] ] ;
254- if ( values . length === 0 ) return values . length === 0 ;
267+ if ( values . length === 0 ) return true ;
255268
256269 let matchingText = item . querySelector ( `[wt-cmsfilter-category="${ category } "]` ) ?. innerText . toLowerCase ( ) || '' ;
257270 matchingText = matchingText . replace ( / (?: & n b s p ; | \s ) + / gi, ' ' ) ;
258271
259272 if ( category === '*' ) {
260- return values . length === 0 ||
261- values . some ( value => matchingText . includes ( value . toLowerCase ( ) ) ) ||
273+ return values . some ( value => matchingText . includes ( value . toLowerCase ( ) ) ) ||
262274 Object . values ( item . dataset ) . some ( dataValue =>
263275 values . some ( value => dataValue . toLowerCase ( ) . includes ( value . toLowerCase ( ) ) )
264276 ) ;
265277 } else {
266- return values . length === 0 || values . some ( value => {
278+ return values . some ( value => {
267279 if ( typeof value === 'object' ) {
268280 const itemValue = parseFloat ( item . dataset [ category ] ) ;
269- if ( value . from !== null && value . to !== null ) {
281+ if ( value . from !== null && value . to !== null ) {
270282 return itemValue >= value . from && itemValue <= value . to ;
271283 } else if ( value . from !== null && value . to == null ) {
272284 return itemValue >= value . from ;
@@ -492,7 +504,6 @@ class CMSFilter {
492504 UpdatePaginationDisplay ( ) {
493505 if ( ! this . paginationWrapper ) return ;
494506
495- this . paginationcounter = this . paginationcounter ? this . paginationcounter : this . paginationWrapper . querySelector ( '.w-page-count' ) ;
496507 if ( this . paginationcounter ) {
497508 this . paginationcounter . innerText = `${ this . currentPage } / ${ this . totalPages } ` ;
498509 }
@@ -562,7 +573,7 @@ class CMSFilter {
562573
563574 this . ApplyFilters ( ) ;
564575 }
565-
576+
566577 ResetInteraction ( element ) {
567578 if ( ! element ) {
568579 console . error ( 'Element not found' ) ;
0 commit comments