Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bocharovatd committed Dec 19, 2024
1 parent a5b16d8 commit 4d33794
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/shared/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class Router {
this.layout = [];
this.routes = [];
this.currentView = null;
this.isRendering = false;

this.onPopState = this.onPopState.bind(this);
this.onNavigate = this.onNavigate.bind(this);
Expand Down Expand Up @@ -71,6 +72,10 @@ export class Router {
* Popstate event handler for browser back/forward navigation
*/
async onPopState() {
if (this.isRendering) {
return;
}
eventBus.emit("popstate", window.location.pathname);
await this.goToImpl();
}

Expand All @@ -90,6 +95,9 @@ export class Router {
* @returns {Promise<void>} promise that resolves when navigation is complete
*/
async goTo(path) {
if (this.isRendering) {
return;
}
window.history.pushState({}, "", path);
await this.goToImpl();
}
Expand All @@ -100,6 +108,8 @@ export class Router {
* @returns {Promise<void>} promise that resolves when the rendering is complete
*/
async goToImpl() {
this.isRendering = true;

const currentPath = window.location.pathname;
const targetRoute = this.findRoute(currentPath);

Expand All @@ -116,6 +126,7 @@ export class Router {
this.currentView = new ErrorPage('Ошибка', 'Такой страницы не существует.');
await this.currentView.render();
}
this.isRendering = false;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/shared/player/model/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ class PlayerStore {
state.status === PLAYER_PLAYING_STATE &&
this.isPlaying
) {
this.currentTrack.pause();
this.isPlaying = false;
eventBus.emit('playPauseTrack');
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/footerPlayer/ui/footerPlayer.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<img src="{{ backwardIcon }}" alt="prev_track" class="{{ styles.footer_player__icon }}">
</div>
<div class="buttons_player__play_track player_button">
<img src="{{#if isPlaying }}{{ pauseCircleBlackIcon }}{{else}}{{ playCircleBlackIcon }}{{/if}}" alt="play_pause" class="{{ styles.footer_player__icon }}">
<img src="{{ playCircleBlackIcon }}" alt="play_pause" class="{{ styles.footer_player__icon }}">
</div>
<div class="buttons_player__next_track player_button">
<img src="{{ forwardIcon }}" alt="next_track" class="{{ styles.footer_player__icon }}">
Expand Down
5 changes: 0 additions & 5 deletions src/widgets/footerPlayer/ui/footerPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export class FooterPlayerView {
footerPlayerElement.innerHTML = template({
styles,
playCircleBlackIcon,
pauseCircleBlackIcon,
backwardIcon,
forwardIcon,
heartBlackIcon,
Expand Down Expand Up @@ -96,8 +95,6 @@ export class FooterPlayerView {
eventBus.on("showPlayer", this.showPlayer);
eventBus.on("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.on("playById", this.changePlayPauseBtnImg);
eventBus.on("prevTrack", this.changePlayPauseBtnImg);
eventBus.on("nextTrack", this.changePlayPauseBtnImg);
}

offEvents() {
Expand All @@ -106,8 +103,6 @@ export class FooterPlayerView {
eventBus.off("showPlayer", this.showPlayer);
eventBus.on("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.on("playById", this.changePlayPauseBtnImg);
eventBus.on("prevTrack", this.changePlayPauseBtnImg);
eventBus.on("nextTrack", this.changePlayPauseBtnImg);
}

addEvents() {
Expand Down
13 changes: 7 additions & 6 deletions src/widgets/header/ui/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export class Header {

constructor() {
this.parent = document.querySelector('#header');
// this.eventBus = eventBus;
// this.userStore = userStore;
this.handleNavigation = this.handleNavigation.bind(this);
}

render() {
Expand Down Expand Up @@ -48,8 +47,6 @@ export class Header {
links.forEach((link) => {
link.addEventListener('click', handleLink);
});

eventBus.on('navigate', this.handleNavigation.bind(this));
}

handleSignOutBtn(event) {
Expand Down Expand Up @@ -77,13 +74,17 @@ export class Header {
eventBus.on('signUpSuccess', this.onSignUpSuccess);
eventBus.on('signOutSuccess', this.onSignOutSuccess);
eventBus.on('unauthorized', this.onSignOutSuccess);
eventBus.on('navigate', this.handleNavigation);
eventBus.on('popstate', this.handleNavigation);
}

offEvents() {
eventBus.off('signInSuccess', this.onSignInSuccess);
eventBus.off('signUpSuccess', this.onSignUpSuccess);
eventBus.off('signOutSuccess', this.onSignOutSuccess);
eventBus.off('unauthorized', this.onSignOutSuccess);
eventBus.off('navigate', this.handleNavigation);
eventBus.off('popstate', this.handleNavigation);
}

onSignInSuccess = (user) => {
Expand All @@ -102,11 +103,11 @@ export class Header {
this.offEvents();
}

handleNavigation(href) {
handleNavigation (href) {
this.switchActiveNavlink(href);
}

switchActiveNavlink(href) {
switchActiveNavlink (href) {
let navlinks = document.querySelectorAll('.navlink_switch');
navlinks.forEach((navlink) => {
if (navlink.getAttribute('href') == href) {
Expand Down
31 changes: 29 additions & 2 deletions src/widgets/listenBlock/ui/listenBlock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { eventBus } from '../../../shared/lib/eventbus.js';
import { player } from "../../../shared/player/model/store.js";
import template from './listenBlock.hbs';
import * as styles from './listenBlock.scss';
import playCircleIcon from '../../../../public/images/icons/play-circle.svg';
import pauseCircleIcon from '../../../../public/images/icons/pause-circle.svg';

export class ListenBlockView {
/**
Expand All @@ -24,11 +26,17 @@ export class ListenBlockView {
async render() {
const listenBlockElement = document.createElement('div');
listenBlockElement.classList.add('listen');
listenBlockElement.innerHTML = template({ styles, playCircleIcon });
listenBlockElement.innerHTML = template({ styles, playCircleIcon, pauseCircleIcon });
this.parent.appendChild(listenBlockElement);

this.playPauseBtn = document.querySelector(`.${styles['listen__btn_img']}`);
this.getElements();

this.addEvents();
this.onEvents();
}

async getElements() {
this.playPauseBtn = document.querySelector(`.${styles['listen__btn_img']}`);
}

addEvents() {
Expand All @@ -39,11 +47,30 @@ export class ListenBlockView {
this.playPauseBtn.removeEventListener('click', this.handlePlayPauseBtn);
}

onEvents() {
eventBus.on("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.on("playById", this.changePlayPauseBtnImg);
}

offEvents() {
eventBus.off("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.off("playById", this.changePlayPauseBtnImg);
}

changePlayPauseBtnImg = () => {
if (player.isPlaying) {
this.playPauseBtn.src = pauseCircleIcon;
} else {
this.playPauseBtn.src = playCircleIcon;
}
}

handlePlayPauseBtn() {
eventBus.emit('playPauseTrack');
}

destructor() {
this.deleteEvents();
this.offEvents();
}
}
6 changes: 6 additions & 0 deletions src/widgets/listenBlock/ui/listenBlock.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
&__btn_img {
display: block;
margin: 0 auto;
transition-duration: 0.2s;
transition-timing-function: ease;

&:hover {
transform: scale(1.1);
}
}

&__title {
Expand Down
9 changes: 4 additions & 5 deletions src/widgets/trackList/ui/trackList.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ export class TrackListView {
this.parent.appendChild(trackListElement);

const tracksBlock = document.getElementById("tracks");
let promises = [];
Array.from(tracks).forEach(async (track, index) => {
for (const [index, track] of Array.from(tracks).entries()) {
const trackView = new TrackView(tracksBlock, index);
promises.push(trackView.render(track, this.myPlaylistId));
});
Promise.all(promises).then(() => eventBus.emit("tracks:rendered"));
await trackView.render(track, this.myPlaylistId);
}
eventBus.emit("tracks:rendered");

this.bindEvents();
this.setTitle(titleText);
Expand Down

0 comments on commit 4d33794

Please sign in to comment.