/**
* @file win.js
* @version 20170907 ms
* @type JS
* GUI-Komponente zur Darstellung ein oder mehrerer DIV-basierter Fenster
*/
function KWin(){
var m=this;
if("ontouchstart" in document){
document.addEventListener("touchstart",function(e){e.button=0;e.clientX=e.touches[0].clientX;e.clientY=e.touches[0].clientY;m.MouseDown(e)});
document.addEventListener("touchmove",function(e){e.clientX=e.touches[0].clientX;e.clientY=e.touches[0].clientY;m.MouseMove(e)});
document.addEventListener("touchend",function(e){m.MouseUp(e)});
}
document.addEventListener("mousedown",function(e){m.MouseDown(e)});
document.addEventListener("mousemove",function(e){m.MouseMove(e)});
document.addEventListener("mouseup",function(e){m.MouseUp(e)});
};
/**
* @fn Html2Object
* @memberof KWin
* @param html
* @return DOMnode of the first ChildNode of div
* Private static method.
* Create a div with fnparam html as content.
*/
KWin.Html2Object=function(html){
var p=document.createElement("div");p.innerHTML=html;
return p.firstChild;
}
KWin.prototype.windows=[];
KWin.TemplateDefault={
bg:"
",
win:"",
bar:"{title}{close}
",
close:"",
body:"{body}
"
}
KWin.TemplateOrange={
bg:"",
win:"",
bar:"{title}{close}
",
close:"X
",
body:"{body}
"
}
/**
* @fn Open
* @memberof KWin
* @param {any} type
* @param {any} id
* @param {object} opts: title (string), modal (boolean), autoclose (boolean), borderless (boolean), iframe (boolean), scrollbars (boolean - iframe content only), x, y, w, h, template
* @returns {object:window}
* Public method.
*/
KWin.prototype.Open=function(type,id,opts){
if(!opts) opts={};
var template=KWin.TemplateDefault;
if(opts.template) template=opts.template;
this.CloseById(type,id);
var win={type:type,id:id,opts:opts,centerX:!opts.x,centerY:!opts.y};
var m=this;
if(opts.modal){
win.bg=KWin.Html2Object(template.bg);
var st=win.bg.style;st.position="absolute";st.left="0";st.top="0";st.width="100%";st.height="100%";
document.body.appendChild(win.bg);
win.bg.ondblclick=function(){m.Close(win)};
}
win.win=KWin.Html2Object(template.win);
var st=win.win.style;st.position="absolute";st.left=opts.x?opts.x+"px":"0";st.top=opts.y?opts.y+"px":"0";
document.body.appendChild(win.win);
win.win.onmousedown=function(e){e.stopPropagation()}; // prevent document.mousedown
if(!opts.borderless){
var bar=KWin.Html2Object(template.bar.replace(/{title}/g,opts.title?opts.title:"").replace(/{close}/g,""));
var st=bar.style;st.cursor="default";st.userSelect="none";st.MozUserSelect="none";st.MsUserSelect="none";st.WebkitUserSelect="none";
win.win.appendChild(bar);
if("ontouchstart" in bar) bar.addEventListener("touchstart",function(e){m.WinDrag(win)});
bar.onmousedown=function(e){m.mx=e.clientX;m.my=e.clientY;m.WinDrag(win)};
var close=KWin.Html2Object(template.close);
var dummy=document.getElementById("KWinDummy");
dummy.parentNode.replaceChild(close,dummy);
}
if(opts.iframe){
win.o=document.createElement("iframe");st=win.o.style;st.display="block";st.border="0";st.overflow=opts.scrollbars?"auto":"hidden";
win.o.scrolling="no"; // Needed for Firefox - it ignores style.overflow="hidden";
}else
win.o=document.createElement("div");
if(typeof opts.w!="undefined") win.o.style.width=opts.w+"px";
if(typeof opts.h!="undefined") win.o.style.height=opts.h+"px";
var body=KWin.Html2Object(template.body.replace(/{body}/g,""));
if(opts.borderless) body.style.padding="0";
win.win.appendChild(body);
dummy=document.getElementById("KWinDummy");
dummy.parentNode.replaceChild(win.o,dummy);
if(opts.borderless){
close=KWin.Html2Object(template.close);
win.win.appendChild(close);
}
close.style.cursor="pointer";
close.onclick=function(e){m.Close(win)};
this.windows.push(win);
setTimeout(function(){m.Resize(win)},0);
return win;
}
/**
* @fn Resize
* @memberof KWin
* @param {object:window} win
* @param {int} w
* @param {int} h
* Public method.
*/
KWin.prototype.Resize=function(win,w,h){
if(win){
if(w>0) win.o.style.width=w+"px";
if(h>0) win.o.style.height=h+"px";
if(win.centerX) win.win.style.left=(pageXOffset+(window.innerWidth-win.win.offsetWidth)/2)+"px";
if(win.centerY) win.win.style.top=(pageYOffset+(window.innerHeight-win.win.offsetHeight)/2)+"px";
}
};
/**
* @fn Close
* @memberof KWin
* @param {object:window} win
* Public method.
*/
KWin.prototype.Close=function(win){
if(win){
document.body.removeChild(win.win);
if(win.bg) document.body.removeChild(win.bg);
for(var a=0;a