I\'m developing an app in ng2 and I\'m struggling with something. I\'m building a calendar where you can pick a date-range and I need to react on click
&
Intro
I stumbled across this today, and I can really see the need for this implementation in a lot of applications. Now I can't vouch that this is 100% the best technique however I have gone out of my way to make this approach as angular inspired as possible.
The approach that I have come up with has 2 stages. Both stage 1 and stage 2 will add a total of years * months + years * months * days
, so for 1 year you will have 12 + 365
events.
Stage 1: Delegate events from when a month is clicked down into the actual day which was clicked without requiring an event on the day.
Stage 2: Propagate the chosen day back to the month.
Just before delving in, the application consists of 3 components which are nested in the following order: app => month => day
This is all the html that is required. app.component hosts a number of months, month.component hosts a number of days and day.component does nothing but display it's day as text.
app.component.html
<app-month *ngFor="let month of months" [data-month]="month"></app-month>
month.component.html
<app-day *ngFor="let day of days" [data-day]="day">{{day}}</app-day>
day.component.html
<ng-content></ng-content>
This is pretty stock standard stuff.
Stage 1
Let's have a look at the month.component.ts
where we want to delegate our event from.
// obtain a reference to the month(this) element
constructor(private element: ElementRef) { }
// when this component is clicked...
@HostListener('click', ['$event'])
public onMonthClick(event) {
// check to see whether the target element was a child or if it was in-fact this element
if (event.target != this.element.nativeElement) {
// if it was a child, then delegate our event to it.
// this is a little bit of javascript trickery where we are going to dispatch a custom event named 'delegateclick' on the target.
event.target.dispatchEvent(new CustomEvent('delegateEvent'));
}
}
In both stage 1 and 2, there is only 1 caveat and that is; if you have nested child elements within your day.component.html
, you will need to either implement bubbling for this, better logic in that if statement, or a quick hack would be.. in day.component.css
:host *{pointer-events: none;}
delegateEvent
event. So in day.component.ts
all you have to do (in the most angular way possible) is...
@HostListener('delegateEvent', ['$event'])
public onEvent() {
console.log("i've been clicked via a delegate!");
}
This works because typescript doesn't care about whether the event is native or not, it will just bind a new javascript event to the element and thus allows us to call it "natively" via event.target.dispatchEvent
as we do above in month.component.ts
.
That concludes Stage 1, we are now successfully delegating events from our month to our days.
So what happens if we say want to run a little bit of logic in our delegated event within day.component
and then return it to month.component
- so that it can then carry on with its own functionality in a very object oriented method? Well fortunately, we can very easily implement this!
In month.component.ts
update to the following. All that has changed is that we are now going to pass a function with our event invocation and we defined our callback function.
@HostListener('click', ['$event'])
public onMonthClick(event) {
if (event.target != this.element.nativeElement) {
event.target.dispatchEvent(new CustomEvent('delegateEvent', { detail: this.eventDelegateCallback}));
}
}
public eventDelegateCallback(data) {
console.log(data);
}
All that is left is to invoke this function within day.component.ts
...
public onEvent(event) {
// run whatever logic you like,
//return whatever data you like to month.component
event.detail(this.day);
}
Unfortunately our callback function is a little ambiguously named here, however typescript will complain about the property not being a defined object literal for CustomEventInit
if named otherwise.
The other cool thing about this approach is that you should never have to define more than this number of events because you can just funnel all events through this delegation and then run logic within day.component.ts
to filter by event.type
...
month.component.ts
@HostListener('click', ['$event'])
@HostListener('mouseover', ['$event'])
@HostListener('mouseout', ['$event'])
public onMonthEvent(event) {
if (event.target != this.element.nativeElement) {
event.target.dispatchEvent(new CustomEvent('delegateEvent', { detail: this.eventDelegateCallback }));
}
}
day.component.ts
private eventDelegateCallback: any;
@HostListener('delegateEvent', ['$event'])
public onEvent(event) {
this.eventDelegateCallback = event.detail;
if(event.type == "click"){
// run click stuff
this.eventDelegateCallback(this.day)
}
}