Files
MoneyFlix/public/assets/js/alpine/forms/form-file-upload.js
T
1sonhador 8542395fbe Add public campaign analytics functionality
- Add PublicCampaignsController for public campaign analytics
- Add public routes for /public/analytics/{slug} and /api/campaigns/{slug}
- Add public campaign analytics view with charts and KPIs
- Support for campaign sharing via custom slugs
- Real-time analytics with ApexCharts integration
2025-10-15 00:30:00 +01:00

1 line
4.1 KiB
JavaScript

export default()=>({uploadedFiles:[],isUploading:!1,uploadProgress:0,init(){this.setupDropzone(),this.setupFilePond(),this.setupCustomUploads()},setupDropzone(){const e=document.querySelector("#dropzone-preview-list");if(e&&"undefined"!=typeof Dropzone){e.id="";const o=e.parentNode.innerHTML;e.parentNode.removeChild(e),new Dropzone(".dropzone",{url:"https://httpbin.org/post",method:"post",previewTemplate:o,previewsContainer:"#dropzone-preview",maxFiles:10,maxFilesize:5,acceptedFiles:"image/*,.pdf,.doc,.docx",addRemoveLinks:!0,dictDefaultMessage:"Drop files here or click to upload",dictFileTooBig:"File is too big ({{filesize}}MB). Max filesize: {{maxFilesize}}MB.",dictInvalidFileType:"You can't upload files of this type.",dictResponseError:"Server responded with {{statusCode}} code.",dictCancelUpload:"Cancel upload",dictUploadCanceled:"Upload canceled.",dictCancelUploadConfirmation:"Are you sure you want to cancel this upload?",dictRemoveFile:"Remove file",dictMaxFilesExceeded:"You can not upload any more files.",init:function(){this.on("addedfile",e=>{console.log("File added:",e.name)}),this.on("success",(e,o)=>{console.log("File uploaded successfully:",e.name)}),this.on("error",(e,o)=>{console.error("Upload error:",o)})}})}},setupFilePond(){if("undefined"!=typeof FilePond){"undefined"!=typeof FilePondPluginFileEncode&&FilePond.registerPlugin(FilePondPluginFileEncode,FilePondPluginFileValidateSize,FilePondPluginImageExifOrientation,FilePondPluginImagePreview),document.querySelectorAll("input.filepond-input-multiple").forEach(e=>{FilePond.create(e,{server:{url:"https://httpbin.org/post",process:{method:"POST",withCredentials:!1,headers:{},timeout:7e3,onload:e=>JSON.parse(e).files.file,onerror:e=>e.data,ondata:e=>e}},maxFiles:10,maxFileSize:"5MB",acceptedFileTypes:["image/*",".pdf",".doc",".docx"],labelIdle:'Drag & Drop files or <span class="filepond--label-action">Browse</span>',labelFileProcessing:"Uploading",labelFileProcessingComplete:"Upload complete",labelTapToCancel:"Tap to cancel",labelTapToRetry:"Tap to retry",labelTapToUndo:"Tap to undo"})});const e=document.querySelector(".filepond-input-circle");e&&FilePond.create(e,{labelIdle:'Drag & Drop your picture or <span class="filepond--label-action">Browse</span>',imagePreviewHeight:170,imageCropAspectRatio:"1:1",imageResizeTargetWidth:200,imageResizeTargetHeight:200,stylePanelLayout:"compact circle",styleLoadIndicatorPosition:"center bottom",styleProgressIndicatorPosition:"right bottom",styleButtonRemoveItemPosition:"left bottom",styleButtonProcessItemPosition:"right bottom",server:{url:"https://httpbin.org/post",process:{method:"POST",withCredentials:!1,headers:{},timeout:7e3,onload:e=>JSON.parse(e).files.file}}})}},setupCustomUploads(){const e=document.querySelector("#profile-img-file-input");e&&e.addEventListener("change",e=>{this.handleProfileImageUpload(e)}),document.querySelectorAll(".custom-file-input").forEach(e=>{e.addEventListener("change",e=>{this.handleCustomFileUpload(e)})})},handleProfileImageUpload(e){const o=e.target.files[0],t=document.querySelector(".user-profile-image");if(o&&t){if(!o.type.startsWith("image/"))return void alert("Please select an image file.");if(o.size>5242880)return void alert("File size must be less than 5MB.");const e=new FileReader;e.onload=e=>{t.src=e.target.result,console.log("Profile image updated")},e.readAsDataURL(o)}},handleCustomFileUpload(e){const o=e.target.files[0],t=e.target.nextElementSibling;o?(t.textContent=o.name,this.uploadedFiles.push({name:o.name,size:o.size,type:o.type,file:o})):t.textContent="Choose file"},async uploadFiles(e,o="/api/upload"){this.isUploading=!0,this.uploadProgress=0;const t=new FormData;e.forEach((e,o)=>{t.append(`file_${o}`,e)});try{const e=await fetch(o,{method:"POST",body:t});if(e.ok){const o=await e.json();return console.log("Files uploaded successfully:",o),this.uploadedFiles=[],o}throw new Error("Upload failed")}catch(e){throw console.error("Upload error:",e),e}finally{this.isUploading=!1,this.uploadProgress=0}},removeFile(e){this.uploadedFiles.splice(e,1)},formatFileSize(e){if(0===e)return"0 Bytes";const o=Math.floor(Math.log(e)/Math.log(1024));return parseFloat((e/Math.pow(1024,o)).toFixed(2))+" "+["Bytes","KB","MB","GB"][o]}});