12311k

Untitled

Feb 24th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. class Song {
  2. constructor(title, artist) {
  3. this.songElement = this.createSong(title, artist);
  4. }
  5.  
  6. createSong(titleValue, artistValue) {
  7. const trackContainer = document.createElement('div');
  8. const artistElement = document.createElement('h4');
  9. const titleElement = document.createElement('p');
  10. const songButtonElement = document.createElement('button');
  11.  
  12. trackContainer.classList.add('song');
  13. artistElement.classList.add('song__artist');
  14. artistElement.textContent = artistValue;
  15. titleElement.classList.add('song__title');
  16. titleElement.textContent = titleValue;
  17. songButtonElement.classList.add('song__like');
  18.  
  19. trackContainer.appendChild(artistElement);
  20. trackContainer.appendChild(titleElement);
  21. trackContainer.appendChild(songButtonElement);
  22.  
  23. return trackContainer;
  24. }
  25. }
  26.  
  27. /* Переменные */
  28.  
  29. const songsContainer = document.querySelector('.songs-container');
  30. const addButton = document.querySelector('.input__btn_add');
  31. const resetButton = document.querySelector('.input__btn_reset');
  32. const coverHeading = document.querySelector('.cover__heading');
  33. const form = document.forms.add;
  34.  
  35. /* Функции */
  36.  
  37. // Перерисовывает блок добавленных песен
  38. function renderAdded() {
  39. const songs = document.querySelectorAll('.song');
  40. const noSongsElement = document.querySelector('.no-songs');
  41.  
  42. if (songs.length === 0) {
  43. noSongsElement.classList.remove('no-songs_hidden');
  44. } else {
  45. noSongsElement.classList.add('no-songs_hidden');
  46. }
  47. }
  48.  
  49. // Создаёт элемент песни и возвращает его
  50. function createSong(titleValue, artistValue) {
  51. const trackContainer = document.createElement('div');
  52. const artistElement = document.createElement('h4');
  53. const titleElement = document.createElement('p');
  54. const songButtonElement = document.createElement('button');
  55.  
  56. trackContainer.classList.add('song');
  57. artistElement.classList.add('song__artist');
  58. artistElement.textContent = artistValue;
  59. titleElement.classList.add('song__title');
  60. titleElement.textContent = titleValue;
  61. songButtonElement.classList.add('song__like');
  62.  
  63. trackContainer.appendChild(artistElement);
  64. trackContainer.appendChild(titleElement);
  65. trackContainer.appendChild(songButtonElement);
  66.  
  67. return trackContainer;
  68. }
  69.  
  70. // Добавляет элемент песни
  71. function addSong() {
  72. const title = form.elements.title;
  73. const artist = form.elements.artist;
  74. const song = createSong(title.value, artist.value);
  75.  
  76. songsContainer.appendChild(song);
  77. renderAdded();
  78. }
  79.  
  80. // Обработчик клика по кнопке «Очистить плейлист»
  81. function resetPlaylist() {
  82. songsContainer.innerHTML = '';
  83. renderAdded();
  84. }
  85.  
  86. function likeHandler(event) {
  87. if (event.target.classList.contains('song__like')) {
  88. event.target.classList.toggle('song__like_active');
  89. }
  90. }
  91.  
  92. /* Слушатели событий */
  93.  
  94. form.addEventListener('submit', function (event) {
  95. event.preventDefault();
  96.  
  97. addSong();
  98.  
  99. form.reset();
  100. });
  101.  
  102. resetButton.addEventListener('click', function () {
  103. resetPlaylist();
  104. });
  105.  
  106. songsContainer.addEventListener('click', likeHandler);
  107.  
  108. /* Вызовы функций */
  109.  
  110. renderAdded();
Advertisement
Add Comment
Please, Sign In to add comment