Files
cmp-go-snippet/static/cmpSwoUtil.js
2024-01-26 07:11:35 +12:00

344 lines
5.8 KiB
JavaScript

function unixtimeToYYYYMMDD(s) {
var n;
if(s)
n = new Date(s * 1000);
else
n = new Date();
var m = n.getMonth()+1;
var d = n.getDate();
return (n.getFullYear()) + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d);
}
function unixtimeToHHMMSS(s) {
var n;
if(s)
n = new Date(s * 1000);
else
n = new Date();
var h = n.getHours();
var m = n.getMinutes();
var s = n.getSeconds();
return (h < 10 ? ("0" + h) : h) + ":" + (m < 10 ? ("0" + m) : m) + ":" + (s < 10 ? ("0" + s) : s);
}
function checkTZ(z) {
if((new Date()).getTimezoneOffset())
return 0;
if(z)
console.log("Auto correct TimeZone");
return 12 * 60 * 60;
}
function setNodeText(nn, obj, idx, q) {
var n = null;
if(typeof q == "undefined")
q = 1;
switch(typeof nn) {
case "string":
n = document.getElementById(nn);
break;
case "object":
if(nn.nodeType != 1)
return console.log("Invalid node object");
n = nn;
break;
default:
return console.log("Invalid node type");
}
if(!n) {
if(q == 1)
return console.log("No node '" + nn + "'");
return;
}
if(!obj[idx])
return;
n.innerHTML = obj[idx];
return;
}
function dpst(no, p) {
var n = null;
do {
if(p.before) {
n = p.before;
break;
}
if(p.after) {
n = p.after;
break;
}
if(p.parn) {
n = p.parn;
break;
}
return;
} while(0);
if(typeof(n) == "string") {
n = document.getElementById(n);
if(!n)
return false;
}
if(n.nodeType !== 1) {
console.log("Invalid node type");
return false;
}
if(p.before) {
n.parentNode.insertBefore(no, n);
return true;
}
if(p.after) {
if(p.after.nextSibling)
n.parentNode.insertBefore(no, n.nextSibling);
else
n.parentNode.appendChild(no);
return true;
}
if(p.parn) {
n.appendChild(no);
return true;
}
return false;
}
function dcrt(p) {
var at = "";
var no = null;
var spc = {
"tag" : 1,
"html" : 1,
"parn" : 1,
"child" : 1,
"class" : 1,
"after" : 1,
"before" : 1,
"click" : 1,
"keyup" : 1,
"keypress" : 1,
"change" : 1,
"load" : 1,
"src" : 1
}
if(!p.tag) {
console.log("Empty .tag");
return null;
}
no = document.createElement(p.tag);
for(at in p) {
if(spc[at])
continue;
no.setAttribute(at, p[at]);
}
if(p.class)
no.className = p.class;
dpst(no, p);
if(p.html) {
no.innerHTML = p.html;
}
if(p.click) {
no.addEventListener("click", p.click, false);
}
if(p.keyup) {
no.addEventListener("keyup", p.keyup, false);
}
if(p.keypress) {
no.addEventListener("keypress", p.keypress, false);
}
if(p.change) {
no.addEventListener("change", p.change, false);
}
if(p.load) {
no.addEventListener("load", p.load, false);
// no.onload = p.load;
}
if(p.src) {
no.setAttribute("src", p.src);
// no.src = p.src;
}
if(!p.child)
return no;
var ch = null;
for(at = 0; at < p.child.length; at++) {
if(!p.child[at])
continue;
if(!p.child[at].nodeType)
ch = dcrt(p.child[at]);
else
ch = p.child[at]
if(!ch)
continue;
no.appendChild(ch);
}
return no;
}
class diag {
constructor(parm) {
this.buildBody(parm);
}
buildBody(parm) {
var top = 0.8 * (window.innerHeight- 480)/2;
var left = 1.0 * (window.innerWidth - 640)/2;
this.diagRoot = dcrt({
tag : "div",
parn : "body",
id : "diag",
style: "display:none; top:"+top+"px; left:"+left+"px;"
});
this.diagHead = dcrt({
tag : "div",
style: "position:absolute; top:0px; left:-2px; right:-2px; height:20px; background:#000; padding:10px; font-weight:bold;",
html : "DIALOG",
parn : this.diagRoot
});
this.diagBody = dcrt({
tag : "div",
style: "position:absolute; left:0; right:0; top:40px; bottom:40px; padding:6px; border:0px solid white;",
parn : this.diagRoot,
// child: parm.child
});
this.diagFloor= dcrt({
tag : "div",
style: "position:absolute; left:-2px; right:-2px; bottom:0px; height:20px; background:#000; padding:10px;",
parn : this.diagRoot
});
do {
if(!parm.head)
break;
if(!parm.head.html)
break;
this.diagHead.innerHTML = parm.head.html;
} while(0);
do {
if(!parm.body)
break;
if(!parm.body.node)
break;
parm.body.node.parn = this.diagBody;
dcrt(parm.body.node);
} while(0);
do {
if(!parm.floor)
break;
if(!parm.floor.node)
break;
parm.floor.node.parn = this.diagFloor;
dcrt(parm.floor.node);
} while(0);
this.diagHead.addEventListener('mousedown', this.diagDraggable.bind(this), false);
}
diagDraggable(e) {
var e = e || window.event;
if(window.diag)
window.diag.diagRoot.style.zIndex = 49;
window.diag = this;
this.diagInnerX = e.clientX + window.pageXOffset - this.diagRoot.offsetLeft;
this.diagInnerY = e.clientY + window.pageYOffset - this.diagRoot.offsetTop;
this.diagHandMove = this.diagMove .bind(this);
this.diagHandMouseUp = this.diagMouseUp.bind(this);
window.addEventListener('mousemove', this.diagHandMove , false);
window.addEventListener('mouseup' , this.diagHandMouseUp, true);
}
diagMouseUp(e) {
window.removeEventListener('mousemove', this.diagHandMove);
window.removeEventListener('mouseup' , this.diagHandMouseUp);
}
diagMove(e) {
this.diagRoot.style.zIndex = 50;
this.diagRoot.style.position = 'fixed';
this.diagRoot.style.left = e.clientX + window.pageXOffset - this.diagInnerX + 'px';
this.diagRoot.style.top = e.clientY + window.pageYOffset - this.diagInnerY + 'px';
}
show() {
this.diagRoot.style.display = "block";
}
hide() {
this.diagRoot.style.display = "none";
}
// CLASS
};