Skip to content

Commit 0c2749c

Browse files
committed
refactor(scheduler): pass config object to grouped strategies
1 parent 069e8ea commit 0c2749c

5 files changed

Lines changed: 220 additions & 109 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
describe, expect, it,
3+
} from '@jest/globals';
4+
5+
import type { WorkspaceGroupedStrategyConfig } from '../workspaces/types';
6+
import HorizontalGroupedStrategy from '../workspaces/work_space_grouped_strategy_horizontal';
7+
import VerticalGroupedStrategy from '../workspaces/work_space_grouped_strategy_vertical';
8+
9+
const createConfig = (
10+
overrides: Partial<WorkspaceGroupedStrategyConfig> = {},
11+
): WorkspaceGroupedStrategyConfig => ({
12+
isGroupedByDate: () => false,
13+
getCellCount: () => 7,
14+
getGroupCount: () => 3,
15+
getRowCount: () => 48,
16+
getCellWidth: () => 75,
17+
getCellHeight: () => 50,
18+
getAllDayHeight: () => 30,
19+
getTimePanelWidth: () => 100,
20+
getGroupTableWidth: () => 60,
21+
getWorkSpaceWidth: () => 800,
22+
getWorkSpaceLeftOffset: () => 160,
23+
getIndicatorOffset: () => 10,
24+
getIndicationHeight: () => 200,
25+
getIndicationWidth: () => 300,
26+
getCellIndexByCoordinates: () => 5,
27+
supportAllDayRow: () => true,
28+
getScrollableScrollTop: () => 0,
29+
getScrollableContentElement: () => document.createElement('div'),
30+
getElement: () => document.createElement('div'),
31+
getHeaderPanelContainerElement: () => document.createElement('div'),
32+
isRtlEnabled: () => false,
33+
isShowAllDayPanel: () => true,
34+
isCrossScrollingEnabled: () => false,
35+
getStartDayHour: () => 0,
36+
getEndDayHour: () => 24,
37+
getHoursInterval: () => 0.5,
38+
getHeaderHeight: () => 40,
39+
...overrides,
40+
});
41+
42+
describe('HorizontalGroupedStrategy with config object', () => {
43+
it('should work with config interface instead of workspace', () => {
44+
const strategy = new HorizontalGroupedStrategy(
45+
createConfig({ getCellCount: () => 5, getGroupCount: () => 4 }),
46+
);
47+
48+
const result = strategy.prepareCellIndexes({ rowIndex: 2, columnIndex: 3 }, 2);
49+
50+
expect(result.columnIndex).toBe(3 + 2 * 5);
51+
expect(result.rowIndex).toBe(2);
52+
});
53+
});
54+
55+
describe('VerticalGroupedStrategy with config object', () => {
56+
it('should work with config interface instead of workspace', () => {
57+
const strategy = new VerticalGroupedStrategy(createConfig({
58+
getRowCount: () => 10,
59+
supportAllDayRow: () => true,
60+
isShowAllDayPanel: () => true,
61+
}));
62+
63+
const result = strategy.prepareCellIndexes({ rowIndex: 3, columnIndex: 2 }, 2, false);
64+
65+
expect(result.rowIndex).toBe(3 + 2 * 10 + 2 + 1);
66+
expect(result.columnIndex).toBe(2);
67+
});
68+
});

