html = '<div class="input-group mb-3">';
html += '<input type="text" class="form-control" name="Page[Image]" id="Projet_recaptchaConfigFile" data-bs-toggle="modal" data-bs-target="#modal-gallery">';
// html += '<input type="text" id="Projet_recaptchaConfigFile" name="Page[image]" data-ea-align="left" class="form-control">';
html += '<label class="input-group-text" for="Projet_recaptchaConfigFile">Upload</label>';
html += '</div>';
//$("#Projet_recaptchaConfigFile").parent().html(html);
var indexOfPoint,
indexOf,
element,
dropArea,
dragText,
button,
input,
nfile,
fileType,
validExtensions,
fileReader,
fileURL,
mvalidExtension;
$("#Projet_recaptchaConfigFile").attr('data-bs-toggle', 'modal');
$("#Projet_recaptchaConfigFile").attr('data-bs-target', '#modal-gallery');
$("#Projet_recaptchaConfigFile").prop('readonly', true);
$("#Projet_recaptchaConfigFile").attr('onclick', 'getdirfiles(document.getElementById("Projet_recaptchaConfigFile").value, "nnvalid")');
$("<div class='input-group-prepend' style='margin-bottom: -3%; margin-left: 100%;'><i class='fa fa-trash-o mx-2' style='cursor: pointer;' onclick='trashgall()'></i></div>").insertBefore("#Projet_recaptchaConfigFile");
$('#Projet_recaptchaConfigFile').keypress(function (e) {
return false;
});
// onclick="sendCode('', 'text-')"
function getdirfiles(dirname, valid) {
selected = '';
indexOfPoint = dirname.lastIndexOf(".");
if (indexOfPoint >= 0) {
const substring = dirname.slice(indexOfPoint, dirname.length);
if (dirname.includes(substring)) {
indexOf = dirname.lastIndexOf("/");
selected = dirname.slice(indexOf, dirname.length);
dirname = dirname.replace(selected, '') + '/';
// "data-123".replace(/data-/g,'');
}
}
if ('{{entity.instance.switchEnLigne}}') {
$.ajax({
url: "{{ path('get_dirs_content_projet') }}",
type: 'POST',
timeout: 15000,
data: {
'dirname': dirname,
'selected': selected,
'validinput': valid,
'imageFtpServer': '{{entity.instance.imageFtpServerEnligne}}',
'imageFtpLogin': '{{entity.instance.imageFtpLoginEnligne}}',
'imageFtpMdp': '{{entity.instance.imageFtpMdpEnligne}}',
'imageHttpServer': '{{entity.instance.imageHttpServerEnligne}}',
},
dataType: 'json',
success: function (data) {
$('#valdircontent').html(data.content);
// dirPathNiveauIndicator
$('#dirPathNiveauIndicator').html(data.niveauindicator);
}
});
} else {
$.ajax({
url: "{{ path('get_dirs_content_projet') }}",
type: 'POST',
timeout: 15000,
data: {
'dirname': dirname,
'selected': selected,
'validinput': valid,
'imageFtpServer': '{{entity.instance.imageFtpServer}}',
'imageFtpLogin': '{{entity.instance.imageFtpLogin}}',
'imageFtpMdp': '{{entity.instance.imageFtpMdp}}',
'imageHttpServer': '{{entity.instance.imageHttpServer}}',
},
dataType: 'json',
success: function (data) {
$('#valdircontent').html(data.content);
// dirPathNiveauIndicator
$('#dirPathNiveauIndicator').html(data.niveauindicator);
}
});
}
}
function selectedimage(obj) {
element = $(obj);
$('.selectedFtpImg').css("border", "0px");
$('.selectedFtpImg').css("border-color", "");
$('.selectedFtpImg').css("background-color", "white");
element.css("border", "1px solid #5656b5;");
element.css("border-color", "#5656b5");
element.css("background-color", "#5656b5");
$('#Projet_recaptchaConfigFile').val(element.attr('data-dir'));
rescheckext = checkcurrentExtProjet($('#Projet_recaptchaConfigFile').val());
if (rescheckext == 'file') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="images/fileicon.webp" class="img-fluid mt-2" style="max-height: 100px;">';
} else if (rescheckext == 'json') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="images/jsonfileicon.png" class="img-fluid mt-2" style="max-height: 100px;">';
} else {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="' + element.attr('data-racine') + $('#Projet_recaptchaConfigFile').val() + '" class="img-fluid w-100 mt-2" style="max-height: 100px;">';
}
$("#imagepreviewtoremove").remove();
$(".setrecaptchafile").append(setrecaptchafile);
}
// clickedImg
// image preview
const imgphtml = $(".setrecaptchafile");
rescheckext = checkcurrentExtProjet($('#Projet_recaptchaConfigFile').val());
if (rescheckext == 'file') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="images/fileicon.webp" class="img-fluid mt-2" style="max-height: 100px;">';
} else if (rescheckext == 'json') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="images/jsonfileicon.png" class="img-fluid mt-2" style="max-height: 100px;">';
} else {
if ('{{entity.instance.switchEnLigne}}') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="{{entity.instance.imageHttpServerEnligne}}' + $('#Projet_recaptchaConfigFile').val() + '" class="img-fluid w-100 mt-2" style="max-height: 100px;">';
} else {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="{{entity.instance.imageHttpServer}}' + $('#Projet_recaptchaConfigFile').val() + '" class="img-fluid w-100 mt-2" style="max-height: 100px;">';
}
}
if ($('#Projet_recaptchaConfigFile').val() != '' && $('#Projet_recaptchaConfigFile').val() != undefined) {
imgphtml.append(setrecaptchafile);
}
$(".setrecaptchafile").change(function () {
$("#imagepreviewtoremove").remove();
});
// reset text in image
function trashgall() {
$('#Projet_recaptchaConfigFile').val('');
$("#imagepreviewtoremove").remove();
}
// umpload image script
submit = false;
// selecting all required elements
dropArea = document.querySelector(".drag-area");
dragText = dropArea.querySelector("header");
button = dropArea.querySelector("button");
input = dropArea.querySelector("input");
nfile; // this is a global variable and we'll use it inside multiple functions
button.onclick = () => {
input.click(); // if user click on the button then the input also clicked
};
input.addEventListener("change", function () { // getting user select file and [0] this means if user select multiple files then we'll select only the first one
nfile = this.files[0];
dropArea.classList.add("active");
//file.forEach(showFile);
showFile(); // calling function
});
// If user Drag File Over DropArea
dropArea.addEventListener("dragover", (event) => {
event.preventDefault(); // preventing from default behaviour
dropArea.classList.add("active");
dragText.textContent = "Libérer pour télécharger le fichier";
});
// If user leave dragged File from DropArea
dropArea.addEventListener("dragleave", () => {
dropArea.classList.remove("active");
dragText.textContent = "Drag & Drop pour télécharger le fichier";
});
// If user drop File on DropArea
dropArea.addEventListener("drop", (event) => {
event.preventDefault();
// preventing from default behaviour
// getting user select file and [0] this means if user select multiple files then we'll select only the first one
nfile = event.dataTransfer.files[0];
input.file = event.dataTransfer.files[0];
//file.forEach(showFile);
showFile(); // calling function
});
function showFile() {
fileType = nfile.type; //getting selected file type
validExtensions = ["application/json"]; //adding some valid image extensions in array
// alert(fileType);
if (validExtensions.includes(fileType)) { //if user selected file is an image file
fileReader = new FileReader(); //creating new FileReader object
fileReader.onload = () => {
fileURL = fileReader.result; //passing user file source in fileURL variable
// UNCOMMENT THIS BELOW LINE. I GOT AN ERROR WHILE UPLOADING THIS POST SO I COMMENTED IT
// imgTag = `<img src="${fileURL}" alt="image">`; //creating an img tag and passing user selected file source inside src attribute
// dropArea.innerHTML = imgTag; //adding that created img tag inside dropArea container
imgTag = `Fichier déposé avec succès <br>`;
filename = '<img src="' + fileURL + '" data-name="' + nfile.name + '" id="jsonToUpload" class="h-100 d-none">';
filename += '<img src="images/jsonfileicon.png" data-name="' + nfile.name + '" id="imageToUpload" class="h-100">';
$("#uploadedimgdisplay").html(filename);
$("#dropImageArea").removeClass("d-none");
$("#dragArea").addClass("d-none");
}
fileReader.readAsDataURL(nfile);
} else {
/* alert("This is not an Image File!");
dropArea.classList.remove("active");
dragText.textContent = "Drag & Drop to Upload File"; */
//alert("Ceci n'est pas un Image!");
dropArea.classList.remove("active");
dragText.textContent = "Drag & Drop pour télécharger le fichier";
submit = false;
}
}
function mySubmitFunctionbtn(e) {
submit = true;
document.getElementById("mform_id").submit();
}
function mySubmitFunction(e) {
if (!submit) {
e.preventDefault();
}
}
function setuploadimgvalue() {
/* console.log($("#imageToUpload").attr('src'));
console.log($("#imageToUpload").attr('data-name')); */
$('#Projet_recaptchaConfigFile').val('/upload/' + $("#imageToUpload").attr('data-name'));
rescheckext = checkcurrentExtProjet($("#imageToUpload").attr('src'));
if (rescheckext == 'file') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="images/fileicon.webp" class="img-fluid mt-2" style="max-height: 100px;">';
} else if (rescheckext == 'json') {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="images/jsonfileicon.png" class="img-fluid mt-2" style="max-height: 100px;">';
} else {
var setrecaptchafile = '<img id="imagepreviewtoremove" src="' + $("#imageToUpload").attr('src') + '" class="img-fluid mt-2" style="max-height: 100px;">';
}
$("#imagepreviewtoremove").remove();
$(".setrecaptchafile").append(setrecaptchafile);
if ('{{entity.instance.switchEnLigne}}') {
$.ajax({
url: "{{ path('get_image_projet_upload_json') }}",
type: 'POST',
timeout: 15000,
data: {
'file': $("#jsonToUpload").attr('src'),
'name': $("#jsonToUpload").attr('data-name'),
'imageFtpServer': '{{entity.instance.imageFtpServerEnligne}}',
'imageFtpLogin': '{{entity.instance.imageFtpLoginEnligne}}',
'imageFtpMdp': '{{entity.instance.imageFtpMdpEnligne}}',
'imageHttpServer': '{{entity.instance.imageHttpServerEnligne}}',
},
dataType: 'json',
success: function (data) {
// console.log(data.content);
// console.log();
// console.log(data.msg);
}
});
} else {
$.ajax({
url: "{{ path('get_image_projet_upload_json') }}",
type: 'POST',
timeout: 15000,
data: {
'file': $("#jsonToUpload").attr('src'),
'name': $("#jsonToUpload").attr('data-name'),
'imageFtpServer': '{{entity.instance.imageFtpServer}}',
'imageFtpLogin': '{{entity.instance.imageFtpLogin}}',
'imageFtpMdp': '{{entity.instance.imageFtpMdp}}',
'imageHttpServer': '{{entity.instance.imageHttpServer}}',
},
dataType: 'json',
success: function (data) {
// console.log(data.content);
// console.log();
// console.log(data.msg);
}
});
}
}
function resetuploadvalues() {
$("#dropImageArea").addClass("d-none");
$("#dragArea").removeClass("d-none");
$("#imageToUpload").attr("src", "");
}
function getFileExtension(filename) {
var ext = /^.+\.([^.]+)$/.exec(filename);
return ext == null ? "" : ext[1];
}
function checkImageGalleyExt() {
mvalidExtension = ["jpg",
"jpeg", "bmp",
"gif", "png",
"apng", "webp",
"ico", "icon", "cur",
"jfif", "pjpeg",
"pjp", "svg"];
$('[id=imagegellery]').each(function () {
// console.log($(this).attr("src"));
// console.log(mvalidExtension.includes(getFileExtension($(this).attr("src"))));
// console.log(getFileExtension($(this).attr("src")));
if (getFileExtension($(this).attr("src")) == 'json') {
$(this).attr("src", "images/jsonfileicon.png");
$(this).css("width", "80px");
}
if (!mvalidExtension.includes(getFileExtension($(this).attr("src")))) {
$(this).attr("src", "images/fileicon.webp");
$(this).css("width", "80px");
}
});
}
function checkcurrentExtProjet(idtocheck) {
mvalidExtension = ["jpg",
"jpeg", "bmp",
"gif", "png",
"apng", "webp",
"ico", "icon", "cur",
"jfif", "pjpeg",
"pjp", "svg"];
if (getFileExtension(idtocheck) == 'json') {
return 'json';
} else if (mvalidExtension.includes(getFileExtension(idtocheck))) {
return 'image';
} else {
return 'file';
}
}