jetpack
/
jetpack_vendor
/
automattic
/
jetpack-forms
/
dist
/
modules
/
file-field
Last commit date
view.asset.php
2 weeks ago
view.js
2 weeks ago
view.js
1 lines
| 1 | import*as e from"@wordpress/interactivity";var t={2833(t,i,n){t.exports=(e=>{var t={};return n.d(t,e),t})({getConfig:()=>e.getConfig,getContext:()=>e.getContext,getElement:()=>e.getElement,store:()=>e.store,withScope:()=>e.withScope})}},i={};function n(e){var o=i[e];if(void 0!==o)return o.exports;var r=i[e]={exports:{}};return t[e](r,r.exports,n),r.exports}n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var o=n(2833);const r="jetpack/field-file";let s=null,a=null;const l=(0,o.store)("jetpack/form"),d=async()=>{if(s&&a&&Date.now()<a)return s;const{token:e,expiresAt:t}=await c();return s=e,a=1e3*t,s},c=async()=>{const{endpoint:e}=(0,o.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 n=await i.json();return{token:n.token,expiresAt:n.expiration}}catch(e){if(e)return t}return t},p=(e,t=2)=>{const i=(0,o.getConfig)(r);if(0===e)return i.i18n.zeroBytes;const n=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(n));return`${new Intl.NumberFormat(i.i18n.locale,{minimumFractionDigits:n,maximumFractionDigits:n}).format(l)} ${s[a]}`},f=e=>{const t=(0,o.getConfig)(r),i=(0,o.getContext)();let n=null;e.size>t.maxUploadSize&&(n=t.i18n.fileTooLarge),i.allowedMimeTypes.includes(e.type)||(n=t.i18n.invalidType);const s=i.files.filter(e=>!e.error);i.maxFiles<s.length+1&&(n=t.i18n.maxFiles);const a=performance.now()+"-"+Math.random(),d=["image/gif","image/jpg","image/png","image/jpeg"].includes(e.type)&&URL.createObjectURL,c=d?"url("+URL.createObjectURL(e)+")":(e=>{const t=(0,o.getConfig)(r),i=e.type.split("/")[0],n={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+n+".svg)"})(e);i.files.push({name:e.name,formattedSize:p(e.size,2),hasIcon:!d,isUploaded:!1,hasError:!!n,id:a,url:d?c:null,mask:d?null:c,error:n}),l.actions.updateFieldValue(i.fieldId,i.files),!n&&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,o.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,o.getContext)(),n=i.files.findIndex(e=>e.id===t);i.files[n]=Object.assign(i.files[n],e),l.actions.updateFieldValue(i.fieldId,i.files)},{state:y,actions:h}=(0,o.store)(r,{state:{get isInlineForm(){const{ref:e}=(0,o.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,o.getContext)().files.length>0},get hasMaxFiles(){const e=(0,o.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,o.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(),l.actions.trackFirstInteraction(),e.dataTransfer)for(const t of Array.from(e.dataTransfer.items)){if(t.webkitGetAsEntry()?.isDirectory)return;f(t.getAsFile())}(0,o.getContext)().isDropping=!1},dragOver:e=>{(0,o.getContext)().isDropping=!0,e.preventDefault()},dragLeave:()=>{(0,o.getContext)().isDropping=!1},uploadFile:function*(e,t){const{endpoint:i,i18n:n}=(0,o.getConfig)(r),s=yield d();if(!s)return void x({error:n.uploadFailed,hasError:!0},t);const a=new XMLHttpRequest,l=new FormData,c=new AbortController;u.set(t,c),a.open("POST",i,!0),a.upload.addEventListener("progress",(0,o.withScope)(g.bind(this,t))),a.addEventListener("readystatechange",(0,o.withScope)(m.bind(this,t))),c.signal.addEventListener("abort",()=>{a.abort()}),l.append("file",e),l.append("token",s),a.send(l)},resetFiles:()=>{(0,o.getContext)().files=[]},removeFile:function*(e){e.preventDefault();const t=(0,o.getContext)(),i=e.target.dataset.id;if(u.has(i)){u.get(i).abort(),u.delete(i)}const n=t.files.find(e=>e.id===i);if(n&&n.url){const e=n.url.substring(4,n.url.length-1);URL.revokeObjectURL(e)}if(n&&n.file_id){const{endpoint:e}=(0,o.getConfig)(r),t=yield d();if(t){const i=new FormData;i.append("token",t),i.append("file_id",n.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,o.getElement)();return setTimeout(()=>{e.focus({focusVisible:!0})},100),(0,o.withScope)(function(){const t=e.closest(".jetpack-form-file-field__container").querySelector(".jetpack-form-file-field__dropzone-inner");setTimeout(()=>{t.focus({focusVisible:!0})},100)})}}}); |