packages/devextreme/js/__internal/scheduler/workspaces/m_work_space.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import {
9898
getMaxAllowedPosition,
9999
PositionHelper,
100100
} from './helpers/position_helper';
101+
import type { WorkspaceGroupedStrategyConfig } from './types';
101102
import type { ViewDataProviderOptions } from './view_model/m_types';
102103
import ViewDataProvider from './view_model/m_view_data_provider';
103104
import { VirtualScrollingDispatcher, VirtualScrollingRenderer } from './virtual_scrolling';
@@ -2625,7 +2626,37 @@ class SchedulerWorkSpace extends Widget<WorkspaceOptionsInternal> {
26252626
? VerticalGroupedStrategy
26262627
: HorizontalGroupedStrategy;
26272628

2628-
this.groupedStrategy = new Strategy(this);
2629+
const config: WorkspaceGroupedStrategyConfig = {
2630+
isGroupedByDate: this.isGroupedByDate.bind(this),
2631+
getCellCount: this.getCellCount.bind(this),
2632+
getGroupCount: this.getGroupCount.bind(this),
2633+
getRowCount: this.getRowCount.bind(this),
2634+
getCellWidth: this.getCellWidth.bind(this),
2635+
getCellHeight: this.getCellHeight.bind(this),
2636+
getAllDayHeight: this.getAllDayHeight.bind(this),
2637+
getTimePanelWidth: this.getTimePanelWidth.bind(this),
2638+
getGroupTableWidth: this.getGroupTableWidth.bind(this),
2639+
getWorkSpaceWidth: this.getWorkSpaceWidth.bind(this),
2640+
getWorkSpaceLeftOffset: this.getWorkSpaceLeftOffset.bind(this),
2641+
getIndicatorOffset: (this as any).getIndicatorOffset.bind(this),
2642+
getIndicationHeight: (this as any).getIndicationHeight.bind(this),
2643+
getIndicationWidth: (this as any).getIndicationWidth.bind(this),
2644+
getCellIndexByCoordinates: this.getCellIndexByCoordinates.bind(this),
2645+
supportAllDayRow: this.supportAllDayRow.bind(this),
2646+
getScrollableScrollTop: () => this.getScrollable().scrollTop(),
2647+
getScrollableContentElement: () => this.getScrollable().$content().get(0) as HTMLElement,
2648+
getElement: () => (this.$element() as any).get(0) as HTMLElement,
2649+
getHeaderPanelContainerElement: () => this.$headerPanelContainer.get(0) as HTMLElement,
2650+
isRtlEnabled: () => this.option('rtlEnabled'),
2651+
isShowAllDayPanel: () => this.option('showAllDayPanel'),
2652+
isCrossScrollingEnabled: () => this.option('crossScrollingEnabled'),
2653+
getStartDayHour: () => this.option('startDayHour'),
2654+
getEndDayHour: () => this.option('endDayHour'),
2655+
getHoursInterval: () => this.option('hoursInterval'),
2656+
getHeaderHeight: () => this.option('getHeaderHeight')(),
2657+
};
2658+
2659+
this.groupedStrategy = new Strategy(config);
26292660
}
26302661

26312662
protected getDefaultGroupStrategy() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface WorkspaceGroupedStrategyConfig {
2+
isGroupedByDate: () => boolean;
3+
getCellCount: () => number;
4+
getGroupCount: () => number;
5+
getRowCount: () => number;
6+
getCellWidth: () => number;
7+
getCellHeight: () => number;
8+
getAllDayHeight: () => number;
9+
getTimePanelWidth: () => number;
10+
getGroupTableWidth: () => number;
11+
getWorkSpaceWidth: () => number;
12+
getWorkSpaceLeftOffset: () => number;
13+
getIndicatorOffset: (groupIndex: number) => number;
14+
getIndicationHeight: () => number;
15+
getIndicationWidth: () => number;
16+
getCellIndexByCoordinates: (
17+
coordinates: { top: number; left: number },
18+
allDay?: boolean,
19+
) => number;
20+
supportAllDayRow: () => boolean;
21+
getScrollableScrollTop: () => number;
22+
getScrollableContentElement: () => HTMLElement;
23+
getElement: () => HTMLElement;
24+
getHeaderPanelContainerElement: () => HTMLElement;
25+
isRtlEnabled: () => boolean;
26+
isShowAllDayPanel: () => boolean;
27+
isCrossScrollingEnabled: () => boolean;
28+
getStartDayHour: () => number;
29+
getEndDayHour: () => number;
30+
getHoursInterval: () => number;
31+
getHeaderHeight: () => number;
32+
}

packages/devextreme/js/__internal/scheduler/workspaces/work_space_grouped_strategy_horizontal.ts

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,46 @@ import type {
66
GroupBoundsOffset,
77
} from '@ts/scheduler/types';
88
import { WORK_SPACE_BORDER_PX } from '@ts/scheduler/workspaces/const';
9-
import type SchedulerWorkSpace from '@ts/scheduler/workspaces/m_work_space';
109

1110
import { FIRST_GROUP_CELL_CLASS, LAST_GROUP_CELL_CLASS } from '../classes';
1211
import type { ResourceLoader } from '../utils/loader/resource_loader';
12+
import type { WorkspaceGroupedStrategyConfig } from './types';
1313

