-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-preview.min.js
1 lines (1 loc) · 1.08 KB
/
upload-preview.min.js
1
(function(a){function c(e,d){for(var f in d){if(d.hasOwnProperty(f)){e[f]=d[f]}}return e}function b(){function d(e){e=e||{};this.input=null;this.preview=null;this.defaultOptions={inputSelector:"",previewSelector:"",imgClass:""};this.options=c(this.defaultOptions,e);this.init=function(){if(a.File&&a.FileList&&a.FileReader){if(this.options.inputSelector&&this.options.previewSelector){this.input=document.querySelector(this.options.inputSelector);this.preview=document.querySelector(this.options.previewSelector);var f=this;this.input.addEventListener("change",function(){if(f.input.files.length){var i=f.input.files[0];var h=document.createElement("img");var g=new FileReader();g.addEventListener("load",function(j){h.src=j.target.result;while(f.preview.hasChildNodes()){f.preview.removeChild(f.preview.lastChild)}if(f.options.imgClass){h.classList.add(f.options.imgClass)}f.preview.appendChild(h)});g.readAsDataURL(i)}})}}else{console.log("You need a browser with file reader support, to use this form properly.")}};this.init()}return d}if(typeof UploadPreview==="undefined"){a.UploadPreview=b()}})(window);