forked from postlund/search-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-card.js
214 lines (183 loc) · 5.52 KB
/
search-card.js
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
customElements.whenDefined('card-tools').then(() => {
var ct = customElements.get('card-tools');
const BUILTIN_ACTIONS = [
{
matches: '^((magnet:.*)|(.*\.torrent.*))$',
name: 'Add to Transmission',
icon: 'mdi:progress-download',
service: 'transmission.add_torrent',
service_data: {
torrent: '{1}'
},
}
];
const matchAndReplace = (text, matches) => {
for (var i = 0; i < matches.length; i++) {
text = text.replace('{' + i + '}', matches[i]);
}
return text;
}
class SearchCard extends ct.LitElement {
static get properties() {
return {
config: {},
hass: {},
};
}
setConfig(config) {
this.results = [];
this.config = config;
this.active_actions = [];
this.max_results = this.config.max_results || 10;
this.search_text = this.config.search_text || "Type to search...";
this.actions = BUILTIN_ACTIONS.concat(this.config.actions || []);
this.included_domains = this.config.included_domains;
this.excluded_domains = this.config.excluded_domains || [];
}
getCardSize() {
return 4;
}
render() {
var results = this.results.slice(0, this.max_results).sort();
var rows = results.map((entity_id) => this._createResultRow(entity_id));
var actions = this.active_actions.map((x) => this._createActionRow(x[0], x[1]));
return ct.LitHtml `
<ha-card>
<div id="searchContainer">
<paper-input id="searchText"
@value-changed="${this._valueChanged}"
no-label-float type="text" autocomplete="off"
label="${this.search_text}">
<ha-icon icon="mdi:magnify" id="searchIcon"
slot="prefix"></ha-icon>
<ha-icon-button slot="suffix"
@click="${this._clearInput}"
alt="Clear"
title="Clear"><ha-icon icon="mdi:close"></ha-icon></ha-icon-button>
</paper-input>
${results.length > 0 ?
ct.LitHtml `<div id="count">Showing ${results.length} of ${this.results.length} results</div>`
: ''}
</div>
${(rows.length > 0 || actions.length > 0) ?
ct.LitHtml `<div id="results">${actions}${rows}</div>`
: ''}
</ha-card>
`;
}
_createResultRow(entity_id) {
var row = ct.createEntityRow({entity: entity_id});
row.addEventListener("click", () => ct.moreInfo(entity_id));
row.hass = this.hass;
return row;
}
_createActionRow(action, matches) {
var service_data = action.service_data;
for (var key in service_data) {
service_data[key] = matchAndReplace(service_data[key], matches);
}
const elem = cardTools.createThing("service-row", {
type: "call",
name: matchAndReplace(action.name, matches),
icon: action.icon || 'mdi:lamp',
service: action.service,
service_data: service_data,
});
elem.hass = this.hass;
return elem;
}
_clearInput()
{
this.shadowRoot.getElementById('searchText').value = '';
super.update()
}
_valueChanged(ev) {
var searchText = ev.target.value;
this.results = [];
this.active_actions = [];
if (!this.config || !this.hass || searchText === "") {
this.update();
return;
}
try {
var searchRegex = new RegExp(searchText, 'i');
for (var entity_id in this.hass.states) {
if (
(
entity_id.search(searchRegex) >= 0 ||
this.hass.states[entity_id].attributes.friendly_name?.search(searchRegex) >= 0
)
&&
(
this.included_domains
? this.included_domains.includes(entity_id.split(".")[0])
: !this.excluded_domains.includes(entity_id.split(".")[0])
)
) {
this.results.push(entity_id);
}
}
} catch (err) {
console.warn(err);
}
this.active_actions = this._getActivatedActions(searchText);
this.update();
}
_getActivatedActions(searchText) {
var active = [];
for (const action of this.actions) {
if (this._serviceExists(action.service)) {
var matches = searchText.match(action.matches);
if (matches != null) {
active.push([action, matches]);
}
}
}
return active;
}
_serviceExists(serviceCall) {
var [domain, service] = serviceCall.split('.');
var servicesForDomain = this.hass.services[domain];
return servicesForDomain && service in servicesForDomain;
}
static get styles() {
return ct.LitCSS `
#searchContainer {
width: 90%;
display: block;
margin-left: auto;
margin-right: auto;
}
#count {
text-align: right;
font-style: italic;
}
#results {
width: 90%;
display: block;
padding-bottom: 15px;
margin-top: 15px;
margin-left: auto;
margin-right: auto;
}
#searchIcon {
padding: 10px;
}
`;
}
}
customElements.define('search-card', SearchCard);
});
setTimeout(() => {
if(customElements.get('card-tools')) return;
customElements.define('search-card', class extends HTMLElement{
setConfig() { throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools");}
});
}, 2000);
window.customCards = window.customCards || [];
window.customCards.push({
type: "search-card",
name: "Search Card",
preview: true,
description: "Card to search entities"
});