-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GridCore: Refactoring of the fluent borders related code. (#26393)
- Loading branch information
1 parent
d136b0f
commit fd8c47d
Showing
2 changed files
with
118 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/devextreme/js/__internal/grids/grid_core/views/utils/update_views_borders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-return */ | ||
import { dxElementWrapper } from '@js/core/renderer'; | ||
import { isDefined } from '@js/core/utils/type'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type View = any; | ||
|
||
// TODO: Move to the grid_core/m_types views interface. | ||
export interface ViewsWithBorder { | ||
columnHeadersView: View; | ||
rowsView: View; | ||
filterPanelView: View; | ||
footerView: View; | ||
} | ||
|
||
const CLASSES = { | ||
borderedTop: 'dx-bordered-top-view', | ||
borderedBottom: 'dx-bordered-bottom-view', | ||
}; | ||
|
||
const getFirstVisibleViewElement = ({ | ||
columnHeadersView, | ||
rowsView, | ||
}: ViewsWithBorder): dxElementWrapper => { | ||
if (columnHeadersView?.isVisible()) { | ||
return columnHeadersView.element(); | ||
} | ||
|
||
return rowsView.element(); | ||
}; | ||
|
||
const getLastVisibleViewElement = ({ | ||
filterPanelView, | ||
footerView, | ||
rowsView, | ||
}: ViewsWithBorder): dxElementWrapper => { | ||
if (filterPanelView?.isVisible()) { | ||
return filterPanelView.element(); | ||
} | ||
|
||
if (footerView?.isVisible()) { | ||
return footerView.element(); | ||
} | ||
|
||
return rowsView.element(); | ||
}; | ||
|
||
const getViewElementWithClass = ( | ||
viewsWithBorder: ViewsWithBorder, | ||
className: string, | ||
): dxElementWrapper | null => { | ||
const borderedView = Object.values(viewsWithBorder) | ||
.find((view) => view?.element()?.hasClass(className)); | ||
|
||
return borderedView?.element() ?? null; | ||
}; | ||
|
||
const shouldUpdateBorders = ( | ||
viewName: string, | ||
viewsWithBorder: ViewsWithBorder, | ||
): boolean => { | ||
if (!Object.keys(viewsWithBorder).includes(viewName)) { | ||
return false; | ||
} | ||
|
||
const { rowsView, ...otherViews } = viewsWithBorder; | ||
if (!isDefined(rowsView?.element?.())) { | ||
return false; | ||
} | ||
|
||
return Object.values(otherViews) | ||
.filter((view) => view?.isVisible?.()) | ||
.every((view) => isDefined(view?.element())); | ||
}; | ||
|
||
export const updateViewsBorders = ( | ||
viewName: string, | ||
viewsWithBorder: ViewsWithBorder, | ||
): void => { | ||
if (!shouldUpdateBorders(viewName, viewsWithBorder)) { | ||
return; | ||
} | ||
|
||
const $oldFirst = getViewElementWithClass(viewsWithBorder, CLASSES.borderedTop); | ||
const $oldLast = getViewElementWithClass(viewsWithBorder, CLASSES.borderedBottom); | ||
const $newFirst = getFirstVisibleViewElement(viewsWithBorder); | ||
const $newLast = getLastVisibleViewElement(viewsWithBorder); | ||
|
||
// @ts-expect-error The dxElementWrapper's "is" method is badly typed. | ||
if ($oldFirst && !$oldFirst.is($newFirst)) { | ||
$oldFirst.removeClass(CLASSES.borderedTop); | ||
} | ||
|
||
// @ts-expect-error The dxElementWrapper's "is" method is badly typed. | ||
if ($oldLast && !$oldLast.is($newLast)) { | ||
$oldLast.removeClass(CLASSES.borderedBottom); | ||
} | ||
|
||
if (!$newFirst.hasClass(CLASSES.borderedTop)) { | ||
$newFirst.addClass(CLASSES.borderedTop); | ||
} | ||
|
||
if (!$newLast.hasClass(CLASSES.borderedBottom)) { | ||
$newLast.addClass(CLASSES.borderedBottom); | ||
} | ||
}; |