-
Notifications
You must be signed in to change notification settings - Fork 3
/
ddbselection.ts
168 lines (144 loc) · 5.12 KB
/
ddbselection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Action } from '@ngxs/store';
import { DDBState } from './ddb';
import { Message } from '../../../state/status';
import { Selector } from '@ngxs/store';
import { State } from '@ngxs/store';
import { StateContext } from '@ngxs/store';
import { Store } from '@ngxs/store';
import { pluralize } from 'ellib';
/** NOTE: actions must come first because of AST */
export class AddRowToSelection {
static readonly type = '[DDBSelection] add row';
constructor(public readonly payload: { row: number }) { }
}
export class ClearSelection {
static readonly type = '[DDBSelection] clear';
constructor(public readonly payload?: any) { }
}
export class RemoveRowFromSelection {
static readonly type = '[DDBSelection] remove row';
constructor(public readonly payload: { row: number }) { }
}
export class ReplaceRowsInSelection {
static readonly type = '[DDBSelection] replace rows';
constructor(public readonly payload: { rows: number[] }) { }
}
export class SelectColumn {
static readonly type = '[DDBSelection] select column';
constructor(public readonly payload: { column: string }) { }
}
export class SelectionColumnUpdated {
static readonly type = '[DDBSelection] column selected';
constructor(public readonly payload: { column: string }) { }
}
export class SelectionRowsUpdated {
static readonly type = '[DDBSelection] rows selected';
constructor(public readonly payload: { rows: number[] }) { }
}
export class ToggleRowInSelection {
static readonly type = '[DDBSelection] toggle row';
constructor(public readonly payload: { row: number }) { }
}
export interface DDBSelectionStateModel {
column: string;
rows: number[];
}
@State<DDBSelectionStateModel>({
name: 'ddbselection',
defaults: {
column: null,
rows: []
}
}) export class DDBSelectionState {
@Selector() static getColumn(state: DDBSelectionStateModel): string {
return state.column;
}
@Selector() static getRows(state: DDBSelectionStateModel): number[] {
return state.rows;
}
/** ctor */
constructor(private store: Store) { }
@Action(AddRowToSelection)
addRowToSelection({ dispatch, getState, patchState }: StateContext<DDBSelectionStateModel>,
{ payload }: AddRowToSelection) {
const { row } = payload;
const state = getState();
if (!state.rows.includes(row)) {
const rows = state.rows.slice(0);
rows.push(row);
patchState({ rows });
dispatch(new SelectionRowsUpdated({ rows }));
}
}
@Action(ClearSelection)
clearSelection({ dispatch, patchState }: StateContext<DDBSelectionStateModel>,
{ payload }: ClearSelection) {
patchState({ column: null, rows: [] });
dispatch([
new SelectionColumnUpdated({ column: null }),
new SelectionRowsUpdated({ rows: [] })
]);
}
@Action(RemoveRowFromSelection)
removeRowFromSelection({ dispatch, getState, patchState }: StateContext<DDBSelectionStateModel>,
{ payload }: RemoveRowFromSelection) {
const { row } = payload;
const state = getState();
if (state.rows.includes(row)) {
const rows = state.rows.slice(0);
const ix = rows.indexOf(row);
rows.splice(ix, 1);
patchState({ rows });
dispatch(new SelectionRowsUpdated({ rows }));
}
}
@Action(ReplaceRowsInSelection)
replaceRowsInSelection({ dispatch, patchState }: StateContext<DDBSelectionStateModel>,
{ payload }: ReplaceRowsInSelection) {
const { rows } = payload;
patchState({ rows });
dispatch(new SelectionRowsUpdated({ rows }));
}
@Action(SelectColumn)
selectColumn({ dispatch, patchState }: StateContext<DDBSelectionStateModel>,
{ payload }: SelectColumn) {
const { column } = payload;
patchState({ column });
dispatch(new SelectionColumnUpdated({ column }));
}
@Action(SelectionColumnUpdated)
selectionColumnUpdated({ dispatch }: StateContext<DDBSelectionStateModel>,
{ payload }: SelectionColumnUpdated) {
const { column } = payload;
dispatch(new Message({ text: column? `Column ${column} selected` : '' }));
}
@Action(SelectionRowsUpdated)
selectionRowsUpdated({ dispatch }: StateContext<DDBSelectionStateModel>,
{ payload }: SelectionRowsUpdated) {
const { rows } = payload;
const index = this.store.selectSnapshot(DDBState.getIndex);
let text = '';
if (rows.length === 1)
text = `Row ${rows[0] + index + 1} selected`;
else if (rows.length > 1) {
const others = pluralize(rows.length - 1, {
'=1': 'one other', 'other': '# others'
});
text = `Row ${rows[0] + index + 1} and ${others} selected`;
}
dispatch(new Message({ text }));
}
@Action(ToggleRowInSelection)
toggleRowInSelection({ dispatch, getState, patchState }: StateContext<DDBSelectionStateModel>,
{ payload }: ToggleRowInSelection) {
const { row } = payload;
const state = getState();
const rows = state.rows.slice(0);
const ix = rows.indexOf(row);
if (ix !== -1)
rows.splice(ix, 1);
else rows.push(row);
patchState({ rows });
dispatch(new SelectionRowsUpdated({ rows }));
}
}