Skip to content

Commit cb9833c

Browse files
authored
Merge pull request #340 from DCMLab/fix_relation_hovering_error
Fix relation hovering error
2 parents 72b7b68 + 3e7cb38 commit cb9833c

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/js/action/draw.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
draw_slur,
2727
isSlurDownward,
2828
get_by_id,
29+
get_by_oldid,
2930
handleFlip
3031
} from '../utils/misc'
3132

@@ -466,6 +467,25 @@ export function draw_metarelation(draw_context, mei_graph, g_elem) {
466467
return added
467468
}
468469

470+
/**
471+
* Gets the element by old ID, and gets it by ID if not found
472+
*
473+
* @param {MEI_graph} mei_graph The graph to look the element into
474+
* @param {element} el The element to look for
475+
*
476+
* @return {MEI_node} The node in the MEI of the element
477+
*/
478+
function get_by_oldid_and_id(mei_graph, el) {
479+
480+
let meiNode = get_by_oldid(mei_graph, el.getAttribute('oldid'))
481+
if (!meiNode) {
482+
let id = el.id.slice(el.id.match(/^\d/))
483+
meiNode = get_by_id(mei_graph, id)
484+
}
485+
486+
return meiNode
487+
}
488+
469489
// Recursively add hover class to elements and their children
470490
function addHoverClassToChildren(element, isRoot, isPrimary, draw_context, mei_graph) {
471491
if (!element) return
@@ -475,8 +495,9 @@ function addHoverClassToChildren(element, isRoot, isPrimary, draw_context, mei_g
475495
// Add hover class to the meta-relation itself
476496
if (!isRoot) element.classList.add(isPrimary ? 'extrarelationhover' : 'relationhover')
477497

478-
// Get the corresponding MEI node using the oldid attribute
479-
let meiNode = get_by_id(mei_graph, element.getAttribute('oldid') || element.id)
498+
let meiNode = get_by_oldid_and_id(mei_graph, element)
499+
500+
if (!meiNode || !meiNode.length) return
480501

481502
// Recursively add hover class to the children
482503
let primaries = relation_primaries(mei_graph, meiNode).map(
@@ -504,9 +525,9 @@ function removeHoverClassToChildren(element, isRoot, isPrimary, draw_context, me
504525
// Remove hover class from the meta-relation itself
505526
if (!isRoot) element.classList.remove(isPrimary ? 'extrarelationhover' : 'relationhover')
506527

507-
// Get the corresponding MEI node using the oldid attribute
508-
// TODO: might be undefined and produce error
509-
let meiNode = get_by_id(mei_graph, element.getAttribute('oldid') || element.id)
528+
let meiNode = get_by_oldid_and_id(mei_graph, element)
529+
530+
if (!meiNode || !meiNode.length) return
510531

511532
// Recursively remove hover class from the children
512533
let primaries = relation_primaries(mei_graph, meiNode).map(

src/js/utils/misc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ export function note_coords(note) {
294294

295295
// Gets all elements from the doc with the oldid
296296
export function get_by_oldid(doc, id) {
297+
if (!id)
298+
return null
297299
if (id[0] == '#') { id = id.slice(1) }
298300
var elems = doc.querySelectorAll('[*|oldid=\'' + id + '\']')
299301
if (elems) {

0 commit comments

Comments
 (0)