partridgejiang / Kekule.js

A Javascript cheminformatics toolkit.
http://partridgejiang.github.io/Kekule.js
MIT License
248 stars 61 forks source link

Interact with chemViewer objects/molecules #314

Open cleblond opened 9 months ago

cleblond commented 9 months ago

I want to hover over a molecule in a chemViewer, have it highlighted and then run some code based on the molecule hovered over. I have been playing with hot tracking event but it seems to focus on atoms and bonds, not molecules.

Kekule.X.Event.addListener(chemViewer, 'hotTrackOnObjects', function(e) {
console.log(e]); // if molecule A is selected run code });

Thank you for your help! Carl

partridgejiang commented 9 months ago

The molecule can be retrieved by accessing the parent property recursively on atom or bond, or by calling the getStandaloneAncestor() function e.g.:

chemViewer.addEventListener('hotTrackOnObjects', e => {
  let hotTrackedMolecules = [];
  e.objects.forEach(obj => {
    if (obj.getStandaloneAncestor)
      Kekule.ArrayUtils.pushUnique(hotTrackedMolecules, obj.getStandaloneAncestor());
  });
  chemViewer.setHotTrackedObjects(hotTrackedMolecules);
});
cleblond commented 9 months ago

getStandaloneAncestor did the trick. Thank you!