From 6b180988fd11d02a99670127d73250767cc9ae29 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 21 Dec 2017 17:15:05 +0200 Subject: [PATCH] Make sure tap listener is added at most once. (#745) * Make sure tap listener is added at most once. * Add comment --- src/cards/ha-entities-card.html | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cards/ha-entities-card.html b/src/cards/ha-entities-card.html index 129191c8ff..a8314665a0 100644 --- a/src/cards/ha-entities-card.html +++ b/src/cards/ha-entities-card.html @@ -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) { if (groupEntity) { return window.hassUtil.computeStateName(groupEntity).trim(); @@ -98,9 +105,13 @@ class HaEntitiesCard extends } addTapEvents() { - const cards = this.root.querySelectorAll('.state.more-info'); + const cards = this.root.querySelectorAll('.state'); 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); + } }); }