11import type { dxElementWrapper } from '@js/core/renderer' ;
2- import type CollectionWidget from '@ts/ui/collection/m_collection_widget.edit' ;
3- import type { CollectionWidgetEditProperties } from '@ts/ui/collection/m_collection_widget.edit' ;
4-
5- import EditStrategy from './m_collection_widget.edit.strategy' ;
2+ import type { CollectionWidgetItem } from '@js/ui/collection/ui.collection_widget.base' ;
3+ import type { CollectionItemIndex } from '@ts/ui/collection/m_collection_widget.edit.strategy' ;
4+ import EditStrategy from '@ts/ui/collection/m_collection_widget.edit.strategy' ;
65
76class PlainEditStrategy <
8- // @ts -expect-error
9- TComponent extends CollectionWidget < CollectionWidgetEditProperties > = CollectionWidget < CollectionWidgetEditProperties > ,
10- > extends EditStrategy < TComponent > {
11- _getPlainItems ( ) {
12- return this . _collectionWidget . option ( 'items' ) || [ ] ;
7+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8+ TItem extends CollectionWidgetItem = any ,
9+ > extends EditStrategy < TItem > {
10+ _getPlainItems ( ) : TItem [ ] {
11+ return this . _getItems ( ) ?? [ ] ;
1312 }
1413
15- getIndexByItemData ( itemData ) {
14+ getIndexByItemData ( itemData : TItem ) : number {
1615 const keyOf = this . _collectionWidget . keyOf . bind ( this . _collectionWidget ) ;
1716 if ( keyOf ) {
1817 return this . getIndexByKey ( keyOf ( itemData ) ) ;
1918 }
2019 return this . _getPlainItems ( ) . indexOf ( itemData ) ;
2120 }
2221
23- getItemDataByIndex ( index ) {
22+ getItemDataByIndex ( index : number ) : TItem {
2423 return this . _getPlainItems ( ) [ index ] ;
2524 }
2625
2726 deleteItemAtIndex ( index : number ) : void {
2827 this . _getPlainItems ( ) . splice ( index , 1 ) ;
2928 }
3029
31- itemsGetter ( ) {
30+ itemsGetter ( ) : TItem [ ] {
3231 return this . _getPlainItems ( ) ;
3332 }
3433
35- getKeysByItems ( items ) {
34+ getKeysByItems ( items : TItem [ ] ) : ( string | number ) [ ] {
3635 const keyOf = this . _collectionWidget . keyOf . bind ( this . _collectionWidget ) ;
37- let result = items ;
36+ let result : ( string | number | TItem ) [ ] = items ;
3837 if ( keyOf ) {
39- result = [ ] ;
40- for ( let i = 0 ; i < items . length ; i ++ ) {
41- result . push ( keyOf ( items [ i ] ) ) ;
42- }
38+ result = items . map ( ( item ) => keyOf ( item ) as string | number ) ;
4339 }
44- return result ;
40+ return result as ( string | number ) [ ] ;
4541 }
4642
47- getIndexByKey ( key ) : number {
43+ getIndexByKey ( key : string | number ) : number {
4844 const cache = this . _cache ;
49- const keys = cache && cache . keys || this . getKeysByItems ( this . _getPlainItems ( ) ) ;
45+ const keys = cache ? .keys ?? this . getKeysByItems ( this . _getPlainItems ( ) ) ;
5046
5147 if ( cache && ! cache . keys ) {
5248 cache . keys = keys ;
5349 }
5450
5551 if ( typeof key === 'object' ) {
56- for ( let i = 0 , { length } = keys ; i < length ; i ++ ) {
52+ for ( let i = 0 ; i < keys . length ; i += 1 ) {
5753 if ( this . _equalKeys ( key , keys [ i ] ) ) return i ;
5854 }
5955 } else {
@@ -63,11 +59,15 @@ class PlainEditStrategy<
6359 return - 1 ;
6460 }
6561
66- getItemsByKeys ( keys , items ) {
67- return ( items || keys ) . slice ( ) ;
62+ // eslint-disable-next-line class-methods-use-this
63+ getItemsByKeys ( keys : ( string | number ) [ ] , items ?: TItem [ ] ) : TItem [ ] {
64+ return ( items ?? keys ) . slice ( ) as TItem [ ] ;
6865 }
6966
70- moveItemAtIndexToIndex ( movingIndex , destinationIndex ) : void {
67+ moveItemAtIndexToIndex (
68+ movingIndex : number ,
69+ destinationIndex : number ,
70+ ) : void {
7171 const items = this . _getPlainItems ( ) ;
7272 const movedItemData = items [ movingIndex ] ;
7373
@@ -76,29 +76,40 @@ class PlainEditStrategy<
7676 }
7777
7878 // eslint-disable-next-line class-methods-use-this
79- _isItemIndex ( index : Element | number ) : boolean {
79+ _isItemIndex ( index : number | Element | TItem ) : boolean {
8080 return ( typeof index === 'number' ) && Math . round ( index ) === index ;
8181 }
8282
8383 _getNormalizedItemIndex ( itemElement : Element ) : number {
8484 return this . _collectionWidget . _itemElements ( ) . index ( itemElement ) ;
8585 }
8686
87- _normalizeItemIndex ( index ) {
88- return index ;
87+ // eslint-disable-next-line class-methods-use-this
88+ _normalizeItemIndex (
89+ index : CollectionItemIndex ,
90+ ) : number {
91+ return index as number ;
8992 }
9093
91- _denormalizeItemIndex ( index ) {
94+ // eslint-disable-next-line class-methods-use-this
95+ _denormalizeItemIndex (
96+ index : number ,
97+ ) : CollectionItemIndex {
9298 return index ;
9399 }
94100
95101 _getItemByNormalizedIndex ( index : number ) : dxElementWrapper {
96- // @ts -expect-error ts-error
102+ // @ts -expect-error - eq() can return null but we handle it appropriately
97103 return index > - 1 ? this . _collectionWidget . _itemElements ( ) . eq ( index ) : null ;
98104 }
99105
100- // eslint-disable-next-line @typescript-eslint/no-unused-vars
101- _itemsFromSameParent ( firstIndex , secondIndex ) : boolean {
106+ // eslint-disable-next-line class-methods-use-this
107+ _itemsFromSameParent (
108+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
109+ _firstIndex : number ,
110+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
111+ _secondIndex : number ,
112+ ) : boolean {
102113 return true ;
103114 }
104115}
0 commit comments