Skip to content

Commit

Permalink
fix play buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiXxD committed Dec 21, 2024
1 parent 047c28c commit 82fe212
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 76 deletions.
14 changes: 11 additions & 3 deletions src/widgets/albumCard/ui/albumCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import heartIcon from "../../../../public/images/icons/heart.svg";
import playCircleIcon from "../../../../public/images/icons/play-circle.svg";
import sendSquareWhiteIcon from "../../../../public/images/icons/send-square-white.svg";
import { userStore } from "../../../entities/user/index.js";
import { player } from "../../../shared/player/model/store.js";

export class AlbumCardView {
/**
Expand Down Expand Up @@ -40,7 +41,9 @@ export class AlbumCardView {
album.image = `${S3_BUCKETS.ALBUM_IMAGES}/${album.image}`;
}

const albumLikesCount = (await albumCardAPI.GetAlbumLikesCount(this.albumId))?.count;
const albumLikesCount = (
await albumCardAPI.GetAlbumLikesCount(this.albumId)
)?.count;

this.albumCardElement = document.createElement("div");
this.albumCardElement.classList.add("album_card");
Expand All @@ -50,7 +53,7 @@ export class AlbumCardView {
heartIcon,
playCircleIcon,
sendSquareWhiteIcon,
albumLikesCount
albumLikesCount,
});
this.parent.appendChild(this.albumCardElement);

Expand Down Expand Up @@ -92,7 +95,12 @@ export class AlbumCardView {
}

handlePlayPauseBtn() {
eventBus.emit("playPauseTrack");
if (player.queue.length > 0) {
eventBus.emit("reloadTracks");
eventBus.emit("playById", 0);
} else {
eventBus.emit("playPauseTrack");
}
}

handleShareBtn = () => {
Expand Down
14 changes: 11 additions & 3 deletions src/widgets/artistCard/ui/artistCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as styles from "./artistCard.scss";
import heartIcon from "../../../../public/images/icons/heart.svg";
import playCircleIcon from "../../../../public/images/icons/play-circle.svg";
import sendSquareWhiteIcon from "../../../../public/images/icons/send-square-white.svg";
import { player } from "../../../shared/player/model/store.js";

export class ArtistCardView {
/**
Expand Down Expand Up @@ -38,7 +39,9 @@ export class ArtistCardView {
artist.image = `${S3_BUCKETS.ARTIST_IMAGES}/${artist.image}`;
}

const artistLikesCount = (await artistCardAPI.GetArtistLikesCount(this.artistId))?.count;
const artistLikesCount = (
await artistCardAPI.GetArtistLikesCount(this.artistId)
)?.count;

const artistCardElement = document.createElement("div");
artistCardElement.classList.add("artist_card");
Expand All @@ -49,7 +52,7 @@ export class ArtistCardView {
heartIcon,
playCircleIcon,
sendSquareWhiteIcon,
artistLikesCount
artistLikesCount,
});
this.parent.appendChild(artistCardElement);

Expand Down Expand Up @@ -81,7 +84,12 @@ export class ArtistCardView {
}

handlePlayPauseBtn() {
eventBus.emit("playPauseTrack");
if (player.queue.length > 0) {
eventBus.emit("reloadTracks");
eventBus.emit("playById", 0);
} else {
eventBus.emit("playPauseTrack");
}
}

handleShareBtn = () => {
Expand Down
141 changes: 75 additions & 66 deletions src/widgets/listenBlock/ui/listenBlock.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,91 @@
import { eventBus } from '../../../shared/lib/eventbus.js';
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';
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 {
/**
* The parent HTML element.
* @type {HTMLElement}
*/
parent;
/**
* The parent HTML element.
* @type {HTMLElement}
*/
parent;

/**
* Initializes the ListenBlockView.
*
*/
constructor(parent) {
this.parent = parent ? parent : document.querySelector('#root');
}
/**
* Initializes the ListenBlockView.
*
*/
constructor(parent) {
this.parent = parent ? parent : document.querySelector("#root");
}

/**
* Renders the listen block view.
*/
async render() {
const listenBlockElement = document.createElement('div');
listenBlockElement.classList.add('listen');
listenBlockElement.innerHTML = template({ styles, playCircleIcon, pauseCircleIcon });
this.parent.appendChild(listenBlockElement);
/**
* Renders the listen block view.
*/
async render() {
const listenBlockElement = document.createElement("div");
listenBlockElement.classList.add("listen");
listenBlockElement.innerHTML = template({
styles,
playCircleIcon,
pauseCircleIcon,
});
this.parent.appendChild(listenBlockElement);

this.getElements();
this.getElements();

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

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

addEvents() {
this.playPauseBtn.addEventListener('click', this.handlePlayPauseBtn);
}
addEvents() {
this.playPauseBtn.addEventListener("click", this.handlePlayPauseBtn);
}

deleteEvents() {
this.playPauseBtn.removeEventListener('click', this.handlePlayPauseBtn);
}
deleteEvents() {
this.playPauseBtn.removeEventListener("click", this.handlePlayPauseBtn);
}

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

offEvents() {
eventBus.off("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.off("playById", this.changePlayPauseBtnImg);
}
onEvents() {
eventBus.on("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.on("playById", this.changePlayPauseBtnImg);
eventBus.on("nextTrack", this.setPlayBtnImg);
eventBus.on("prevTrack", this.setPlayBtnImg);
}

changePlayPauseBtnImg = () => {
if (player.isPlaying) {
this.playPauseBtn.src = pauseCircleIcon;
} else {
this.playPauseBtn.src = playCircleIcon;
}
}
offEvents() {
eventBus.off("playPauseTrack", this.changePlayPauseBtnImg);
eventBus.off("playById", this.changePlayPauseBtnImg);
}

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

handlePlayPauseBtn() {
eventBus.emit('playPauseTrack');
}
setPlayBtnImg = () => {
this.playPauseBtn.src = pauseCircleIcon;
};

destructor() {
this.deleteEvents();
this.offEvents();
}
handlePlayPauseBtn() {
if (player.queue.length > 0) {
eventBus.emit("reloadTracks");
eventBus.emit("playById", 0);
} else {
eventBus.emit("playPauseTrack");
}
}

destructor() {
this.deleteEvents();
this.offEvents();
}
}
16 changes: 12 additions & 4 deletions src/widgets/playlistCard/ui/playlistCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import musicSquareRemoveIcon from "../../../../public/images/icons/music-square-
import sendSquareWhiteIcon from "../../../../public/images/icons/send-square-white.svg";
import { ImageUploaderView } from "../../imageUploader/index.js";
import { playlistAPI } from "../../../entities/playlist/api/api.js";
import { player } from "../../../shared/player/model/store.js";

export class PlaylistCardView {
/**
Expand Down Expand Up @@ -45,7 +46,9 @@ export class PlaylistCardView {
playlist.image = `${S3_BUCKETS.PLAYLIST_IMAGES}/default.webp`;
}

const playlistLikesCount = (await playlistCardAPI.GetPlaylistLikesCount(this.playlistId))?.count;
const playlistLikesCount = (
await playlistCardAPI.GetPlaylistLikesCount(this.playlistId)
)?.count;

const playlistCardElement = document.createElement("div");
playlistCardElement.classList.add("playlist_card");
Expand All @@ -66,7 +69,7 @@ export class PlaylistCardView {
playCircleIcon,
musicSquareRemoveIcon,
sendSquareWhiteIcon,
playlistLikesCount
playlistLikesCount,
});
this.parent.appendChild(playlistCardElement);

Expand All @@ -84,7 +87,7 @@ export class PlaylistCardView {
parent: document.querySelector(".image_uploader"),
uploadFunction: (formData) =>
playlistAPI.updateImage(playlist.id, formData),
onSuccessEvent: 'updatePlaylistImageSuccess',
onSuccessEvent: "updatePlaylistImageSuccess",
navigateUrl: `/playlist/${playlist.id}`,
});
await this.imageUploaderView.render();
Expand Down Expand Up @@ -128,7 +131,12 @@ export class PlaylistCardView {
};

handlePlayPauseBtn() {
eventBus.emit("playPauseTrack");
if (player.queue.length > 0) {
eventBus.emit("reloadTracks");
eventBus.emit("playById", 0);
} else {
eventBus.emit("playPauseTrack");
}
}

handleShareBtn = () => {
Expand Down

0 comments on commit 82fe212

Please sign in to comment.