1- import { onChildEvent } from '../services/dom.ts ' ;
1+ import { onChildEvent } from '../services/dom' ;
22import { Component } from './component' ;
33
4- /**
5- * @typedef EntitySelectorSearchOptions
6- * @property entityTypes string
7- * @property entityPermission string
8- * @property searchEndpoint string
9- * @property initialValue string
10- */
11-
12- /**
13- * Entity Selector
14- */
4+ export interface EntitySelectorSearchOptions {
5+ entityTypes : string ;
6+ entityPermission : string ;
7+ searchEndpoint : string ;
8+ initialValue : string ;
9+ }
10+
11+ export type EntitySelectorEntity = {
12+ id : number ,
13+ name : string ,
14+ link : string ,
15+ } ;
16+
1517export class EntitySelector extends Component {
18+ protected elem ! : HTMLElement ;
19+ protected input ! : HTMLInputElement ;
20+ protected searchInput ! : HTMLInputElement ;
21+ protected loading ! : HTMLElement ;
22+ protected resultsContainer ! : HTMLElement ;
23+
24+ protected searchOptions ! : EntitySelectorSearchOptions ;
25+
26+ protected search = '' ;
27+ protected lastClick = 0 ;
1628
1729 setup ( ) {
1830 this . elem = this . $el ;
1931
20- this . input = this . $refs . input ;
21- this . searchInput = this . $refs . search ;
32+ this . input = this . $refs . input as HTMLInputElement ;
33+ this . searchInput = this . $refs . search as HTMLInputElement ;
2234 this . loading = this . $refs . loading ;
2335 this . resultsContainer = this . $refs . results ;
2436
@@ -29,9 +41,6 @@ export class EntitySelector extends Component {
2941 initialValue : this . searchInput . value || '' ,
3042 } ;
3143
32- this . search = '' ;
33- this . lastClick = 0 ;
34-
3544 this . setupListeners ( ) ;
3645 this . showLoading ( ) ;
3746
@@ -40,16 +49,13 @@ export class EntitySelector extends Component {
4049 }
4150 }
4251
43- /**
44- * @param {EntitySelectorSearchOptions } options
45- */
46- configureSearchOptions ( options ) {
52+ configureSearchOptions ( options : Partial < EntitySelectorSearchOptions > ) : void {
4753 Object . assign ( this . searchOptions , options ) ;
4854 this . reset ( ) ;
4955 this . searchInput . value = this . searchOptions . initialValue ;
5056 }
5157
52- setupListeners ( ) {
58+ setupListeners ( ) : void {
5359 this . elem . addEventListener ( 'click' , this . onClick . bind ( this ) ) ;
5460
5561 let lastSearch = 0 ;
@@ -67,7 +73,7 @@ export class EntitySelector extends Component {
6773 } ) ;
6874
6975 // Keyboard navigation
70- onChildEvent ( this . $el , '[data-entity-type]' , 'keydown' , event => {
76+ onChildEvent ( this . $el , '[data-entity-type]' , 'keydown' , ( ( event : KeyboardEvent ) => {
7177 if ( event . ctrlKey && event . code === 'Enter' ) {
7278 const form = this . $el . closest ( 'form' ) ;
7379 if ( form ) {
@@ -83,7 +89,7 @@ export class EntitySelector extends Component {
8389 if ( event . code === 'ArrowUp' ) {
8490 this . focusAdjacent ( false ) ;
8591 }
86- } ) ;
92+ } ) as ( event : Event ) => void ) ;
8793
8894 this . searchInput . addEventListener ( 'keydown' , event => {
8995 if ( event . code === 'ArrowDown' ) {
@@ -93,10 +99,10 @@ export class EntitySelector extends Component {
9399 }
94100
95101 focusAdjacent ( forward = true ) {
96- const items = Array . from ( this . resultsContainer . querySelectorAll ( '[data-entity-type]' ) ) ;
102+ const items : ( Element | null ) [ ] = Array . from ( this . resultsContainer . querySelectorAll ( '[data-entity-type]' ) ) ;
97103 const selectedIndex = items . indexOf ( document . activeElement ) ;
98104 const newItem = items [ selectedIndex + ( forward ? 1 : - 1 ) ] || items [ 0 ] ;
99- if ( newItem ) {
105+ if ( newItem instanceof HTMLElement ) {
100106 newItem . focus ( ) ;
101107 }
102108 }
@@ -132,7 +138,7 @@ export class EntitySelector extends Component {
132138 }
133139
134140 window . $http . get ( this . searchUrl ( ) ) . then ( resp => {
135- this . resultsContainer . innerHTML = resp . data ;
141+ this . resultsContainer . innerHTML = resp . data as string ;
136142 this . hideLoading ( ) ;
137143 } ) ;
138144 }
@@ -142,15 +148,15 @@ export class EntitySelector extends Component {
142148 return `${ this . searchOptions . searchEndpoint } ?${ query } ` ;
143149 }
144150
145- searchEntities ( searchTerm ) {
151+ searchEntities ( searchTerm : string ) {
146152 if ( ! this . searchOptions . searchEndpoint ) {
147153 throw new Error ( 'Search endpoint not set for entity-selector load' ) ;
148154 }
149155
150156 this . input . value = '' ;
151157 const url = `${ this . searchUrl ( ) } &term=${ encodeURIComponent ( searchTerm ) } ` ;
152158 window . $http . get ( url ) . then ( resp => {
153- this . resultsContainer . innerHTML = resp . data ;
159+ this . resultsContainer . innerHTML = resp . data as string ;
154160 this . hideLoading ( ) ;
155161 } ) ;
156162 }
@@ -162,16 +168,16 @@ export class EntitySelector extends Component {
162168 return answer ;
163169 }
164170
165- onClick ( event ) {
166- const listItem = event . target . closest ( '[data-entity-type]' ) ;
167- if ( listItem ) {
171+ onClick ( event : MouseEvent ) {
172+ const listItem = ( event . target as HTMLElement ) . closest ( '[data-entity-type]' ) ;
173+ if ( listItem instanceof HTMLElement ) {
168174 event . preventDefault ( ) ;
169175 event . stopPropagation ( ) ;
170176 this . selectItem ( listItem ) ;
171177 }
172178 }
173179
174- selectItem ( item ) {
180+ selectItem ( item : HTMLElement ) : void {
175181 const isDblClick = this . isDoubleClick ( ) ;
176182 const type = item . getAttribute ( 'data-entity-type' ) ;
177183 const id = item . getAttribute ( 'data-entity-id' ) ;
@@ -180,14 +186,14 @@ export class EntitySelector extends Component {
180186 this . unselectAll ( ) ;
181187 this . input . value = isSelected ? `${ type } :${ id } ` : '' ;
182188
183- const link = item . getAttribute ( 'href' ) ;
184- const name = item . querySelector ( '.entity-list-item-name' ) . textContent ;
185- const data = { id : Number ( id ) , name, link} ;
189+ const link = item . getAttribute ( 'href' ) || '' ;
190+ const name = item . querySelector ( '.entity-list-item-name' ) ? .textContent || '' ;
191+ const data : EntitySelectorEntity = { id : Number ( id ) , name, link} ;
186192
187193 if ( isSelected ) {
188194 item . classList . add ( 'selected' ) ;
189195 } else {
190- window . $events . emit ( 'entity-select-change' , null ) ;
196+ window . $events . emit ( 'entity-select-change' ) ;
191197 }
192198
193199 if ( ! isDblClick && ! isSelected ) return ;
@@ -200,7 +206,7 @@ export class EntitySelector extends Component {
200206 }
201207 }
202208
203- confirmSelection ( data ) {
209+ confirmSelection ( data : EntitySelectorEntity ) {
204210 window . $events . emit ( 'entity-select-confirm' , data ) ;
205211 }
206212
0 commit comments