var NRInlineFileUploadSelector=function(){function e(){this.url_path="?option=com_ajax&format=raw&plugin=nrframework&path=plugins/system/nrframework/fields/&class=JFormFieldNRInlineFileUpload&file=nrinlinefileupload&task=Include",this.initEvents()}var t=e.prototype;return t.initEvents=function(){document.addEventListener("click",function(e){this.handleOpen(e),this.handleClearUploadedItem(e),this.handleRemoveUploadedItem(e)}.bind(this)),document.addEventListener("change",function(e){this.handleChange(e)}.bind(this))},t.handleOpen=function(e){var t=e.target.closest(".file-selector-opener");t&&(e.preventDefault(),t.nextElementSibling.click())},t.handleClearUploadedItem=function(e){var t=e.target.closest(".nr-inline-file-upload-item-clear");t&&(e.preventDefault(),t.closest(".error").querySelector('input[type="hidden"]').value="",t.closest(".error").classList.remove("visible"),this.showUploadArea(t.closest(".nr-inline-file-upload")))},t.handleRemoveUploadedItem=function(e){var t,n,r,i=e.target.closest(".nr-inline-file-upload-item-remove");i&&(e.preventDefault(),confirm(i.dataset.confirm))&&(t=this,n=e.target.closest(".nr-inline-file-upload"),this.showLoader(n),(r=e.target.closest(".uploaded-files")).innerHTML="",(e=new FormData).append("remove_file",i.nextElementSibling.value),e.append(Joomla.getOptions("csrf.token"),1),fetch(n.dataset.baseUrl+this.url_path+"&action=remove",{method:"post",body:e}).then(function(e){return e.json()}).then(function(e){e.error?(t.showUploadArea(n),t.showError(n,e.response)):(t.hideError(n),r.previousElementSibling.classList.remove("hidden")),t.hideLoader(n)}))},t.handleChange=function(e){var t,n,r=e.target.closest(".file-selector");r&&(t=this,n=r.closest(".nr-inline-file-upload"),this.hideError(n),this.hideUploadArea(n),this.hideSelectedFile(n),this.showLoader(n),(e=new FormData).append("file",r.files[0]),e.append("upload_folder",n.dataset.uploadFolder),e.append(Joomla.getOptions("csrf.token"),1),fetch(n.dataset.baseUrl+this.url_path,{method:"post",body:e}).then(function(e){return e.json()}).then(function(e){e.error?(t.showUploadArea(n),t.showError(n,e.response)):(t.hideError(n),t.replaceSelectedFile(n,e)),t.hideLoader(n),r.value=""}))},t.hideSelectedFile=function(e){e.querySelector(".uploaded-files").innerHTML=""},t.replaceSelectedFile=function(e,t){e.querySelector(".uploaded-files").innerHTML="";var n=document.querySelector("template.nr-inline-file-upload-item").content.cloneNode(!0),r=(n.querySelector(".file-name").innerHTML=atob(t.file_name),n.querySelector(".size").innerHTML=t.file_size,document.createElement("div")),n=(r.classList.add("nr-inline-file-upload-item"),r.append(n),document.createElement("input"));n.setAttribute("type","hidden"),n.setAttribute("name",e.dataset.name),n.setAttribute("value",atob(t.file)),r.appendChild(n),e.querySelector(".uploaded-files").append(r),this.hideUploadArea(e)},t.showError=function(e,t){e.querySelector(".error").innerHTML=t,e.querySelector(".error").classList.add("visible")},t.hideError=function(e){e.querySelector(".error").innerHTML="",e.querySelector(".error").classList.remove("visible")},t.showLoader=function(e){e.classList.add("loading")},t.hideLoader=function(e){e.classList.remove("loading")},t.hideUploadArea=function(e){e.querySelector(".upload-area").classList.add("hidden")},t.showUploadArea=function(e){e.querySelector(".upload-area").classList.remove("hidden")},e}();document.addEventListener("DOMContentLoaded",function(){new NRInlineFileUploadSelector});

