Skip to content

Commit 7dc218c

Browse files
committed
refactor: add new select actions provider
1 parent cbdbeee commit 7dc218c

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ObjectItem } from "mendix";
2+
import { SelectAdjacentFx, SelectAllFx, SelectFx, SelectionType } from "../selection";
3+
4+
export interface SelectActionsService {
5+
selectionType: SelectionType;
6+
select: SelectFx;
7+
selectPage: SelectAllFx;
8+
selectAdjacent: SelectAdjacentFx;
9+
isSelected(item: ObjectItem): boolean;
10+
clearSelection(): void;
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export { DatasourceService } from "./core/Datasource.service";
22
export { ProgressService } from "./core/Progress.service";
33
export type { QueryService } from "./interfaces/QueryService";
4+
export type { SelectActionsService } from "./interfaces/SelectActionsService";
45
export type { SelectionDynamicProps } from "./interfaces/SelectionDynamicProps";
56
export { type SelectionHelperService } from "./interfaces/SelectionHelperService";
67
export type { TaskProgressService } from "./interfaces/TaskProgressService";
78
export { SelectAllService } from "./select-all/SelectAll.service";
89
export { SelectionCounterViewModel } from "./selection-counter/SelectionCounter.viewModel";
910
export * from "./selection/context";
1011
export { createSelectionHelper } from "./selection/createSelectionHelper";
12+
export { SelectActionsProvider } from "./selection/SelectActionsProvider.service";
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { ObjectItem } from "mendix";
2+
import { SelectActionsService } from "../interfaces/SelectActionsService";
3+
import { SelectionHelperService } from "../interfaces/SelectionHelperService";
4+
import { SelectAdjacentFx, SelectAllFx, SelectFx, SelectionType } from "./types";
5+
6+
export class SelectActionsProvider implements SelectActionsService {
7+
constructor(
8+
private type: SelectionType,
9+
private selectionHelper?: SelectionHelperService
10+
) {
11+
if (type === "Multi") {
12+
if (!selectionHelper || selectionHelper.type !== "Multi") {
13+
throw new Error("SelectionHelperService of type Multi is required for Multi selection type");
14+
}
15+
}
16+
if (type === "Single") {
17+
if (!selectionHelper || selectionHelper.type !== "Single") {
18+
throw new Error("SelectionHelperService of type Single is required for Single selection type");
19+
}
20+
}
21+
}
22+
23+
get selectionType(): SelectionType {
24+
return this.type;
25+
}
26+
27+
select: SelectFx = (...params) => {
28+
if (!this.selectionHelper) {
29+
return;
30+
}
31+
32+
if (this.selectionHelper.type === "Multi") {
33+
this.selectItemMulti(...params);
34+
} else {
35+
this.selectItemSingle(...params);
36+
}
37+
};
38+
39+
selectPage: SelectAllFx = (requestedAction?: "selectAll") => {
40+
if (!this.selectionHelper) {
41+
return;
42+
}
43+
44+
if (this.selectionHelper.type === "Single") {
45+
console.warn("Calling selectPage in single selection mode have no effect");
46+
return;
47+
}
48+
49+
if (requestedAction === "selectAll") {
50+
this.selectionHelper.selectAll();
51+
return;
52+
}
53+
54+
if (this.selectionHelper.selectionStatus === "all") {
55+
this.selectionHelper.selectNone();
56+
} else {
57+
this.selectionHelper.selectAll();
58+
}
59+
};
60+
61+
selectAdjacent: SelectAdjacentFx = (...params) => {
62+
if (this.selectionHelper?.type === "Multi") {
63+
this.selectionHelper.selectUpToAdjacent(...params);
64+
}
65+
};
66+
67+
isSelected = (item: ObjectItem): boolean => {
68+
return this.selectionHelper ? this.selectionHelper.isSelected(item) : false;
69+
};
70+
71+
clearSelection = (): void => {
72+
if (this.selectionHelper?.type === "Multi") {
73+
this.selectionHelper.clearSelection();
74+
} else if (this.selectionHelper?.type === "Single") {
75+
this.selectionHelper.remove();
76+
}
77+
};
78+
79+
private selectItemMulti: SelectFx = (item, shiftKey, toggleMode = false) => {
80+
if (this.selectionHelper?.type !== "Multi") {
81+
return;
82+
}
83+
84+
if (shiftKey) {
85+
this.selectionHelper.selectUpTo(item, toggleMode ? "toggle" : "clear");
86+
return;
87+
}
88+
89+
this.selectItem(item, toggleMode);
90+
};
91+
92+
private selectItemSingle: SelectFx = (item, _, toggleMode = false) => {
93+
this.selectItem(item, toggleMode);
94+
};
95+
96+
private selectItem = (item: ObjectItem, toggleMode: boolean): void => {
97+
if (this.selectionHelper === undefined) {
98+
return;
99+
}
100+
101+
// clear mode
102+
if (toggleMode === false) {
103+
this.selectionHelper.reduceTo(item);
104+
return;
105+
}
106+
107+
// toggle mode
108+
if (this.selectionHelper.isSelected(item)) {
109+
this.selectionHelper.remove(item);
110+
} else if (this.selectionHelper.type === "Multi") {
111+
this.selectionHelper.add(item);
112+
} else {
113+
this.selectionHelper.reduceTo(item);
114+
}
115+
};
116+
}

packages/shared/widget-plugin-grid/src/selection/select-action-handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ObjectItem } from "mendix";
22
import { SelectionHelperService } from "../interfaces/SelectionHelperService";
33
import { SelectAdjacentFx, SelectAllFx, SelectFx, SelectionType, WidgetSelectionProperty } from "./types";
44

5+
/** @deprecated use `SelectActionsProvider` instead */
56
export class SelectActionHandler {
67
constructor(
78
private selection: WidgetSelectionProperty,

0 commit comments

Comments
 (0)