1414
class HorizontalGroupedStrategy {
15-
constructor(private readonly workspace: SchedulerWorkSpace) {}
15+
constructor(private readonly config: WorkspaceGroupedStrategyConfig) {}
1616

1717
prepareCellIndexes(
1818
cellCoordinates: CellPositionData,
1919
groupIndex: number,
2020
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2121
inAllDay?: boolean,
2222
): CellPositionData {
23-
const groupByDay = this.workspace.isGroupedByDate();
23+
const groupByDay = this.config.isGroupedByDate();
2424

2525
if (!groupByDay) {
2626
return {
2727
rowIndex: cellCoordinates.rowIndex,
28-
// @ts-expect-error
29-
columnIndex: cellCoordinates.columnIndex + groupIndex * this.workspace.getCellCount(),
28+
columnIndex: cellCoordinates.columnIndex + groupIndex * this.config.getCellCount(),
3029
};
3130
}
3231
return {
3332
rowIndex: cellCoordinates.rowIndex,
34-
// @ts-expect-error
35-
columnIndex: cellCoordinates.columnIndex * this.workspace.getGroupCount() + groupIndex,
33+
columnIndex: cellCoordinates.columnIndex * this.config.getGroupCount() + groupIndex,
3634
};
3735
}
3836

3937
getGroupIndex(rowIndex: number, columnIndex: number): number {
40-
const groupByDay = this.workspace.isGroupedByDate();
41-
// @ts-expect-error
42-
const groupCount = this.workspace.getGroupCount();
38+
const groupByDay = this.config.isGroupedByDate();
39+
const groupCount = this.config.getGroupCount();
4340

4441
if (groupByDay) {
4542
return columnIndex % groupCount;
4643
}
47-
// @ts-expect-error
48-
return Math.floor(columnIndex / this.workspace.getCellCount());
44+
return Math.floor(columnIndex / this.config.getCellCount());
4945
}
5046

5147
calculateHeaderCellRepeatCount(): number {
52-
// @ts-expect-error
53-
return this.workspace.getGroupCount() || 1;
48+
return this.config.getGroupCount() || 1;
5449
}
5550

5651
insertAllDayRowsIntoDateTable(): boolean {
@@ -60,28 +55,28 @@ class HorizontalGroupedStrategy {
6055
getTotalCellCount(groupCount: number): number {
6156
const effectiveGroupCount = groupCount || 1;
6257

63-
// @ts-expect-error
64-
return this.workspace.getCellCount() * effectiveGroupCount;
58+
return this.config.getCellCount() * effectiveGroupCount;
6559
}
6660

6761
getTotalRowCount(): number {
68-
// @ts-expect-error
69-
return this.workspace.getRowCount();
62+
return this.config.getRowCount();
7063
}
7164

7265
calculateTimeCellRepeatCount(): number {
7366
return 1;
7467
}
7568

7669
getWorkSpaceMinWidth(): number {
77-
const workSpaceElementWidth = getBoundingRect(this.workspace.$element().get(0)).width;
70+
const workSpaceElementWidth = getBoundingRect(
71+
this.config.getElement(),
72+
).width;
7873
return workSpaceElementWidth
79-
- this.workspace.getTimePanelWidth()
74+
- this.config.getTimePanelWidth()
8075
- 2 * WORK_SPACE_BORDER_PX;
8176
}
8277

8378
getAllDayOffset(): number {
84-
return this.workspace.getAllDayHeight();
79+
return this.config.getAllDayHeight();
8580
}
8681

8782
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -90,7 +85,7 @@ class HorizontalGroupedStrategy {
9085
}
9186

9287
getLeftOffset(): number {
93-
return this.workspace.getTimePanelWidth();
88+
return this.config.getTimePanelWidth();
9489
}
9590

9691
private createGroupBoundOffset(
@@ -130,11 +125,11 @@ class HorizontalGroupedStrategy {
130125
coordinates: { top: number; left: number; groupIndex?: number },
131126
groupedDataMap: { dateTableGroupedMap: CellInfo[][][] },
132127
): GroupBoundsOffset {
133-
if (this.workspace.isGroupedByDate()) {
128+
if (this.config.isGroupedByDate()) {
134129
return this.getGroupedByDateBoundOffset($cells, cellWidth);
135130
}
136131

137-
const cellIndex = this.workspace.getCellIndexByCoordinates(coordinates);
132+
const cellIndex = this.config.getCellIndexByCoordinates(coordinates);
138133
const groupIndex = coordinates.groupIndex ?? Math.floor(cellIndex / cellCount);
139134

140135
const currentCellGroup = groupedDataMap.dateTableGroupedMap[groupIndex];
@@ -168,37 +163,33 @@ class HorizontalGroupedStrategy {
168163
}
169164

170165
private getIndicatorOffset(groupIndex: number): number {
171-
const groupByDay = this.workspace.isGroupedByDate();
166+
const groupByDay = this.config.isGroupedByDate();
172167

173168
return groupByDay
174169
? this.calculateGroupByDateOffset(groupIndex)
175170
: this.calculateOffset(groupIndex);
176171
}
177172

178173
private calculateOffset(groupIndex: number): number {
179-
// @ts-expect-error
180-
const indicatorStartPosition = this.workspace.getIndicatorOffset(groupIndex) as number;
181-
// @ts-expect-error
182-
const offset = this.workspace.getCellCount() * this.workspace.getCellWidth() * groupIndex;
174+
const indicatorStartPosition = this.config.getIndicatorOffset(groupIndex);
175+
const offset = this.config.getCellCount() * this.config.getCellWidth() * groupIndex;
183176

184177
return indicatorStartPosition + offset;
185178
}
186179

187180
private calculateGroupByDateOffset(groupIndex: number): number {
188-
// @ts-expect-error
189-
return this.workspace.getIndicatorOffset(0) * this.workspace.getGroupCount()
190-
+ this.workspace.getCellWidth() * groupIndex;
181+
return this.config.getIndicatorOffset(0) * this.config.getGroupCount()
182+
+ this.config.getCellWidth() * groupIndex;
191183
}
192184

193185
getShaderOffset(i: number, width: number): number {
194-
// @ts-expect-error
195-
const offset = this.workspace.getCellCount() * this.workspace.getCellWidth() * i;
186+
const offset = this.config.getCellCount() * this.config.getCellWidth() * i;
196187

197-
if (this.workspace.option('rtlEnabled')) {
188+
if (this.config.isRtlEnabled()) {
198189
const containerWidth = getBoundingRect(
199-
this.workspace.getScrollable().$content().get(0),
190+
this.config.getScrollableContentElement(),
200191
).width;
201-
return containerWidth - offset - this.workspace.getTimePanelWidth() - width;
192+
return containerWidth - offset - this.config.getTimePanelWidth() - width;
202193
}
203194

204195
return offset;
@@ -209,21 +200,21 @@ class HorizontalGroupedStrategy {
209200
}
210201

211202
getShaderHeight(): number {
212-
// @ts-expect-error
213-
return this.workspace.getIndicationHeight() as number;
203+
return this.config.getIndicationHeight();
214204
}
215205

216206
getShaderMaxHeight(): number {
217-
return (getBoundingRect(this.workspace.getScrollable().$content().get(0)) as DOMRect).height;
207+
return (getBoundingRect(
208+
this.config.getScrollableContentElement(),
209+
) as DOMRect).height;
218210
}
219211

220212
getShaderWidth(): number {
221-
// @ts-expect-error
222-
return this.workspace.getIndicationWidth() as number;
213+
return this.config.getIndicationWidth();
223214
}
224215

225216
getScrollableScrollTop(allDay: boolean): number {
226-
return !allDay ? this.workspace.getScrollable().scrollTop() : 0;
217+
return !allDay ? this.config.getScrollableScrollTop() : 0;
227218
}
228219

229220
// ---------------
@@ -251,15 +242,13 @@ class HorizontalGroupedStrategy {
251242
return `${cellClass} ${LAST_GROUP_CELL_CLASS}`;
252243
}
253244

254-
const groupByDate = this.workspace.isGroupedByDate();
245+
const groupByDate = this.config.isGroupedByDate();
255246

256247
if (groupByDate) {
257-
// @ts-expect-error
258-
if (index % this.workspace.getGroupCount() === 0) {
248+
if (index % this.config.getGroupCount() === 0) {
259249
return `${cellClass} ${LAST_GROUP_CELL_CLASS}`;
260250
}
261-
// @ts-expect-error
262-
} else if (index % this.workspace.getCellCount() === 0) {
251+
} else if (index % this.config.getCellCount() === 0) {
263252
return `${cellClass} ${LAST_GROUP_CELL_CLASS}`;
264253
}
265254

@@ -275,15 +264,13 @@ class HorizontalGroupedStrategy {
275264
return `${cellClass} ${FIRST_GROUP_CELL_CLASS}`;
276265
}
277266

278-
const groupByDate = this.workspace.isGroupedByDate();
267+
const groupByDate = this.config.isGroupedByDate();
279268

280269
if (groupByDate) {
281-
// @ts-expect-error
282-
if ((index - 1) % this.workspace.getGroupCount() === 0) {
270+
if ((index - 1) % this.config.getGroupCount() === 0) {
283271
return `${cellClass} ${FIRST_GROUP_CELL_CLASS}`;
284272
}
285-
// @ts-expect-error
286-
} else if ((index - 1) % this.workspace.getCellCount() === 0) {
273+
} else if ((index - 1) % this.config.getCellCount() === 0) {
287274
return `${cellClass} ${FIRST_GROUP_CELL_CLASS}`;
288275
}
289276

0 commit comments

Comments
 (0)