-
Notifications
You must be signed in to change notification settings - Fork 168
/
hui-card-element-editor.ts
86 lines (78 loc) · 2.95 KB
/
hui-card-element-editor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { CardMod } from "../card-mod";
customElements.whenDefined("hui-card-element-editor").then(() => {
const HuiCardElementEditor = customElements.get("hui-card-element-editor");
if (HuiCardElementEditor.prototype.cardmod_patched) return;
HuiCardElementEditor.prototype.cardmod_patched = true;
const _getConfigElement = HuiCardElementEditor.prototype.getConfigElement;
HuiCardElementEditor.prototype.getConfigElement = async function () {
const retval = await _getConfigElement.bind(this)();
// Catch and patch the configElement
if (retval) {
const _setConfig = retval.setConfig;
retval.setConfig = function (config: any, ...rest) {
// Strip card_mod from the data that's sent to the config element
// and put it back after the config has been checked
const newConfig = JSON.parse(JSON.stringify(config));
this._cardModData = {
card: newConfig.card_mod,
entities: [],
};
if (newConfig.entities) {
for (const [i, e] of newConfig.entities?.entries()) {
this._cardModData.entities[i] = e.card_mod;
delete e.card_mod;
}
}
delete newConfig.card_mod;
_setConfig.bind(this)(newConfig, ...rest);
if (newConfig.entities) {
for (const [i, e] of newConfig.entities?.entries()) {
if (this._cardModData.entities[i])
e.card_mod = this._cardModData.entities[i];
}
}
};
}
return retval;
};
const _handleUIConfigChanged =
HuiCardElementEditor.prototype._handleUIConfigChanged;
HuiCardElementEditor.prototype._handleUIConfigChanged = function (
ev,
...rest
) {
if (this._configElement && this._configElement._cardModData) {
const cardMod = this._configElement._cardModData;
if (cardMod.card) ev.detail.config.card_mod = cardMod.card;
}
_handleUIConfigChanged.bind(this)(ev, ...rest);
};
});
customElements.whenDefined("hui-dialog-edit-card").then(() => {
const HuiDialogEditCard = customElements.get("hui-dialog-edit-card");
if (HuiDialogEditCard.prototype.cardmod_patched) return;
HuiDialogEditCard.prototype.cardmod_patched = true;
const _updated = HuiDialogEditCard.prototype.updated;
HuiDialogEditCard.prototype.updated = function (...args) {
_updated?.bind(this)(...args);
this.updateComplete.then(async () => {
if (!this._cardModIcon) {
this._cardModIcon = document.createElement("ha-icon");
this._cardModIcon.icon = "mdi:brush";
}
const button = this.shadowRoot.querySelector(
"mwc-button[slot=secondaryAction]"
);
if (!button) return;
button.appendChild(this._cardModIcon);
if (
this._cardConfig?.card_mod ||
this._cardConfig?.entities?.some((e: any) => e.card_mod)
) {
this._cardModIcon.style.visibility = "visible";
} else {
this._cardModIcon.style.visibility = "hidden";
}
});
};
});