Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
<script src="./scripts/index.js" defer></script>
</head>
<body>


<div id="songs">
<!-- Generated songs go here -->
<header> Mp3 Player </header>
<!-- Generated songs go here -->

</div>
<br>
<div id="playlists">
<!-- Generated playlists go here -->
</div>
Expand Down
31 changes: 17 additions & 14 deletions index.new.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,9 @@
<script src="./scripts/index.new.js" defer></script>
</head>
<body>
<h1>Awesome Player!!!</h1>
<div id="add-section">
<h2>Add a new song</h2>
<div id="inputs">
<input name="title" placeholder="Title">
<input name="album" placeholder="Album">
<input name="artist" placeholder="Artist">
<input name="duration" placeholder="Duration (mm:ss)">
<input name="cover-art" placeholder="Cover art link">
</div>
<button id="add-button">Add</button>
</div>
<div id="songs">
<h2>Songs</h2>
<div>
<div id="songs">
<h2>Mp3 Player</h2>
<div class="list">
<!-- <div class="song">
<div class="left">
Expand Down Expand Up @@ -57,5 +46,19 @@ <h2>Playlists</h2>
</div> -->
</div>
</div>
<div id="add-section" class="add-song">
<h2>Add a new song</h2>
<div id="inputs">
<input name="title" placeholder="Title" id="title" />
<input name="album" placeholder="Album" id="album" />
<input name="artist" placeholder="Artist" id="artist" />
<input name="duration" placeholder="Duration (mm:ss)" id="duration" />
<input name="cover-art" placeholder="Cover art link" id="cover-art" />

</div>
<button id="add-button">Add</button>
</div>

</div>
</body>
</html>
94 changes: 86 additions & 8 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,42 @@
*
* @param {String} songId - the ID of the song to play
*/
function playSong(songId) {
// Your code here

function playSong(songId) {
for (let song of player.songs) {
document.getElementById("song" + song.id).classList.remove("playing")
if (song.id === songId) {
document.getElementById("song" + song.id).classList.add("playing")
}
}
}

/**
* Creates a song DOM element based on a song object.
*/
function createSongElement({ id, title, album, artist, duration, coverArt }) {
const children = []
const children = [
createElement("img", [], [], { src: coverArt }),
createElement("span", title +" | ", [], {}),
createElement("span", album+" | ", [], {}),
createElement("span", artist+" | ", [], {}),
createElement("span", durationFormat(duration), ["duration"], {}, ),

]
const classes = []
const attrs = { onclick: `playSong(${id})` }
const attrs = { onclick: `playSong(${id})`, id: "song" + id }
return createElement("div", children, classes, attrs)
}

/**
* Creates a playlist DOM element based on a playlist object.
*/
function createPlaylistElement({ id, name, songs }) {
const children = []
const children = [
createElement("span", name+" | ", [], {}),
createElement("span", [songs.length], [], {}),
createElement("span", playlistDuration(id), ["duration"], {}),
]
const classes = []
const attrs = {}
return createElement("div", children, classes, attrs)
Expand All @@ -34,14 +51,75 @@ function createPlaylistElement({ id, name, songs }) {
* Example usage:
* createElement("div", ["just text", createElement(...)], ["nana", "banana"], {id: "bla"})
*
* @param {String} tagName - the type of the element
* @param {Array} children - the child elements for the new element.
* @param {String} tagName - the type of the element: for example- p,h1,div...
* @param {Array} children - the child elements for the new element: for example- assume ew have <ul> his children <li>
* Each child can be a DOM element, or a string (if you just want a text element).
* @param {Array} classes - the class list of the new element
* @param {Object} attributes - the attributes for the new element
*/
function createElement(tagName, children = [], classes = [], attributes = {}) {
// Your code here
const element = document.createElement(tagName)
for (let child of children) {
element.append(child)
}
for (let name of classes) {
element.classList.add(name)
}
for (let attribute in attributes) {
element.setAttribute(attribute, attributes[attribute])
}
return element
}

// You can write more code below this line

function durationFormat(duration) {
//converting from seconds to mm:ss format
let minutes = Math.floor(duration / 60)
let seconds = duration % 60
if (minutes < 10 && seconds < 10) return "0" + minutes + ":" + "0" + seconds
else if (minutes < 10) return "0" + minutes + ":" + seconds
else if (seconds < 10) return minutes + ":" + "0" + seconds
else return minutes + ":" + seconds
}

function playlistDuration(id) {
let correctPlaylist = findPlaylistById(id) //correctPlaylist contain the wanted playlist
let saveSongId = 0,
sum = 0
for (let i = 0; i < correctPlaylist.songs.length; i++) {
//run on the songs array inside this playlist
saveSongId = correctPlaylist.songs[i]
for (let j = 0; j < player.songs.length; j++) {
//run on the songs array
if (player.songs[j].id === saveSongId) sum += player.songs[j].duration
}
}
return durationFormat(sum)
}

function findPlaylistById(id) {
//Get a playlist id and return the wanted playlist by his id
let correctPlaylist
for (let i = 0; i < player.playlists.length; i++) {
//run on playlists array
if (id === player.playlists[i].id) correctPlaylist = player.playlists[i]
}
return correctPlaylist
}

const elemHtml = document.getElementById("songs")
const elemHtml1 = document.getElementById("playlists")

player.playlists.sort((playlistA, playlistB) => playlistA.name.localeCompare(playlistB.name))
player.songs.sort((songA, songB) => songA.title.localeCompare(songB.title))

for (let i = 0; i < player.songs.length; i++) {
//songs
elemHtml.append(createSongElement(player.songs[i]))
}

for (let i = 0; i < player.playlists.length; i++) {
//playlists
elemHtml1.append(createPlaylistElement(player.playlists[i]))
}
Loading