core/api/modules/foto_show_album/mod.js

204 lines
5.6 KiB
JavaScript
Executable File

/* загрузчик изображений */
// ************************ Drag and drop ***************** //
let dropArea = document.getElementById("drop-area")
// Prevent default drag behaviors
;
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false)
document.body.addEventListener(eventName, preventDefaults, false)
})
// Highlight drop area when item is dragged over it
;
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false)
})
;
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false)
})
// Handle dropped files
dropArea.addEventListener('drop', handleDrop, false)
function preventDefaults(e) {
e.preventDefault()
e.stopPropagation()
}
function highlight(e) {
dropArea.classList.add('highlight')
}
function unhighlight(e) {
dropArea.classList.remove('active')
}
function handleDrop(e) {
var dt = e.dataTransfer
var files = dt.files
handleFiles(files)
}
let uploadProgress = []
let progressBar = document.getElementById('progress-bar')
function initializeProgress(numFiles) {
progressBar.value = 0
uploadProgress = []
for (let i = numFiles; i > 0; i--) {
uploadProgress.push(0)
}
}
function updateProgress(fileNumber, percent) {
uploadProgress[fileNumber] = percent
let total = uploadProgress.reduce((tot, curr) => tot + curr, 0) / uploadProgress.length
console.debug('update', fileNumber, percent, total)
progressBar.value = total
}
function handleFiles(files) {
files = [...files]
initializeProgress(files.length)
files.forEach(uploadFile)
files.forEach(previewFile)
}
function previewFile(file) {
let reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = function() {
let img = document.createElement('img')
img.src = reader.result
document.getElementById('gallery').appendChild(img)
}
}
function uploadFile(file, i) {
var url = '/act/foto_show_album'
var xhr = new XMLHttpRequest()
var formData = new FormData()
xhr.open('POST', url, true)
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
// Update progress (can be used to show progress indicator)
xhr.upload.addEventListener("progress", function(e) {
updateProgress(i, (e.loaded * 100.0 / e.total) || 100)
})
xhr.addEventListener('readystatechange', function(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
updateProgress(i, 100) // <- Add this
} else if (xhr.readyState == 4 && xhr.status != 200) {
// Error. Inform the user
}
})
formData.append('upload_preset', 'ujpu6gyk')
formData.append('act', 'foto_upload')
formData.append('file', file)
xhr.send(formData)
}
/**************** */
$("#btn-balans-plus").click(function(event) {
$('#modal-balans-plus').modal('show');
return false;
})
//кнопка удаления в модальном окошке
$("#input-summ").keyup(function(event) {
var rnd = getRandomInt(100000, 999999);
$("#div-img-qr").load("/api/modules/lk/load.php?summ=" + $(this).val());
/*$.ajax({
type: "POST",
url: '/act/lk',
data: "act=creat_qr&id=" + $(this).val(),
success: function(data) {
$("#img-qr").attr('src', data + '?id=' + rnd);
}
});
*/
})
//Показываем окошко удаления обложки
$("#btn-open-modal-del-img").click(function(event) {
$('#exampleModal2').modal('show');
return false;
})
/*************************** 02.05.2021 */
$(".input-update").keyup(function(event) {
var t = $(this).data('t');
var t2 = $(this).data('t2');
var id = $(this).data('id');
var txt = $(this).val();
$.ajax({
type: "POST",
url: '/act/foto_show_album',
data: "act=update&id=" + id + "&t=" + t + "&txt=" + txt + "&t2=" + t2
});
return false;
})
/*************************** 02.05.2021 */
$(".audit").click(function(event) {
if ($(this).is(':checked')) {
var txt = 1
} else {
var txt = 0;
}
var t = $(this).data('t');
var id = $(this).data('id');
$.ajax({
type: "POST",
url: '/act/foto_show_album',
data: "act=update&id=" + id + "&t=" + t + "&txt=" + txt
});
})
/*************************** 02.05.2021 */
$(".del-foto").click(function(event) {
var id = $(this).data('id');
$.ajax({
type: "POST",
url: '/act/foto_show_album',
data: "act=del_foto&id=" + id
});
$("#tr_" + id).hide('2000');
})
/*************************** 05.05.2021 */
$("#change_cena").keyup(function(event) {
var id = $(this).data('id');
var cena = $(this).val();
$.ajax({
type: "POST",
url: '/act/foto_show_album',
data: "act=change_cena&id=" + id + "&cena=" + cena
});
})
/* 06.05.2021 удаление альбоама */
$("button.del_album").click(function(event) {
$.ajax({
type: "POST",
url: '/act/foto_edit_album',
data: "act=del_album"
})
$("#spinner").removeClass("hidden");
$("#spinner").attr('src', '/img/loading-45.gif');
var delay = 5000;
setTimeout("document.location.href='/foto_my_albums/'", delay);
})