// this generates the 'editor' - used for both the static and the draggable version
function simpleEditor(d3Object) {
d3Object.append("span")
.text("content: ");
d3Object.append("span")
.text(" * edit me * ")
.attr("contenteditable", "true")
.style("border", "solid black 1pt")
.style("background", "lightblue")
.on("click", function() {
popupEditor.on(".drag", null)
})
.on("mouseout", function() {
popupEditor.call(editTaskdrag)
})
}
// arithmetic on strings with px at the end: addPx("110px", 23) = "133px"
function addPx(s, d) {
var number = parseInt(s, 10); // this really works even if the string is "120px"
number += d;
return number.toString() + "px";
}
// defined the drag behavior
var editTaskdrag = d3.behavior.drag()
.on("drag", function(d, i) {
var d3EditTaskObject = d3.select(this);
d3EditTaskObject.style("left", addPx(d3EditTaskObject.style("left"), d3.event.dx));
d3EditTaskObject.style("top", addPx(d3EditTaskObject.style("top"), d3.event.dy));
});
// generate the static editor
simpleEditor(d3.select("#staticEditor"))
// and now the draggable editor, hidden, but with drag behavior
var popupEditor = d3.select("#popupEditor");
simpleEditor(popupEditor);
popupEditor.attr("hidden", "true")
.call(editTaskdrag);
// click this button to get the draggable popup. However, it is not editable anymore
d3.select("#button")
.on("click", function() {
d3.select("#popupEditor")
.style("position", "fixed")
.style("left", "30%")
.style("top", "30%")
.style("background", "grey")
.style("padding", "1em")
.style("box-shadow", "0.5em 0.5em lightgrey")
.attr("hidden", null)
})
#button {
background-color: blue;
color: white;
border: solid black 1pt;
padding: 0.2em;
border-radius: 1em
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<body>
<p id="staticEditor">
</p>
<p id="popupEditor">
</p>
<p>
<span id="button">
Click here to open the pop up editor
</span>
</p>
</body>