[HOME] Панель: редактор границ комнат (тянуть вершины, копировать координаты) + правки по списку
lint / hygiene (push) Failing after 21s
lint / hygiene (push) Failing after 21s
This commit is contained in:
1 parent
99903819be
commit
623d8ea8ee
1 file changed
+311
-218
+311
-218
@@ -1,218 +1,311 @@
|
||||
/* Покомнатная панель: план рисуется по стенам (SVG), зоны = комнаты. */
|
||||
const ROOMS = [
|
||||
{ id: "terrasa", name: "Терраса", icon: "mdi:deck",
|
||||
poly: [[0.00, 0.00], [2.45, 0.00], [2.45, 8.60], [0.00, 8.60]] },
|
||||
{ id: "kitchen", name: "Кухня-гостиная", icon: "mdi:silverware-fork-knife",
|
||||
poly: [[2.45, 0.00], [6.21, 0.00], [6.21, 8.60], [2.45, 8.60]] },
|
||||
{ id: "postirochnaia", name: "Тех.пом.", icon: "mdi:server",
|
||||
poly: [[5.62, -1.50], [7.60, -1.50], [7.60, 0.00], [5.62, 0.00]] },
|
||||
{ id: "sanuzel", name: "Санузел", icon: "mdi:shower",
|
||||
poly: [[5.83, 0.00], [7.95, 0.00], [7.95, 2.97], [5.83, 2.97]] },
|
||||
{ id: "tualet", name: "Туалет", icon: "mdi:toilet",
|
||||
poly: [[7.95, 1.55], [9.00, 1.55], [9.00, 2.97], [7.95, 2.97]] },
|
||||
{ id: "garderob", name: "Гардероб", icon: "mdi:hanger",
|
||||
poly: [[9.00, 0.00], [11.33, 0.00], [11.33, 1.26], [9.00, 1.26]] },
|
||||
{ id: "kladovaia", name: "Кладовая", icon: "mdi:package-variant",
|
||||
poly: [[9.00, 1.26], [10.81, 1.26], [10.81, 2.40], [9.00, 2.40]] },
|
||||
{ id: "koridor", name: "Холл", icon: "mdi:door-open",
|
||||
poly: [[6.21, 2.97], [11.33, 2.97], [11.33, 3.41], [14.52, 3.41],
|
||||
[14.52, 4.30], [11.33, 4.30], [11.33, 5.29], [6.21, 5.29]] },
|
||||
{ id: "prihozhaya", name: "Прихожая", icon: "mdi:door",
|
||||
poly: [[6.21, 5.29], [7.74, 5.29], [7.74, 7.35], [6.21, 7.35]] },
|
||||
{ id: "tambur", name: "Тамбур", icon: "mdi:door",
|
||||
poly: [[6.21, 7.35], [7.74, 7.35], [7.74, 8.60], [6.21, 8.60]] },
|
||||
{ id: "kabinet", name: "Комната 11,45", icon: "mdi:desk",
|
||||
poly: [[11.33, 0.00], [14.52, 0.00], [14.52, 3.41], [11.33, 3.41]] },
|
||||
{ id: "danina_komnata", name: "Комната 12,04", icon: "mdi:teddy-bear",
|
||||
poly: [[7.74, 5.29], [11.33, 5.29], [11.33, 8.60], [7.74, 8.60]] },
|
||||
{ id: "spalnia", name: "Комната 14,34", icon: "mdi:bed",
|
||||
poly: [[11.33, 4.30], [14.52, 4.30], [14.52, 8.60], [11.33, 8.60]] },
|
||||
{ id: "kryltso", name: "Крыльцо", icon: "mdi:stairs",
|
||||
poly: [[6.21, 8.60], [7.74, 8.60], [7.74, 10.10], [6.21, 10.10]] },
|
||||
];
|
||||
const KEY_DOMAINS = ["light", "switch", "climate", "media_player", "cover", "fan"];
|
||||
const SENSOR_HINT = /temperature|humidity|co2|illuminance/;
|
||||
/* рамка плана в метрах */
|
||||
const VB = [-0.4, -1.9, 15.0, 10.5];
|
||||
|
||||
class RoomsCard extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this._areas = [];
|
||||
this._ent2area = {};
|
||||
this._selected = null;
|
||||
this._loaded = false;
|
||||
this._mode = "plan";
|
||||
this.attachShadow({ mode: "open" });
|
||||
}
|
||||
|
||||
setConfig(config) { this._config = config || {}; }
|
||||
|
||||
set hass(hass) {
|
||||
this._hass = hass;
|
||||
if (!this._loaded) { this._loaded = true; this._loadRegistries(); }
|
||||
else this._render();
|
||||
}
|
||||
|
||||
async _loadRegistries() {
|
||||
try {
|
||||
const [areas, devices, entities] = await Promise.all([
|
||||
this._hass.callWS({ type: "config/area_registry/list" }),
|
||||
this._hass.callWS({ type: "config/device_registry/list" }),
|
||||
this._hass.callWS({ type: "config/entity_registry/list" }),
|
||||
]);
|
||||
this._areas = areas;
|
||||
const devArea = {};
|
||||
for (const d of devices) if (d.area_id) devArea[d.id] = d.area_id;
|
||||
this._ent2area = {};
|
||||
for (const e of entities) {
|
||||
const a = e.area_id || devArea[e.device_id];
|
||||
if (a) this._ent2area[e.entity_id] = a;
|
||||
}
|
||||
} catch (err) { this._error = String(err); }
|
||||
this._render();
|
||||
}
|
||||
|
||||
_entitiesOf(areaId) {
|
||||
if (!areaId) return [];
|
||||
return Object.entries(this._ent2area).filter(([, a]) => a === areaId)
|
||||
.map(([e]) => e).filter((e) => this._hass.states[e]);
|
||||
}
|
||||
|
||||
_domain(e) { return e.split(".")[0]; }
|
||||
|
||||
_on(areaId) {
|
||||
return this._entitiesOf(areaId).filter((e) => {
|
||||
const st = this._hass.states[e];
|
||||
return (this._domain(e) === "light" || this._domain(e) === "switch") && st.state === "on";
|
||||
}).length;
|
||||
}
|
||||
|
||||
_sensors(areaId) {
|
||||
const out = [];
|
||||
for (const e of this._entitiesOf(areaId)) {
|
||||
if (this._domain(e) !== "sensor" || !SENSOR_HINT.test(e)) continue;
|
||||
const st = this._hass.states[e];
|
||||
out.push(`${(st.attributes.friendly_name || e).split(" ").slice(-1)[0]}: ${st.state}${st.attributes.unit_of_measurement || ""}`);
|
||||
}
|
||||
return out.slice(0, 2);
|
||||
}
|
||||
|
||||
_toggle(e) {
|
||||
const d = this._domain(e);
|
||||
if (d === "light" || d === "switch") this._hass.callService(d, "toggle", { entity_id: e });
|
||||
}
|
||||
|
||||
_planSvg(areaId) {
|
||||
const [vx, vy, vw, vh] = VB;
|
||||
let shapes = "";
|
||||
let labels = "";
|
||||
for (const r of ROOMS) {
|
||||
const pts = r.poly.map(([x, y]) => `${x},${y}`).join(" ");
|
||||
const sel = r.id === areaId;
|
||||
const on = this._on(r.id);
|
||||
shapes += `<polygon class="zone ${sel ? "sel" : ""}" data-a="${r.id}" points="${pts}"><title>${r.name}</title></polygon>`;
|
||||
const cx = r.poly.reduce((s, p) => s + p[0], 0) / r.poly.length;
|
||||
const cy = r.poly.reduce((s, p) => s + p[1], 0) / r.poly.length;
|
||||
labels += `<text class="lbl" x="${cx}" y="${cy}" text-anchor="middle">${r.name}${on ? " · " + on : ""}</text>`;
|
||||
}
|
||||
return `<svg class="plan" viewBox="${vx} ${vy} ${vw} ${vh}">
|
||||
<rect x="${vx}" y="${vy}" width="${vw}" height="${vh}" fill="none"/>
|
||||
${shapes}${labels}</svg>`;
|
||||
}
|
||||
|
||||
_render() {
|
||||
if (!this._hass) return;
|
||||
const areaId = this._selected;
|
||||
const tiles = ROOMS.map((r) => {
|
||||
const ens = this._entitiesOf(r.id);
|
||||
const on = this._on(r.id);
|
||||
const sensors = this._sensors(r.id).join(" · ");
|
||||
return `<button class="tile ${r.id === areaId ? "sel" : ""}" data-a="${r.id}">
|
||||
<div class="tile-top"><ha-icon icon="${r.icon}"></ha-icon><span class="cnt">${ens.length}</span></div>
|
||||
<div class="tile-name">${r.name}</div>
|
||||
${on ? `<div class="tile-meta">💡 ${on}</div>` : ""}
|
||||
${sensors ? `<div class="tile-sens">${sensors}</div>` : ""}</button>`;
|
||||
}).join("");
|
||||
|
||||
let detail = "";
|
||||
if (areaId) {
|
||||
const room = ROOMS.find((r) => r.id === areaId);
|
||||
const ens = this._entitiesOf(areaId);
|
||||
const groups = {};
|
||||
for (const e of ens) (groups[this._domain(e)] ||= []).push(e);
|
||||
const rows = Object.keys(groups).sort((a, b) => {
|
||||
const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 60 + a.charCodeAt(0);
|
||||
const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 60 + b.charCodeAt(0);
|
||||
return ia - ib;
|
||||
}).map((dom) => {
|
||||
const items = groups[dom].map((e) => {
|
||||
const st = this._hass.states[e];
|
||||
const nm = st.attributes.friendly_name || e;
|
||||
const val = st.state + (st.attributes.unit_of_measurement || "");
|
||||
const act = ["light", "switch"].includes(dom)
|
||||
? `<span class="sw ${st.state === "on" ? "on" : ""}" data-e="${e}">${st.state === "on" ? "вкл" : "выкл"}</span>` : "";
|
||||
return `<div class="row"><span class="nm">${nm}</span><span class="vl">${val}</span>${act}</div>`;
|
||||
}).join("");
|
||||
return `<div class="grp"><div class="grp-h">${dom} · ${groups[dom].length}</div>${items}</div>`;
|
||||
}).join("");
|
||||
detail = `<div class="detail"><div class="d-head">${room ? room.name : areaId}
|
||||
<button class="close" data-close="1">×</button></div>${rows || "<div class='row'>устройств пока нет</div>"}</div>`;
|
||||
}
|
||||
|
||||
this.shadowRoot.innerHTML = `<style>
|
||||
:host { display:block; font-family: var(--paper-font-body1_-_font-family, system-ui); }
|
||||
.bar { display:flex; gap:6px; margin-bottom:8px; }
|
||||
.bar button { background:rgba(30,36,44,.9); color:#cfe0f0; border:1px solid #39424e;
|
||||
border-radius:9px; padding:4px 10px; font-size:12px; cursor:pointer; }
|
||||
.bar button.on { border-color:#4aa3ff; color:#dceaff; background:rgba(74,163,255,.16); }
|
||||
.plan { display:block; width:100%; height:auto; background:#f4f3ee; border-radius:12px; }
|
||||
.zone { fill:#e9e4d8; stroke:#2b2b2b; stroke-width:0.06; cursor:pointer; transition:.12s; }
|
||||
.zone:hover { fill:#bcd6f5; stroke:#2f7fe0; }
|
||||
.zone.sel { fill:#9ec4f0; stroke:#1f6fd0; stroke-width:0.09; }
|
||||
.lbl { font-size:0.30px; fill:#2a2a2a; pointer-events:none; }
|
||||
.grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap:10px; }
|
||||
.tile { text-align:left; border:1px solid #2a2f36; border-radius:14px;
|
||||
background: linear-gradient(160deg, rgba(30,36,44,.95), rgba(18,22,27,.95));
|
||||
color: #e8eef5; padding:12px; cursor:pointer; }
|
||||
.tile:hover { border-color:#4aa3ff; }
|
||||
.tile.sel { border-color:#4aa3ff; box-shadow:0 0 0 1px #4aa3ff inset; }
|
||||
.tile-top { display:flex; justify-content:space-between; align-items:center; opacity:.85; }
|
||||
.cnt { font-size:11px; padding:1px 7px; border-radius:9px; background:rgba(74,163,255,.18); color:#8fc4ff; }
|
||||
.tile-name { font-weight:600; margin-top:6px; font-size:14px; }
|
||||
.tile-meta { font-size:11px; opacity:.7; margin-top:2px; }
|
||||
.tile-sens { font-size:11px; opacity:.85; margin-top:6px; color:#9ed3ff; }
|
||||
.detail { border:1px solid #2a2f36; border-radius:14px; padding:12px; margin-top:10px;
|
||||
background: rgba(14,17,21,.92); max-height:420px; overflow:auto; }
|
||||
.d-head { font-weight:700; margin-bottom:8px; display:flex; justify-content:space-between; }
|
||||
.close { background:none;border:none;color:#9aa7b4;font-size:18px;cursor:pointer; }
|
||||
.grp-h { font-size:11px; text-transform:uppercase; letter-spacing:.06em; color:#7d8b99; margin:10px 0 4px; }
|
||||
.row { display:flex; gap:8px; align-items:center; font-size:12px; padding:3px 0;
|
||||
border-bottom:1px dashed rgba(255,255,255,.05); }
|
||||
.nm { flex:1; opacity:.9; } .vl { opacity:.65; }
|
||||
.sw { font-size:11px; padding:1px 8px; border-radius:8px; border:1px solid #3a4350; cursor:pointer; }
|
||||
.sw.on { background:rgba(74,163,255,.2); border-color:#4aa3ff; color:#bcdcff; }
|
||||
</style>
|
||||
<div class="bar">
|
||||
<button class="${this._mode === "plan" ? "on" : ""}" data-mode="plan">План</button>
|
||||
<button class="${this._mode === "tiles" ? "on" : ""}" data-mode="tiles">Плитки</button>
|
||||
</div>
|
||||
${this._mode === "plan" ? this._planSvg(areaId) : `<div class="grid">${tiles}</div>`}
|
||||
${detail}`;
|
||||
|
||||
this.shadowRoot.querySelectorAll("[data-mode]").forEach((b) =>
|
||||
b.addEventListener("click", () => { this._mode = b.dataset.mode; this._render(); }));
|
||||
this.shadowRoot.querySelectorAll(".zone, .tile").forEach((el) =>
|
||||
el.addEventListener("click", () => {
|
||||
this._selected = this._selected === el.dataset.a ? null : el.dataset.a; this._render();
|
||||
}));
|
||||
this.shadowRoot.querySelectorAll(".sw").forEach((s) =>
|
||||
s.addEventListener("click", (ev) => { ev.stopPropagation(); this._toggle(s.dataset.e); }));
|
||||
const close = this.shadowRoot.querySelector(".close");
|
||||
if (close) close.addEventListener("click", () => { this._selected = null; this._render(); });
|
||||
}
|
||||
|
||||
getCardSize() { return 12; }
|
||||
}
|
||||
|
||||
customElements.define("rooms-card", RoomsCard);
|
||||
/* Покомнатная панель с редактором границ: тянете вершины — получаете точный план. */
|
||||
const ROOMS = [
|
||||
{ id: "terrasa", name: "Терраса", icon: "mdi:deck",
|
||||
poly: [[0.00, 0.00], [2.45, 0.00], [2.45, 8.60], [0.00, 8.60]] },
|
||||
{ id: "kitchen", name: "Кухня-гостиная", icon: "mdi:silverware-fork-knife",
|
||||
poly: [[2.45, 0.00], [5.60, 0.00], [5.60, 4.30], [6.21, 4.30],
|
||||
[6.21, 8.60], [2.45, 8.60]] },
|
||||
{ id: "postirochnaia", name: "Тех.пом.", icon: "mdi:server",
|
||||
poly: [[4.90, -1.50], [7.40, -1.50], [7.40, 0.00], [4.90, 0.00]] },
|
||||
{ id: "sanuzel", name: "Санузел", icon: "mdi:shower",
|
||||
poly: [[5.83, 0.00], [7.95, 0.00], [7.95, 1.55], [7.00, 1.55],
|
||||
[7.00, 2.97], [5.83, 2.97]] },
|
||||
{ id: "tualet", name: "Туалет", icon: "mdi:toilet",
|
||||
poly: [[8.00, 2.20], [9.20, 2.20], [9.20, 3.30], [8.00, 3.30]] },
|
||||
{ id: "garderob", name: "Гардероб", icon: "mdi:hanger",
|
||||
poly: [[9.30, 0.00], [11.33, 0.00], [11.33, 1.30], [9.30, 1.30]] },
|
||||
{ id: "kladovaia", name: "Кладовая", icon: "mdi:package-variant",
|
||||
poly: [[9.30, 1.30], [10.81, 1.30], [10.81, 2.40], [9.30, 2.40]] },
|
||||
{ id: "koridor", name: "Холл", icon: "mdi:door-open",
|
||||
poly: [[7.60, 2.97], [11.33, 2.97], [11.33, 5.29], [7.60, 5.29]] },
|
||||
{ id: "prihozhaya", name: "Прихожая", icon: "mdi:door",
|
||||
poly: [[5.95, 5.10], [8.00, 5.10], [8.00, 7.20], [5.95, 7.20]] },
|
||||
{ id: "tambur", name: "Тамбур", icon: "mdi:door",
|
||||
poly: [[5.95, 7.20], [8.00, 7.20], [8.00, 8.60], [5.95, 8.60]] },
|
||||
{ id: "kabinet", name: "Комната 11,45", icon: "mdi:desk",
|
||||
poly: [[11.60, 0.00], [14.52, 0.00], [14.52, 4.30], [11.60, 4.30]] },
|
||||
{ id: "danina_komnata", name: "Комната 12,04", icon: "mdi:teddy-bear",
|
||||
poly: [[7.30, 5.29], [11.33, 5.29], [11.33, 8.60], [7.30, 8.60]] },
|
||||
{ id: "spalnia", name: "Комната 14,34", icon: "mdi:bed",
|
||||
poly: [[11.33, 4.30], [14.52, 4.30], [14.52, 8.60], [11.33, 8.60]] },
|
||||
{ id: "kryltso", name: "Крыльцо", icon: "mdi:stairs",
|
||||
poly: [[5.95, 8.60], [8.20, 8.60], [8.20, 10.10], [5.95, 10.10]] },
|
||||
];
|
||||
const KEY_DOMAINS = ["light", "switch", "climate", "media_player", "cover", "fan"];
|
||||
const SENSOR_HINT = /temperature|humidity|co2|illuminance/;
|
||||
const VB = [-0.4, -1.9, 15.0, 10.5];
|
||||
const STORE = "house3d_room_polys";
|
||||
|
||||
class RoomsCard extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this._areas = [];
|
||||
this._ent2area = {};
|
||||
this._selected = null;
|
||||
this._loaded = false;
|
||||
this._mode = "plan";
|
||||
this._edit = false;
|
||||
this._polys = JSON.parse(JSON.stringify(ROOMS.map((r) => r.poly)));
|
||||
try {
|
||||
const saved = localStorage.getItem(STORE);
|
||||
if (saved) {
|
||||
const arr = JSON.parse(saved);
|
||||
if (Array.isArray(arr) && arr.length === ROOMS.length) this._polys = arr;
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
this.attachShadow({ mode: "open" });
|
||||
}
|
||||
|
||||
setConfig(config) { this._config = config || {}; }
|
||||
|
||||
set hass(hass) {
|
||||
this._hass = hass;
|
||||
if (!this._loaded) { this._loaded = true; this._loadRegistries(); }
|
||||
else if (!this._dragging) this._render();
|
||||
}
|
||||
|
||||
async _loadRegistries() {
|
||||
try {
|
||||
const [areas, devices, entities] = await Promise.all([
|
||||
this._hass.callWS({ type: "config/area_registry/list" }),
|
||||
this._hass.callWS({ type: "config/device_registry/list" }),
|
||||
this._hass.callWS({ type: "config/entity_registry/list" }),
|
||||
]);
|
||||
this._areas = areas;
|
||||
const devArea = {};
|
||||
for (const d of devices) if (d.area_id) devArea[d.id] = d.area_id;
|
||||
this._ent2area = {};
|
||||
for (const e of entities) {
|
||||
const a = e.area_id || devArea[e.device_id];
|
||||
if (a) this._ent2area[e.entity_id] = a;
|
||||
}
|
||||
} catch (err) { this._error = String(err); }
|
||||
this._render();
|
||||
}
|
||||
|
||||
_entitiesOf(areaId) {
|
||||
if (!areaId) return [];
|
||||
return Object.entries(this._ent2area).filter(([, a]) => a === areaId)
|
||||
.map(([e]) => e).filter((e) => this._hass.states[e]);
|
||||
}
|
||||
|
||||
_domain(e) { return e.split(".")[0]; }
|
||||
_on(id) {
|
||||
return this._entitiesOf(id).filter((e) => {
|
||||
const st = this._hass.states[e];
|
||||
return (this._domain(e) === "light" || this._domain(e) === "switch") && st.state === "on";
|
||||
}).length;
|
||||
}
|
||||
_sensors(id) {
|
||||
const out = [];
|
||||
for (const e of this._entitiesOf(id)) {
|
||||
if (this._domain(e) !== "sensor" || !SENSOR_HINT.test(e)) continue;
|
||||
const st = this._hass.states[e];
|
||||
out.push(`${(st.attributes.friendly_name || e).split(" ").slice(-1)[0]}: ${st.state}${st.attributes.unit_of_measurement || ""}`);
|
||||
}
|
||||
return out.slice(0, 2);
|
||||
}
|
||||
_toggle(e) {
|
||||
const d = this._domain(e);
|
||||
if (d === "light" || d === "switch") this._hass.callService(d, "toggle", { entity_id: e });
|
||||
}
|
||||
|
||||
_planSvg(areaId) {
|
||||
const [vx, vy, vw, vh] = VB;
|
||||
let shapes = "", labels = "", handles = "";
|
||||
ROOMS.forEach((r, ri) => {
|
||||
const poly = this._polys[ri];
|
||||
const pts = poly.map(([x, y]) => `${x},${y}`).join(" ");
|
||||
const sel = r.id === areaId;
|
||||
shapes += `<polygon class="zone ${sel ? "sel" : ""} ${this._edit ? "edit" : ""}" data-a="${r.id}" points="${pts}"><title>${r.name}</title></polygon>`;
|
||||
const cx = poly.reduce((s, p) => s + p[0], 0) / poly.length;
|
||||
const cy = poly.reduce((s, p) => s + p[1], 0) / poly.length;
|
||||
labels += `<text class="lbl" x="${cx}" y="${cy}" text-anchor="middle">${r.name}${this._on(r.id) ? " · " + this._on(r.id) : ""}</text>`;
|
||||
if (this._edit) {
|
||||
poly.forEach(([x, y], vi) => {
|
||||
handles += `<circle class="hnd" data-r="${ri}" data-v="${vi}" cx="${x}" cy="${y}" r="0.12"/>`;
|
||||
});
|
||||
}
|
||||
});
|
||||
return `<svg class="plan" viewBox="${vx} ${vy} ${vw} ${vh}">${shapes}${labels}${handles}</svg>`;
|
||||
}
|
||||
|
||||
_bindEditor() {
|
||||
const svg = this.shadowRoot.querySelector("svg.plan");
|
||||
if (!svg) return;
|
||||
const toM = (ev) => {
|
||||
const pt = svg.createSVGPoint();
|
||||
pt.x = ev.clientX; pt.y = ev.clientY;
|
||||
const loc = pt.matrixTransform(svg.getScreenCTM().inverse());
|
||||
return [Math.round(loc.x * 20) / 20, Math.round(loc.y * 20) / 20];
|
||||
};
|
||||
svg.querySelectorAll(".hnd").forEach((h) => {
|
||||
h.addEventListener("pointerdown", (ev) => {
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
this._dragging = true;
|
||||
const ri = +h.dataset.r, vi = +h.dataset.v;
|
||||
h.setPointerCapture(ev.pointerId);
|
||||
const move = (e) => {
|
||||
const [x, y] = toM(e);
|
||||
this._polys[ri][vi] = [x, y];
|
||||
const pg = svg.querySelectorAll(".zone")[ri];
|
||||
if (pg) pg.setAttribute("points", this._polys[ri].map(([a, b]) => `${a},${b}`).join(" "));
|
||||
h.setAttribute("cx", x);
|
||||
h.setAttribute("cy", y);
|
||||
};
|
||||
const up = () => {
|
||||
this._dragging = false;
|
||||
try { h.releasePointerCapture(ev.pointerId); } catch (e) { /* ignore */ }
|
||||
h.removeEventListener("pointermove", move);
|
||||
h.removeEventListener("pointerup", up);
|
||||
this._save();
|
||||
};
|
||||
h.addEventListener("pointermove", move);
|
||||
h.addEventListener("pointerup", up);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_save() {
|
||||
try { localStorage.setItem(STORE, JSON.stringify(this._polys)); } catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
_json() {
|
||||
const out = {};
|
||||
ROOMS.forEach((r, i) => { out[r.id] = this._polys[i].map(([x, y]) => [x, y]); });
|
||||
return JSON.stringify(out);
|
||||
}
|
||||
|
||||
_render() {
|
||||
if (!this._hass) return;
|
||||
const areaId = this._selected;
|
||||
const tiles = ROOMS.map((r) => {
|
||||
const ens = this._entitiesOf(r.id);
|
||||
const on = this._on(r.id);
|
||||
const sensors = this._sensors(r.id).join(" · ");
|
||||
return `<button class="tile ${r.id === areaId ? "sel" : ""}" data-a="${r.id}">
|
||||
<div class="tile-top"><ha-icon icon="${r.icon}"></ha-icon><span class="cnt">${ens.length}</span></div>
|
||||
<div class="tile-name">${r.name}</div>
|
||||
${on ? `<div class="tile-meta">💡 ${on}</div>` : ""}
|
||||
${sensors ? `<div class="tile-sens">${sensors}</div>` : ""}</button>`;
|
||||
}).join("");
|
||||
|
||||
let detail = "";
|
||||
if (areaId) {
|
||||
const room = ROOMS.find((r) => r.id === areaId);
|
||||
const ens = this._entitiesOf(areaId);
|
||||
const groups = {};
|
||||
for (const e of ens) (groups[this._domain(e)] ||= []).push(e);
|
||||
const rows = Object.keys(groups).sort((a, b) => {
|
||||
const ia = KEY_DOMAINS.includes(a) ? KEY_DOMAINS.indexOf(a) : 60 + a.charCodeAt(0);
|
||||
const ib = KEY_DOMAINS.includes(b) ? KEY_DOMAINS.indexOf(b) : 60 + b.charCodeAt(0);
|
||||
return ia - ib;
|
||||
}).map((dom) => {
|
||||
const items = groups[dom].map((e) => {
|
||||
const st = this._hass.states[e];
|
||||
const nm = st.attributes.friendly_name || e;
|
||||
const val = st.state + (st.attributes.unit_of_measurement || "");
|
||||
const act = ["light", "switch"].includes(dom)
|
||||
? `<span class="sw ${st.state === "on" ? "on" : ""}" data-e="${e}">${st.state === "on" ? "вкл" : "выкл"}</span>` : "";
|
||||
return `<div class="row"><span class="nm">${nm}</span><span class="vl">${val}</span>${act}</div>`;
|
||||
}).join("");
|
||||
return `<div class="grp"><div class="grp-h">${dom} · ${groups[dom].length}</div>${items}</div>`;
|
||||
}).join("");
|
||||
detail = `<div class="detail"><div class="d-head">${room ? room.name : areaId}
|
||||
<button class="close" data-close="1">×</button></div>${rows || "<div class='row'>устройств пока нет</div>"}</div>`;
|
||||
}
|
||||
|
||||
const editorBar = this._edit ? `<div class="editbar">
|
||||
<button data-act="copy">Скопировать координаты</button>
|
||||
<button data-act="reset">Сбросить правки</button>
|
||||
<span class="hint">тяните оранжевые точки по углам комнат — сохраняется автоматически</span>
|
||||
</div>` : "";
|
||||
|
||||
this.shadowRoot.innerHTML = `<style>
|
||||
:host { display:block; font-family: var(--paper-font-body1_-_font-family, system-ui); }
|
||||
.bar { display:flex; gap:6px; margin-bottom:8px; flex-wrap:wrap; }
|
||||
.bar button { background:rgba(30,36,44,.9); color:#cfe0f0; border:1px solid #39424e;
|
||||
border-radius:9px; padding:4px 10px; font-size:12px; cursor:pointer; }
|
||||
.bar button.on { border-color:#4aa3ff; color:#dceaff; background:rgba(74,163,255,.16); }
|
||||
.plan { display:block; width:100%; height:auto; background:#f4f3ee; border-radius:12px; touch-action:none; }
|
||||
.zone { fill:#e9e4d8; stroke:#2b2b2b; stroke-width:0.06; cursor:pointer; transition:.12s; }
|
||||
.zone:hover { fill:#bcd6f5; stroke:#2f7fe0; }
|
||||
.zone.sel { fill:#9ec4f0; stroke:#1f6fd0; stroke-width:0.09; }
|
||||
.zone.edit { cursor:default; }
|
||||
.hnd { fill:#ff7a00; stroke:#fff; stroke-width:0.03; cursor:grab; }
|
||||
.lbl { font-size:0.30px; fill:#2a2a2a; pointer-events:none; }
|
||||
.editbar { display:flex; gap:8px; align-items:center; margin-top:8px; flex-wrap:wrap; }
|
||||
.editbar button { background:#243040; color:#cfe0f0; border:1px solid #3a4a5e; border-radius:9px;
|
||||
padding:5px 10px; font-size:12px; cursor:pointer; }
|
||||
.hint { font-size:11px; opacity:.6; }
|
||||
.grid { display:grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap:10px; }
|
||||
.tile { text-align:left; border:1px solid #2a2f36; border-radius:14px;
|
||||
background: linear-gradient(160deg, rgba(30,36,44,.95), rgba(18,22,27,.95));
|
||||
color: #e8eef5; padding:12px; cursor:pointer; }
|
||||
.tile:hover { border-color:#4aa3ff; }
|
||||
.tile.sel { border-color:#4aa3ff; box-shadow:0 0 0 1px #4aa3ff inset; }
|
||||
.tile-top { display:flex; justify-content:space-between; align-items:center; opacity:.85; }
|
||||
.cnt { font-size:11px; padding:1px 7px; border-radius:9px; background:rgba(74,163,255,.18); color:#8fc4ff; }
|
||||
.tile-name { font-weight:600; margin-top:6px; font-size:14px; }
|
||||
.tile-meta { font-size:11px; opacity:.7; margin-top:2px; }
|
||||
.tile-sens { font-size:11px; opacity:.85; margin-top:6px; color:#9ed3ff; }
|
||||
.detail { border:1px solid #2a2f36; border-radius:14px; padding:12px; margin-top:10px;
|
||||
background: rgba(14,17,21,.92); max-height:420px; overflow:auto; }
|
||||
.d-head { font-weight:700; margin-bottom:8px; display:flex; justify-content:space-between; }
|
||||
.close { background:none;border:none;color:#9aa7b4;font-size:18px;cursor:pointer; }
|
||||
.grp-h { font-size:11px; text-transform:uppercase; letter-spacing:.06em; color:#7d8b99; margin:10px 0 4px; }
|
||||
.row { display:flex; gap:8px; align-items:center; font-size:12px; padding:3px 0;
|
||||
border-bottom:1px dashed rgba(255,255,255,.05); }
|
||||
.nm { flex:1; opacity:.9; } .vl { opacity:.65; }
|
||||
.sw { font-size:11px; padding:1px 8px; border-radius:8px; border:1px solid #3a4350; cursor:pointer; }
|
||||
.sw.on { background:rgba(74,163,255,.2); border-color:#4aa3ff; color:#bcdcff; }
|
||||
</style>
|
||||
<div class="bar">
|
||||
<button class="${this._mode === "plan" ? "on" : ""}" data-mode="plan">План</button>
|
||||
<button class="${this._mode === "tiles" ? "on" : ""}" data-mode="tiles">Плитки</button>
|
||||
<button class="${this._edit ? "on" : ""}" data-edit="1">Правка границ</button>
|
||||
</div>
|
||||
${this._mode === "plan" ? this._planSvg(areaId) + editorBar : `<div class="grid">${tiles}</div>`}
|
||||
${detail}`;
|
||||
|
||||
this.shadowRoot.querySelectorAll("[data-mode]").forEach((b) =>
|
||||
b.addEventListener("click", () => { this._mode = b.dataset.mode; this._render(); }));
|
||||
const eb = this.shadowRoot.querySelector("[data-edit]");
|
||||
if (eb) eb.addEventListener("click", () => { this._edit = !this._edit; this._render(); });
|
||||
if (this._mode === "plan") {
|
||||
this.shadowRoot.querySelectorAll(".zone").forEach((el) =>
|
||||
el.addEventListener("click", () => {
|
||||
if (this._edit) return;
|
||||
this._selected = this._selected === el.dataset.a ? null : el.dataset.a;
|
||||
this._render();
|
||||
}));
|
||||
if (this._edit) this._bindEditor();
|
||||
}
|
||||
this.shadowRoot.querySelectorAll(".tile").forEach((el) =>
|
||||
el.addEventListener("click", () => {
|
||||
this._selected = this._selected === el.dataset.a ? null : el.dataset.a; this._render();
|
||||
}));
|
||||
this.shadowRoot.querySelectorAll(".sw").forEach((s) =>
|
||||
s.addEventListener("click", (ev) => { ev.stopPropagation(); this._toggle(s.dataset.e); }));
|
||||
const close = this.shadowRoot.querySelector(".close");
|
||||
if (close) close.addEventListener("click", () => { this._selected = null; this._render(); });
|
||||
const cp = this.shadowRoot.querySelector("[data-act=copy]");
|
||||
if (cp) cp.addEventListener("click", async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(this._json());
|
||||
cp.textContent = "Скопировано ✓";
|
||||
} catch (e) { cp.textContent = "Не удалось скопировать"; }
|
||||
});
|
||||
const rs = this.shadowRoot.querySelector("[data-act=reset]");
|
||||
if (rs) rs.addEventListener("click", () => {
|
||||
this._polys = JSON.parse(JSON.stringify(ROOMS.map((r) => r.poly)));
|
||||
try { localStorage.removeItem(STORE); } catch (e) { /* ignore */ }
|
||||
this._render();
|
||||
});
|
||||
}
|
||||
|
||||
getCardSize() { return 14; }
|
||||
}
|
||||
|
||||
customElements.define("rooms-card", RoomsCard);
|
||||
Reference in new issue
Block a user