Skip to content

Commit a23101a

Browse files
committed
Enable adding click handlers to pathway nodes
1 parent eeaa588 commit a23101a

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

src/js/ideogram.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,23 @@ export default class Ideogram {
359359
* @param {String} outerSelector DOM selector of container, e.g. "#my-diagram"
360360
* @param {Object} dimensions Height and width of pathway diagram
361361
* @param {Boolean} showClose Whether to show close button
362-
* @param {Function} nodeHoverFn Function to call upon hovering diagram node
362+
* @param {Function} geneNodeHoverFn Function to call upon hovering diagram node
363363
*/
364364
static drawPathway(
365365
pwId, sourceGene, destGene,
366366
outerSelector,
367367
dimensions={height: 440, width: 900},
368368
showClose=true,
369-
nodeHoverFn=undefined
369+
geneNodeHoverFn=undefined,
370+
pathwayNodeClickFn=undefined
370371
) {
371372
_drawPathway(
372373
pwId, sourceGene, destGene,
373374
outerSelector,
374375
dimensions=dimensions,
375376
showClose=showClose,
376-
nodeHoverFn=nodeHoverFn
377+
geneNodeHoverFn=geneNodeHoverFn,
378+
pathwayNodeClickFn=pathwayNodeClickFn
377379
);
378380
}
379381

src/js/kit/pathway-viewer.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ export async function drawPathway(
300300
outerSelector='#_ideogramOuterWrap',
301301
dimensions={height: 440, width: 900},
302302
showClose=true,
303-
nodeHoverFn,
303+
geneNodeHoverFn,
304+
pathwayNodeClickFn,
304305
retryAttempt=0
305306
) {
306307
const pvjsScript = document.querySelector(`script[src="${PVJS_URL}"]`);
@@ -321,7 +322,8 @@ export async function drawPathway(
321322
setTimeout(() => {
322323
drawPathway(
323324
pwId, sourceGene, destGene,
324-
outerSelector, dimensions, showClose, nodeHoverFn,
325+
outerSelector, dimensions, showClose,
326+
geneNodeHoverFn, pathwayNodeClickFn,
325327
retryAttempt++
326328
);
327329
}, 250);
@@ -401,20 +403,41 @@ export async function drawPathway(
401403
const ideogramPathwayEvent = new CustomEvent('ideogramDrawPathway', {detail});
402404
document.dispatchEvent(ideogramPathwayEvent);
403405

404-
pathwayContainer.querySelectorAll('g.GeneProduct').forEach(g => {
405-
const geneName = g.getAttribute('name');
406+
// Add mouseover handler to gene nodes in this pathway diagram
407+
pathwayContainer.querySelectorAll('g.GeneProduct').forEach(geneNode => {
408+
const geneName = geneNode.getAttribute('name');
406409
let tooltipContent = geneName;
407-
g.addEventListener('mouseover', (event) => {
408-
if (nodeHoverFn) {
409-
tooltipContent = nodeHoverFn(event, geneName);
410-
g.setAttribute('data-tippy-content', tooltipContent);
411-
tippy('g.GeneProduct[data-tippy-content]', tippyConfig);
410+
geneNode.addEventListener('mouseover', (event) => {
411+
if (geneNodeHoverFn) {
412+
tooltipContent = geneNodeHoverFn(event, geneName);
413+
geneNode.setAttribute('data-tippy-content', tooltipContent);
412414
}
413415
});
414416

415-
g.setAttribute(`data-tippy-content`, tooltipContent);
417+
geneNode.setAttribute(`data-tippy-content`, tooltipContent);
416418
});
417-
418419
const tippyConfig = getTippyConfig();
419420
tippy('g.GeneProduct[data-tippy-content]', tippyConfig);
421+
422+
// Add click handler to pathway nodes in this pathway diagram
423+
if (pathwayNodeClickFn) {
424+
pathwayContainer.querySelectorAll('g.Pathway').forEach(pathwayNode => {
425+
426+
// Add customizable click handler
427+
pathwayNode.addEventListener('click', (event) => {
428+
const domClasses = Array.from(pathwayNode.classList);
429+
const pathwayId = domClasses
430+
.find(c => c.startsWith('WikiPathways_'))
431+
.split('WikiPathways_')[1]; // e.g. WikiPathways_WP2815 -> WP2815
432+
433+
pathwayNodeClickFn(event, pathwayId);
434+
});
435+
436+
// Indicate this new pathway can be rendered on click
437+
const tooltipContent = 'Click to show pathway';
438+
pathwayNode.setAttribute('data-tippy-content', tooltipContent);
439+
});
440+
441+
tippy('g.Pathway[data-tippy-content]', tippyConfig);
442+
}
420443
}

src/js/kit/related-genes.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,12 +1657,20 @@ function addPathwayListeners(ideo) {
16571657
// const pathwayName = target.getAttribute('data-pathway-name');
16581658
// const pathway = {id: pathwayId, name: pathwayName};
16591659
// plotPathwayGenes(searchedGene, pathway, ideo);
1660-
function nodeHoverFn(event, geneName) {
1661-
console.log('in nodeHoverFn')
1660+
function geneNodeHoverFn(event, geneName) {
1661+
console.log('in geneNodeHoverFn')
16621662
return '<div>ok ' + geneName + '</div><div>1234</div>';
16631663
}
1664+
1665+
function pathwayNodeClickFn(event, pathwayId) {
1666+
const pathwayNode = event.target;
1667+
console.log('in pathwayNodeClickFn, pathwayNode', pathwayNode);
1668+
console.log('in pathwayNodeClickFn, pathwayId', pathwayId);
1669+
}
1670+
16641671
drawPathway(pathwayId, searchedGene, interactingGene,
1665-
undefined, undefined, undefined, nodeHoverFn);
1672+
undefined, undefined, undefined,
1673+
geneNodeHoverFn, pathwayNodeClickFn);
16661674
event.stopPropagation();
16671675
});
16681676
});

0 commit comments

Comments
 (0)