/** * @file mediagrid.js * @version 20170907 ms * @type JS * @use internet.js, menu.js, win.js, upload.js, imageslider.js * GUI-Komponenten zur Auflistung von Medien-Dateien */ /** * @class KMediaGrid * @constructor complex * @param {HTMLDivElement} o * @param {string:url} adapterUrl * @param {object} opts: {bool} showToolbar, {bool} allowSlideshow, {bool} upload, {bool} uploadMultiple, {string:language} lang, {bool} allowMultiProperties, {string:url} propertiesUrl, {bool} allowMultiEdit, {string:url} editUrl), {string:url} uploadUrl, {string:url} printoutUrl), {string} slideshowTitleKey * @param {KMediaGridBridge} bridge */ function KMediaGrid(o,adapterUrl,opts,bridge){ var m=this; m.res={ Parent:{de:"Aufwärts",en:"Parent"}, Upload:{de:"Hochladen",en:"Upload"}, Approve:{de:"Freigeben",en:"Approve"}, Unapprove:{de:"Sperren",en:"Set as unapproved"}, Download:{de:"Herunterladen",en:"Download"}, Copy:{de:"Kopieren",en:"Copy"}, Cut:{de:"Ausschneiden",en:"Cut"}, Paste:{de:"Einfügen",en:"Paste"}, Edit:{de:"Bearbeiten",en:"Edit"}, Properties:{de:"Eigenschaften",en:"Properties"}, MakeFolder:{de:"Ordner erstellen",en:"Make folder"}, Rename:{de:"Umbenennen",en:"Rename"}, Delete:{de:"Löschen",en:"Delete"}, DeleteConfirm:{de:"Diese Datei wirklich löschen?",en:"Really delete this file?"}, DeleteConfirmFolder:{de:"Diesen Ordner wirklich löschen?",en:"Really delete this folder?"}, DeleteConfirmMultiple:{de:"Diese {count} Elemente wirklich löschen?",en:"Really delete these {count} elements?"}, ApproveConfirm:{de:"Diese Datei wirklich freischalten?",en:"Really approve this file?"}, ApproveConfirmMultiple:{de:"Diese {count} Dateien wirklich freischalten?",en:"Really approve these {count} files?"}, UnapproveConfirm:{de:"Diese Datei wirklich sperren?",en:"Really set this file as unapproved?"}, UnapproveConfirmMultiple:{de:"Diese {count} Dateien wirklich sperren?",en:"Really set these {count} files as unapproved?"}, Error:{de:"Fehler",en:"Error"}, Printout:{de:"Ausdrucken",en:"Printout"}, Slideshow:{de:"Diashow",en:"Slideshow"} }; m.Lang="de"; m.DefaultLang="en"; m.win=new KWin(); m.menu; m.loading; m.jut=1; m.lastScrollTop=0; m.v=0; m.id=Math.floor((Math.random()*999999)+1); m.MouseKey=-1; m.o=o; m.opts=opts || {}; m.bridge=bridge || new KMediaGridBridge(); // Toolbar: m.tbO=m.Html2Object(m.Template.tb.replace(/{bgColor}/g,KMediaGrid.prototype.tbBgColor));m.o.appendChild(m.tbO); var st=m.tbO.style;st.position="absolute";st.left="0";st.top="0";st.overflow="hidden"; if(!opts.showToolbar){m.tbHeight=0;m.tbO.style.height="0";} m.TbLeft=document.getElementById("mTbLeft");m.TbLeft.id="";st=m.TbLeft.style;st.padding="0";st.textAlign="left";st.verticalAlign="middle"; m.TbRight=document.getElementById("mTbRight");m.TbRight.id="";st=m.TbRight.style;st.padding="0";st.textAlign="right";st.verticalAlign="middle"; m.btnParent=m.Html2Object(m.Template.btnParent);m.TbLeft.appendChild(m.btnParent); m.btnParent.title=m.s("Parent"); m.btnParent.onclick=function(){m.Parent()}; m.btnParent.style.display="none"; if(opts.allowSlideshow){ m.btnSlideshow=document.createElement("button");m.TbRight.appendChild(m.btnSlideshow); m.btnSlideshow.innerHTML=m.s("Slideshow"); ///add DOMEvent::onclick m.btnSlideshow.onclick=function(){m.Slideshow()}; st=m.btnSlideshow.style;st.display="none";st.marginLeft="2px";st.marginRight="6px"; } if(opts.upload || opts.uploadMultiple){ m.opts.upload=true; //m.btnUpload=m.Html2Object(this.Template.btnUpload);m.TbLeft.appendChild(m.btnUpload); //m.btnUpload.title=m.s("Upload"); m.upload=new KUpload(); //m.btnUpload.onclick=function(){m.UploadShow()}; } // Address: m.addrO=m.Html2Object(m.Template.addr);m.o.appendChild(m.addrO); st=m.addrO.style;st.position="absolute";st.left="0";st.top=m.tbHeight+"px"; if(!opts.showAddress){m.addrHeight=0;m.addrO.style.height="0"} m.addrI=document.getElementById("mAddrInner");m.addrI.id=""; // Foot: m.footO=m.Html2Object(m.Template.foot.replace(/{bgColor}/g,KMediaGrid.prototype.tbBgColor));m.o.appendChild(m.footO); st=m.footO.style;st.position="absolute";st.left="0"; m.footI=document.getElementById("mFootInner");m.footI.id=""; // Body: m.bodyO=m.Html2Object(m.Template.body.replace(/{borderColor}/g,KMediaGrid.prototype.bodyBorderColor));m.o.appendChild(m.bodyO); st=m.bodyO.style;st.position="absolute";st.left="0";st.top=(m.tbHeight+m.addrHeight)+"px";st.height=(m.o.offsetHeight-m.tbHeight-m.addrHeight-m.footO.offsetHeight)+"px"; if(opts.focusable){ m.bodyO.tabIndex="0"; m.bodyO.onfocus=function(){m.focussed=true}; m.bodyO.onblur=function(){m.focussed=false}; }else m.focussed=true; m.bodyI=document.getElementById("mBodyInner");m.bodyI.id=""; st=m.bodyI.style;st.position="absolute";st.left="0";st.top="0";st.width="100%";st.height="100%";st.overflowX="hidden";st.overflowY="scroll"; m.bodyI.onscroll=function(){m.Scroll()} m.bodyI.onmouseup=function(e){if(!e.ctrlKey && !e.shiftKey) m.DirMenu(e)}; m.bodyI.oncontextmenu=function(e){e.preventDefault()}; m.bodyI.ondragover=function(e){m.AllowDrop(e)}; m.bodyI.ondrop=function(e){m.Drop(e)}; m.dummy=document.createElement("div");m.bodyI.appendChild(m.dummy); st=m.dummy.style;st.position="absolute";st.left="0";st.top="0";st.width="1px";st.height="1px"; m.table=document.createElement("table");m.bodyI.appendChild(m.table); st=m.table.style;st.borderSpacing=0;st.borderCollapse="collapse";st.position="absolute";st.left="0";st.top="0";st.tableLayout="fixed"; m.InsertRow(0); m.rowFrom=0; m.rowTo=0; m.fileFrom=0; m.fileTo=-1; m.SetAdapterUrl(adapterUrl); document.addEventListener("mousedown",function(e){m.MouseDown(e)}); document.addEventListener("mousemove",function(e){m.MouseMove(e)}); document.addEventListener("mouseup",function(e){m.MouseUp(e)}); document.addEventListener("keydown",function(e){m.KeyDown(e)}); document.addEventListener("keypress",function(e){m.KeyPress(e)}); if(opts.lang) m.Lang=opts.lang; if(opts.propertiesUrl) m.SetPropertiesUrl(opts.propertiesUrl); if(opts.editUrl) m.SetEditUrl(opts.editUrl); if(opts.uploadUrl) m.SetUploadUrl(opts.uploadUrl); if(opts.printoutUrl) m.SetPrintoutUrl(opts.printoutUrl); }; /** * @fn Clear * @memberof KMediaGrid */ KMediaGrid.prototype.Clear=function(){ this.dir=0; this.history={}; this.files=[]; this.icons=[]; this.selected=[]; this.bridge.ClearClipboard(this); this.lastSel=-1; this.numFiles=-1; // -1 means no files had been loaded yet this.fileVisibleFrom=0; this.fileVisibleTo=0; this.rowVisibleFrom=0; this.rowVisibleTo=0; this.w=0; this.h=0; this.filesPerRow=1; this.orderBy=""; this.orderDesc=0; this.MenuClose(); this.AdjustRows(0,0); }; KMediaGrid.prototype.Destroy=function(){ while(this.o.firstChild) this.o.removeChild(this.o.firstChild); }; KMediaGrid.prototype.s=function(id){ var r=this.res[id]; if(r && Object.keys(r).length) return r[this.Lang] || r[this.DefaultLang] || r[Object.keys(r)[0]]; else return "["+id+"]"; }; KMediaGrid.prototype.LangChange=function(l){ this.Lang=l; this.Load(); }; KMediaGrid.GetAbsLeft=function(o){ return o.getBoundingClientRect().left+pageXOffset; }; KMediaGrid.GetAbsTop=function(o){ return o.getBoundingClientRect().top+pageYOffset; }; KMediaGrid.prototype.WinOpen=function(){ var o=this.win.Open("medium",0,{modal:true,iframe:true,borderless:true}).o; var m=this; o.onload=function(){ if(typeof o.contentWindow.BindMediaGrid=="function") o.contentWindow.BindMediaGrid(m); } return o; }; KMediaGrid.prototype.WinResize=function(w,h){ this.win.Resize(this.win.Find("medium"),w,h); }; KMediaGrid.prototype.WinClose=function(){ this.win.CloseById("medium"); }; KMediaGrid.prototype.ShowAddress=function(path){ this.path=path; while(this.addrI.firstChild) this.addrI.removeChild(this.addrI.firstChild); var m=this; for(var a=0;athis.rowTo || to=0;a--) this.table.deleteRow(a); for(a=from;a<=to;a++) this.InsertRow(a-from); }else{ while(from>this.rowFrom){this.DeleteRow(0);this.rowFrom++} while(fromthis.rowTo){this.InsertRow(this.table.rows.length);this.rowTo++} } this.rowFrom=from; this.rowTo=to; this.table.style.top=(from*this.rowHeight)+"px"; if(forceNew) this.Refresh(); }; KMediaGrid.prototype.InsertRow=function(index){ var tr=this.table.insertRow(index);tr.style.height=this.rowHeight+"px"; var m=this; tr.oncontextmenu=function(e){e.preventDefault()}; tr.onmouseup=function(e){if(!e.ctrlKey && !e.shiftKey) m.DirMenu(e)}; tr.ondragover=function(e){m.AllowDrop(e)}; tr.ondrop=function(e){m.Drop(e)}; }; KMediaGrid.prototype.DeleteRow=function(index){ this.table.deleteRow(index); }; KMediaGrid.prototype.FindIcon=function(url){ for(var a=0;a-1) return a; return -1; }; KMediaGrid.prototype.GetIcon=function(url){ var i=this.FindIcon(url); if(i>-1) return this.icons[i]; else return null; }; KMediaGrid.prototype.Scroll=function(){ this.Redraw(); }; KMediaGrid.prototype.Scroll2IDs=function(ids){ var i=-1; for(var a=0;a-1) {i=a;break} if(i>-1) this.bodyI.scrollTop=Math.floor((this.fileFrom+i)/this.filesPerRow)*this.rowHeight; this.Redraw(); }; KMediaGrid.prototype.LoadSoon=function(){ this.bodyI.style.cursor="wait";this.loading=true; if(this.tmrLoad) clearTimeout(this.tmrLoad); var m=this;this.tmrLoad=setTimeout(function(){m.Load()},200); }; KMediaGrid.prototype.Load=function(dir,cmd,showIDs,paste){ if(typeof dir=="undefined") dir=this.dir; if(!this.adapterUrl) return; this.bodyI.style.cursor="wait";this.loading=true; this.v++; var v=Math.floor(this.v); var m=this; var from=this.rowFrom*this.filesPerRow; var to=(this.rowTo+1)*this.filesPerRow; if(dir!=this.dir){to-=from;from=0} //alert(this.adapterUrl.replace(/{lang}/g,this.Lang)+"from="+from+"&to="+to+"&orderBy="+this.orderBy+"&orderDesc="+this.orderDesc+(dir?"&dir="+dir:"")+(cmd?"&command="+cmd:"")+(showIDs?"&find="+showIDs.join(","):"")); KInternet.HTTPRequest(this.adapterUrl.replace(/{lang}/g,this.Lang)+"from="+from+"&to="+to+"&orderBy="+this.orderBy+"&orderDesc="+this.orderDesc+(dir?"&dir="+dir:"")+(cmd?"&command="+cmd:"")+(showIDs?"&find="+showIDs.join(","):""),function(r,json){ if(v==m.v){ //alert(r); if(json){ m.numFiles=json.total; m.files=json.files; m.fileFrom=json.from; m.fileTo=json.to; for(var a=0;a=0;a--){ var isIn=false; for(var b=0;b1?"":"none"; }else{ dir=0; m.btnParent.style.display="none"; } m.dir=dir; if(m.opts.allowSlideshow){ var anyUrl=false; for(var a=0;a-1) t=this.Template.fileAddSelected; file.o=this.Html2Object(t); if(td.firstChild) td.replaceChild(file.o,td.firstChild); else td.appendChild(file.o); file.o.onclick=function(e){m.UploadShow();e.stopPropagation()}; file.o.onmouseup=function(e){e.stopPropagation()}; }else{ t=this.Template.file; if(this.selected.indexOf(file.id)>-1) t=this.Template.fileSelected; var mark="";if(file.unapproved) mark="?";if(file.deleted) mark="X"; file.o=this.Html2Object(t.replace(/{imgID}/g,"mIcon"+this.id+"_"+file.id).replace(/{captionID}/g,"mCaption"+this.id+"_"+file.id).replace(/{caption}/g,KMediaGrid.HtmlEntities(file.caption)).replace(/{mark}/g,mark)); if(this.bridge.clipboardModeCut && this.bridge.clipboard.indexOf(file.id)>-1) file.o.style.opacity="0.4"; if(td.firstChild) td.replaceChild(file.o,td.firstChild); else td.appendChild(file.o); KMediaGrid.HookEvent(file.o,"mousedown",this,"FileMouseDown",file.id); KMediaGrid.HookEvent(file.o,"mouseup",this,"FileMouseUp",file.id); KMediaGrid.HookEvent(file.o,"dblclick",this,"FileDblClick",file.id); KMediaGrid.HookEvent(file.o,"mousemove",this,"TipShow",file.tip); KMediaGrid.HookEvent(file.o,"mouseout",this,"TipShow",""); var i=document.getElementById("mIcon"+this.id+"_"+file.id); if(file.isFolder){ i.style.backgroundImage="url('"+m.imgFolder+"')"; i.style.width=this.iconWidth+"px"; i.style.height=this.iconHeight+"px"; }else{ // TODO (mediagrid.js) Codeänderung an Maik senden: // media-grid loads thumbnails twice - here and by this.LoadIcons() below: // var icon=this.GetIcon(file.icon); // if(icon && icon.loaded) i.style.backgroundImage="url("+file.icon+")"; } } }else if(td.firstChild) td.removeChild(td.firstChild); } } this.LoadIcons(); }; KMediaGrid.prototype.LoadIcons=function(){ if(this.files.length==0) return; var urls=[]; for(var a=this.fileVisibleFrom;a<=this.fileVisibleTo;a++){ var file=this.files[a-this.fileFrom]; if(file && file.icon && urls.indexOf(file.icon)==-1) urls.push(file.icon); } for(a=0;a-1){ files.push(f); if(f.approvable && f.unapproved) approvable.push(f.id); if(f.approvable && !f.unapproved) unapprovable.push(f.id); if(f.copyable) copyable.push(f.id); if(f.movable) movable.push(f.id); if(f.deletable) deletable.push(f.id); if(f.editable) editable.push(f.id); if(f.properties) properties.push(f.id); if(f.printable) printable.push(f.id); } } if(approvable.length) this.menu.Insert(this.s("Approve"),"",this,"Approve",approvable); if(unapprovable.length) this.menu.Insert(this.s("Unapprove"),"",this,"Unapprove",unapprovable); //if(this.bridge.clipboard.length && this.dir!=this.bridge.clipboardDir) && this.allowAddFiles) this.menu.Insert(this.s("Paste"),"",this,"Paste",file.allowAddFiles?file.id:this.dir); if(file.url && ids.length==1) this.menu.Insert(this.s("Download"),"",this,"Download",file.id); if(properties.length && (this.opts.allowMultiProperties || properties.length==1)) this.menu.Insert(this.s("Properties"),"",this,"Properties",properties); if(this.editUrl && editable.length && (this.opts.allowMultiEdit || editable.length==1)) this.menu.Insert(this.s("Edit"),"",this,"Edit",editable); if(this.menu.Count() && copyable.length+movable.length+deletable.length) this.menu.Insert("-"); if(file.renamable && ids.length==1) this.menu.Insert(this.s("Rename"),"",this,"Rename",file.id); if(copyable.length) this.menu.Insert(this.s("Copy"),"",this,"Copy",copyable); if(movable.length) this.menu.Insert(this.s("Cut"),"",this,"Cut",movable); if(deletable.length) this.menu.Insert(this.s("Delete"),"",this,"Delete",deletable); if(printable.length) this.menu.Insert(this.s("Printout"),"",this,"Printout",printable); //if(this.allowMakeFolders){ // if(this.menu.Count()) this.menu.Insert("-"); // this.menu.Insert(this.s("MakeFolder"),"",this,"MakeFolder",0); //} if(this.opts.allowSlideshow && this.lastSel && KImageSlider.FullscreenEnabled()){ var i=this.FindFile(this.lastSel); if(i>-1 && this.files[i].url){ this.menu.Insert("-"); this.menu.Insert(this.s("Slideshow"),"",this,"Slideshow",0); } } var addOns=this.onFileMenu(files); if(Array.isArray(addOns) && addOns.length){ this.menu.Insert("-"); for(var a=0;a-1){ var y=Math.floor((this.fileFrom+i)/this.filesPerRow)*this.rowHeight; if(ythis.bodyI.scrollTop+this.bodyI.offsetHeight) this.bodyI.scrollTop=y+this.rowHeight-this.bodyI.offsetHeight; } }; KMediaGrid.prototype.Select=function(id){ var i=this.FindFile(id); if(i>-1){ this.selected=[id]; this.lastSel=id; this.Refresh(); } }; KMediaGrid.prototype.Toggle=function(id){ var i=this.selected.indexOf(id); if(i>-1) this.selected.splice(i,1); else this.selected.push(id); this.Refresh(); this.lastSel=id; }; KMediaGrid.prototype.SelectRange=function(fromID,toID,clear){ if(clear) this.selected=[]; if(fromID==-1) fromID=this.lastSel; if(fromID==-1) return; var from=this.FindFile(fromID); var to=this.FindFile(toID); if(from==-1 || to==-1) return; //for(var a=Math.min(from,to);a<=Math.max(from,to);a++) if(this.selected.indexOf(this.files[a].id)==-1) this.selected.push(this.files[a].id); var a=from; while(true){ if(this.selected.indexOf(this.files[a].id)==-1) this.selected.push(this.files[a].id); if(a==to) break; if(to=0;a--) if(this.selected[a]==0) this.selected.splice(a,1); // remove "add new" icon this.Refresh(); }; KMediaGrid.prototype.AllowDrop=function(e){ e.preventDefault(); if(this.opts.upload) e.dataTransfer.dropEffect="copy"; }; KMediaGrid.prototype.Drop=function(e){ e.preventDefault(); var m=this; if(m.opts.upload){ var files=e.dataTransfer.files; if(files.length){ if(!m.opts.uploadMultiple) files=[files[0]]; m.upload.Upload(m.uploadUrl,files,function(ids){m.UploadComplete(ids)}); } } }; KMediaGrid.prototype.UploadShow=function(e){ var m=this; m.upload.UploadShow(m.uploadUrl+(m.dir?"dir="+m.dir:""),m.opts.uploadMultiple,function(ids){m.UploadComplete(ids)}); }; KMediaGrid.prototype.UploadComplete=function(ids){ if(ids.length){ this.selected=[]; for(var a=0;a1) s=this.s("DeleteConfirmMultiple").replace(/{count}/g,ids.length); else if(this.GetFile(ids[0]).isFolder) s=this.s("DeleteConfirmFolder"); if(confirm(s)) this.Load(this.dir,"delete|"+ids.join(",")); }; /** * @fn Approve * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.Approve=function(ids){ if(confirm(ids.length>1 ? this.s("ApproveConfirmMultiple").replace(/{count}/g,ids.length) : this.s("ApproveConfirm"))) this.Load(this.dir,"approve|"+ids.join(",")); }; /** * @fn Unapprove * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.Unapprove=function(ids){ if(confirm(ids.length>1 ? this.s("UnapproveConfirmMultiple").replace(/{count}/g,ids.length) : this.s("UnapproveConfirm"))) this.Load(this.dir,"unapprove|"+ids.join(",")); }; /** * @fn MakeFolder * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.MakeFolder=function(){ this.Load(this.dir,"makefolder"); }; /** * @fn OpenFolder * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.OpenFolder=function(id){ this.Load(id); }; /** * @fn Slideshow * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.Slideshow=function(){ var m=this; m.imageSlider=new KImageSlider(null,[],{fullscreenOnly:true},function(){m.blockKeys=false}); m.blockKeys=true; m.LoadSlides(function(imgs,index){ m.imageSlider.SetImages(imgs,index); }); }; /** * @fn LoadSlides * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.LoadSlides=function(func){ var m=this; //alert(this.adapterUrl.replace(/{lang}/g,this.Lang)+"from="+from+"&to="+to+"&orderBy="+this.orderBy+"&orderDesc="+this.orderDesc+(dir?"&dir="+dir:"")+(cmd?"&command="+cmd:"")+(showIDs?"&find="+showIDs.join(","):"")); KInternet.HTTPRequest(this.adapterUrl.replace(/{lang}/g,this.Lang)+"from=0&to="+(m.numFiles-1)+"&orderBy="+this.orderBy+"&orderDesc="+this.orderDesc+(m.dir?"&dir="+m.dir:""),function(r,json){ //alert(r); if(json){ var imgs=[],index=undefined; for(var a=0;a-1) return this.files[i]; else return null; }; /** * @fn HookEvent * @memberof KMediaGrid * Private method. */ KMediaGrid.HookEvent=function(o,eventName,target,funcName,funcParam){ o.addEventListener(eventName,function(e){target[funcName](e,funcParam)}); }; /** * @fn TipShow * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.TipShow=function(e,tip){ if(tip && !this.menu &&!this.Dragging){ var x=e.clientX;var y=e.clientY; if(!this.tip){ this.tip=this.Html2Object(this.Template.tip);document.body.appendChild(this.tip); this.tip.style.position="absolute";this.tip.style.zIndex="9999"; this.tip.innerHTML=tip; } var w=this.tip.offsetWidth; var h=this.tip.offsetHeight; var oY=window.pageYOffset;if(!oY) o=0; var wh=document.documentElement.clientHeight; var ww=document.documentElement.clientWidth; if(xwh+oY) y=wh+oY-h; this.tip.style.left=x+"px"; this.tip.style.top=y+"px"; if(x+this.tip.offsetWidth+10>ww) this.tip.style.width=(this.tip.offsetWidth-x-10)+"px"; }else if(this.tip){ document.body.removeChild(this.tip); this.tip=null; } }; /** * @fn MenuClose * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.MenuClose=function(){ if(this.menu) this.menu.Close(); }; /** * @fn MenuClosed * @memberof KMediaGrid * Required callback method of KMenu. */ KMediaGrid.prototype.MenuClosed=function(){ this.menu=null; }; /** * @fn Redraw * @memberof KMediaGrid * Private method. */ KMediaGrid.prototype.Redraw=function(force){ var numRows=Math.ceil((this.numFiles+(this.allowAddFiles?1:0))/this.filesPerRow); this.dummy.style.height=(numRows*this.rowHeight)+"px"; this.rowVisibleFrom=Math.floor(this.bodyI.scrollTop/this.rowHeight); this.rowVisibleTo=Math.max(0,Math.ceil((this.bodyI.scrollTop+this.bodyI.offsetHeight-1)/this.rowHeight)-1); this.fileVisibleFrom=this.rowVisibleFrom*this.filesPerRow; this.fileVisibleTo=(this.rowVisibleTo+1)*this.filesPerRow; if(this.numFiles>-1 && this.fileVisibleTo>this.numFiles-1) this.fileVisibleTo=this.numFiles-1; if(this.fileVisibleFromthis.fileTo || force){ var n=this.rowVisibleTo-this.rowVisibleFrom+1; var from=Math.max(0,this.rowVisibleFrom-n*this.jut); var to=this.rowVisibleTo+n*this.jut;if(numRows && to>numRows-1) to=numRows-1; this.AdjustRows(from,to,force); this.LoadSoon(); }else this.LoadIcons(); }; KMediaGrid.prototype.Resize=function(w,h){ if(w>0 && h>0 && (w!=this.w || h!=this.h)){ this.o.style.width=w+"px";this.o.style.height=h+"px"; this.tbO.style.width=w+"px"; this.addrO.style.width=w+"px"; var st=this.bodyO.style;st.width=w+"px";st.height=(h-this.tbHeight-this.addrHeight-this.footO.offsetHeight)+"px"; st=this.footO.style;st.width=w+"px";st.top=(h-this.footO.offsetHeight)+"px"; if(this.bodyI.clientWidth){ // only when visible this.w=w;this.h=h; var n=Math.max(1,Math.floor(this.bodyI.clientWidth/this.cellWidth)); var force=false; if(n!=this.filesPerRow){this.filesPerRow=n;force=true} this.Redraw(force); this.WinResize(); } } }; KMediaGrid.prototype.KeyDown=function(e){ if(this.o.offsetWidth && this.focussed && !this.blockKeys){ if(e.key=="Delete"){ var deletable=[]; for(var a=0;a-1 && this.files[i].deletable) deletable.push(this.selected[a]); } if(deletable.length) this.Delete(deletable); }else if(e.key=="ArrowLeft"){ if(this.selected.length){ var i=this.FindFile(this.selected[this.selected.length-1]); if(i>0) if(e.shiftKey) this.SelectRange(-1,this.files[i-1].id,true); else this.Select(this.files[i-1].id); }else if(this.files.length) this.Select(this.files[this.files.length-1].id); if(this.selected.length) this.EnsureVisible(this.selected[this.selected.length-1]); e.preventDefault(); }else if(e.key=="ArrowRight"){ if(this.selected.length){ i=this.FindFile(this.selected[this.selected.length-1]); if(i=0) if(e.shiftKey) this.SelectRange(-1,this.files[i-this.filesPerRow].id,true); else this.Select(this.files[i-this.filesPerRow].id); } if(this.selected.length) this.EnsureVisible(this.selected[this.selected.length-1]); e.preventDefault(); }else if(e.key=="ArrowDown"){ if(this.selected.length){ i=this.FindFile(this.selected[this.selected.length-1]); if(i+this.filesPerRow1) this.Parent(); e.preventDefault(); } } }; KMediaGrid.prototype.KeyPress=function(e){ if(this.o.offsetWidth && this.focussed && !this.blockKeys){ if(e.key=="Enter"){ if(this.selected.length==1) if(this.selected[0]) this.FileDblClick(null,this.selected[0]);else this.UploadShow(); } } }; KMediaGrid.prototype.MouseDown=function(e){ this.MouseKey=e.button; this.eX=e.clientX;this.eY=e.clientY; if(e.button>-1){this.mX=this.eX;this.mY=this.eY} }; KMediaGrid.prototype.MouseMove=function(e){ this.eX=e.clientX;this.eY=e.clientY; if(this.MouseKey==0 && !this.Dragging && this.dragFile) if(Math.abs(this.mX-this.eX)>9 || Math.abs(this.mY-this.eY)>9){this.FileDrag(this.dragFile);this.dragFile=null} if(this.Dragging){ this.DragO2.style.left=Math.max(0,Math.min(this.bodyI.clientWidth-this.DragO2.offsetWidth,this.DragOX+this.eX-this.mX))+"px"; this.DragO2.style.top=Math.max(0,Math.min(this.dummy.offsetHeight-this.DragO2.offsetHeight,this.DragOY+this.eY-this.mY))+"px"; this.CheckDrop(this.eX-KMediaGrid.GetAbsLeft(this.bodyI),this.eY-KMediaGrid.GetAbsTop(this.bodyI)+this.bodyI.scrollTop); } }; KMediaGrid.prototype.MouseUp=function(e){ if(this.Dragging){ this.RemoveDrop(); this.FileDragFinish(); } this.MouseKey=-1; }; KMediaGrid.prototype.CheckDrop=function(x,y){ var i=-1,file; if(x && y){ var row=Math.floor((y+this.rowFrom*this.rowHeight)/this.rowHeight); var col=Math.floor(x/this.cellWidth); var xx=x/this.cellWidth-col;var amRand=(xx<0.2 || xx>0.8); var i=row*this.filesPerRow+col; if(i==this.dragIndex) i=-1; else{ file=this.files[i-this.fileFrom]; if(file && file.isFolder && file.allowAddFiles && this.allowMoveToFolder && (!amRand || !this.allowReOrder)){ this.dropFolder=true; this.DragO2.style.cursor="pointer"; if(i>this.numFiles || i==this.dragIndex) i=-1; }else{ this.dropFolder=false; this.DragO2.style.cursor=""; if(this.allowReOrder){ col=Math.round(x/this.cellWidth); i=row*this.filesPerRow+col; }else i=-1; if(i>this.numFiles || i==this.dragIndex || i==this.dragIndex+1) i=-1; } } this.dropIndex=i; } this.RemoveDrop(); if(i>-1){ if(this.dropFolder){ this.dropMarkO=document.createElement("div");this.bodyI.appendChild(this.dropMarkO); var st=this.dropMarkO.style;st.position="absolute";st.left=(col*this.cellWidth)+"px";st.top=(row*this.rowHeight)+"px";st.width=this.cellWidth+"px";st.height=this.rowHeight+"px";st.backgroundColor=this.dropFolderColor; }else{ if(col==0 && row>0){row--;col=this.filesPerRow} this.dropMarkO=document.createElement("div");this.bodyI.appendChild(this.dropMarkO); var st=this.dropMarkO.style;st.position="absolute";st.left=(col*this.cellWidth-1)+"px";st.top=(row*this.rowHeight)+"px";st.width="3px";st.height=this.rowHeight+"px";st.backgroundColor="black"; } } }; KMediaGrid.prototype.RemoveDrop=function(){ if(this.dropMarkO){this.bodyI.removeChild(this.dropMarkO);this.dropMarkO=null} }; KMediaGrid.prototype.FileDrag=function(file){ if(this.selected.indexOf(file.id)==-1){this.selected=[];this.Toggle(file.id)} this.Dragging=true; this.dragIndex=this.FindFile(file.id); this.DragOX=KMediaGrid.GetAbsLeft(file.o)-KMediaGrid.GetAbsLeft(this.bodyI)+this.bodyI.scrollLeft; this.DragOY=KMediaGrid.GetAbsTop(file.o)-KMediaGrid.GetAbsTop(this.bodyI)+this.bodyI.scrollTop; this.DragO2=file.o.cloneNode(true); var st=this.DragO2.style;st.position="absolute";st.left=this.DragOX+"px";st.top=this.DragOY+"px";st.zIndex="9999";st.opacity="0.55"; this.bodyI.appendChild(this.DragO2); }; KMediaGrid.prototype.FileDragFinish=function(){ if(this.DragO2) this.bodyI.removeChild(this.DragO2); this.Dragging=false; if(this.dropIndex>-1){ var cmd,mFiles=[]; if(this.dropFolder){ var dropID=this.files[this.dropIndex].id; for(var a=this.files.length-1;a>=0;a--) if(this.selected.indexOf(this.files[a].id)>-1 && !this.files[a].immovable){mFiles.unshift(this.files[a].id);this.files.splice(a,1)} if(mFiles.length){ cmd="move|"+dropID+"|"+mFiles.join(","); this.selected=[]; } }else{ cmd="reorder|"+this.dropIndex+"|"; for(var a=this.files.length-1;a>=0;a--) if(this.selected.indexOf(this.files[a].id)>-1){mFiles.unshift(this.files[a]);this.files.splice(a,1);if(a-1) url+="&"; else url+="?"; return url; }; KMediaGrid.prototype.SetAdapterUrl=function(url){ this.adapterUrl=this.CheckUrl(url); this.Clear(); this.Redraw(true); }; KMediaGrid.prototype.SetPropertiesUrl=function(url){ this.propertiesUrl=this.CheckUrl(url); }; KMediaGrid.prototype.SetEditUrl=function(url){ this.editUrl=this.CheckUrl(url); }; KMediaGrid.prototype.SetUploadUrl=function(url){ this.uploadUrl=this.CheckUrl(url); }; KMediaGrid.prototype.SetPrintoutUrl=function(url){ this.printoutUrl=this.CheckUrl(url); }; KMediaGrid.prototype.onDeleted=function(){ // can be overwritten }; KMediaGrid.prototype.onApproved=function(){ // can be overwritten }; KMediaGrid.prototype.onUnapproved=function(){ // can be overwritten }; KMediaGrid.prototype.onFileMenu=function(files){ return []; // can be overwritten }; KMediaGrid.prototype.Html2Object=function(html){ var p=document.createElement("div");p.innerHTML=html; return p.firstChild; }; KMediaGrid.HtmlEntities=function(s){ if(s==null) s=""; return s.replace(/&/g,"&").replace(//g,">").replace(/"/g,"""); }; KMediaGrid.prototype.tbHeight=40; KMediaGrid.prototype.tbBgColor="#F4F5F6"; KMediaGrid.prototype.bodyBorderColor="#F4F5F6"; KMediaGrid.prototype.addrHeight=26; KMediaGrid.prototype.iconWidth=144; KMediaGrid.prototype.iconHeight=144; KMediaGrid.prototype.fileWidth=152; KMediaGrid.prototype.fileHeight=196; KMediaGrid.prototype.cellWidth=160; KMediaGrid.prototype.rowHeight=204; KMediaGrid.prototype.dropFolderColor="rgba(255,255,0,0.6)"; KMediaGrid.prototype.Template={ tb:"
", btnParent:"", //btnUpload:"", addr:"
", addrItem:"{caption}", addrSeparator:"/", body:"
", file:"
{mark}
{caption}
", fileSelected:"
{mark}
{caption}
", fileAdd:"
+
", fileAddSelected:"
+
", foot:"
", tip:"
" }; KMediaGrid.prototype.imgFolder="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB4bWxuczpzdmc9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiI+PHJlY3Qgd2lkdGg9IjMyIiBoZWlnaHQ9IjMyIiB4PSIwIiB5PSIwIiBzdHlsZT0iZmlsbDojZmZmZmZmIi8+PHBhdGggZD0ibSAyMiwxMy45NzY2NzYgYyAwLC0wLjIyODE0MyAtMC4xNjczNzQsLTAuNDE3NzUyIC0wLjM4ODkwMSwtMC40NTkwMDggbCAwLC0wLjAwODYgLTExLjIyMzkwOSwwIDAsMC4wMDg4IEMgMTAuMTY2NTI5LDEzLjU1OTc3OCAxMCwxMy43NDkxMjkgMTAsMTMuOTc2NjE2IGwgMC4yOTkxMjQsNi4yODU4NCBjIDAsMC4yNTgyMzUgMC4wNTQ3MiwwLjQ2NzU0NCAwLjMxOTM4MSwwLjQ2NzU0NCBsIDEwLjc5MzAzNiwwIGMgMC4yNjQ2ODUsMCAwLjMxOTM4MSwtMC4yMDkzMDkgMC4zMTkzODEsLTAuNDY3NTQ0IHoiIHN0eWxlPSJmaWxsOiNmZjk2MzIiLz48cGF0aCBkPSJtIDIxLjYxMTA5OSwxMi42Nzk1MjMgYyAwLC0wLjI0Mjk0MyAtMC4yMDE4MjcsLTAuNDM5Nzc0IC0wLjQ1MDk2MywtMC40Mzk3NzQgbCAtNS41Njk5MTMsNy4xZS01IGMgLTAuMTc4MzQ4LC0wLjAwNDQgLTAuMzE1NzY4LC0wLjMyMDM2NyAtMC4zMTU3NjgsLTAuNDQxMjc2IGwgNi44ZS01LC0wLjA1MTQ2IGMgMCwtMC4yNTgyNTcgLTAuMjE0NTg0LC0wLjQ2NzUyIC0wLjQ3OTI5MywtMC40Njc1MiBsIC0zLjE3MDgzMiwtNy4xZS01IGMgLTAuMjY0NzU2LDAgLTAuNDc5MzY3LDAuMjA5MjYyIC0wLjQ3OTM2NywwLjQ2NzUyIGwgMS4xN2UtNCwwLjA1MTQ2IGMgMCwwLjEyMTc1NCAtMC4xMzkyNzIsMC40NDE2MDUgLTAuMzE5NDI4LDAuNDQxNjA1IGwgMC4wMDQzLDAgYyAtMC4yNDUzMTEsMC4wMDQzIC0wLjQ0Mjg0NiwwLjE5OTIyNCAtMC40NDI4NDYsMC40Mzk0NyBsIDAsMC4zODI1NjUgMTEuMjIzOTM0LDAgeiIgc3R5bGU9ImZpbGw6I2ZmOTYzMiIvPjwvc3ZnPg=="; /** * @class KMediaGridBridge */ function KMediaGridBridge(){ this.clipboardClient=null; this.clipboard=[]; this.clipboardDir=false; this.clipboardModeCut=false; /** * @fn Copy * @memberof KMediaGridBridge * @param {KMediaGrid} client * @param {int[]} ids * @param {int|string} dir */ this.Copy=function(client,ids,dir){ this.ClearClipboard(client); this.clipboardClient=client; this.clipboard=ids; this.clipboardDir=dir; this.clipboardModeCut=false; client.Refresh(); }; /** * @fn Cut * @memberof KMediaGridBridge * @param {KMediaGrid} client * @param {int[]} ids * @param {int|string} dir */ this.Cut=function(client,ids,dir){ this.ClearClipboard(client); this.clipboardClient=client; this.clipboard=ids; this.clipboardDir=dir; this.clipboardModeCut=true; client.Refresh(); }; /** * @fn ClearClipboard * @memberof KMediaGridBridge * @param {KMediaGrid} client */ this.ClearClipboard=function(client){ this.clipboard=[]; this.clipboardDir=false; if(this.clipboardClient && this.clipboardClient!=client) this.clipboardClient.Refresh(); }; /** * @fn ReloadSourceClient * @memberof KMediaGridBridge * @param {KMediaGrid} client */ this.ReloadSourceClient=function(client){ if(this.clipboardClient && this.clipboardClient!=client) this.clipboardClient.Load(); }; };