-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-input.min.js
1 lines (1 loc) · 1.15 KB
/
file-input.min.js
1
function fileInput(e,t){0===arguments.length?(t={},e='input[type="file"]'):1!==arguments.length||e instanceof HTMLElement||'string'==typeof e||null===e||(t=e,e='input[type="file"]');const n=(t=t||{}).buttonText||'Browse…',l=t.buttonClasses||[],i=t.noFileText||'No file selected.',s=t.multiFilesText||'{num} files selected.',o=t.noFilesText||'No files selected.',a=t.fileNameClasses||[],c=e instanceof HTMLElement?[e]:document.querySelectorAll(e);for(let e=0;e<c.length;e++){const t=c[e];if('input'!==t.tagName.toLowerCase()||'file'!==t.type.toLowerCase()||'hidden'===t.style.visibility)continue;t.style.position='absolute',t.style.visibility='hidden';const p=document.createElement('label');p.className=t.className,t.removeAttribute('class');const u=document.createElement('a');u.classList.add(...l),u.innerHTML=n;const d=document.createElement('span');d.classList.add(...a),function(e,t){(t.onchange=function(){const n=t.hasAttribute('multiple'),l=t.files.length;if(n&&l>1)e.innerHTML=s.replace('{num}',''+l);else{const l=(t.value||'').split(/[\\/]/).pop();e.innerHTML=l||(n?o:i)}})()}(d,t),t.parentNode.replaceChild(p,t),p.appendChild(t),p.appendChild(u),p.appendChild(d)}}