How to raise a click event for a vis.js Timeline (to get the result of a click when the clickToUse option is set to True)?

别来无恙 提交于 2020-06-17 12:40:35

问题


I have multiple timelines in a project that uses vis.js, generated by a PHP code.

There is an option for timelines, called clickToUse, if which is set to True, it causes the specific timeline to get selected when the user clicks on the timeline, causing a shadowed outline appearing around it.

I'm using an additional div header with title and various information regarding to the timeline below it.

I would like to have the timeline selected from code (causing to have the shadowed outline) when I click on the header div, so something that happens when the user clicks on the timeline.

How can this be done? Any help would be appreciated.


回答1:


Finally I've figured it out, this is my solution. First of all, I had to use stopPropagation, because basically vis.js deactivates a timeline when the user clicks anywhere else but on the timeline. So clicking on the header div would deactivate the timeline right after we activate it.

Looking in the vis.js code, I found that it uses an Activator object to handle activation/deactivation when clickToUse is set to true. If it is set to false, the Activator is not propagated in the Timeline object. The Activator object has an "activate" and "deactivate" function (and some others) that can be called (it's not written in the vis.js documentation).

So, I'm using the following code and it works:

document.getElementById('headerdiv').addEventListener('click', function() {
  event.stopPropagation();
  timeline.activator.activate();
});

If you call .activate(), it also deactivates the other timelines (if you have multiple), so no need to call any other functions, because everything is written in the activate() function: the css change, the emitters, the key binding.



来源:https://stackoverflow.com/questions/53252310/how-to-raise-a-click-event-for-a-vis-js-timeline-to-get-the-result-of-a-click-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!