From e61e1a477bb5562c25b6fc71d89d4be05e638b97 Mon Sep 17 00:00:00 2001 From: benct Date: Wed, 7 Oct 2020 16:32:47 +0200 Subject: [PATCH] Support navigation action on tap actions (#66, #82) --- README.md | 3 ++- multiple-entity-row.js | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d8fa539..e764eef 100644 --- a/README.md +++ b/README.md @@ -94,10 +94,11 @@ If `toggle` is set to `true` the default action is `toggle`, otherwise it is `mo | Name | Type | Default | Description | ---- | ---- | ------- | ----------- -| action | string | **Required** | Action to perform (`more-info`, `toggle`, `call-service`, `url`, `none`) +| action | string | **Required** | Action to perform (`more-info`, `toggle`, `call-service`, `url`, `navigate`, `none`) | service | string | | Service to call (e.g. `light.turn_on`) when `action` is `call-service` | service_data | object | | Optional data to include when `action` is `call-service` | url_path | string | | URL to open when `action` is `url` +| navigation_path | string | | Path to navigate to when `action` is `navigate` | confirmation | bool/string | `false` | Enable/set text to present in a confirmation dialog | entity | string | | A valid entity_id override when `action` is `more-info` diff --git a/multiple-entity-row.js b/multiple-entity-row.js index de2fb0f..8e8d603 100644 --- a/multiple-entity-row.js +++ b/multiple-entity-row.js @@ -288,7 +288,7 @@ } const confirmation = config.confirmation === true ? 'Are you sure?' : config.confirmation; if (config.action === 'call-service') { - const [domain, service] = config.service.split("."); + const [domain, service] = config.service.split('.'); return () => { if (!confirmation || confirm(confirmation)) { this._hass.callService(domain, service, config.service_data); @@ -311,26 +311,34 @@ } } } + if (config.action === 'navigate') { + return () => { + if (config.navigation_path && (!confirmation || confirm(confirmation))) { + history.pushState(null, '', config.navigation_path); + this.fireEvent(window, 'location-changed', {replace: false}); + } + } + } } return this.moreInfoAction(config, entityId); } moreInfoAction(config, entityId) { - return () => this.fireEvent('hass-more-info', (config && config.entity) || entityId); + return () => this.fireEvent(this, 'hass-more-info', {entityId: (config && config.entity) || entityId}); } - fireEvent(type, entity, options = {}) { + fireEvent(node, type, detail = {}, options = {}) { const event = new Event(type, { bubbles: options.bubbles || true, cancelable: options.cancelable || true, composed: options.composed || true, }); - event.detail = {entityId: entity}; - this.dispatchEvent(event); + event.detail = detail; + node.dispatchEvent(event); } forwardHaptic(type) { - const event = new Event("haptic", {bubbles: true, cancelable: false, composed: true}); + const event = new Event('haptic', {bubbles: true, cancelable: false, composed: true}); event.detail = type; this.dispatchEvent(event); }