Skip to content

Commit 4ded346

Browse files
authored
Scheduler: simplify grouping functions (DevExpress#31365)
1 parent 5db5e68 commit 4ded346

File tree

14 files changed

+90
-234
lines changed

14 files changed

+90
-234
lines changed

packages/devextreme/js/__internal/scheduler/m_table_creator.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { data as elementData } from '@js/core/element_data';
44
import $ from '@js/core/renderer';
55
import { isFunction } from '@js/core/utils/type';
66

7+
import type { GroupNode } from './utils/resource_manager/types';
8+
79
const ROW_SELECTOR = 'tr';
810

911
class SchedulerTableCreator {
@@ -139,16 +141,14 @@ class SchedulerTableCreator {
139141
return rows;
140142
}
141143

142-
makeGroupedTableFromJSON(type, data, config) {
144+
makeGroupedTableFromJSON(tree: GroupNode[], config?) {
143145
let table;
144146
const cellStorage: any[] = [];
145147
let rowIndex = 0;
146148

147149
config = config || {};
148150

149151
const cellTag = config.cellTag || 'td';
150-
const childrenField = config.childrenField || 'children';
151-
const titleField = config.titleField || 'title';
152152
const { groupTableClass } = config;
153153
const { groupRowClass } = config;
154154
const { groupCellClass } = config;
@@ -162,14 +162,14 @@ class SchedulerTableCreator {
162162
}
163163
}
164164

165-
function getChildCount(item) {
166-
if (item[childrenField]) {
167-
return item[childrenField].length;
165+
function getChildCount(item: GroupNode) {
166+
if (item.children) {
167+
return item.children.length;
168168
}
169169
return 0;
170170
}
171171

172-
function createCell(text, childCount, index, data) {
172+
function createCell(text: string, childCount, index, node: GroupNode) {
173173
const cell = {
174174
element: domAdapter.createElement(cellTag),
175175
childCount,
@@ -181,26 +181,26 @@ class SchedulerTableCreator {
181181

182182
const cellText = (domAdapter as any).createTextNode(text);
183183
if (typeof groupCellCustomContent === 'function') {
184-
groupCellCustomContent(cell.element, cellText, index, data);
184+
groupCellCustomContent(cell.element, cellText, index, node);
185185
} else {
186186
cell.element.appendChild(cellText);
187187
}
188188

189189
return cell;
190190
}
191191

192-
function generateCells(data) {
193-
for (let i = 0; i < data.length; i++) {
194-
const childCount = getChildCount(data[i]);
195-
const cell = createCell(data[i][titleField], childCount, i, data[i]);
192+
function generateCells(groupNodes: GroupNode[]) {
193+
for (let i = 0; i < groupNodes.length; i++) {
194+
const childCount = getChildCount(groupNodes[i]);
195+
const cell = createCell(groupNodes[i].resourceText, childCount, i, groupNodes[i]);
196196

197197
if (!cellStorage[rowIndex]) {
198198
cellStorage[rowIndex] = [];
199199
}
200200
cellStorage[rowIndex].push(cell);
201201

202202
if (childCount) {
203-
generateCells(data[i][childrenField]);
203+
generateCells(groupNodes[i].children);
204204
} else {
205205
rowIndex++;
206206
}
@@ -238,7 +238,7 @@ class SchedulerTableCreator {
238238
}
239239

240240
createTable();
241-
generateCells(data);
241+
generateCells(tree);
242242
putCellsToRows();
243243

244244
return table;

packages/devextreme/js/__internal/scheduler/r1/components/base/date_header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class DateHeader extends BaseInfernoComponent<DateHeaderProps> {
4343
groupOrientation,
4444
groups,
4545
} = this.props;
46-
const isHorizontalGrouping = isHorizontalGroupingApplied(groups, groupOrientation)
46+
const isHorizontalGrouping = isHorizontalGroupingApplied(groups.length, groupOrientation)
4747
&& !groupByDate;
4848

4949
return (

packages/devextreme/js/__internal/scheduler/r1/components/base/group_panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class GroupPanel extends InfernoWrapperComponent<GroupPanelProps> {
4040
groups,
4141
styles,
4242
} = this.props;
43-
const isVerticalLayout = isVerticalGroupingApplied(groups, groupOrientation);
43+
const isVerticalLayout = isVerticalGroupingApplied(groups.length, groupOrientation);
4444

4545
const Layout = isVerticalLayout ? GroupPanelVertical : GroupPanelHorizontal;
4646

packages/devextreme/js/__internal/scheduler/r1/components/base/header_panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class HeaderPanel extends InfernoWrapperComponent<HeaderPanelProps> {
4444
resourceCellTemplate,
4545
timeCellTemplate,
4646
} = this.props;
47-
const isHorizontalGrouping = isHorizontalGroupingApplied(groups, groupOrientation);
47+
const isHorizontalGrouping = isHorizontalGroupingApplied(groups.length, groupOrientation);
4848

4949
return (
5050
<thead>

packages/devextreme/js/__internal/scheduler/r1/components/timeline/date_header_timeline.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class TimelineDateHeaderLayout extends BaseInfernoComponent<DateHeaderPro
3434
weekDayRightVirtualCellCount,
3535
weekDayRightVirtualCellWidth,
3636
} = dateHeaderData;
37-
const isHorizontalGrouping = isHorizontalGroupingApplied(groups, groupOrientation)
37+
const isHorizontalGrouping = isHorizontalGroupingApplied(groups.length, groupOrientation)
3838
&& !groupByDate;
3939

4040
return (

packages/devextreme/js/__internal/scheduler/r1/utils/__tests__/base.test.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -687,66 +687,56 @@ describe('base utils', () => {
687687
});
688688

689689
describe('isVerticalGroupingApplied', () => {
690-
const groups = [{
691-
name: 'groupId',
692-
items: [{ id: 1 }],
693-
data: [{ id: 1 }],
694-
}];
695-
696690
it('should return true if group orientation is vertical', () => {
697-
expect(isVerticalGroupingApplied(groups, VERTICAL_GROUP_ORIENTATION))
691+
expect(isVerticalGroupingApplied(1, VERTICAL_GROUP_ORIENTATION))
698692
.toBe(true);
699693
});
700694

701695
it('should return false if group orientation is not vertical', () => {
702-
expect(isVerticalGroupingApplied(groups, HORIZONTAL_GROUP_ORIENTATION))
696+
expect(isVerticalGroupingApplied(1, HORIZONTAL_GROUP_ORIENTATION))
703697
.toBe(false);
704-
expect(isVerticalGroupingApplied(groups))
698+
expect(isVerticalGroupingApplied(1))
705699
.toBe(false);
706700
});
707701

708702
it('should return false if groups are empty', () => {
709-
expect(isVerticalGroupingApplied([], VERTICAL_GROUP_ORIENTATION))
703+
expect(isVerticalGroupingApplied(0, VERTICAL_GROUP_ORIENTATION))
710704
.toBe(false);
711705
});
712706
});
713707

714708
describe('isHorizontalGroupingApplied', () => {
715-
const testGroups = [{}] as any;
716-
717709
it('should return true if group orientation is horizontal and groups length is more than 0', () => {
718-
expect(isHorizontalGroupingApplied(testGroups, HORIZONTAL_GROUP_ORIENTATION))
710+
expect(isHorizontalGroupingApplied(1, HORIZONTAL_GROUP_ORIENTATION))
719711
.toBe(true);
720712
});
721713

722714
it('should return false if group orientation is not horizontal', () => {
723-
expect(isHorizontalGroupingApplied(testGroups, VERTICAL_GROUP_ORIENTATION))
715+
expect(isHorizontalGroupingApplied(1, VERTICAL_GROUP_ORIENTATION))
724716
.toBe(false);
725-
expect(isHorizontalGroupingApplied(testGroups))
717+
expect(isHorizontalGroupingApplied(1))
726718
.toBe(false);
727719
});
728720

729721
it('should return false if groups length is 0', () => {
730-
expect(isHorizontalGroupingApplied([], HORIZONTAL_GROUP_ORIENTATION))
722+
expect(isHorizontalGroupingApplied(0, HORIZONTAL_GROUP_ORIENTATION))
731723
.toBe(false);
732724
});
733725
});
734726

735727
describe('isGroupingByDate', () => {
736-
const testGroups = [{}] as any;
737-
738728
it('should return true if group orientation is horizontal and groupByDate is true', () => {
739-
expect(isGroupingByDate(testGroups, HORIZONTAL_GROUP_ORIENTATION, true))
729+
expect(isGroupingByDate(1, HORIZONTAL_GROUP_ORIENTATION, true))
740730
.toBe(true);
741731
});
742732

743733
it('should return false if group orientation is horizontal and groupByDate is false', () => {
744-
expect(isGroupingByDate(testGroups, HORIZONTAL_GROUP_ORIENTATION, false))
734+
expect(isGroupingByDate(1, HORIZONTAL_GROUP_ORIENTATION, false))
745735
.toBe(false);
746736
});
747737

748738
it('should return false if group orientation is vertical', () => {
749-
expect(isGroupingByDate(testGroups, VERTICAL_GROUP_ORIENTATION, false))
739+
expect(isGroupingByDate(1, VERTICAL_GROUP_ORIENTATION, false))
750740
.toBe(false);
751741
});
752742
});

packages/devextreme/js/__internal/scheduler/r1/utils/base.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
import type { ResourceLoader } from '../../utils/loader/resource_loader';
2525
import type { ResourceId } from '../../utils/loader/types';
2626
import { VIEWS } from '../../utils/options/constants_view';
27-
import type { GroupLeaf } from '../../utils/resource_manager/types';
2827

2928
const toMs = dateUtils.dateToMilliseconds;
3029
const DAY_HOURS = 24;
@@ -197,19 +196,17 @@ export const getHeaderCellText = (
197196
};
198197

199198
export const isVerticalGroupingApplied = (
200-
groups: unknown[],
199+
groupCount: number,
201200
groupOrientation?: GroupOrientation,
202-
): boolean => groupOrientation === VERTICAL_GROUP_ORIENTATION
203-
&& Boolean(groups.length);
201+
): boolean => groupOrientation === VERTICAL_GROUP_ORIENTATION && groupCount > 0;
204202

205-
// TODO(9): Get rid of it as soon as you can. More parameters then needed
206203
export const getHorizontalGroupCount = (
207-
groupLeafs: GroupLeaf[],
204+
groupCount: number,
208205
groupOrientation: GroupOrientation,
209206
): number => {
210-
const isVerticalGrouping = isVerticalGroupingApplied(groupLeafs, groupOrientation);
207+
const isVerticalGrouping = isVerticalGroupingApplied(groupCount, groupOrientation);
211208

212-
return isVerticalGrouping ? 1 : groupLeafs.length;
209+
return isVerticalGrouping ? 1 : groupCount;
213210
};
214211

215212
const TIMELINE_VIEWS = [
@@ -275,10 +272,10 @@ export const getViewStartByOptions = (
275272
};
276273

277274
export const calculateIsGroupedAllDayPanel = (
278-
groups: unknown[],
275+
groupCount: number,
279276
groupOrientation: GroupOrientation,
280277
isAllDayPanelVisible: boolean,
281-
): boolean => isVerticalGroupingApplied(groups, groupOrientation) && isAllDayPanelVisible;
278+
): boolean => isVerticalGroupingApplied(groupCount, groupOrientation) && isAllDayPanelVisible;
282279

283280
export const calculateViewStartDate = (
284281
startDateOption: Date | undefined,
@@ -355,16 +352,16 @@ export const getCalculatedFirstDayOfWeek = (
355352
: dateLocalization.firstDayOfWeekIndex());
356353

357354
export const isHorizontalGroupingApplied = (
358-
groups: unknown[],
355+
groupCount: number,
359356
groupOrientation?: GroupOrientation,
360-
): boolean => groupOrientation === HORIZONTAL_GROUP_ORIENTATION && Boolean(groups.length);
357+
): boolean => groupOrientation === HORIZONTAL_GROUP_ORIENTATION && groupCount > 0;
361358

362359
export const isGroupingByDate = (
363-
groups: unknown[],
360+
groupCount: number,
364361
groupOrientation: GroupOrientation | undefined,
365362
groupByDate: boolean,
366363
): boolean => {
367-
const isHorizontalGrouping = isHorizontalGroupingApplied(groups, groupOrientation);
364+
const isHorizontalGrouping = isHorizontalGroupingApplied(groupCount, groupOrientation);
368365

369366
return groupByDate && isHorizontalGrouping;
370367
};
Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
describe, expect, it,
33
} from '@jest/globals';
4-
import { getResourceManagerMock, resourceItemsByIdMock } from '@ts/scheduler/__mock__/resource_manager.mock';
54

6-
import { convertToOldTree, reduceResourcesTree } from './agenda_group_utils';
5+
import { getResourceManagerMock } from '../../__mock__/resource_manager.mock';
6+
import { reduceResourcesTree } from './agenda_group_utils';
77

88
describe('agenda group utils', () => {
99
describe('reduceResourcesTree', () => {
@@ -61,88 +61,4 @@ describe('agenda group utils', () => {
6161
expect(reduceResourcesTree(manager.resourceById, manager.groupsTree, [])).toEqual([]);
6262
});
6363
});
64-
65-
describe('convertToOldTree', () => {
66-
it('should convert to old tree structure', async () => {
67-
const manager = getResourceManagerMock();
68-
await manager.loadGroupResources(['roomId', 'nested.priorityId']);
69-
expect(convertToOldTree(manager.resourceById, manager.groupsTree)).toEqual([
70-
{
71-
children: [
72-
{
73-
children: [],
74-
name: 'nested.priorityId',
75-
title: 'Low Priority',
76-
color: '#1e90ff',
77-
data: resourceItemsByIdMock['nested.priorityId'][0],
78-
value: 1,
79-
},
80-
{
81-
children: [],
82-
name: 'nested.priorityId',
83-
title: 'High Priority',
84-
color: '#ff9747',
85-
data: resourceItemsByIdMock['nested.priorityId'][1],
86-
value: 2,
87-
},
88-
],
89-
name: 'roomId',
90-
title: 'Room 1',
91-
color: '#aaa',
92-
data: resourceItemsByIdMock.roomId[0],
93-
value: 0,
94-
},
95-
{
96-
children: [
97-
{
98-
children: [],
99-
name: 'nested.priorityId',
100-
title: 'Low Priority',
101-
color: '#1e90ff',
102-
data: resourceItemsByIdMock['nested.priorityId'][0],
103-
value: 1,
104-
},
105-
{
106-
children: [],
107-
name: 'nested.priorityId',
108-
title: 'High Priority',
109-
color: '#ff9747',
110-
data: resourceItemsByIdMock['nested.priorityId'][1],
111-
value: 2,
112-
},
113-
],
114-
name: 'roomId',
115-
title: 'Room 2',
116-
color: '#ccc',
117-
data: resourceItemsByIdMock.roomId[1],
118-
value: 1,
119-
},
120-
{
121-
children: [
122-
{
123-
children: [],
124-
name: 'nested.priorityId',
125-
title: 'Low Priority',
126-
color: '#1e90ff',
127-
data: resourceItemsByIdMock['nested.priorityId'][0],
128-
value: 1,
129-
},
130-
{
131-
children: [],
132-
name: 'nested.priorityId',
133-
title: 'High Priority',
134-
color: '#ff9747',
135-
data: resourceItemsByIdMock['nested.priorityId'][1],
136-
value: 2,
137-
},
138-
],
139-
name: 'roomId',
140-
title: 'Room 3',
141-
color: '#777',
142-
data: resourceItemsByIdMock.roomId[2],
143-
value: 2,
144-
},
145-
]);
146-
});
147-
});
14864
});

0 commit comments

Comments
 (0)