/** *@file upload.js *@version 20170907 ms *@type JS * * Komponente zum Hochladen einer oder mehrerer Dateien mit Fortschrittsanzeige. Setzt die Server-seitige Verwendung eines speziellen PHP-Skipts voraus. */ /** * @class KUpload * @constructor */ function KUpload() { this.fieldName="file"; this.running=false; this.uploadedTotalBytes=0; this.uploadFiles; this.countProgress=null; this.win=null; // define in WinOpen() as table this.winModal=null; //define this.inpUpload=null; // HTMLElement::input@type=file var d=document.createElement("div");with(d.style){position="absolute";width="0";height="0";overflow="hidden"};document.body.appendChild(d); this.inpUpload=document.createElement("input");this.inpUpload.type="file";d.appendChild(this.inpUpload); var m=this; m.inpUpload.onchange=function(){if(!m.running) m.UploadPrepare(m.inpUpload.files)}; } /** * @fn UploadShow * @memberof KUpload * @param {string} url * @param {boolean} multiple * @param {function} completedFunc * @param {function} checkFunc * @param {function} beforeFunc * @param {function} afterFunc * Public method. * Open browsers file dialog. */ KUpload.prototype.UploadShow=function(url,multiple,completedFunc,checkFunc,beforeFunc,afterFunc){ if(this.running) return; this.uploadCompleteFunc=completedFunc; this.checkFunc=checkFunc; this.beforeFunc=beforeFunc; this.afterFunc=afterFunc; this.uploadUrl=url; this.inpUpload.multiple=multiple?true:false; this.inpUpload.click(); }; /** * @fn Upload * @memberof KUpload * @param {string} url * @param {FileList} files * @param {function} completedFunc * @param {function} checkFunc * @param {function} beforeFunc * @param {function} afterFunc * Public method. * Begin file upload. */ KUpload.prototype.Upload=function(url,files,completedFunc,checkFunc,beforeFunc,afterFunc){ if(this.running) return; this.uploadUrl=url; this.uploadCompleteFunc=completedFunc; this.checkFunc=checkFunc; this.beforeFunc=beforeFunc; this.afterFunc=afterFunc; this.UploadPrepare(files); }; /** * @fn UploadPrepare * @memberof KUpload * @param {FileList} files * @call this.UploadInit() * @callby this.Upload(), Event::onchange of HTMLElement::input * Private method. */ KUpload.prototype.UploadPrepare=function(files){ var m=this,a=-1,upFiles=[]; var doFunc=function(){ a++; if(a==files.length){ if(upFiles.length) m.UploadInit(upFiles); }else{ if(files[a].size){ if(m.checkFunc){ m.checkFunc(files[a],function(file,formData){ if(file) upFiles.push({file:file,formData:formData?formData:{}}); doFunc(); }); }else{ upFiles.push({file:files[a],formData:{}}); doFunc(); } }else doFunc(); } }; doFunc(); }; /** * @fn UploadInit * @memberof KUpload * @param upFiles * @call this.UploadStart() * @referredby this.UploadPrepare() * Private method. */ KUpload.prototype.UploadInit=function(upFiles){ this.uploadFiles=upFiles; this.uploadedTotalBytes=0; this.uploadTotalBytes=0; for(var a=0;a0 && h>0){this.win.style.width=w+"px";this.win.style.height=h+"px";} this.win.style.left=(KUpload.WindowWidth()-this.win.offsetWidth)/2+"px"; this.win.style.top=(KUpload.WindowHeight()-this.win.offsetHeight)/2+"px"; }; /** * @fn WinClose * @memberof KUpload * Private method. */ KUpload.prototype.WinClose=function(){ if(this.win){document.body.removeChild(this.win);this.win=null;document.body.removeChild(this.winModal)} }; /** * @fn WindowWidth * @returns * Private method. */ KUpload.WindowWidth=function(){ return window.innerWidth && document.documentElement.clientWidth? Math.min(window.innerWidth, document.documentElement.clientWidth) : window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; }; /** * @fn WindowHeight * @returns * Private method. */ KUpload.WindowHeight=function(){ return window.innerHeight && document.documentElement.clientHeight? Math.min(window.innerHeight, document.documentElement.clientHeight) : window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; };