Skip to content

Commit

Permalink
feat(Events): Added support for vis events (#181)
Browse files Browse the repository at this point in the history
* Updated vis-timeline.service.ts

I've tried to update the vis timeline service to give the ability to use any of the events declared in the vis timeline documentation. Right now, through ngx-vis, you can only use the events specified in the VisTimelineService class, this is fine for common use cases but if you need to use other events (e.g. "mouseOver" or "drop") you'll have an hard time figuring out how to do so.
I already know that my implementation of the feature is not perfect because a new "EventEmitter" is created every time you call the "on" method but if you like the idea i could work on this a bit more and fix it by creating all the necessary event emitters beforehand.
The nice part is that the old implementation (using `visTimelineService.click.subscribe(() => { ... })`) will still work 😊.
I hope you'll consider this pull request.
Thanks in advice.

* Fixed string type
  • Loading branch information
lucaato authored and hypery2k committed Feb 26, 2019
1 parent f89dc10 commit 2bf0a1b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions components/timeline/vis-timeline.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export class VisTimelineService {
*/
public timechanged: EventEmitter<any> = new EventEmitter<any>();

private events: Map<string, EventEmitter<any>> = new Map();

private timelines: { [id: string]: VisTimeline } = {};

/**
Expand Down Expand Up @@ -679,11 +681,11 @@ export class VisTimelineService {
*/
public on(visTimeline: string, eventName: VisTimelineEvents, preventDefault?: boolean): boolean {
if (this.timelines[visTimeline]) {
/* tslint:disable */
this.events.set(eventName, new EventEmitter<any>());

const that: { [index: string]: any } = this;
/* tslint:enable */
this.timelines[visTimeline].on(eventName, (params: any) => {
const emitter = that[eventName] as EventEmitter<any>;
const emitter = (that[eventName] || that.events.get(eventName)) as EventEmitter<any>;
if (emitter) {
emitter.emit(params ? [visTimeline].concat(params) : visTimeline);
}
Expand All @@ -708,10 +710,20 @@ export class VisTimelineService {
*/
public off(visTimeline: string, eventName: VisTimelineEvents): void {
if (this.timelines[visTimeline]) {
this.events.delete(eventName);
this.timelines[visTimeline].off(eventName, undefined);
}
}

/**
* Get the event emitter associated with the specified event name.
* @param {VisTimelineEvents} eventName The event name.
* @returns {EventEmitter<any>} The event emitter of the specified event name.
*/
public getEmitter(eventName: VisTimelineEvents): EventEmitter<any> {
return this.events.get(eventName);
}

private doesNotExistError(visTimeline: string): string {
return `Timeline with id ${visTimeline} does not exist.`;
}
Expand Down

0 comments on commit 2bf0a1b

Please sign in to comment.