@@ -83,30 +83,18 @@ export class FocusController extends core.ViewController {
8383 if ( ! this . option ( 'focusedRowEnabled' ) ) {
8484 return ;
8585 }
86- const isEmptyData = this . getDataController ( ) . isEmpty ( ) ;
87- const currentIndex = this . _getCurrentFocusRowIndex ( isEmptyData , index ) ;
86+
87+ const currentIndex = index !== undefined ? index : this . option ( 'focusedRowIndex' ) ;
8888
8989 if ( currentIndex < 0 ) {
90- if ( isEmptyData || this . isAutoNavigateToFocusedRow ( ) ) {
90+ if ( this . isAutoNavigateToFocusedRow ( ) ) {
9191 this . _resetFocusedRow ( ) ;
9292 }
9393 } else {
9494 this . _focusRowByIndexCore ( currentIndex , operationTypes ) ;
9595 }
9696 }
9797
98- private _getCurrentFocusRowIndex ( isEmptyData , index ?) : number {
99- let currentIndex = index ;
100- if ( currentIndex === undefined ) {
101- if ( isEmptyData ) {
102- currentIndex = - 1 ;
103- } else {
104- currentIndex = this . option ( 'focusedRowIndex' ) ;
105- }
106- }
107- return currentIndex ;
108- }
109-
11098 private _focusRowByIndexCore ( index , operationTypes ) {
11199 const pageSize = this . getDataController ( ) . pageSize ( ) ;
112100 const setKeyByIndex = ( ) => {
@@ -559,7 +547,7 @@ const columns = (Base: ModuleType<ColumnsController>) => class FocusColumnsExten
559547} ;
560548
561549const data = ( Base : ModuleType < DataController > ) => class FocusDataControllerExtender extends Base {
562- private _needToUpdateFocusedRowByIndex = false ;
550+ private _isDataPushed = false ;
563551
564552 protected _applyChange ( change ) {
565553 if ( change && change . changeType === 'updateFocusedRow' ) return ;
@@ -571,20 +559,23 @@ const data = (Base: ModuleType<DataController>) => class FocusDataControllerExte
571559 protected _fireChanged ( e ) {
572560 super . _fireChanged ( e ) ;
573561
562+ const forceUpdateFocusedRow = this . _isDataPushed ;
563+
564+ this . _isDataPushed = false ;
565+
574566 if ( this . option ( 'focusedRowEnabled' ) && this . _dataSource ) {
575567 const isPartialUpdate = e . changeType === 'update' && e . repaintChangesOnly ;
576568 const isPartialUpdateWithDeleting = isPartialUpdate && e . changeTypes && e . changeTypes . indexOf ( 'remove' ) >= 0 ;
577569
578- if ( this . _needToUpdateFocusedRowByIndex ) {
579- this . _needToUpdateFocusedRowByIndex = false ;
580- this . _focusController . _focusRowByIndex ( ) ;
570+ if ( forceUpdateFocusedRow && this . isEmpty ( ) ) {
571+ this . _focusController . _resetFocusedRow ( ) ;
581572 } else if ( e . changeType === 'refresh' && e . items . length || isPartialUpdateWithDeleting ) {
582573 this . _updatePageIndexes ( ) ;
583- this . _updateFocusedRow ( e ) ;
574+ this . _updateFocusedRowIfNeeded ( e , forceUpdateFocusedRow ) ;
584575 } else if ( e . changeType === 'append' || e . changeType === 'prepend' ) {
585576 this . _updatePageIndexes ( ) ;
586- } else if ( e . changeType === 'update' && e . repaintChangesOnly ) {
587- this . _updateFocusedRow ( e ) ;
577+ } else if ( isPartialUpdate ) {
578+ this . _updateFocusedRowIfNeeded ( e , forceUpdateFocusedRow ) ;
588579 }
589580 }
590581 }
@@ -594,7 +585,7 @@ const data = (Base: ModuleType<DataController>) => class FocusDataControllerExte
594585
595586 const focusedRowKey = this . option ( 'focusedRowKey' ) ;
596587
597- this . _needToUpdateFocusedRowByIndex = changes ?. some ( ( change ) => change . type === 'remove' && equalByValue ( change . key , focusedRowKey ) ) ;
588+ this . _isDataPushed = isDefined ( focusedRowKey ) && ! ! changes . length ;
598589 }
599590
600591 private _updatePageIndexes ( ) {
@@ -609,7 +600,7 @@ const data = (Base: ModuleType<DataController>) => class FocusDataControllerExte
609600 return this . _isPagingByRendering ;
610601 }
611602
612- private _updateFocusedRow ( e ) {
603+ private _updateFocusedRowIfNeeded ( e , forceUpdate = false ) {
613604 const operationTypes = e . operationTypes || { } ;
614605 const {
615606 reload, fullReload, pageIndex, paging,
@@ -619,30 +610,44 @@ const data = (Base: ModuleType<DataController>) => class FocusDataControllerExte
619610 const focusedRowKey = this . option ( 'focusedRowKey' ) ;
620611 const isAutoNavigate = this . _focusController . isAutoNavigateToFocusedRow ( ) ;
621612 const isReload = reload && pageIndex === false ;
622- if ( isReload && ! fullReload && isDefined ( focusedRowKey ) ) {
623- this . _focusController . _navigateToRow ( focusedRowKey , true )
624- . done ( ( focusedRowIndex ) => {
625- if ( focusedRowIndex < 0 ) {
626- this . _focusController . _focusRowByIndex ( undefined , operationTypes ) ;
627- }
628- } ) ;
629- } else if ( pagingWithoutVirtualScrolling && isAutoNavigate ) {
630- const rowIndexByKey = this . getRowIndexByKey ( focusedRowKey ) ;
631- const focusedRowIndex = this . option ( 'focusedRowIndex' ) ! ;
632- const isValidRowIndexByKey = rowIndexByKey >= 0 ;
633- const isValidFocusedRowIndex = focusedRowIndex >= 0 ;
634- const isSameRowIndex = focusedRowIndex === rowIndexByKey ;
635- if ( isValidFocusedRowIndex && ( isSameRowIndex || ! isValidRowIndexByKey ) ) {
636- this . _focusController . _focusRowByIndex ( focusedRowIndex , operationTypes ) ;
613+ const rowIndexByKey = this . getRowIndexByKey ( focusedRowKey ) ;
614+
615+ switch ( true ) {
616+ case forceUpdate : {
617+ this . _focusController . _focusRowByKeyOrIndex ( ) ;
618+ break ;
619+ }
620+ case isReload && ! fullReload && isDefined ( focusedRowKey ) : {
621+ this . _focusController . _navigateToRow ( focusedRowKey , true )
622+ . done ( ( focusedRowIndex ) => {
623+ if ( focusedRowIndex < 0 ) {
624+ this . _focusController . _focusRowByIndex ( undefined , operationTypes ) ;
625+ }
626+ } ) ;
627+ break ;
628+ }
629+ case pagingWithoutVirtualScrolling && isAutoNavigate : {
630+ const focusedRowIndex = this . option ( 'focusedRowIndex' ) ! ;
631+ const isValidRowIndexByKey = rowIndexByKey >= 0 ;
632+ const isValidFocusedRowIndex = focusedRowIndex >= 0 ;
633+ const isSameRowIndex = focusedRowIndex === rowIndexByKey ;
634+
635+ if ( isValidFocusedRowIndex && ( isSameRowIndex || ! isValidRowIndexByKey ) ) {
636+ this . _focusController . _focusRowByIndex ( focusedRowIndex , operationTypes ) ;
637+ }
638+ break ;
639+ }
640+ case pagingWithoutVirtualScrolling && ! isAutoNavigate && ( rowIndexByKey < 0 ) : {
641+ this . option ( 'focusedRowIndex' , - 1 ) ;
642+ break ;
643+ }
644+ case operationTypes . fullReload : {
645+ this . _focusController . _focusRowByKeyOrIndex ( ) ;
646+ break ;
647+ }
648+ default : {
649+ break ;
637650 }
638- } else if (
639- pagingWithoutVirtualScrolling
640- && ! isAutoNavigate
641- && ( this . getRowIndexByKey ( focusedRowKey ) < 0 )
642- ) {
643- this . option ( 'focusedRowIndex' , - 1 ) ;
644- } else if ( operationTypes . fullReload ) {
645- this . _focusController . _focusRowByKeyOrIndex ( ) ;
646651 }
647652 }
648653
0 commit comments