11jQuery ( function ( $ ) {
22
33 const rows = $ ( ".result-row" ) ,
4- body = $ ( "body" ) ;
4+ body = $ ( "body" ) ,
5+ rowsContainer = rows . parent ( ) ;
56
67 $ ( ".results-text" ) . text ( rows . length ) ;
78
8- $ ( "select" ) . change ( function ( ) {
9+ // Search functionality
10+ $ ( ".search-input" ) . on ( "keyup" , function ( ) {
11+ const searchTerm = $ ( this ) . val ( ) . toLowerCase ( ) ;
12+
13+ rows . each ( function ( ) {
14+ const el = $ ( this ) ,
15+ name = el . attr ( "data-name" ) ? el . attr ( "data-name" ) . toLowerCase ( ) : "" ,
16+ category = el . attr ( "data-category" ) ? el . attr ( "data-category" ) . toLowerCase ( ) : "" ,
17+ requires = el . attr ( "data-requires" ) ? el . attr ( "data-requires" ) . toLowerCase ( ) : "" ,
18+ description = el . find ( ".description" ) . text ( ) . toLowerCase ( ) ;
19+
20+ if ( name . indexOf ( searchTerm ) >= 0 ||
21+ category . indexOf ( searchTerm ) >= 0 ||
22+ requires . indexOf ( searchTerm ) >= 0 ||
23+ description . indexOf ( searchTerm ) >= 0 ) {
24+ el . removeClass ( "js-hidden-search" ) ;
25+ } else {
26+ el . addClass ( "js-hidden-search" ) ;
27+ }
28+ } ) ;
29+
30+ updateResultsCount ( ) ;
31+ } ) ;
32+
33+ // Sort functionality
34+ $ ( ".sort-by" ) . change ( function ( ) {
35+ const sortValue = $ ( this ) . val ( ) ;
36+
37+ if ( sortValue === "none" ) {
38+ return ;
39+ }
40+
41+ const sortedRows = rows . sort ( function ( a , b ) {
42+ const aEl = $ ( a ) ,
43+ bEl = $ ( b ) ;
44+
45+ if ( sortValue === "name-asc" ) {
46+ const aName = aEl . attr ( "data-name" ) || "" ,
47+ bName = bEl . attr ( "data-name" ) || "" ;
48+ return aName . localeCompare ( bName ) ;
49+ } else if ( sortValue === "name-desc" ) {
50+ const aName = aEl . attr ( "data-name" ) || "" ,
51+ bName = bEl . attr ( "data-name" ) || "" ;
52+ return bName . localeCompare ( aName ) ;
53+ } else if ( sortValue === "created-asc" ) {
54+ const aYear = parseInt ( aEl . attr ( "data-created" ) ) || 0 ,
55+ bYear = parseInt ( bEl . attr ( "data-created" ) ) || 0 ;
56+ return aYear - bYear ;
57+ } else if ( sortValue === "created-desc" ) {
58+ const aYear = parseInt ( aEl . attr ( "data-created" ) ) || 0 ,
59+ bYear = parseInt ( bEl . attr ( "data-created" ) ) || 0 ;
60+ return bYear - aYear ;
61+ }
62+ return 0 ;
63+ } ) ;
64+
65+ rowsContainer . append ( sortedRows ) ;
66+ } ) ;
67+
68+ // Filter functionality
69+ $ ( "select:not(.sort-by)" ) . change ( function ( ) {
970 const el = $ ( this ) ,
1071 attr = el . attr ( "class" ) . replace ( "filter-" , "" ) ,
1172 selected = el . val ( ) ;
@@ -28,9 +89,15 @@ jQuery(function($) {
2889 $ ( this ) . removeClass ( "js-hidden-" + attr ) ;
2990 } ) ;
3091 }
31-
92+
93+ updateResultsCount ( ) ;
3294 } ) ;
3395
96+ function updateResultsCount ( ) {
97+ const visibleRows = rows . filter ( ":visible" ) . length ;
98+ $ ( ".results-text" ) . text ( visibleRows ) ;
99+ }
100+
34101 // Click to open a project
35102 $ ( ".js-more" ) . click ( function ( e ) {
36103 e . preventDefault ( ) ;
0 commit comments