@@ -11,37 +11,41 @@ const FilterId = {
1111 DISCUSSED : 'filter-discussed' ,
1212} ;
1313
14- const filtersSection = document . querySelector ( '.img-filters' ) ;
15- const filtersForm = document . querySelector ( '.img-filters__form' ) ;
14+ const filtersSectionNode = document . querySelector ( '.img-filters' ) ;
15+ const filtersFormNode = document . querySelector ( '.img-filters__form' ) ;
1616
1717let currentFilter = FilterId . DEFAULT ;
18- let photos = [ ] ;
18+ let loadedPhotos = [ ] ;
1919
20- const getRandomPhotos = ( photosArray ) => {
21- const shuffled = [ ...photosArray ] . sort ( ( ) => Math . random ( ) - 0.5 ) ;
20+ const getRandomPhotos = ( photos ) => {
21+ const shuffled = [ ...photos ] ;
22+ for ( let i = shuffled . length - 1 ; i > 0 ; i -- ) {
23+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
24+ [ shuffled [ i ] , shuffled [ j ] ] = [ shuffled [ j ] , shuffled [ i ] ] ;
25+ }
2226 return shuffled . slice ( 0 , RANDOM_PHOTOS_COUNT ) ;
2327} ;
2428
25- const getDiscussedPhotos = ( photosArray ) =>
26- [ ...photosArray ] . sort ( ( a , b ) => b . comments . length - a . comments . length ) ;
29+ const getDiscussedPhotos = ( photos ) =>
30+ [ ...photos ] . sort ( ( a , b ) => b . comments . length - a . comments . length ) ;
2731
2832const filterPhotos = ( ) => {
2933 switch ( currentFilter ) {
3034 case FilterId . RANDOM :
31- return getRandomPhotos ( photos ) ;
35+ return getRandomPhotos ( loadedPhotos ) ;
3236 case FilterId . DISCUSSED :
33- return getDiscussedPhotos ( photos ) ;
37+ return getDiscussedPhotos ( loadedPhotos ) ;
3438 default :
35- return [ ...photos ] ;
39+ return [ ...loadedPhotos ] ;
3640 }
3741} ;
3842
39- const updateActiveButton = ( clickedButton ) => {
40- const activeButton = filtersForm . querySelector ( `.${ ACTIVE_BUTTON_CLASS } ` ) ;
41- if ( activeButton ) {
42- activeButton . classList . remove ( ACTIVE_BUTTON_CLASS ) ;
43+ const updateActiveButton = ( clickedButtonNode ) => {
44+ const activeButtonNode = filtersFormNode . querySelector ( `.${ ACTIVE_BUTTON_CLASS } ` ) ;
45+ if ( activeButtonNode ) {
46+ activeButtonNode . classList . remove ( ACTIVE_BUTTON_CLASS ) ;
4347 }
44- clickedButton . classList . add ( ACTIVE_BUTTON_CLASS ) ;
48+ clickedButtonNode . classList . add ( ACTIVE_BUTTON_CLASS ) ;
4549} ;
4650
4751const debouncedRenderThumbnails = debounce (
@@ -66,13 +70,13 @@ const onFilterButtonClick = (evt) => {
6670} ;
6771
6872const showFilters = ( ) => {
69- filtersSection . classList . remove ( 'img-filters--inactive' ) ;
73+ filtersSectionNode . classList . remove ( 'img-filters--inactive' ) ;
7074} ;
7175
72- const initFilters = ( loadedPhotos ) => {
73- photos = loadedPhotos ;
76+ const initFilters = ( photos ) => {
77+ loadedPhotos = photos ;
7478 showFilters ( ) ;
75- filtersForm . addEventListener ( 'click' , onFilterButtonClick ) ;
79+ filtersFormNode . addEventListener ( 'click' , onFilterButtonClick ) ;
7680} ;
7781
7882export { initFilters } ;
0 commit comments