@@ -10,6 +10,7 @@ module.exports = panels.view.dialog.extend( {
1010
1111 currentTab : false ,
1212 directoryPage : 1 ,
13+ maxPages : 1 ,
1314 itemsPer : 16 ,
1415 activeFilter : [ ] ,
1516
@@ -234,7 +235,7 @@ module.exports = panels.view.dialog.extend( {
234235 type = 'directory-siteorigin' ;
235236 }
236237
237- if ( type . match ( '^directory-' ) && ! panelsOptions . directory_enabled ) {
238+ if ( this . isLayoutDirectory ( ) && ! panelsOptions . directory_enabled ) {
238239 // Display the button to enable the prebuilt layout
239240 c . removeClass ( 'so-panels-loading' ) . html ( $ ( '#siteorigin-panels-directory-enable' ) . html ( ) ) ;
240241 c . find ( '.so-panels-enable-directory' ) . on ( 'click' , function ( e ) {
@@ -278,6 +279,7 @@ module.exports = panels.view.dialog.extend( {
278279 // Depending on the active type, we need to handle things slightly differently.
279280 if ( type . match ( '^directory-' ) ) {
280281 thisView . directoryPage = page ;
282+ thisView . maxPages = data . max_num_pages ;
281283 thisView . updatePagination ( ) ;
282284 } else {
283285 // Lets setup the next and previous buttons
@@ -369,32 +371,86 @@ module.exports = panels.view.dialog.extend( {
369371 }
370372 } ,
371373
374+ /**
375+ * Get the maximum number of pages for the current context.
376+ *
377+ * @param {* } $items An optional jQuery collection of items to calculate the maximum pages for.
378+ * @returns int The maximum number of pages based on the items and items per page.
379+ */
380+ contextualMaxPages : function ( $items = null ) {
381+ if ( $items === null ) {
382+ $items = this . getItems ( ) ;
383+ }
384+
385+ const contextualMaxPages = Math . ceil ( $items . length / this . itemsPer ) ;
386+ return contextualMaxPages > this . maxPages ? contextualMaxPages : this . maxPages ;
387+ } ,
388+
372389 updatePagination : function ( ) {
373390 var $items = this . getItems ( ) ;
374391
375- // Hide any items not on the current page.
376- var startIndex = ( this . directoryPage - 1 ) * this . itemsPer ;
377- var endIndex = startIndex + this . itemsPer ;
392+ const startIndex = this . isLayoutDirectory ( ) ?
393+ this . directoryPage - 1 :
394+ ( this . directoryPage - 1 ) * this . itemsPer ;
395+
396+ const endIndex = startIndex + this . itemsPer ;
378397
398+ // Hide any items not on the current page.
379399 this . $ ( '.so-directory-items-wrapper .so-directory-item' ) . addClass ( 'so-hidden' ) ;
380400 $items . slice ( startIndex , endIndex ) . removeClass ( 'so-hidden' ) ;
381401
382402 this . $ ( '.so-previous' ) . toggleClass ( 'button-disabled' , this . directoryPage === 1 ) ;
383- this . $ ( '.so-next' ) . toggleClass ( 'button-disabled' , this . directoryPage === Math . ceil ( $items . length / this . itemsPer ) ) ;
403+ this . $ ( '.so-next' ) . toggleClass (
404+ 'button-disabled' ,
405+ this . directoryPage === this . contextualMaxPages ( $items )
406+ ) ;
407+ } ,
408+
409+ isLayoutDirectory : function ( ) {
410+ return this . currentTab . match ( '^directory-' ) ;
411+ } ,
412+
413+ /**
414+ * Update the layout directory interface with the new search value and page.
415+ *
416+ * @param {string } directoryPath - The path to the layout directory.
417+ * @param {Object } config - Configuration options for the update.
418+ * @returns {boolean } - Returns true if the update was successful, otherwise false.
419+ */
420+ updateLayoutDirectory : function ( ) {
421+ if ( ! this . isLayoutDirectory ( ) ) {
422+ return false ;
423+ }
424+
425+ const searchVal = this . $ ( '.so-sidebar-search' ) . val ( ) . toLowerCase ( ) ;
426+ this . displayLayoutDirectory (
427+ searchVal ,
428+ this . directoryPage ,
429+ this . currentTab
430+ ) ;
431+
432+ return true ;
384433 } ,
385434
386435 previousPage : function ( ) {
387436 if ( this . directoryPage > 1 ) {
388437 this . directoryPage -- ;
389- this . updatePagination ( ) ;
438+
439+ if ( ! this . updateLayoutDirectory ( ) ) {
440+ this . updatePagination ( ) ;
441+ }
390442 }
391443 } ,
392444
393445 nextPage : function ( ) {
394- var numPages = Math . ceil ( this . getItems ( ) . length / this . itemsPer ) ;
395- if ( this . directoryPage < numPages ) {
446+ const maxPages = this . contextualMaxPages ( ) ;
447+
448+ if ( this . directoryPage < maxPages ) {
396449 this . directoryPage ++ ;
397- this . updatePagination ( ) ;
450+
451+ if ( ! this . updateLayoutDirectory ( ) ) {
452+ this . updatePagination ( ) ;
453+ }
398454 }
399455 } ,
400456
@@ -503,47 +559,13 @@ module.exports = panels.view.dialog.extend( {
503559 return deferredLayout . promise ( ) ;
504560 } ,
505561
506- filterByTitle : function ( title ) {
507-
508- if ( title === '' ) {
509- this . $ ( '.so-directory-items-wrapper .so-directory-item' ) . removeClass ( 'so-hidden so-search-hidden' ) ;
510- } else {
511- this . $ ( '.so-directory-items' ) . removeClass ( 'so-search-hidden' ) ;
512- var $items = this . getItems ( ) ;
513-
514- $items . each ( function ( ) {
515- let $$ = $ ( this ) ;
516- let found = $$ . find ( '.so-title' ) . text ( ) . toLowerCase ( ) . includes ( title ) ;
517-
518- if ( found && $$ . hasClass ( 'so-hidden' ) ) {
519- $$ . removeClass ( 'so-hidden so-search-hidden' )
520- } else if ( ! found ) {
521- $$ . addClass ( 'so-hidden so-search-hidden' ) ;
522- }
523- } ) ;
524-
525- }
526-
527- // Show or hide the no results message.
528- this . $ ( '.so-directory-items' ) . toggleClass (
529- 'so-empty' ,
530- ! this . $ ( '.so-directory-items-wrapper .so-directory-item:not(.so-hidden)' ) . length
531- ) ;
532-
533- this . updatePagination ( ) ;
534- } ,
535-
536562 /**
537563 * Handle an update to the search
538564 */
539565 searchHandler : function ( e ) {
540566 if ( e . keyCode === 13 ) {
541567 var search = $ ( e . currentTarget ) . val ( ) . toLowerCase ( ) ;
542- if ( this . currentTab . match ( '^directory-' ) ) {
543- this . filterByTitle ( search ) ;
544- } else {
545- this . displayLayoutDirectory ( search , 1 , this . currentTab ) ;
546- }
568+ this . displayLayoutDirectory ( search , 1 , this . currentTab ) ;
547569 }
548570 } ,
549571
0 commit comments