Make sure tap listener is added at most once. (#745)

* Make sure tap listener is added at most once.

* Add comment
This commit is contained in:
Andrey 2017-12-21 17:15:05 +02:00 committed by GitHub
parent c8c21e6fac
commit 6b180988fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -77,6 +77,13 @@ class HaEntitiesCard extends
}; };
} }
constructor() {
super();
// We need to save a single bound function reference to ensure the event listener
// can identify it properly.
this.entityTapped = this.entityTapped.bind(this);
}
computeTitle(states, groupEntity, localize) { computeTitle(states, groupEntity, localize) {
if (groupEntity) { if (groupEntity) {
return window.hassUtil.computeStateName(groupEntity).trim(); return window.hassUtil.computeStateName(groupEntity).trim();
@ -98,9 +105,13 @@ class HaEntitiesCard extends
} }
addTapEvents() { addTapEvents() {
const cards = this.root.querySelectorAll('.state.more-info'); const cards = this.root.querySelectorAll('.state');
cards.forEach((card) => { cards.forEach((card) => {
card.addEventListener('tap', this.entityTapped.bind(this)); if (card.classList.contains('more-info')) {
card.addEventListener('tap', this.entityTapped);
} else {
card.removeEventListener('tap', this.entityTapped);
}
}); });
} }