|
2 | 2 | const markerDeleteButton = |
3 | 3 | '<button class="marker-delete-button btn btn-danger">Delete</button>'; |
4 | 4 |
|
5 | | - async function setupMarkerDeleteButton() { |
| 5 | + async function setupMarkerDeleteButtonForMarkersWall() { |
6 | 6 | document |
7 | 7 | .querySelectorAll("div.wall-item-container") |
8 | 8 | .forEach(function (node) { |
|
18 | 18 | var markerID = markerImg.split("/")[6]; |
19 | 19 |
|
20 | 20 | // Register click handler. |
21 | | - deleteButton.addEventListener("click", function (e) { |
22 | | - deleteMarker(markerID); |
| 21 | + deleteButton.addEventListener("click", async () => { |
| 22 | + await deleteMarker(markerID); |
| 23 | + window.location.reload(); |
23 | 24 | }); |
24 | 25 | }); |
25 | 26 | } |
26 | 27 |
|
| 28 | + async function setupMarkerDeleteButtonForScenePage() { |
| 29 | + const markerMap = new Map(); |
| 30 | + |
| 31 | + // Build a map of marker identifiers based on the preview videos. |
| 32 | + document |
| 33 | + .querySelectorAll("div.wall-item-container") |
| 34 | + .forEach(function (node) { |
| 35 | + const markerTag = node.querySelector(".wall-tag").innerText; |
| 36 | + const markerTime = node |
| 37 | + .querySelector(".wall-item-text div") |
| 38 | + .innerText.split(" - ")[1]; |
| 39 | + const markerImg = node |
| 40 | + .querySelector(".wall-item-media") |
| 41 | + .getAttribute("src"); |
| 42 | + const markerID = markerImg.split("/")[6]; |
| 43 | + |
| 44 | + // Use a combined key of tag and time to uniquely identify markers. |
| 45 | + const markerKey = `${markerTag}_${markerTime}`; |
| 46 | + markerMap.set(markerKey, markerID); |
| 47 | + }); |
| 48 | + |
| 49 | + // Now, add the delete button to the appropriate markers. |
| 50 | + document |
| 51 | + .querySelectorAll("div.primary-card-body .row") |
| 52 | + .forEach(function (node) { |
| 53 | + // Insert delete button. |
| 54 | + var deleteButton = document.createElement("button"); |
| 55 | + deleteButton.type = "button"; |
| 56 | + deleteButton.className = "btn btn-link ml-auto"; |
| 57 | + deleteButton.innerText = "Delete"; |
| 58 | + |
| 59 | + // Parse marker tag and time. |
| 60 | + const markerTag = node.querySelector(".btn.btn-link").innerText; |
| 61 | + const timeDiv = node.nextElementSibling; |
| 62 | + const markerTime = timeDiv ? timeDiv.innerText : null; |
| 63 | + |
| 64 | + // Generate the key to find the marker ID. |
| 65 | + const markerKey = `${markerTag}_${markerTime}`; |
| 66 | + const markerID = markerMap.get(markerKey); |
| 67 | + |
| 68 | + if (markerID) { |
| 69 | + // Insert the delete button next to the Edit button. |
| 70 | + var editButton = node.querySelector(".btn.btn-link.ml-auto"); |
| 71 | + if (editButton) { |
| 72 | + editButton.insertAdjacentElement("afterend", deleteButton); |
| 73 | + } |
| 74 | + |
| 75 | + // Register click handler with the correct marker ID. |
| 76 | + deleteButton.addEventListener("click", async (e) => { |
| 77 | + await deleteMarker(markerID); |
| 78 | + |
| 79 | + var markerContainer = deleteButton.parentElement.parentElement; |
| 80 | + var markersContainer = markerContainer.parentElement; |
| 81 | + var markerTagContainer = markersContainer.parentElement; |
| 82 | + |
| 83 | + // Remove the element for this marker. |
| 84 | + deleteButton.parentElement.parentElement.remove(); |
| 85 | + |
| 86 | + // If there are no more markers for this tag, remove the marker tag container. |
| 87 | + if (!markersContainer.hasChildNodes()) { |
| 88 | + markerTagContainer.remove(); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
27 | 95 | async function deleteMarker(markerID) { |
28 | 96 | const variables = { id: markerID }; |
29 | 97 | const query = `mutation SceneMarkerDestroy($id: ID!) {sceneMarkerDestroy(id: $id)}`; |
30 | | - await csLib.callGQL({ query, variables }).then(() => { |
31 | | - window.location.reload(); |
32 | | - }); |
| 98 | + await csLib.callGQL({ query, variables }); |
33 | 99 | } |
34 | 100 |
|
35 | 101 | // Wait for markers page to load. |
| 102 | + // PathElementListener is from cs-ui-lib.js |
36 | 103 | csLib.PathElementListener( |
37 | 104 | "/scenes/markers", |
38 | 105 | "div.wall", |
39 | | - setupMarkerDeleteButton |
40 | | - ); // PathElementListener is from cs-ui-lib.js |
| 106 | + setupMarkerDeleteButtonForMarkersWall |
| 107 | + ); |
| 108 | + |
| 109 | + csLib.PathElementListener( |
| 110 | + "/scenes/", |
| 111 | + "div.scene-markers-panel", |
| 112 | + setupMarkerDeleteButtonForScenePage |
| 113 | + ); |
41 | 114 | })(); |
0 commit comments