-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickfile.min.js
8 lines (8 loc) · 3.98 KB
/
pickfile.min.js
1
2
3
4
5
6
7
8
(function(d){"undefined"!==typeof module&&"undefined"!==typeof module.exports&&"undefined"!==typeof require?module.exports=d():window.PickFile=d()})(function(){function d(a,b){b=b||{};this.options=a;for(var c in b)this.options[c]=b[c];"string"===typeof this.options.el?this.el=document.querySelector(this.options.el):this.options.el&&"innerHTML"in this.options.el&&(this.el=this.options.el);this.el||(this.el=document.createElement("div"));this.el.innerHTML=this.template;this.els={};for(c=0;c<this.elements.length;c+=
1)this.els[this.elements[c]]=this.el.querySelector(".pickfile-"+this.elements[c])}var e=function(a){this.init({datatype:"Text",onLoad:function(){},onError:function(){}},a);this.els.fileselect.addEventListener("change",this.onFileSelect.bind(this),!1);this.els.loadbtn.addEventListener("click",this.onLoadbtnClick.bind(this),!1);this.xhr=new XMLHttpRequest;this.xhr.upload?(this.els.filedrop.addEventListener("dragover",this.onFiledropHover.bind(this),!1),this.els.filedrop.addEventListener("dragleave",
this.onFiledropHover.bind(this),!1),this.els.filedrop.addEventListener("drop",this.onFileSelect.bind(this),!1)):this.els.filedrop.style.display="none"};e.prototype={elements:["fileselect","filedrop","loadbtn","url"],template:'<div class="pickfile"><form><input class="pickfile-url" type="text" placeholder="type or paste remote URL here"><button class="pickfile-loadbtn btn btn-primary">Load</button><div class="pickfile-filedrop">or Drop file here</div><div class="pickfile-fileinputs btn btn-primary"><input class="pickfile-fileselect" type="file">or Choose file</div></form></div>',
onFiledropHover:function(a){a.stopPropagation();a.preventDefault();a.target.className=("dragover"==a.type?"hover ":"")+a.target.className.replace("hover ","")},onFileSelect:function(a){this.onFiledropHover(a);a=a.target.files||a.dataTransfer.files;for(var b=0,c;c=a[b];b++)this.parseFile(c)},onLoadbtnClick:function(a){a.preventDefault();var b=new XMLHttpRequest,c=this.els.url.value;c&&(b.onreadystatechange=function(a){if(4==b.readyState)if(200==b.status)b.url=c,this.options.onLoad(b.response,b);else this.options.onError(b.statusText,
b)}.bind(this),b.open("GET",this.els.url.value,!0),b.responseType="BinaryString"===this.options.datatype?"blob":this.options.datatype.toLowerCase(),b.send())},init:d,parseFile:function(a){var b="readAs"+this.options.datatype,c=new FileReader;b in c&&(c.onload=function(b){this.options.onLoad(b.target.result,a)}.bind(this),c[b](a))}};e.Downloader=function(a){this.init({filename:"myfile.txt",placeholder:"filename.txt",type:"text/plain",content:""},a);this.els.filename.placeholder=this.options.placeholder;
this.els.filename.value=this.options.filename||"";this.els.savebtn.addEventListener("click",this.onDownload.bind(this),!1)};e.Downloader.prototype={elements:["filename","savebtn","download"],template:'<div class="pickfile"><input type="text" class="pickfile-filename"><button class="btn btn-primary pickfile-savebtn">Save file</button><output class="pickfile-download"></output></div>',init:d,onDownload:function(){this.options.getFile?this.options.getFile(this.downloadFile.bind(this)):this.options.getContent?
this.options.getContent(function(a){this.downloadFile({content:a})}.bind(this)):this.downloadFile({})},downloadFile:function(a){a.type=a.type||this.options.type;a.filename=a.filename||this.els.filename.value||this.options.filename;a.content=a.content||this.options.content;window.URL=window.webkitURL||window.URL;var b=this.els.download.querySelector("a");b&&(window.URL.revokeObjectURL(b.href),this.els.download.innerHTML="");var b=new Blob([a.content],{type:a.type}),c=document.createElement("a");c.download=
a.filename;c.href=window.URL.createObjectURL(b);c.textContent="Download";c.dataset.downloadurl=[a.type,c.download,c.href].join(":");this.els.download.appendChild(c);var d=this;c.onclick=function(a){if("disabled"in this.dataset)return!1;d.cleanUp(this)}},cleanUp:function(a){a.textContent="Downloaded";a.dataset.disabled=!0;setTimeout(function(){window.URL.revokeObjectURL(a.href)},1500)}};return e});