function gridit(did,its, w,h,s) {
console.log(did)
let div = d3.selectAll("#"+did)
div.classed("container", true)
let height = parseInt(div.style("height"))
let wd = parseInt(div.style("width"))
let content = d3.select("#"+did+"> .owncontent")
console.log("content",content,content.size() )
div.style("background-size",wd/w+"px "+height/h+"px")
if (content.size() > 0) {
div=content.html("")
} else {
div = div.append("div").style("height","100%")
.style("width","100%")
.classed("owncontent", true)
}
console.log(div,height,wd)
div.style("background-size",wd/w+"px "+height/h+"px;")
div.on("dblclick", newitem)
for (let i in its) {
let item = div.append("div").attr("style", "height:"+parseInt(height/h*its[i].h)+"px;"
+"width:"+parseInt(wd/w*its[i].w)+"px;" +"left:"+parseInt(wd/w*its[i].x)+"px;"+"top:"+parseInt(height/h*its[i].y)+"px;")
.attr("data-i", i).attr("id", its[i].id)
.classed("grid-item",true)
item.text(its[i].id)
item.on("click", upitem)
item.on("dblclick", nitem)
let mover = item.append("div").classed("mover",true)
let resizer = item.append("div").classed("resizer",true)
let deleter = item.append("div").classed("deleter",true).attr("data-i", i)
if (its[i].children){
gridit(its[i].id,its[i].children, w,h,s)
}
mover.call(d3.drag().on("start", dstarted).on("drag",ddrag).on("end", dend));
resizer.call(d3.drag().on("start", estarted).on("drag",edrag).on("end", dend));
deleter.on("click", function(){
let e = d3.event
let i = d3.select(this).attr("data-i")
let it = its.splice(i,1)
console.log(e, i, it)
//mutable items.splice(i,1)
e.stopPropagation()
return gridit(did, its, w, h, s)
})
}
function dstarted(e){
console.log(this,e)
let coords = d3.event
console.log(coords)
this.container= {container: this.parentNode, grand:this.parentNode.parentNode.parentNode, coords: d3.mouse(this.parentNode.parentNode.parentNode)}
}
function ddrag(){
console.log(this)
let e = d3.event
console.log(e)
//let p = this.container()
let p = d3.select(this.container.container)
let coords = d3.mouse(this.container.grand) // p
//let coords0 = d3.mouse(this.container.container)
console.log(p,coords)
p.style("left",coords[0]+"px").style("top",coords[1]+"px")
}
function dend(e){
console.log(this, this.container.container)
let p = d3.select(this.container.container)
let i = p.attr("data-i")
its[i].x = Math.round(parseInt(p.style("left"))/width*w)
its[i].y = Math.round(parseInt(p.style("top"))/height*h)
its[i].w = Math.round(parseInt(p.style("width"))/width*w)
its[i].h = Math.round(parseInt(p.style("height"))/height*h)
return gridit(did, its, w, h, s)
}
function estarted(e){
console.log(this,e)
let coords = d3.event
console.log(coords)
this.container= {container: this.parentNode, grand:this.parentNode.parentNode.parentNode, coords: d3.mouse(this.parentNode.parentNode.parentNode)}
console.log(this.parentNode, this.parentNode.parentNode.parentNode)
}
function edrag(){
console.log(this)
let e = d3.event
console.log(e)
//let p = this.container()
let p = d3.select(this.container.container)
let coords = d3.mouse(this.container.grand) // p
//console.log(p,coords, parseInt(p.style("width")), parseInt(p.style("left")), p.width)
//let w = parseInt(p.style("width"))
let x = parseInt(p.style("left"))
let y = parseInt(p.style("top"))
p.style("width",(coords[0]-x)+"px").style("height",(coords[1]-y)+"px")
}
function newitem(){
let coords = d3.mouse(this)
console.log(this)
its.push({"x":Math.floor(parseInt(coords[0])/width*w),"y":Math.floor(parseInt(coords[1])/height*h),"w":1,"h":1,"id":id()})
return gridit(did, its, w, h, s)
}
const id = () =>
"_" +
Math.random()
.toString(36)
.substr(2, 9);
function upitem(){
let e = d3.event
e.stopPropagation()
d3.selectAll("#"+did+" > div").style("z-index", "100");
let d = d3.select(this)
d.style("z-index", "1000");
//let coords = d3.mouse(this)
console.log(this)
//items.push({"drag":{"top":null,"left":null,"dragging":false},"resize":{"width":null,"height":null,"resizing":false},"responsive":{"valueW":0},"static":false,"resizable":true,"draggable":true,"min":{},"max":{},"x":Math.round(parseInt(coords[0])/width*w),"y":Math.round(parseInt(coords[1])/height*h),"w":1,"h":1,"id":"_igtfu"})
//return gridit(did, items, w, h, s)
}
function nitem(){
let e = d3.event
e.stopPropagation()
d3.selectAll("#"+did+" > div").style("z-index", "100");
let d = d3.select(this)
d.style("z-index", "1000");
//let coords = d3.mouse(this)
console.log(this)
//items.push({"drag":{"top":null,"left":null,"dragging":false},"resize":{"width":null,"height":null,"resizing":false},"responsive":{"valueW":0},"static":false,"resizable":true,"draggable":true,"min":{},"max":{},"x":Math.round(parseInt(coords[0])/width*w),"y":Math.round(parseInt(coords[1])/height*h),"w":1,"h":1,"id":"_igtfu"})
//return gridit(did, items, w, h, s)
}
}