jetpack
/
jetpack_vendor
/
automattic
/
jetpack-forms
/
dist
/
modules
/
file-field
Last commit date
view.asset.php
6 months ago
view.js
6 months ago
view.js
1 lines
| 1 | import*as e from"@wordpress/interactivity";var t={2833(t,i,o){t.exports=(e=>{var t={};return o.d(t,e),t})({getConfig:()=>e.getConfig,getContext:()=>e.getContext,getElement:()=>e.getElement,store:()=>e.store,withScope:()=>e.withScope})}},i={};function o(e){var n=i[e];if(void 0!==n)return n.exports;var r=i[e]={exports:{}};return t[e](r,r.exports,o),r.exports}o.d=(e,t)=>{for(var i in t)o.o(t,i)&&!o.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n=o(2833);const r="jetpack/field-file";let s=null,a=null;const l=(0,n.store)("jetpack/form"),d=async()=>{if(s&&a&&Date.now()<a)return s;const{token:e,expiresAt:t}=await p();return s=e,a=1e3*t,s},p=async()=>{const{endpoint:e}=(0,n.getConfig)(r),t={token:null,expiresAt:0};try{const i=await fetch(`${e}/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({context:"file-upload"})});if(!i.ok)return t;const o=await i.json();return{token:o.token,expiresAt:o.expiration}}catch(e){if(e)return t}return t},c=(e,t=2)=>{const i=(0,n.getConfig)(r);if(0===e)return i.i18n.zeroBytes;const o=t<0?0:t,s=i.i18n.fileSizeUnits||["Bytes","KB","MB","GB","TB"],a=Math.floor(Math.log(e)/Math.log(1024)),l=parseFloat((e/Math.pow(1024,a)).toFixed(o));return`${new Intl.NumberFormat(i.i18n.locale,{minimumFractionDigits:o,maximumFractionDigits:o}).format(l)} ${s[a]}`},f=e=>{const t=(0,n.getConfig)(r),i=(0,n.getContext)();let o=null;e.size>t.maxUploadSize&&(o=t.i18n.fileTooLarge),i.allowedMimeTypes.includes(e.type)||(o=t.i18n.invalidType);const s=i.files.filter(e=>!e.error);i.maxFiles<s.length+1&&(o=t.i18n.maxFiles);const a=performance.now()+"-"+Math.random(),d=["image/gif","image/jpg","image/png","image/jpeg"].includes(e.type)&&URL.createObjectURL,p=d?"url("+URL.createObjectURL(e)+")":(e=>{const t=(0,n.getConfig)(r),i=e.type.split("/")[0],o={pdf:"pdf",doc:"doc",docx:"doc",txt:"txt",ppt:"ppt",pptx:"ppt",xls:"xls",xlsx:"xls",csv:"xls",zip:"zip",sql:"sql",cal:"cal"}[e.name.split(".").pop().toLowerCase()]||{image:"png",video:"mp4",audio:"mp3",document:"pdf",application:"txt"}[i]||"txt";return"url("+t.iconsPath+o+".svg)"})(e);i.files.push({name:e.name,formattedSize:c(e.size,2),hasIcon:!d,isUploaded:!1,hasError:!!o,id:a,url:d?p:null,mask:d?null:p,error:o}),l.actions.updateFieldValue(i.fieldId,i.files),!o&&h.uploadFile(e,a)},u=new Map,g=(e,t)=>{const i=t.loaded/t.total*100;x({progress:Math.min(i,97)},e)},m=(e,t)=>{const i=t.target;if(4===i.readyState){if(200!==i.status){const t=(0,n.getConfig)(r);return void x({error:t.i18n.uploadFailed,hasError:!0},e)}{const t=JSON.parse(i.responseText);if(t.success)return void x({file_id:t.data.file_id,isUploaded:!0,name:t.data.name,type:t.data.type,size:t.data.size,fileJson:JSON.stringify({file_id:t.data.file_id,name:t.data.name,size:t.data.size,type:t.data.type})},e)}if(i.responseText){const t=JSON.parse(i.responseText);x({error:t.message,hasError:!0},e)}}},x=(e,t)=>{const i=(0,n.getContext)(),o=i.files.findIndex(e=>e.id===t);i.files[o]=Object.assign(i.files[o],e),l.actions.updateFieldValue(i.fieldId,i.files)},{state:y,actions:h}=(0,n.store)(r,{state:{get isInlineForm(){const{ref:e}=(0,n.getElement)(),t=e.closest(".wp-block-jetpack-contact-form");return t&&t.classList.contains("is-style-outlined")||t.classList.contains("is-style-animated")},get hasFiles(){return!!(0,n.getContext)().files.length>0},get hasMaxFiles(){const e=(0,n.getContext)();return e.maxFiles<=e.files.length}},actions:{handleKeyDown:e=>{13!==e.keyCode&&32!==e.keyCode||(e.preventDefault(),h.openFilePicker(e))},openFilePicker(){const{ref:e}=(0,n.getElement)(),t=e.parentNode.querySelector(".jetpack-form-file-field");t&&(t.value="",t.click())},fileAdded(e){Array.from(e.target.files).forEach(f)},fileDropped:e=>{if(e.preventDefault(),e.dataTransfer)for(const t of Array.from(e.dataTransfer.items)){if(t.webkitGetAsEntry()?.isDirectory)return;f(t.getAsFile())}(0,n.getContext)().isDropping=!1},dragOver:e=>{(0,n.getContext)().isDropping=!0,e.preventDefault()},dragLeave:()=>{(0,n.getContext)().isDropping=!1},uploadFile:function*(e,t){const{endpoint:i,i18n:o}=(0,n.getConfig)(r),s=yield d();if(!s)return void x({error:o.uploadFailed,hasError:!0},t);const a=new XMLHttpRequest,l=new FormData,p=new AbortController;u.set(t,p),a.open("POST",i,!0),a.upload.addEventListener("progress",(0,n.withScope)(g.bind(this,t))),a.addEventListener("readystatechange",(0,n.withScope)(m.bind(this,t))),p.signal.addEventListener("abort",()=>{a.abort()}),l.append("file",e),l.append("token",s),a.send(l)},resetFiles:()=>{(0,n.getContext)().files=[]},removeFile:function*(e){e.preventDefault();const t=(0,n.getContext)(),i=e.target.dataset.id;if(u.has(i)){u.get(i).abort(),u.delete(i)}const o=t.files.find(e=>e.id===i);if(o&&o.url){const e=o.url.substring(4,o.url.length-1);URL.revokeObjectURL(e)}if(o&&o.file_id){const{endpoint:e}=(0,n.getConfig)(r),t=yield d();if(t){const i=new FormData;i.append("token",t),i.append("file_id",o.file_id),fetch(`${e}/remove`,{method:"POST",body:i})}}t.files=t.files.filter(e=>e.id!==i),l.actions.updateFieldValue(t.fieldId,y.hasFiles?t.files:"")},removeFileKeydown:e=>{13!==e.keyCode&&32!==e.keyCode||(e.preventDefault(),h.removeFile(e))}},callbacks:{focusElement:function(){const{ref:e}=(0,n.getElement)();return setTimeout(()=>{e.focus({focusVisible:!0})},100),(0,n.withScope)(function(){const t=e.closest(".jetpack-form-file-field__container").querySelector(".jetpack-form-file-field__dropzone-inner");setTimeout(()=>{t.focus({focusVisible:!0})},100)})}}}); |