class QHCard extends HTMLElement {
static instances = []; // 用于存储所有卡片实例
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.isActive = false; // 记录当前卡片的状态
}
setConfig(config) {
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
const card = document.createElement('div');
card.style.backgroundColor = 'rgba(255, 255, 255, 1)';
card.style.width = '7vw';
card.style.height = '2.6vw';
card.style.borderRadius = '2vw';
card.style.display = 'flex';
card.style.justifyContent = 'center';
card.style.alignItems = 'center';
card.style.cursor = 'pointer';
card.style.transition = 'background-color 0.3s, color 0.3s';
const text = document.createElement('span');
text.innerText = config.name || 'Custom Button';
text.style.fontWeight = '800';
text.style.fontSize = '1.43vw';
text.style.color = 'rgba(1, 12, 24, 1)';
card.appendChild(text);
// 添加卡片点击事件
card.addEventListener('click', () => {
this.toggleCard(card, text);
});
root.appendChild(card);
QHCard.instances.push(this); // 将当前实例添加到静态数组中
}
toggleCard(card, text) {
this.isActive = !this.isActive; // 切换当前卡片的状态
if (this.isActive) {
card.style.backgroundColor = 'rgba(96, 96, 96, 0)';
text.style.color = 'rgba(255, 255, 255, 0.8)';
this.lockOtherCards(); // 锁定其他卡片
} else {
card.style.backgroundColor = 'rgba(255, 255, 255, 1)';
text.style.color = 'rgba(1, 12, 24, 1)';
}
}
lockOtherCards() {
QHCard.instances.forEach((instance) => {
if (instance !== this) {
const card = instance.shadowRoot.firstChild;
const text = card.querySelector('span');
card.style.backgroundColor = 'rgba(255, 255, 255, 1)';
text.style.color = 'rgba(1, 12, 24, 1)';
instance.isActive = false; // 重置其他卡片的状态
}
});
}
getCardSize() {
return 1;
}
}
customElements.define('qh-card', QHCard);
type: horizontal-stack
cards:
- type: custom:qh-card
name: Card 1
tap_action:
action: fire-dom-event
local_conditional_card:
action: set
ids:
- sun1: show
- sun2: hide
- type: custom:qh-card
name: Card 2
tap_action:
action: fire-dom-event
local_conditional_card:
action: set
ids:
- sun2: show
- sun1: hide
type: horizontal-stack
cards:
- type: custom:local-conditional-card
default: show
id: sun1
card:
entities:
- sun.sun
title: Sun 1
type: entities
- type: custom:local-conditional-card
default: hide
id: sun2
card:
entities:
- sun.sun
title: Sun 2
type: entities
Sure! Here’s the translation of your message into English:
Hello, I’m using AI ChatGPT to write an interlocking button card, but the following configuration is not working:
tap_action:
action: fire-dom-event
local_conditional_card:
action: set
ids:
- sun1: show
- sun2: hide
Do you know how to write this button card to fix the issue?
Let me know if you need further adjustments!