document.getElementById("app-window").innerHTML =
"
Загрузка произведений...";
function toggleShow(authorId) {
document.getElementById(`playlist-author-${authorId}`).hidden =
!document.getElementById(`playlist-author-${authorId}`).hidden;
}
function showAudio(settingsAuthors, settingsAudio) {
showAudio.settingsAuthors = settingsAuthors;
showAudio.settingsAudio = settingsAudio;
const step2 = (muzic) => {
//console.log(muzic.items);
const showedAuthors = [
...new Set(muzic.items.map((i) => `author-${i.author_id}`)),
];
[...document.getElementsByClassName("author-element")].forEach((el) => {
if (
JSON.stringify(settingsAuthors) === "{}" &&
JSON.stringify(settingsAudio) === "{}"
) {
if (!showedAuthors.includes(el.id)) {
const id = el.id.slice(7);
document.getElementById(`loader-muzic-${id}`).innerHTML =
"Пусто. Добавьте произведения
";
}
return;
}
el.hidden = !showedAuthors.includes(el.id);
});
muzic.items
.sort((a, b) => a.date - b.date)
.forEach((muzic) => {
document.getElementById(
`loader-muzic-${muzic.author_id}`,
).hidden = true;
if (muzic.is_data_exists)
document.getElementById(
`playlist-author-${muzic.author_id}`,
).innerHTML += ``;
else
document.getElementById(
`playlist-author-${muzic.author_id}`,
).innerHTML += `
${muzic.name}
`;
});
};
api.getAuthors(settingsAuthors, (authors) => {
document.getElementById("app-window").innerHTML = "";
authors.items
.sort((a, b) => a.date - b.date)
.forEach((author) => {
document.getElementById(
"app-window",
).innerHTML += `
${author.name}
Произведения
Показать/Скрыть
`;
const addMuzicButtonFunct = () =>
setTimeout(
() =>
(document.getElementById(
`add-muzic-button-${author.id}`,
).onclick = async () => {
// console.log('>>', author.id);
document.getElementById(
`add-muzic-${author.id}`,
).innerHTML = `
`;
setTimeout(() => {
document.getElementById(
`cancel-add-muzic-${author.id}`,
).onclick = function () {
document.getElementById(
`add-muzic-${author.id}`,
).innerHTML = ``;
addMuzicButtonFunct();
};
document.getElementById(`add-muzic-act-${author.id}`).onclick =
function () {
if (
document.getElementById(`muzic-name-input-${author.id}`)
.value.length >= 2
) {
api.createMuzic(
author.id,
document.getElementById(
`muzic-name-input-${author.id}`,
).value,
document.getElementById(`attach-file-${author.id}`)
.filedata,
() => {
showAudio(settingsAuthors, settingsAudio);
},
);
document.getElementById(
`add-muzic-${author.id}`,
).innerHTML = "Добавление файла...";
}
};
document.getElementById(`attach-file-${author.id}`).onclick =
function () {
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(
`attach-file-${author.id}`,
).filedata = arrayBufferToBase64(reader.result);
document.getElementById(
`attach-file-${author.id}`,
).innerText = "Загружен файл: " + file.name;
}
// console.log(arrayBufferToBase64(reader.result));
};
};
};
}, 0);
}),
0,
);
addMuzicButtonFunct();
});
api.getMuzic(settingsAudio, step2);
document.getElementById("app-window").innerHTML +=
'';
});
}
setTimeout(() => showAudio({}, {}), 0);
document.getElementById("search-input").onchange = function () {
console.log(this.value);
setTimeout(
() =>
showAudio(
{},
{
q: this.value,
},
),
0,
);
};