function removeAuthor(authorId) { api.removeAuthor(authorId, () => { showAudio(showAudio.settingsAuthors, showAudio.settingsAudio); }); } function editAuthor(authorId) { const name = document.getElementById(`input-author-name-${authorId}`).value; api.editAuthor(authorId, name, (res) => { if (res !== undefined) removeAuthorEditor(authorId, name); }); } function removeAuthorEditor(authorId, authorName) { document.getElementById( `author-info-${authorId}`, ).innerHTML = `

${authorName}

`; setTimeout(() => { document.getElementById(`edit-author-${authorId}`).onclick = () => showAuthorEditor(authorId, authorName); }, 5); } function showAuthorEditorButtonInit(authorId) { const authorName = document.getElementById( `author-info-${authorId}`, ).lastValue; document.getElementById(`cancel-edit-author-${authorId}`).onclick = () => removeAuthorEditor(authorId, authorName); document.getElementById(`remove-author-${authorId}`).onclick = () => removeAuthor(authorId); document.getElementById(`edit-author-${authorId}`).onclick = () => editAuthor(authorId); // document.getElementById(`add-author-button`).onclick = addAuthor; } function showAuthorEditor(authorId, lastValue) { document.getElementById(`author-info-${authorId}`).lastValue = lastValue; document.getElementById( `author-info-${authorId}`, ).innerHTML = `

`; setTimeout(() => showAuthorEditorButtonInit(authorId), 5); } function addAuthor() { const name = document.getElementById(`author-name-input`).value; document.getElementById("add-author").innerHTML = "

Добавляем..

"; api.createAuthor(name, () => { showAudio(showAudio.settingsAuthors, showAudio.settingsAudio); }); } function rmAuthorMenuButtonInit() { document.getElementById(`add-author-button`).onclick = addAuthorMenu; } function removeAuthorMenu() { document.getElementById( "add-author", ).innerHTML = `
`; setTimeout(rmAuthorMenuButtonInit, 5); } function authorMenuButtonInit() { document.getElementById(`cancel-add-author`).onclick = removeAuthorMenu; document.getElementById(`add-author-button`).onclick = addAuthor; } function addAuthorMenu() { document.getElementById( "add-author", ).innerHTML = `

`; setTimeout(authorMenuButtonInit, 5); } function selectFile(muzicId) { const input = document.createElement("input"); input.type = "file"; input.accept = "audio/mpeg"; input.click(); input.onchange = function (e) { function arrayBufferToBase64(buffer) { let binary = ""; let bytes = new Uint8Array(buffer); let len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); } const file = e.target.files[0]; // console.log(file); const reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onload = function () { // console.log('reader.result', reader.result); if (file.type === "audio/mpeg") { document.getElementById(`edit-attach-file-${muzicId}`).filedata = arrayBufferToBase64(reader.result); document.getElementById(`edit-attach-file-${muzicId}`).innerText = "Загружен файл: " + file.name; document.getElementById(`delete-attach-${muzicId}`).disabled = false; } // console.log(arrayBufferToBase64(reader.result)); }; }; } function editMuzic(muzicId, muzicName) { api.editMuzic( muzicId, muzicName, document.getElementById(`edit-attach-file-${muzicId}`).filedata, undefined, (res) => { showAudio(showAudio.settingsAuthors, showAudio.settingsAudio); }, ); } function removeMuzic(muzicId) { api.deleteMuzic(muzicId, (res) => { showAudio(showAudio.settingsAuthors, showAudio.settingsAudio); }); } function bindShowEditMuzicButtons(muzicId, muzicName, dataExists) { document.getElementById(`cancel-edit-muzic-${muzicId}`).onclick = () => { document.getElementById(`muzic-item-${muzicId}`).innerHTML = dataExists ? `
${muzicName}
` : `

${muzicName}

`; }; document.getElementById(`edit-attach-file-${muzicId}`).onclick = () => selectFile(muzicId); document.getElementById(`delete-attach-${muzicId}`).onclick = () => { document.getElementById(`edit-attach-file-${muzicId}`).innerText = "Добавить файл (.mp3)"; document.getElementById(`edit-attach-file-${muzicId}`).filedata = null; document.getElementById(`delete-attach-${muzicId}`).disabled = true; }; document.getElementById(`edit-muzic-${muzicId}`).onclick = () => editMuzic(muzicId, muzicName); document.getElementById(`delete-muzic-${muzicId}`).onclick = () => removeMuzic(muzicId); } function showEditMuzic(muzicId, muzicName, dataExists) { document.getElementById( `muzic-item-${muzicId}`, ).innerHTML = `

`; setTimeout(() => bindShowEditMuzicButtons(muzicId, muzicName, dataExists), 5); }