Skip to content

Commit

Permalink
refactor(#5): ♻️ Made a helper function to reduce redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
gautegf committed Oct 4, 2025
1 parent 4f44bf3 commit e1175ca
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/MeasurementControl/measurementsPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ window.initMeasurementsPanel = function initMeasurementsPanel(viewer) {
)
}

// Update visual active state in list and track lastSelection label
function updateActiveSelection(uuid) {
;[...listRoot.querySelectorAll('.m-row[data-uuid]')].forEach((el) => {
el.classList.toggle('active', el.dataset.uuid === uuid)
})
if (uuid) {
const labelEl = listRoot.querySelector(
`.m-row[data-uuid="${uuid}"] .m-row-label`
)
if (labelEl) {
lastSelection.uuid = uuid
lastSelection.label = labelEl.textContent.trim()
}
} else {
lastSelection.uuid = null
lastSelection.label = ''
}
}

// If on load there's already a selected measurement, move panel immediately
setTimeout(() => {
try {
Expand Down Expand Up @@ -393,16 +412,7 @@ window.initMeasurementsPanel = function initMeasurementsPanel(viewer) {
$.jstree.reference(node.id).select_node(node.id)
}
}
;[...listRoot.querySelectorAll('.m-row[data-uuid]')].forEach((el) => {
el.classList.toggle('active', el.dataset.uuid === uuid)
})
const thisRow = listRoot.querySelector(
`.m-row[data-uuid="${uuid}"] .m-row-label`
)
if (thisRow) {
lastSelection.uuid = uuid
lastSelection.label = thisRow.textContent.trim()
}
updateActiveSelection(uuid)
showPanelInMeasurements()
}
})
Expand All @@ -416,16 +426,7 @@ window.initMeasurementsPanel = function initMeasurementsPanel(viewer) {
const sel = tree.get_selected(true)[0]
if (sel && sel.data && sel.data.uuid) {
const uuid = sel.data.uuid
;[...listRoot.querySelectorAll('.m-row[data-uuid]')].forEach((el) => {
el.classList.toggle('active', el.dataset.uuid === uuid)
})
const activeRow = listRoot.querySelector(
`.m-row[data-uuid="${uuid}"] .m-row-label`
)
if (activeRow) {
lastSelection.uuid = uuid
lastSelection.label = activeRow.textContent.trim()
}
updateActiveSelection(uuid)
// Determine if selected node is a measurement-like object; if not, restore.
const isMeasurement =
sel && sel.data && isMeasurementUUID(sel.data.uuid)
Expand Down

0 comments on commit e1175ca

Please sign in to comment.