2023-09-13 20:03:36 +00:00
|
|
|
// Map valid UUIDs used by draggable-item elements in order to better validate data transfers to ignore random data transfers.
|
|
|
|
const draggableItemUUIDs = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a data transfer object through its data types. Valid draggable-item events have one type of the format `application/json/${uuid}`.
|
|
|
|
* The function takes the entire type list from event.dataTransfer.types and returns true if the dataTransfer object is likely to be valid.
|
|
|
|
* @param {*} formatList from event.dataTransfer.types
|
|
|
|
* @returns {Boolean} true if dataTransfer is valid (from a draggable-item source on this page), false otherwise
|
|
|
|
*/
|
|
|
|
function getDragSource (typesList) {
|
|
|
|
if (typesList.length !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const typeString = typesList[0];
|
|
|
|
const type = typeString.split("/");
|
|
|
|
if (type.length === 3 && type[0] === "application" && type[1] === "json" && draggableItemUUIDs[type[2]]) {
|
|
|
|
return { type: typeString, uuid: type[2], element: draggableItemUUIDs[type[2]] };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:42:02 +00:00
|
|
|
class DraggableContainer extends HTMLElement {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.attachShadow({ mode: "open" });
|
|
|
|
this.shadowRoot.innerHTML = `
|
|
|
|
<label id="title"></label>
|
|
|
|
<div id="wrapper">
|
|
|
|
<draggable-item id="bottom" class="drop-target"></draggable-item>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
this.content = this.shadowRoot.querySelector("#wrapper");
|
|
|
|
this.bottom = this.shadowRoot.querySelector("#bottom");
|
|
|
|
this.titleElem = this.shadowRoot.querySelector("#title");
|
|
|
|
}
|
|
|
|
|
|
|
|
get title () {
|
|
|
|
return this.titleElem.innerText;
|
|
|
|
}
|
|
|
|
|
|
|
|
set title (title) {
|
|
|
|
this.titleElem.innerText = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
append (newNode) {
|
2023-09-13 20:03:36 +00:00
|
|
|
newNode.uuid = window.crypto.randomUUID();
|
|
|
|
draggableItemUUIDs[newNode.uuid] = newNode;
|
2023-08-14 23:42:02 +00:00
|
|
|
this.content.insertBefore(newNode, this.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
insertBefore (newNode, referenceNode) {
|
2023-09-13 20:03:36 +00:00
|
|
|
newNode.uuid = window.crypto.randomUUID();
|
|
|
|
draggableItemUUIDs[newNode.uuid] = newNode;
|
2023-08-14 23:42:02 +00:00
|
|
|
this.content.insertBefore(newNode, referenceNode);
|
|
|
|
}
|
2023-08-16 23:14:42 +00:00
|
|
|
|
2023-09-07 21:42:19 +00:00
|
|
|
querySelector (query) {
|
|
|
|
return this.content.querySelector(query);
|
2023-08-22 21:23:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 21:42:19 +00:00
|
|
|
removeChild (node) {
|
|
|
|
if (node && this.content.contains(node)) {
|
2023-09-13 20:03:36 +00:00
|
|
|
draggableItemUUIDs[node.uuid] = null;
|
2023-08-16 23:14:42 +00:00
|
|
|
this.content.removeChild(node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-08-19 03:32:52 +00:00
|
|
|
|
|
|
|
set value (value) {}
|
|
|
|
|
|
|
|
get value () {
|
|
|
|
const value = [];
|
|
|
|
this.content.childNodes.forEach((element) => {
|
|
|
|
if (element.value) {
|
|
|
|
value.push(element.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return value;
|
|
|
|
}
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DraggableItem extends HTMLElement {
|
2023-09-13 20:03:36 +00:00
|
|
|
uuid = null;
|
2023-08-14 23:42:02 +00:00
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.attachShadow({ mode: "open" });
|
2023-08-16 23:14:42 +00:00
|
|
|
// for whatever reason, only grid layout seems to respect the parent's content bounds
|
2023-08-14 23:42:02 +00:00
|
|
|
this.shadowRoot.innerHTML = `
|
|
|
|
<style>
|
2023-09-13 20:03:36 +00:00
|
|
|
#drag-over {
|
|
|
|
height: 1.5em;
|
|
|
|
border: 1px dotted var(--main-text-color);
|
|
|
|
border-radius: 5px;
|
|
|
|
background-color: rgba(0,0,0,0.25);
|
2023-08-19 03:32:52 +00:00
|
|
|
}
|
2023-08-16 23:14:42 +00:00
|
|
|
img {
|
|
|
|
height: 1em;
|
|
|
|
width: 1em;
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
</style>
|
2023-09-13 20:03:36 +00:00
|
|
|
<div id="drag-over" style="display: none;"></div>
|
2023-08-16 23:14:42 +00:00
|
|
|
<div id="wrapper">
|
|
|
|
<div style="min-height: 1.5em;"></div>
|
|
|
|
</div>
|
2023-08-14 23:42:02 +00:00
|
|
|
`;
|
|
|
|
this.content = this.shadowRoot.querySelector("#wrapper");
|
|
|
|
// add drag and drop listeners
|
|
|
|
this.addEventListener("dragstart", (event) => {
|
|
|
|
this.content.style.opacity = "0.5";
|
2023-08-29 21:26:57 +00:00
|
|
|
const data = { id: this.id, data: this.data, content: this.content.innerHTML, value: this.value };
|
2023-09-13 20:03:36 +00:00
|
|
|
event.dataTransfer.setData(`application/json/${this.uuid}`, JSON.stringify(data));
|
2023-08-14 23:42:02 +00:00
|
|
|
event.dataTransfer.effectAllowed = "move";
|
2023-09-13 20:03:36 +00:00
|
|
|
const blank = document.createElement("img");
|
|
|
|
event.dataTransfer.setDragImage(blank, 0, 0);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.content.style.visibility = "hidden";
|
|
|
|
this.content.style.height = "0";
|
|
|
|
}, 0);
|
2023-08-14 23:42:02 +00:00
|
|
|
});
|
|
|
|
this.addEventListener("dragend", (event) => {
|
|
|
|
if (event.dataTransfer.dropEffect === "move") {
|
|
|
|
this.parentElement.removeChild(this);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.content.attributeStyleMap.clear();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.addEventListener("dragenter", (event) => {
|
2023-09-13 20:03:36 +00:00
|
|
|
const sourceElement = getDragSource(event.dataTransfer.types);
|
|
|
|
if (event.target.dropTarget && sourceElement) {
|
|
|
|
event.target.dragOver = sourceElement.element.innerHTML;
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
this.addEventListener("dragleave", (event) => {
|
2023-09-13 20:03:36 +00:00
|
|
|
if (event.target.dragOver && getDragSource(event.dataTransfer.types)) {
|
2023-09-07 21:42:19 +00:00
|
|
|
event.target.dragOver = false;
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
this.addEventListener("dragover", (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
this.addEventListener("drop", (event) => {
|
2023-09-07 21:42:19 +00:00
|
|
|
if (event.target.dragOver) {
|
|
|
|
event.target.dragOver = false;
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
2023-09-13 20:03:36 +00:00
|
|
|
const sourceElement = getDragSource(event.dataTransfer.types);
|
|
|
|
if (event.target.dropTarget && sourceElement) {
|
|
|
|
const transfer = JSON.parse(event.dataTransfer.getData(sourceElement.type));
|
2023-08-14 23:42:02 +00:00
|
|
|
const item = document.createElement("draggable-item");
|
2023-08-22 21:23:14 +00:00
|
|
|
item.data = transfer.data;
|
|
|
|
item.innerHTML = transfer.content;
|
2023-08-14 23:42:02 +00:00
|
|
|
item.draggable = true;
|
2023-09-07 21:42:19 +00:00
|
|
|
item.dropTarget = true;
|
2023-08-29 21:26:57 +00:00
|
|
|
item.id = transfer.id;
|
2023-08-22 21:23:14 +00:00
|
|
|
item.value = transfer.data.value;
|
2023-09-13 20:03:36 +00:00
|
|
|
item.uuid = sourceElement.uuid;
|
2023-08-14 23:42:02 +00:00
|
|
|
event.target.parentElement.insertBefore(item, event.target);
|
|
|
|
}
|
|
|
|
this.content.attributeStyleMap.clear();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get innerHTML () {
|
|
|
|
return this.content.innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
set innerHTML (innerHTML) {
|
|
|
|
this.content.innerHTML = innerHTML;
|
|
|
|
}
|
|
|
|
|
2023-09-07 21:42:19 +00:00
|
|
|
get dropTarget () {
|
|
|
|
return this.classList.contains("drop-target");
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 21:42:19 +00:00
|
|
|
set dropTarget (dropTarget) {
|
|
|
|
if (dropTarget) {
|
|
|
|
this.classList.add("drop-target");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.classList.remove("drop-target");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get dragOver () {
|
|
|
|
return this.classList.contains("drag-over");
|
|
|
|
}
|
|
|
|
|
|
|
|
set dragOver (dragOver) {
|
|
|
|
if (dragOver) {
|
|
|
|
this.classList.add("drag-over");
|
2023-09-13 20:03:36 +00:00
|
|
|
this.shadowRoot.querySelector("#drag-over").style.display = "block";
|
|
|
|
this.shadowRoot.querySelector("#drag-over").innerHTML = dragOver;
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-09-07 21:42:19 +00:00
|
|
|
this.classList.remove("drag-over");
|
2023-09-13 20:03:36 +00:00
|
|
|
this.shadowRoot.querySelector("#drag-over").style.display = "none";
|
|
|
|
this.shadowRoot.querySelector("#drag-over").innerHTML = "";
|
2023-08-14 23:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("draggable-container", DraggableContainer);
|
|
|
|
customElements.define("draggable-item", DraggableItem);
|