forked from marrobHD/tv-card
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtv-card-editor.js
More file actions
225 lines (205 loc) 路 5.98 KB
/
Copy pathtv-card-editor.js
File metadata and controls
225 lines (205 loc) 路 5.98 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
if (!customElements.get("paper-input")) {
console.log("imported", "paper-input");
import("https://unpkg.com/@polymer/paper-input/paper-input.js?module");
}
const fireEvent = (node, type, detail, options) => {
options = options || {};
detail = detail === null || detail === undefined ? {} : detail;
const event = new Event(type, {
bubbles: options.bubbles === undefined ? true : options.bubbles,
cancelable: Boolean(options.cancelable),
composed: options.composed === undefined ? true : options.composed
});
event.detail = detail;
node.dispatchEvent(event);
return event;
};
const LitElement = Object.getPrototypeOf(
customElements.get("ha-panel-lovelace")
);
const html = LitElement.prototype.html;
// Translations for editor
const editorTranslations = {
"en": {
"name": "Name (Optional)",
"entity": "Entity",
"remote": "Remote (Optional)",
"theme": "Theme (Optional)",
"roku_tv": "Roku TV?",
"yaml_hint": "Use the YAML editor if you need to specify custom services"
},
"tr": {
"name": "Ad (陌ste臒e Ba臒l谋)",
"entity": "Varl谋k",
"remote": "Uzaktan Kumanda (陌ste臒e Ba臒l谋)",
"theme": "Tema (陌ste臒e Ba臒l谋)",
"roku_tv": "Roku TV?",
"yaml_hint": "脰zel servisler belirtmek i莽in YAML edit枚r眉n眉 kullan谋n"
}
};
// Helper function to get current language for editor
function getEditorLanguage(hass) {
if (!hass) return "en";
const lang = hass.selectedLanguage || hass.language || "en";
const langCode = lang.substring(0, 2).toLowerCase();
return editorTranslations[langCode] ? langCode : "en";
}
// Helper function to get editor translation
function te(hass, key) {
const lang = getEditorLanguage(hass);
const langData = editorTranslations[lang] || editorTranslations["en"];
return langData[key] || editorTranslations["en"][key] || key;
}
export class TVCardEditor extends LitElement {
setConfig(config) {
this._config = config;
}
static get properties() {
return { hass: {}, _config: {} };
}
get _name() {
return this._config.name || "";
}
get _entity() {
return this._config.entity || "";
}
get _remote() {
return this._config.remote || "";
}
get _theme() {
return this._config.theme;
}
get _tv() {
return this._config.tv || false;
}
render() {
if (!this.hass) {
return html``;
}
const themes = ["Backend-selected", "default"].concat(
Object.keys(this.hass.themes.themes).sort()
);
const entities = Object.keys(this.hass.states).filter(
eid => eid.substr(0, eid.indexOf(".")) === "media_player"
);
const remotes = [""].concat(
Object.keys(this.hass.states).filter(
eid => eid.substr(0, eid.indexOf(".")) === "remote"
)
);
return html`
${this.renderStyle()}
<div class="card-config">
<div class="side-by-side">
<paper-input
label="${te(this.hass, "name")}"
.value="${this._name}"
.configValue="${"name"}"
@value-changed="${this._valueChanged}"
></paper-input>
<paper-dropdown-menu
label="${te(this.hass, "entity")}"
@value-changed="${this._valueChanged}"
.configValue="${"entity"}"
>
<paper-listbox
slot="dropdown-content"
.selected="${entities.indexOf(this._entity)}"
>
${
entities.map(entity => {
return html`
<paper-item>${entity}</paper-item>
`;
})
}
</paper-listbox>
</paper-dropdown-menu>
</div>
<div class="side-by-side">
<paper-dropdown-menu
label="${te(this.hass, "remote")}"
@value-changed="${this._valueChanged}"
.configValue="${"remote"}"
>
<paper-listbox
slot="dropdown-content"
.selected="${remotes.indexOf(this._remote)}"
>
${
remotes.map(remote => {
return html`
<paper-item>${remote}</paper-item>
`;
})
}
</paper-listbox>
</paper-dropdown-menu>
<paper-dropdown-menu
label="${te(this.hass, "theme")}"
@value-changed="${this._valueChanged}"
.configValue="${"theme"}"
>
<paper-listbox
slot="dropdown-content"
.selected="${themes.indexOf(this._theme)}"
>
${
themes.map(theme => {
return html`
<paper-item>${theme}</paper-item>
`;
})
}
</paper-listbox>
</paper-dropdown-menu>
<paper-toggle-button
?checked="${this._tv !== false}"
.configValue="${"tv"}"
@change="${this._valueChanged}"
>${te(this.hass, "roku_tv")}</paper-toggle-button
>
</div>
<div>${te(this.hass, "yaml_hint")}</div>
</div>
`;
}
renderStyle() {
return html`
<style>
paper-toggle-button {
padding-top: 16px;
}
.side-by-side {
display: flex;
}
.side-by-side > * {
flex: 1;
padding-right: 4px;
}
</style>
`;
}
_valueChanged(ev) {
if (!this._config || !this.hass) {
return;
}
const target = ev.target;
if (this[`_${target.configValue}`] === target.value) {
return;
}
if (target.configValue) {
if (target.value === "") {
delete this._config[target.configValue];
} else {
this._config = {
...this._config,
[target.configValue]:
target.checked !== undefined ? target.checked : target.value
};
}
}
fireEvent(this, "config-changed", { config: this._config });
}
}
customElements.define("tv-card-editor", TVCardEditor);