Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[markerDeleteButton] Delete buttons on Scene page next to Edit button #416

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 81 additions & 8 deletions plugins/markerDeleteButton/markerDeleteButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const markerDeleteButton =
'<button class="marker-delete-button btn btn-danger">Delete</button>';

async function setupMarkerDeleteButton() {
async function setupMarkerDeleteButtonForMarkersWall() {
document
.querySelectorAll("div.wall-item-container")
.forEach(function (node) {
Expand All @@ -18,24 +18,97 @@
var markerID = markerImg.split("/")[6];

// Register click handler.
deleteButton.addEventListener("click", function (e) {
deleteMarker(markerID);
deleteButton.addEventListener("click", async () => {
await deleteMarker(markerID);
window.location.reload();
});
});
}

async function setupMarkerDeleteButtonForScenePage() {
const markerMap = new Map();

// Build a map of marker identifiers based on the preview videos.
document
.querySelectorAll("div.wall-item-container")
.forEach(function (node) {
const markerTag = node.querySelector(".wall-tag").innerText;
const markerTime = node
.querySelector(".wall-item-text div")
.innerText.split(" - ")[1];
const markerImg = node
.querySelector(".wall-item-media")
.getAttribute("src");
const markerID = markerImg.split("/")[6];

// Use a combined key of tag and time to uniquely identify markers.
const markerKey = `${markerTag}_${markerTime}`;
markerMap.set(markerKey, markerID);
});

// Now, add the delete button to the appropriate markers.
document
.querySelectorAll("div.primary-card-body .row")
.forEach(function (node) {
// Insert delete button.
var deleteButton = document.createElement("button");
deleteButton.type = "button";
deleteButton.className = "btn btn-link ml-auto";
deleteButton.innerText = "Delete";

// Parse marker tag and time.
const markerTag = node.querySelector(".btn.btn-link").innerText;
const timeDiv = node.nextElementSibling;
const markerTime = timeDiv ? timeDiv.innerText : null;

// Generate the key to find the marker ID.
const markerKey = `${markerTag}_${markerTime}`;
const markerID = markerMap.get(markerKey);

if (markerID) {
// Insert the delete button next to the Edit button.
var editButton = node.querySelector(".btn.btn-link.ml-auto");
if (editButton) {
editButton.insertAdjacentElement("afterend", deleteButton);
}

// Register click handler with the correct marker ID.
deleteButton.addEventListener("click", async (e) => {
await deleteMarker(markerID);

var markerContainer = deleteButton.parentElement.parentElement;
var markersContainer = markerContainer.parentElement;
var markerTagContainer = markersContainer.parentElement;

// Remove the element for this marker.
deleteButton.parentElement.parentElement.remove();

// If there are no more markers for this tag, remove the marker tag container.
if (!markersContainer.hasChildNodes()) {
markerTagContainer.remove();
}
});
}
});
}

async function deleteMarker(markerID) {
const variables = { id: markerID };
const query = `mutation SceneMarkerDestroy($id: ID!) {sceneMarkerDestroy(id: $id)}`;
await csLib.callGQL({ query, variables }).then(() => {
window.location.reload();
});
await csLib.callGQL({ query, variables });
}

// Wait for markers page to load.
// PathElementListener is from cs-ui-lib.js
csLib.PathElementListener(
"/scenes/markers",
"div.wall",
setupMarkerDeleteButton
); // PathElementListener is from cs-ui-lib.js
setupMarkerDeleteButtonForMarkersWall
);

csLib.PathElementListener(
"/scenes/",
"div.scene-markers-panel",
setupMarkerDeleteButtonForScenePage
);
})();
4 changes: 2 additions & 2 deletions plugins/markerDeleteButton/markerDeleteButton.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Marker Delete Button
# requires: CommunityScriptsUILibrary
description: Adds a delete button to entries on the Markers page.
version: 0.1
description: Adds a delete button to entries on the Markers page and on the Scene page.
version: 0.2
ui:
requires:
- CommunityScriptsUILibrary
Expand Down