home-assistant-query-selector 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +178 -0
- package/dist/esm/index.d.ts +30 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +1 -0
- package/dist/test/index.d.ts +30 -0
- package/dist/test/index.js +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# home-assistant-query-selector (HAQuerySelector)
|
|
2
|
+
|
|
3
|
+
Easily query [Home Assistant] DOM elements in an async way.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/elchininet/home-assistant-query-selector/actions/workflows/deploy.yaml) [](https://coveralls.io/github/elchininet/home-assistant-query-selector?branch=master) [](https://badge.fury.io/js/home-assistant-query-selector)
|
|
6
|
+
|
|
7
|
+
## Introduction
|
|
8
|
+
|
|
9
|
+
When one wants to build a `Home Assistant` front-end plugin, like many of the ones published in [HACS], first thing to do most of the time is dealing with query-selection of `DOM` elements. This is a tedius task, because `Home Assistant` elements are custom [WebComponents], so when the `DOM` loads, most of those elements don‘t exist, they will be created in an asynchronous way and not all of them at the same time. On top of that, as they are `WebComponents`, each one has its own `DOM` subtree under a `ShadowDOM`, something that makes the task of query-selecting elements double tedious.
|
|
10
|
+
|
|
11
|
+
As I develop and maintain several Home Assistant plugins that needs to do the aforementioned tedious task, I find myself repeating the same piece of logic over and over, and when the front-end code of `Home Asistant` changes ([something that occurs more than I would like](https://github.com/NemesisRE/kiosk-mode/issues/27)), you can imagine what needs to be done in all those plugins.
|
|
12
|
+
|
|
13
|
+
This is from where the idea of `home-assistant-query-selector` comes from. Imagine insteantiating a class, and without querying for any element, just wait for them to be created and rendered in the `DOM`, and after that, a function that you have created gets automatically executed. That would be handy, because in that way, `Home Assistant` plugins could be agnostic to the `DOM` tree, and if someting changes with a new version and all the plugins break at the same time, the changes to fix it could be done in a single place, the rest of the plugins just need to update to the new version of this library once the patch is released.
|
|
14
|
+
|
|
15
|
+
And something very important, [several end-to-end tests are in place in this library](https://github.com/elchininet/home-assistant-query-selector/actions/runs/6909640095/job/18801363213), if one wants to test if the new version of `Home Assistant` will break something from the library, one just needs to run a command and a series of test will be run to check that.
|
|
16
|
+
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
So, this is a quick summary about how this library works:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { HAQuerySelector } from 'home-assistant-query-selector';
|
|
23
|
+
|
|
24
|
+
const instance = new HAQuerySelector();
|
|
25
|
+
|
|
26
|
+
//This event will be triggered every time the lovelace dashboard changes
|
|
27
|
+
instance.addEventListener('onLovelacePanelLoad', ({detail}) => {
|
|
28
|
+
|
|
29
|
+
// When the header gets ready to work with it
|
|
30
|
+
detail.HEADER.element
|
|
31
|
+
.then((header) => {
|
|
32
|
+
// Do whatever we want with the header
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// When the sidebar gets ready to work with it
|
|
36
|
+
detail.HA_SIDEBAR.then((sidebar) => {
|
|
37
|
+
// Do whatever we want with the sidebar
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Start to listen
|
|
42
|
+
instance.listen();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
#### npm
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install home-assistant-query-selector
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### yarn
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
yarn add home-assistant-query-selector
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### PNPM
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pnpm add home-assistant-query-selector
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### Class instantiation
|
|
68
|
+
|
|
69
|
+
The `HAQuerySelector` class can be instantiated sending an optional parameter, this parameter will decide how much time is required to query for each element before giving up.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
new HAQuerySelector([config])
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### config optional parameter
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
{
|
|
79
|
+
retries?: number;
|
|
80
|
+
delay?: number;
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| Parameter | Optional | Description |
|
|
85
|
+
| ------------ | ------------- | --------------------------------------------------- |
|
|
86
|
+
| retries | yes | How many retries before giving up (defaults to 100) |
|
|
87
|
+
| delay | yes | Delay between each retry (defaults to 50) |
|
|
88
|
+
|
|
89
|
+
### Public methods
|
|
90
|
+
|
|
91
|
+
`HAQuerySelector` instances count with a public method. This method triggers inmediatly an event with the elements and watches for changes of the `partial-panel-resolver` (every time that one navigates out of the dashboard and comes back again, this event will be triggered with an update of the elements).
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
instance.listen();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Events
|
|
98
|
+
|
|
99
|
+
The `HAQuerySelector` class extends from [EventTarget], so it is possible to add events listeners to it. It will dispatch only one event (`onLovelacePanelLoad`) and it will be triggered when the [listen public method](#public-methods) is executed or when the lovelace dashboard changes (every time that the lovelace dashboard containing your code is selected from the sidebar).
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
instance.addEventListener('onLovelacePanelLoad', function({detail}) {
|
|
103
|
+
/* detail:
|
|
104
|
+
{
|
|
105
|
+
HOME_ASSISTANT: {...},
|
|
106
|
+
HOME_ASSISTANT_MAIN: {...},
|
|
107
|
+
HA_DRAWER: {...},
|
|
108
|
+
HA_SIDEBAR: {...},
|
|
109
|
+
PARTIAL_PANEL_RESOLVER: {...},
|
|
110
|
+
...
|
|
111
|
+
}
|
|
112
|
+
*/
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The dispatched event is a [CustomEvent] and its `detail` property is an object containing all the `Home Assistant`‘s main elements.
|
|
117
|
+
|
|
118
|
+
### Available elements
|
|
119
|
+
|
|
120
|
+
This is the list of the elements available inside the `CustomEvent` `detail` property:
|
|
121
|
+
|
|
122
|
+

|
|
123
|
+
|
|
124
|
+
| Detail element | DOM element |
|
|
125
|
+
| ------------------------ | ------------------------ |
|
|
126
|
+
| `HOME_ASSISTANT` | `home-assistant` |
|
|
127
|
+
| `HOME_ASSISTANT_MAIN` | `home-assistant-main` |
|
|
128
|
+
| `HA_DRAWER` | `ha-drawer` |
|
|
129
|
+
| `HA_SIDEBAR` | `ha-sidebar` |
|
|
130
|
+
| `PARTIAL_PANEL_RESOLVER` | `partial-panel-resolver` |
|
|
131
|
+
| `HA_PANEL_LOVELACE` | `ha-panel-lovelace` |
|
|
132
|
+
| `HUI_ROOT` | `hui-root` |
|
|
133
|
+
| `HEADER` | `.header` |
|
|
134
|
+
| `HUI_VIEW` | `hui-view` |
|
|
135
|
+
|
|
136
|
+
All the available elements contain the same `element` property and three methods:
|
|
137
|
+
|
|
138
|
+
| Property or method | Description |
|
|
139
|
+
| ------------------------- | ------------------------------------------------------------ |
|
|
140
|
+
| `element` | Promise that resolves in the DOM element |
|
|
141
|
+
| `querySelector` | Method to query for descendants of this element |
|
|
142
|
+
| `querySelectorAll` | Method to query multiple decendants elements of this element |
|
|
143
|
+
| `shadowRootQuerySelector` | Method to query for descendants shadowRoots of this element |
|
|
144
|
+
|
|
145
|
+
>`querySelector`, `querySelectorAll` and `shadowRootQuerySelector` return a Promise that resolves in the element specified by their query. Behind the secenes, they use the [asyncQuerySelector], [asyncQuerySelectorAll] and [asyncShadowRootQuerySelector] functions from [shadow-dom-selector], which is highly inspired in the query philosophy of [lovelace-card-mod].
|
|
146
|
+
|
|
147
|
+
#### Examples
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
detail.HOME_ASSISTANT.querySelector('$ home-assistant-main$ ha-sidebar')
|
|
151
|
+
.then((sidebar) => {
|
|
152
|
+
// sidebar === ha-sidebar element
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
detail.HEADER.querySelectorAll('.action-items ha-icon-button')
|
|
156
|
+
.then((buttons) => {
|
|
157
|
+
// buttons === Search, Assist, and Open dashboard menu elements (top-right header buttons)
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// In all the methods one can Specify custom retries and delay
|
|
161
|
+
detail.HA_PANEL_LOVELACE.shadowRootQuerySelector('$ hui-root$', { retries: 50, delay: 20 })
|
|
162
|
+
.then((shadowRoot) => {
|
|
163
|
+
// shadowRoot === hui-root‘s shadowRoot
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
[Home Assistant]: https://www.home-assistant.io
|
|
169
|
+
[HACS]: https://hacs.xyz
|
|
170
|
+
[WebComponents]: https://www.webcomponents.org
|
|
171
|
+
[ShadowDOM]: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
|
|
172
|
+
[EventTarget]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
|
|
173
|
+
[CustomEvent]: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
|
|
174
|
+
[asyncQuerySelector]: https://github.com/elchininet/shadow-dom-selector#asyncqueryselector
|
|
175
|
+
[asyncQuerySelectorAll]: https://github.com/elchininet/shadow-dom-selector#asyncqueryselectorall
|
|
176
|
+
[asyncShadowRootQuerySelector]: https://github.com/elchininet/shadow-dom-selector#asyncshadowrootqueryselector
|
|
177
|
+
[shadow-dom-selector]: https://github.com/elchininet/shadow-dom-selector
|
|
178
|
+
[lovelace-card-mod]: https://github.com/thomasloven/lovelace-card-mod
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type HAQuerySelectorConfig = {
|
|
2
|
+
retries?: number;
|
|
3
|
+
delay?: number;
|
|
4
|
+
};
|
|
5
|
+
type NodeDescriptorProps = {
|
|
6
|
+
selector: string;
|
|
7
|
+
children?: HomeAssistantNodeDescriptor;
|
|
8
|
+
};
|
|
9
|
+
type HomeAssistantNodeDescriptor = {
|
|
10
|
+
[key: string]: NodeDescriptorProps;
|
|
11
|
+
};
|
|
12
|
+
type ElementProps = {
|
|
13
|
+
element: Promise<Element | null>;
|
|
14
|
+
children?: HomeAssistantElement;
|
|
15
|
+
querySelector: <E extends Element = Element>(selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<E | null>;
|
|
16
|
+
querySelectorAll: <E extends Element = Element>(selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<NodeListOf<E>>;
|
|
17
|
+
shadowRootQuerySelector: (selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<ShadowRoot | null>;
|
|
18
|
+
};
|
|
19
|
+
type HomeAssistantElement = {
|
|
20
|
+
[key: string]: ElementProps;
|
|
21
|
+
};
|
|
22
|
+
declare enum HAQuerySelectorEvent {
|
|
23
|
+
ON_LOVELACE_PANEL_LOAD = "onLovelacePanelLoad"
|
|
24
|
+
}
|
|
25
|
+
declare class HAQuerySelector extends EventTarget {
|
|
26
|
+
#private;
|
|
27
|
+
constructor(config?: HAQuerySelectorConfig);
|
|
28
|
+
listen(): void;
|
|
29
|
+
}
|
|
30
|
+
export { HAQuerySelector, HAQuerySelectorEvent, HomeAssistantElement };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t,n,r){return new(n||(n=Promise))((function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,s)}a((r=r.apply(e,t||[])).next())}))}function t(e,t,n,r){if("a"===n&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?r:"a"===n?r.call(e):r?r.value:t.get(e)}function n(e,t,n,r,o){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?o.call(e,n):o?o.value=n:t.set(e,n),n}"function"==typeof SuppressedError&&SuppressedError;const r="$",o={retries:100,delay:50};var i,c,s,a;!function(e){e.HOME_ASSISTANT="HOME_ASSISTANT",e.HOME_ASSISTANT_MAIN="HOME_ASSISTANT_MAIN",e.HA_DRAWER="HA_DRAWER",e.HA_SIDEBAR="HA_SIDEBAR"}(i||(i={})),function(e){e.PARTIAL_PANEL_RESOLVER="PARTIAL_PANEL_RESOLVER",e.HA_PANEL_LOVELACE="HA_PANEL_LOVELACE",e.HUI_ROOT="HUI_ROOT",e.HEADER="HEADER",e.HUI_VIEW="HUI_VIEW"}(c||(c={})),function(e){e.ON_LOVELACE_PANEL_LOAD="onLovelacePanelLoad"}(s||(s={})),function(e){e.HOME_ASSISTANT="home-assistant",e.HOME_ASSISTANT_MAIN="home-assistant-main",e.HA_DRAWER="ha-drawer",e.HA_SIDEBAR="ha-sidebar",e.PARTIAL_PANEL_RESOLVER="partial-panel-resolver",e.HA_PANEL_LOVELACE="ha-panel-lovelace",e.HUI_ROOT="hui-root",e.HEADER=".header",e.HUI_VIEW="hui-view"}(a||(a={}));const l={[i.HOME_ASSISTANT]:{selector:a.HOME_ASSISTANT,children:{shadowRoot:{selector:r,children:{[i.HOME_ASSISTANT_MAIN]:{selector:a.HOME_ASSISTANT_MAIN,children:{shadowRoot:{selector:r,children:{[i.HA_DRAWER]:{selector:a.HA_DRAWER,children:{[i.HA_SIDEBAR]:{selector:a.HA_SIDEBAR,children:{shadowRoot:{selector:r}}}}}}}}}}}}}},u={[c.PARTIAL_PANEL_RESOLVER]:{selector:a.PARTIAL_PANEL_RESOLVER,children:{[c.HA_PANEL_LOVELACE]:{selector:a.HA_PANEL_LOVELACE,children:{shadowRoot:{selector:r,children:{[c.HUI_ROOT]:{selector:a.HUI_ROOT,children:{shadowRoot:{selector:r,children:{[c.HEADER]:{selector:a.HEADER},[c.HUI_VIEW]:{selector:a.HUI_VIEW}}}}}}}}}}}};function h(e,t,n,r){return new(n||(n=Promise))((function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,s)}a((r=r.apply(e,t||[])).next())}))}function f(e,t){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(a){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return c.label++,{value:s[1],done:!1};case 5:c.label++,r=s[1],s=[0];continue;case 7:s=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){c=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){c.label=s[1];break}if(6===s[0]&&c.label<o[1]){c.label=o[1],o=s;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(s);break}o[2]&&c.ops.pop(),c.trys.pop();continue}s=t.call(e,c)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}"function"==typeof SuppressedError&&SuppressedError;var d="$",v=":host",A="invalid selector",E=10,w=10,y=function(e){var t,n=e[0],r=e[1];return(t=n)&&(t instanceof Document||t instanceof Element)&&"string"==typeof r};function p(e,t){return function(e){return e.split(",").map((function(e){return e.trim()}))}(e).map((function(e){var n=function(e){return e.split(d).map((function(e){return e.trim()}))}(e);return t(n)}))}function b(e,t,n,r,o){return void 0===o&&(o=!1),new Promise((function(i){var c=0,s=function(){var a=o?e.querySelectorAll(t):e.querySelector(t);o&&a.length||!o&&null!==a?i(a):++c<n?setTimeout(s,r):i(o?document.querySelectorAll(A):null)};s()}))}function S(e,t,n){return new Promise((function(r){var o=0,i=function(){var c=e.shadowRoot;c?r(c):++o<t?setTimeout(i,n):r(null)};i()}))}function _(e,t){var n=t?" If you want to select a shadowRoot, use ".concat(t," instead."):"";return"".concat(e," cannot be used with a selector ending in a shadowRoot (").concat(d,").").concat(n)}function O(e,t){return"".concat(e," must be used with a selector ending in a shadowRoot (").concat(d,"). If you don't want to select a shadowRoot, use ").concat(t," instead.")}function R(){return"You can not select a shadowRoot (".concat(d,") of the document.")}function m(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s,a,l;return f(this,(function(u){switch(u.label){case 0:o=null,i=e.length,c=0,u.label=1;case 1:if(!(c<i))return[3,15];if(0!==c)return[3,8];if(e[c].length)return[3,5];if(t instanceof Document)throw new SyntaxError(R());return t.shadowRoot?[4,b(t.shadowRoot,e[++c],n,r)]:[3,3];case 2:return s=u.sent(),[3,4];case 3:s=null,u.label=4;case 4:return o=s,[3,7];case 5:return[4,b(t,e[c],n,r)];case 6:o=u.sent(),u.label=7;case 7:return[3,13];case 8:return[4,S(o,n,r)];case 9:return(a=u.sent())?[4,b(a,"".concat(v," ").concat(e[c]),n,r)]:[3,11];case 10:return l=u.sent(),[3,12];case 11:l=null,u.label=12;case 12:o=l,u.label=13;case 13:if(null===o)return[2,null];u.label=14;case 14:return c++,[3,1];case 15:return[2,o]}}))}))}function L(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s,a,l;return f(this,(function(u){switch(u.label){case 0:return o=function(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}([],e,!0),i=o.pop(),o.length?[3,2]:[4,b(t,i,n,r,!0)];case 1:return[2,u.sent()];case 2:return[4,m(o,t,n,r)];case 3:return(c=u.sent())?[4,S(c,n,r)]:[3,5];case 4:return a=u.sent(),[3,6];case 5:a=null,u.label=6;case 6:return(s=a)?[4,b(s,"".concat(v," ").concat(i),n,r,!0)]:[3,8];case 7:return l=u.sent(),[3,9];case 8:l=null,u.label=9;case 9:return[2,l]}}))}))}function g(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i;return f(this,(function(c){switch(c.label){case 0:if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(R());return[2,S(t,n,r)]}return[4,m(e,t,n,r)];case 1:return(o=c.sent())?[4,S(o,n,r)]:[3,3];case 2:return i=c.sent(),[3,4];case 3:i=null,c.label=4;case 4:return[2,i]}}))}))}function I(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s;return f(this,(function(a){switch(a.label){case 0:o=p(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(_("asyncQuerySelector","asyncShadowRootQuerySelector"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,m(o[c],t,n,r)]:[3,4];case 2:if(s=a.sent())return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function H(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s;return f(this,(function(a){switch(a.label){case 0:o=p(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(_("asyncQuerySelectorAll"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,L(o[c],t,n,r)]:[3,4];case 2:if(null==(s=a.sent())?void 0:s.length)return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,document.querySelectorAll(A)]}}))}))}function T(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s;return f(this,(function(a){switch(a.label){case 0:o=p(e,(function(e){if(e.pop().length)throw new SyntaxError(O("asyncShadowRootQuerySelector","asyncQuerySelector"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,g(o[c],t,n,r)]:[3,4];case 2:if(s=a.sent())return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function N(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t,n,r,o,i;return f(this,(function(c){switch(c.label){case 0:return y(e)?(t=e[0],n=e[1],r=e[2],[4,I(n,t,(null==r?void 0:r.retries)||E,(null==r?void 0:r.delay)||w)]):[3,2];case 1:case 3:return[2,c.sent()];case 2:return o=e[0],i=e[1],[4,I(o,document,(null==i?void 0:i.retries)||E,(null==i?void 0:i.delay)||w)]}}))}))}const P=(t,n,o=null,i=!1)=>Object.entries(n||{}).reduce(((n,c)=>{const[s,a]=c;if(a.selector===r&&o)return a.children?Object.assign(Object.assign({},n),P(t,a.children,o,!0)):n;const l=o?o.then((e=>{return N(e,(n=a.selector,i?"$ "+n:n),t);var n})):N(a.selector,t);return n[s]={element:l,children:P(t,a.children,l),querySelector(n,r={}){return e(this,void 0,void 0,(function*(){const e=yield l;return yield N(e,n,Object.assign(Object.assign({},t),r))}))},querySelectorAll(n,r={}){return e(this,void 0,void 0,(function*(){const e=yield l;return yield function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t,n,r,o,i;return f(this,(function(c){switch(c.label){case 0:return y(e)?(t=e[0],n=e[1],r=e[2],[4,H(n,t,(null==r?void 0:r.retries)||E,(null==r?void 0:r.delay)||w)]):[3,2];case 1:return[2,c.sent()];case 2:return o=e[0],i=e[1],[2,H(o,document,(null==i?void 0:i.retries)||E,(null==i?void 0:i.delay)||w)]}}))}))}(e,n,Object.assign(Object.assign({},t),r))}))},shadowRootQuerySelector(n,r={}){return e(this,void 0,void 0,(function*(){const e=yield l;return yield function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t,n,r,o,i;return f(this,(function(c){switch(c.label){case 0:return y(e)?(t=e[0],n=e[1],r=e[2],[4,T(n,t,(null==r?void 0:r.retries)||E,(null==r?void 0:r.delay)||w)]):[3,2];case 1:return[2,c.sent()];case 2:return o=e[0],i=e[1],[2,T(o,document,(null==i?void 0:i.retries)||E,(null==i?void 0:i.delay)||w)]}}))}))}(e,n,Object.assign(Object.assign({},t),r))}))}},n}),{}),M=(e,t)=>{const n=Object.entries(t);for(const t of n){if(t[0]===e)return t[1];{const n=M(e,t[1].children);if(n)return n}}},j=(e,t)=>Object.keys(e).reduce(((e,n)=>{const r=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(M(n,t),["children"]);return e[n]=Object.assign({},r),e}),{});var D,V,W,x,k,C,U,q,Q,B,$,G;class Y extends EventTarget{constructor(e={}){super(),D.add(this),V.set(this,void 0),W.set(this,void 0),x.set(this,void 0),k.set(this,void 0),C.set(this,void 0),U.set(this,void 0),q.set(this,void 0),n(this,V,Object.assign(Object.assign({},o),e),"f")}listen(){var e;n(this,q,t(this,D,"m",G).bind(this),"f"),t(this,D,"m",B).call(this),t(this,D,"m",$).call(this),null===(e=t(this,U,"f"))||void 0===e||e.disconnect(),n(this,U,new MutationObserver(t(this,q,"f")),"f"),t(this,C,"f")[c.PARTIAL_PANEL_RESOLVER].element.then((e=>{t(this,U,"f").observe(e,{childList:!0})}))}}V=new WeakMap,W=new WeakMap,x=new WeakMap,k=new WeakMap,C=new WeakMap,U=new WeakMap,q=new WeakMap,D=new WeakSet,Q=function(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))},B=function(){n(this,W,P(t(this,V,"f"),l),"f"),n(this,k,j(i,t(this,W,"f")),"f")},$=function(){n(this,x,P(t(this,V,"f"),u,t(this,k,"f")[i.HA_DRAWER].element),"f"),n(this,C,j(c,t(this,x,"f")),"f"),t(this,D,"m",Q).call(this,s.ON_LOVELACE_PANEL_LOAD,Object.assign(Object.assign({},t(this,k,"f")),t(this,C,"f")))},G=function(e){e.forEach((({addedNodes:e})=>{e.forEach((e=>{e.localName===a.HA_PANEL_LOVELACE&&t(this,D,"m",$).call(this)}))}))};export{Y as HAQuerySelector,s as HAQuerySelectorEvent};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type HAQuerySelectorConfig = {
|
|
2
|
+
retries?: number;
|
|
3
|
+
delay?: number;
|
|
4
|
+
};
|
|
5
|
+
type NodeDescriptorProps = {
|
|
6
|
+
selector: string;
|
|
7
|
+
children?: HomeAssistantNodeDescriptor;
|
|
8
|
+
};
|
|
9
|
+
type HomeAssistantNodeDescriptor = {
|
|
10
|
+
[key: string]: NodeDescriptorProps;
|
|
11
|
+
};
|
|
12
|
+
type ElementProps = {
|
|
13
|
+
element: Promise<Element | null>;
|
|
14
|
+
children?: HomeAssistantElement;
|
|
15
|
+
querySelector: <E extends Element = Element>(selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<E | null>;
|
|
16
|
+
querySelectorAll: <E extends Element = Element>(selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<NodeListOf<E>>;
|
|
17
|
+
shadowRootQuerySelector: (selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<ShadowRoot | null>;
|
|
18
|
+
};
|
|
19
|
+
type HomeAssistantElement = {
|
|
20
|
+
[key: string]: ElementProps;
|
|
21
|
+
};
|
|
22
|
+
declare enum HAQuerySelectorEvent {
|
|
23
|
+
ON_LOVELACE_PANEL_LOAD = "onLovelacePanelLoad"
|
|
24
|
+
}
|
|
25
|
+
declare class HAQuerySelector extends EventTarget {
|
|
26
|
+
#private;
|
|
27
|
+
constructor(config?: HAQuerySelectorConfig);
|
|
28
|
+
listen(): void;
|
|
29
|
+
}
|
|
30
|
+
export { HAQuerySelector, HAQuerySelectorEvent, HomeAssistantElement };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e,t,n,r){return new(n||(n=Promise))((function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,s)}a((r=r.apply(e,t||[])).next())}))}function t(e,t,n,r){if("a"===n&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?r:"a"===n?r.call(e):r?r.value:t.get(e)}function n(e,t,n,r,o){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?o.call(e,n):o?o.value=n:t.set(e,n),n}"function"==typeof SuppressedError&&SuppressedError;const r="$",o={retries:100,delay:50};var i,c,s;!function(e){e.HOME_ASSISTANT="HOME_ASSISTANT",e.HOME_ASSISTANT_MAIN="HOME_ASSISTANT_MAIN",e.HA_DRAWER="HA_DRAWER",e.HA_SIDEBAR="HA_SIDEBAR"}(i||(i={})),function(e){e.PARTIAL_PANEL_RESOLVER="PARTIAL_PANEL_RESOLVER",e.HA_PANEL_LOVELACE="HA_PANEL_LOVELACE",e.HUI_ROOT="HUI_ROOT",e.HEADER="HEADER",e.HUI_VIEW="HUI_VIEW"}(c||(c={})),exports.HAQuerySelectorEvent=void 0,(exports.HAQuerySelectorEvent||(exports.HAQuerySelectorEvent={})).ON_LOVELACE_PANEL_LOAD="onLovelacePanelLoad",function(e){e.HOME_ASSISTANT="home-assistant",e.HOME_ASSISTANT_MAIN="home-assistant-main",e.HA_DRAWER="ha-drawer",e.HA_SIDEBAR="ha-sidebar",e.PARTIAL_PANEL_RESOLVER="partial-panel-resolver",e.HA_PANEL_LOVELACE="ha-panel-lovelace",e.HUI_ROOT="hui-root",e.HEADER=".header",e.HUI_VIEW="hui-view"}(s||(s={}));const a={[i.HOME_ASSISTANT]:{selector:s.HOME_ASSISTANT,children:{shadowRoot:{selector:r,children:{[i.HOME_ASSISTANT_MAIN]:{selector:s.HOME_ASSISTANT_MAIN,children:{shadowRoot:{selector:r,children:{[i.HA_DRAWER]:{selector:s.HA_DRAWER,children:{[i.HA_SIDEBAR]:{selector:s.HA_SIDEBAR,children:{shadowRoot:{selector:r}}}}}}}}}}}}}},l={[c.PARTIAL_PANEL_RESOLVER]:{selector:s.PARTIAL_PANEL_RESOLVER,children:{[c.HA_PANEL_LOVELACE]:{selector:s.HA_PANEL_LOVELACE,children:{shadowRoot:{selector:r,children:{[c.HUI_ROOT]:{selector:s.HUI_ROOT,children:{shadowRoot:{selector:r,children:{[c.HEADER]:{selector:s.HEADER},[c.HUI_VIEW]:{selector:s.HUI_VIEW}}}}}}}}}}}};function u(e,t,n,r){return new(n||(n=Promise))((function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,s)}a((r=r.apply(e,t||[])).next())}))}function h(e,t){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(a){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return c.label++,{value:s[1],done:!1};case 5:c.label++,r=s[1],s=[0];continue;case 7:s=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){c=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){c.label=s[1];break}if(6===s[0]&&c.label<o[1]){c.label=o[1],o=s;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(s);break}o[2]&&c.ops.pop(),c.trys.pop();continue}s=t.call(e,c)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}"function"==typeof SuppressedError&&SuppressedError;var f="$",d=":host",v="invalid selector",A=10,E=10,w=function(e){var t,n=e[0],r=e[1];return(t=n)&&(t instanceof Document||t instanceof Element)&&"string"==typeof r};function y(e,t){return function(e){return e.split(",").map((function(e){return e.trim()}))}(e).map((function(e){var n=function(e){return e.split(f).map((function(e){return e.trim()}))}(e);return t(n)}))}function p(e,t,n,r,o){return void 0===o&&(o=!1),new Promise((function(i){var c=0,s=function(){var a=o?e.querySelectorAll(t):e.querySelector(t);o&&a.length||!o&&null!==a?i(a):++c<n?setTimeout(s,r):i(o?document.querySelectorAll(v):null)};s()}))}function S(e,t,n){return new Promise((function(r){var o=0,i=function(){var c=e.shadowRoot;c?r(c):++o<t?setTimeout(i,n):r(null)};i()}))}function b(e,t){var n=t?" If you want to select a shadowRoot, use ".concat(t," instead."):"";return"".concat(e," cannot be used with a selector ending in a shadowRoot (").concat(f,").").concat(n)}function _(e,t){return"".concat(e," must be used with a selector ending in a shadowRoot (").concat(f,"). If you don't want to select a shadowRoot, use ").concat(t," instead.")}function O(){return"You can not select a shadowRoot (".concat(f,") of the document.")}function R(e,t,n,r){return u(this,void 0,void 0,(function(){var o,i,c,s,a,l;return h(this,(function(u){switch(u.label){case 0:o=null,i=e.length,c=0,u.label=1;case 1:if(!(c<i))return[3,15];if(0!==c)return[3,8];if(e[c].length)return[3,5];if(t instanceof Document)throw new SyntaxError(O());return t.shadowRoot?[4,p(t.shadowRoot,e[++c],n,r)]:[3,3];case 2:return s=u.sent(),[3,4];case 3:s=null,u.label=4;case 4:return o=s,[3,7];case 5:return[4,p(t,e[c],n,r)];case 6:o=u.sent(),u.label=7;case 7:return[3,13];case 8:return[4,S(o,n,r)];case 9:return(a=u.sent())?[4,p(a,"".concat(d," ").concat(e[c]),n,r)]:[3,11];case 10:return l=u.sent(),[3,12];case 11:l=null,u.label=12;case 12:o=l,u.label=13;case 13:if(null===o)return[2,null];u.label=14;case 14:return c++,[3,1];case 15:return[2,o]}}))}))}function m(e,t,n,r){return u(this,void 0,void 0,(function(){var o,i,c,s,a,l;return h(this,(function(u){switch(u.label){case 0:return o=function(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}([],e,!0),i=o.pop(),o.length?[3,2]:[4,p(t,i,n,r,!0)];case 1:return[2,u.sent()];case 2:return[4,R(o,t,n,r)];case 3:return(c=u.sent())?[4,S(c,n,r)]:[3,5];case 4:return a=u.sent(),[3,6];case 5:a=null,u.label=6;case 6:return(s=a)?[4,p(s,"".concat(d," ").concat(i),n,r,!0)]:[3,8];case 7:return l=u.sent(),[3,9];case 8:l=null,u.label=9;case 9:return[2,l]}}))}))}function H(e,t,n,r){return u(this,void 0,void 0,(function(){var o,i;return h(this,(function(c){switch(c.label){case 0:if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(O());return[2,S(t,n,r)]}return[4,R(e,t,n,r)];case 1:return(o=c.sent())?[4,S(o,n,r)]:[3,3];case 2:return i=c.sent(),[3,4];case 3:i=null,c.label=4;case 4:return[2,i]}}))}))}function L(e,t,n,r){return u(this,void 0,void 0,(function(){var o,i,c,s;return h(this,(function(a){switch(a.label){case 0:o=y(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(b("asyncQuerySelector","asyncShadowRootQuerySelector"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,R(o[c],t,n,r)]:[3,4];case 2:if(s=a.sent())return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function g(e,t,n,r){return u(this,void 0,void 0,(function(){var o,i,c,s;return h(this,(function(a){switch(a.label){case 0:o=y(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(b("asyncQuerySelectorAll"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,m(o[c],t,n,r)]:[3,4];case 2:if(null==(s=a.sent())?void 0:s.length)return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,document.querySelectorAll(v)]}}))}))}function I(e,t,n,r){return u(this,void 0,void 0,(function(){var o,i,c,s;return h(this,(function(a){switch(a.label){case 0:o=y(e,(function(e){if(e.pop().length)throw new SyntaxError(_("asyncShadowRootQuerySelector","asyncQuerySelector"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,H(o[c],t,n,r)]:[3,4];case 2:if(s=a.sent())return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function T(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return u(this,void 0,void 0,(function(){var t,n,r,o,i;return h(this,(function(c){switch(c.label){case 0:return w(e)?(t=e[0],n=e[1],r=e[2],[4,L(n,t,(null==r?void 0:r.retries)||A,(null==r?void 0:r.delay)||E)]):[3,2];case 1:case 3:return[2,c.sent()];case 2:return o=e[0],i=e[1],[4,L(o,document,(null==i?void 0:i.retries)||A,(null==i?void 0:i.delay)||E)]}}))}))}const N=(t,n,o=null,i=!1)=>Object.entries(n||{}).reduce(((n,c)=>{const[s,a]=c;if(a.selector===r&&o)return a.children?Object.assign(Object.assign({},n),N(t,a.children,o,!0)):n;const l=o?o.then((e=>{return T(e,(n=a.selector,i?"$ "+n:n),t);var n})):T(a.selector,t);return n[s]={element:l,children:N(t,a.children,l),querySelector(n,r={}){return e(this,void 0,void 0,(function*(){const e=yield l;return yield T(e,n,Object.assign(Object.assign({},t),r))}))},querySelectorAll(n,r={}){return e(this,void 0,void 0,(function*(){const e=yield l;return yield function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return u(this,void 0,void 0,(function(){var t,n,r,o,i;return h(this,(function(c){switch(c.label){case 0:return w(e)?(t=e[0],n=e[1],r=e[2],[4,g(n,t,(null==r?void 0:r.retries)||A,(null==r?void 0:r.delay)||E)]):[3,2];case 1:return[2,c.sent()];case 2:return o=e[0],i=e[1],[2,g(o,document,(null==i?void 0:i.retries)||A,(null==i?void 0:i.delay)||E)]}}))}))}(e,n,Object.assign(Object.assign({},t),r))}))},shadowRootQuerySelector(n,r={}){return e(this,void 0,void 0,(function*(){const e=yield l;return yield function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return u(this,void 0,void 0,(function(){var t,n,r,o,i;return h(this,(function(c){switch(c.label){case 0:return w(e)?(t=e[0],n=e[1],r=e[2],[4,I(n,t,(null==r?void 0:r.retries)||A,(null==r?void 0:r.delay)||E)]):[3,2];case 1:return[2,c.sent()];case 2:return o=e[0],i=e[1],[2,I(o,document,(null==i?void 0:i.retries)||A,(null==i?void 0:i.delay)||E)]}}))}))}(e,n,Object.assign(Object.assign({},t),r))}))}},n}),{}),P=(e,t)=>{const n=Object.entries(t);for(const t of n){if(t[0]===e)return t[1];{const n=P(e,t[1].children);if(n)return n}}},M=(e,t)=>Object.keys(e).reduce(((e,n)=>{const r=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(P(n,t),["children"]);return e[n]=Object.assign({},r),e}),{});var j,D,x,V,W,k,C,Q,U,q,B,$;class G extends EventTarget{constructor(e={}){super(),j.add(this),D.set(this,void 0),x.set(this,void 0),V.set(this,void 0),W.set(this,void 0),k.set(this,void 0),C.set(this,void 0),Q.set(this,void 0),n(this,D,Object.assign(Object.assign({},o),e),"f")}listen(){var e;n(this,Q,t(this,j,"m",$).bind(this),"f"),t(this,j,"m",q).call(this),t(this,j,"m",B).call(this),null===(e=t(this,C,"f"))||void 0===e||e.disconnect(),n(this,C,new MutationObserver(t(this,Q,"f")),"f"),t(this,k,"f")[c.PARTIAL_PANEL_RESOLVER].element.then((e=>{t(this,C,"f").observe(e,{childList:!0})}))}}D=new WeakMap,x=new WeakMap,V=new WeakMap,W=new WeakMap,k=new WeakMap,C=new WeakMap,Q=new WeakMap,j=new WeakSet,U=function(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))},q=function(){n(this,x,N(t(this,D,"f"),a),"f"),n(this,W,M(i,t(this,x,"f")),"f")},B=function(){n(this,V,N(t(this,D,"f"),l,t(this,W,"f")[i.HA_DRAWER].element),"f"),n(this,k,M(c,t(this,V,"f")),"f"),t(this,j,"m",U).call(this,exports.HAQuerySelectorEvent.ON_LOVELACE_PANEL_LOAD,Object.assign(Object.assign({},t(this,W,"f")),t(this,k,"f")))},$=function(e){e.forEach((({addedNodes:e})=>{e.forEach((e=>{e.localName===s.HA_PANEL_LOVELACE&&t(this,j,"m",B).call(this)}))}))},exports.HAQuerySelector=G;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type HAQuerySelectorConfig = {
|
|
2
|
+
retries?: number;
|
|
3
|
+
delay?: number;
|
|
4
|
+
};
|
|
5
|
+
type NodeDescriptorProps = {
|
|
6
|
+
selector: string;
|
|
7
|
+
children?: HomeAssistantNodeDescriptor;
|
|
8
|
+
};
|
|
9
|
+
type HomeAssistantNodeDescriptor = {
|
|
10
|
+
[key: string]: NodeDescriptorProps;
|
|
11
|
+
};
|
|
12
|
+
type ElementProps = {
|
|
13
|
+
element: Promise<Element | null>;
|
|
14
|
+
children?: HomeAssistantElement;
|
|
15
|
+
querySelector: <E extends Element = Element>(selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<E | null>;
|
|
16
|
+
querySelectorAll: <E extends Element = Element>(selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<NodeListOf<E>>;
|
|
17
|
+
shadowRootQuerySelector: (selector: string, asyncProps?: HAQuerySelectorConfig) => Promise<ShadowRoot | null>;
|
|
18
|
+
};
|
|
19
|
+
type HomeAssistantElement = {
|
|
20
|
+
[key: string]: ElementProps;
|
|
21
|
+
};
|
|
22
|
+
declare enum HAQuerySelectorEvent {
|
|
23
|
+
ON_LOVELACE_PANEL_LOAD = "onLovelacePanelLoad"
|
|
24
|
+
}
|
|
25
|
+
declare class HAQuerySelector extends EventTarget {
|
|
26
|
+
#private;
|
|
27
|
+
constructor(config?: HAQuerySelectorConfig);
|
|
28
|
+
listen(): void;
|
|
29
|
+
}
|
|
30
|
+
export { HAQuerySelector, HAQuerySelectorEvent, HomeAssistantElement };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var HomeAssistantQuerySelector=function(e){"use strict";function t(e,t,n,r){return new(n||(n=Promise))((function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,s)}a((r=r.apply(e,t||[])).next())}))}function n(e,t,n,r){if("a"===n&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?r:"a"===n?r.call(e):r?r.value:t.get(e)}function r(e,t,n,r,o){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?o.call(e,n):o?o.value=n:t.set(e,n),n}"function"==typeof SuppressedError&&SuppressedError;const o="$",i={retries:100,delay:50};var c,s,a;!function(e){e.HOME_ASSISTANT="HOME_ASSISTANT",e.HOME_ASSISTANT_MAIN="HOME_ASSISTANT_MAIN",e.HA_DRAWER="HA_DRAWER",e.HA_SIDEBAR="HA_SIDEBAR"}(c||(c={})),function(e){e.PARTIAL_PANEL_RESOLVER="PARTIAL_PANEL_RESOLVER",e.HA_PANEL_LOVELACE="HA_PANEL_LOVELACE",e.HUI_ROOT="HUI_ROOT",e.HEADER="HEADER",e.HUI_VIEW="HUI_VIEW"}(s||(s={})),e.HAQuerySelectorEvent=void 0,(e.HAQuerySelectorEvent||(e.HAQuerySelectorEvent={})).ON_LOVELACE_PANEL_LOAD="onLovelacePanelLoad",function(e){e.HOME_ASSISTANT="home-assistant",e.HOME_ASSISTANT_MAIN="home-assistant-main",e.HA_DRAWER="ha-drawer",e.HA_SIDEBAR="ha-sidebar",e.PARTIAL_PANEL_RESOLVER="partial-panel-resolver",e.HA_PANEL_LOVELACE="ha-panel-lovelace",e.HUI_ROOT="hui-root",e.HEADER=".header",e.HUI_VIEW="hui-view"}(a||(a={}));const l={[c.HOME_ASSISTANT]:{selector:a.HOME_ASSISTANT,children:{shadowRoot:{selector:o,children:{[c.HOME_ASSISTANT_MAIN]:{selector:a.HOME_ASSISTANT_MAIN,children:{shadowRoot:{selector:o,children:{[c.HA_DRAWER]:{selector:a.HA_DRAWER,children:{[c.HA_SIDEBAR]:{selector:a.HA_SIDEBAR,children:{shadowRoot:{selector:o}}}}}}}}}}}}}},u={[s.PARTIAL_PANEL_RESOLVER]:{selector:a.PARTIAL_PANEL_RESOLVER,children:{[s.HA_PANEL_LOVELACE]:{selector:a.HA_PANEL_LOVELACE,children:{shadowRoot:{selector:o,children:{[s.HUI_ROOT]:{selector:a.HUI_ROOT,children:{shadowRoot:{selector:o,children:{[s.HEADER]:{selector:a.HEADER},[s.HUI_VIEW]:{selector:a.HUI_VIEW}}}}}}}}}}}};function h(e,t,n,r){return new(n||(n=Promise))((function(o,i){function c(e){try{a(r.next(e))}catch(e){i(e)}}function s(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,s)}a((r=r.apply(e,t||[])).next())}))}function f(e,t){var n,r,o,i,c={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(a){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,s[0]&&(c=0)),c;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return c.label++,{value:s[1],done:!1};case 5:c.label++,r=s[1],s=[0];continue;case 7:s=c.ops.pop(),c.trys.pop();continue;default:if(!((o=(o=c.trys).length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){c=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){c.label=s[1];break}if(6===s[0]&&c.label<o[1]){c.label=o[1],o=s;break}if(o&&c.label<o[2]){c.label=o[2],c.ops.push(s);break}o[2]&&c.ops.pop(),c.trys.pop();continue}s=t.call(e,c)}catch(e){s=[6,e],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,a])}}}"function"==typeof SuppressedError&&SuppressedError;var d="$",v=":host",A="invalid selector",E=10,w=10,y=function(e){var t,n=e[0],r=e[1];return(t=n)&&(t instanceof Document||t instanceof Element)&&"string"==typeof r};function S(e,t){return function(e){return e.split(",").map((function(e){return e.trim()}))}(e).map((function(e){var n=function(e){return e.split(d).map((function(e){return e.trim()}))}(e);return t(n)}))}function p(e,t,n,r,o){return void 0===o&&(o=!1),new Promise((function(i){var c=0,s=function(){var a=o?e.querySelectorAll(t):e.querySelector(t);o&&a.length||!o&&null!==a?i(a):++c<n?setTimeout(s,r):i(o?document.querySelectorAll(A):null)};s()}))}function b(e,t,n){return new Promise((function(r){var o=0,i=function(){var c=e.shadowRoot;c?r(c):++o<t?setTimeout(i,n):r(null)};i()}))}function _(e,t){var n=t?" If you want to select a shadowRoot, use ".concat(t," instead."):"";return"".concat(e," cannot be used with a selector ending in a shadowRoot (").concat(d,").").concat(n)}function O(e,t){return"".concat(e," must be used with a selector ending in a shadowRoot (").concat(d,"). If you don't want to select a shadowRoot, use ").concat(t," instead.")}function R(){return"You can not select a shadowRoot (".concat(d,") of the document.")}function m(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s,a,l;return f(this,(function(u){switch(u.label){case 0:o=null,i=e.length,c=0,u.label=1;case 1:if(!(c<i))return[3,15];if(0!==c)return[3,8];if(e[c].length)return[3,5];if(t instanceof Document)throw new SyntaxError(R());return t.shadowRoot?[4,p(t.shadowRoot,e[++c],n,r)]:[3,3];case 2:return s=u.sent(),[3,4];case 3:s=null,u.label=4;case 4:return o=s,[3,7];case 5:return[4,p(t,e[c],n,r)];case 6:o=u.sent(),u.label=7;case 7:return[3,13];case 8:return[4,b(o,n,r)];case 9:return(a=u.sent())?[4,p(a,"".concat(v," ").concat(e[c]),n,r)]:[3,11];case 10:return l=u.sent(),[3,12];case 11:l=null,u.label=12;case 12:o=l,u.label=13;case 13:if(null===o)return[2,null];u.label=14;case 14:return c++,[3,1];case 15:return[2,o]}}))}))}function H(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s,a,l;return f(this,(function(u){switch(u.label){case 0:return o=function(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}([],e,!0),i=o.pop(),o.length?[3,2]:[4,p(t,i,n,r,!0)];case 1:return[2,u.sent()];case 2:return[4,m(o,t,n,r)];case 3:return(c=u.sent())?[4,b(c,n,r)]:[3,5];case 4:return a=u.sent(),[3,6];case 5:a=null,u.label=6;case 6:return(s=a)?[4,p(s,"".concat(v," ").concat(i),n,r,!0)]:[3,8];case 7:return l=u.sent(),[3,9];case 8:l=null,u.label=9;case 9:return[2,l]}}))}))}function L(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i;return f(this,(function(c){switch(c.label){case 0:if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(R());return[2,b(t,n,r)]}return[4,m(e,t,n,r)];case 1:return(o=c.sent())?[4,b(o,n,r)]:[3,3];case 2:return i=c.sent(),[3,4];case 3:i=null,c.label=4;case 4:return[2,i]}}))}))}function g(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s;return f(this,(function(a){switch(a.label){case 0:o=S(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(_("asyncQuerySelector","asyncShadowRootQuerySelector"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,m(o[c],t,n,r)]:[3,4];case 2:if(s=a.sent())return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function I(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s;return f(this,(function(a){switch(a.label){case 0:o=S(e,(function(e){if(!e[e.length-1].length)throw new SyntaxError(_("asyncQuerySelectorAll"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,H(o[c],t,n,r)]:[3,4];case 2:if(null==(s=a.sent())?void 0:s.length)return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,document.querySelectorAll(A)]}}))}))}function T(e,t,n,r){return h(this,void 0,void 0,(function(){var o,i,c,s;return f(this,(function(a){switch(a.label){case 0:o=S(e,(function(e){if(e.pop().length)throw new SyntaxError(O("asyncShadowRootQuerySelector","asyncQuerySelector"));return e})),i=o.length,c=0,a.label=1;case 1:return c<i?[4,L(o[c],t,n,r)]:[3,4];case 2:if(s=a.sent())return[2,s];a.label=3;case 3:return c++,[3,1];case 4:return[2,null]}}))}))}function N(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t,n,r,o,i;return f(this,(function(c){switch(c.label){case 0:return y(e)?(t=e[0],n=e[1],r=e[2],[4,g(n,t,(null==r?void 0:r.retries)||E,(null==r?void 0:r.delay)||w)]):[3,2];case 1:case 3:return[2,c.sent()];case 2:return o=e[0],i=e[1],[4,g(o,document,(null==i?void 0:i.retries)||E,(null==i?void 0:i.delay)||w)]}}))}))}const P=(e,n,r=null,i=!1)=>Object.entries(n||{}).reduce(((n,c)=>{const[s,a]=c;if(a.selector===o&&r)return a.children?Object.assign(Object.assign({},n),P(e,a.children,r,!0)):n;const l=r?r.then((t=>{return N(t,(n=a.selector,i?"$ "+n:n),e);var n})):N(a.selector,e);return n[s]={element:l,children:P(e,a.children,l),querySelector(n,r={}){return t(this,void 0,void 0,(function*(){const t=yield l;return yield N(t,n,Object.assign(Object.assign({},e),r))}))},querySelectorAll(n,r={}){return t(this,void 0,void 0,(function*(){const t=yield l;return yield function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t,n,r,o,i;return f(this,(function(c){switch(c.label){case 0:return y(e)?(t=e[0],n=e[1],r=e[2],[4,I(n,t,(null==r?void 0:r.retries)||E,(null==r?void 0:r.delay)||w)]):[3,2];case 1:return[2,c.sent()];case 2:return o=e[0],i=e[1],[2,I(o,document,(null==i?void 0:i.retries)||E,(null==i?void 0:i.delay)||w)]}}))}))}(t,n,Object.assign(Object.assign({},e),r))}))},shadowRootQuerySelector(n,r={}){return t(this,void 0,void 0,(function*(){const t=yield l;return yield function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return h(this,void 0,void 0,(function(){var t,n,r,o,i;return f(this,(function(c){switch(c.label){case 0:return y(e)?(t=e[0],n=e[1],r=e[2],[4,T(n,t,(null==r?void 0:r.retries)||E,(null==r?void 0:r.delay)||w)]):[3,2];case 1:return[2,c.sent()];case 2:return o=e[0],i=e[1],[2,T(o,document,(null==i?void 0:i.retries)||E,(null==i?void 0:i.delay)||w)]}}))}))}(t,n,Object.assign(Object.assign({},e),r))}))}},n}),{}),M=(e,t)=>{const n=Object.entries(t);for(const t of n){if(t[0]===e)return t[1];{const n=M(e,t[1].children);if(n)return n}}},j=(e,t)=>Object.keys(e).reduce(((e,n)=>{const r=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(M(n,t),["children"]);return e[n]=Object.assign({},r),e}),{});var D,V,W,x,k,Q,C,U,q,B,$,G;class Y extends EventTarget{constructor(e={}){super(),D.add(this),V.set(this,void 0),W.set(this,void 0),x.set(this,void 0),k.set(this,void 0),Q.set(this,void 0),C.set(this,void 0),U.set(this,void 0),r(this,V,Object.assign(Object.assign({},i),e),"f")}listen(){var e;r(this,U,n(this,D,"m",G).bind(this),"f"),n(this,D,"m",B).call(this),n(this,D,"m",$).call(this),null===(e=n(this,C,"f"))||void 0===e||e.disconnect(),r(this,C,new MutationObserver(n(this,U,"f")),"f"),n(this,Q,"f")[s.PARTIAL_PANEL_RESOLVER].element.then((e=>{n(this,C,"f").observe(e,{childList:!0})}))}}return V=new WeakMap,W=new WeakMap,x=new WeakMap,k=new WeakMap,Q=new WeakMap,C=new WeakMap,U=new WeakMap,D=new WeakSet,q=function(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))},B=function(){r(this,W,P(n(this,V,"f"),l),"f"),r(this,k,j(c,n(this,W,"f")),"f")},$=function(){r(this,x,P(n(this,V,"f"),u,n(this,k,"f")[c.HA_DRAWER].element),"f"),r(this,Q,j(s,n(this,x,"f")),"f"),n(this,D,"m",q).call(this,e.HAQuerySelectorEvent.ON_LOVELACE_PANEL_LOAD,Object.assign(Object.assign({},n(this,k,"f")),n(this,Q,"f")))},G=function(e){e.forEach((({addedNodes:e})=>{e.forEach((e=>{e.localName===a.HA_PANEL_LOVELACE&&n(this,D,"m",$).call(this)}))}))},e.HAQuerySelector=Y,e}({});
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "home-assistant-query-selector",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Easily query home-assistant DOM elements in an async way",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"query-selector",
|
|
7
|
+
"dom",
|
|
8
|
+
"dom-query",
|
|
9
|
+
"home-assistant",
|
|
10
|
+
"home-assistant-frontend",
|
|
11
|
+
"javascript-library",
|
|
12
|
+
"library"
|
|
13
|
+
],
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"module": "dist/esm/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"require": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/esm/index.d.ts",
|
|
25
|
+
"default": "./dist/esm/index.js"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist/**/*"
|
|
31
|
+
],
|
|
32
|
+
"author": "ElChiniNet",
|
|
33
|
+
"license": "Apache-2.0",
|
|
34
|
+
"scripts": {
|
|
35
|
+
"clean": "rm -rf dist .nyc_output coverage || true",
|
|
36
|
+
"build": "yarn clean && rollup --config rollup.config.js --bundleConfigAsCjs",
|
|
37
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
38
|
+
"coverage:report": "nyc report --reporter=lcov --reporter=text-summary",
|
|
39
|
+
"test:ts": "tsc --noEmit",
|
|
40
|
+
"test:run": "cypress run",
|
|
41
|
+
"test:open": "cypress open",
|
|
42
|
+
"test:ci": "yarn demo:ha && yarn test:run && yarn coverage:report && yarn stop:ha",
|
|
43
|
+
"test:copy": "cp test/index.js .hass/config/www/home-assistant-query-selector-tests.js",
|
|
44
|
+
"start:ha": "docker run --rm -d -p8123:8123 -v ${PWD}/.hass/config:/config homeassistant/home-assistant:2023.11.1",
|
|
45
|
+
"stop:ha": "docker stop $(docker ps -a -q --filter ancestor=homeassistant/home-assistant:2023.11.1) || true",
|
|
46
|
+
"demo:ha": "yarn build && yarn test:copy && yarn start:ha",
|
|
47
|
+
"prepare": "yarn build",
|
|
48
|
+
"prepublishOnly": "yarn lint && yarn test:ts && yarn test:ci",
|
|
49
|
+
"version": "git add .",
|
|
50
|
+
"postversion": "git push && git push --tags"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"shadow-dom-selector": "^1.0.2"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@cypress/code-coverage": "^3.12.10",
|
|
57
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
58
|
+
"@types/eslint": "^8.44.7",
|
|
59
|
+
"@typescript-eslint/eslint-plugin": "^6.11.0",
|
|
60
|
+
"@typescript-eslint/parser": "^6.11.0",
|
|
61
|
+
"cypress": "^13.5.1",
|
|
62
|
+
"eslint": "^8.53.0",
|
|
63
|
+
"rollup": "^4.4.1",
|
|
64
|
+
"rollup-plugin-istanbul": "^5.0.0",
|
|
65
|
+
"rollup-plugin-serve": "^2.0.2",
|
|
66
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
67
|
+
"rollup-plugin-ts": "^3.4.5",
|
|
68
|
+
"typescript": "^5.2.2"
|
|
69
|
+
}
|
|
70
|
+
}
|