UNPKG

2.35 kBJavaScriptView Raw
1/**
2 * Class with default hotkey actions (e.g. select, clear selection, move up/down)
3 * @param {Selection?} tableSelection selection associated with rg-table
4 * (if tableSelection is not defined, use method setSelection to configure instance)
5 */
6export default class SelectionNavigateActions {
7 constructor(tableSelection) {
8 this._selection = tableSelection;
9 this._addSelectionMode = null;
10 }
11
12 setSelection(tableSelection) {
13 this._selection = tableSelection;
14 }
15
16 moveUp() {
17 if (!this._selection) {
18 return false;
19 }
20 const activeItem = this._selection.activatePreviousItem();
21
22 if (activeItem && activeItem.unselectable) {
23 return this.moveUp();
24 }
25
26 return !activeItem;
27 }
28
29 moveDown() {
30 if (this._selection) {
31 const activeItem = this._selection.activateNextItem();
32 if (!activeItem) {
33 this._selection.setActiveItemIndex(0);
34 } else if (activeItem.unselectable) {
35 return this.moveDown();
36 }
37 }
38 return false;
39 }
40
41 reset() {
42 this._addSelectionMode = null;
43 return !!this._selection;
44 }
45
46 selectUp() {
47 if (!this._selection) {
48 return false;
49 }
50 this._changeCheckedItemsArray();
51 return this.moveUp();
52 }
53
54 selectDown() {
55 if (!this._selection) {
56 return false;
57 }
58 this._changeCheckedItemsArray();
59 return this.moveDown();
60 }
61
62 selectCurrent() {
63 if (!this._selection) {
64 return false;
65 }
66 const activeItem = this._selection.getActiveItem();
67 if (!activeItem) {
68 return true;
69 } else {
70 this._selection.toggleCheck(activeItem);
71 return false;
72 }
73 }
74
75 clearSelection() {
76 if (!this._selection) {
77 return false;
78 }
79 const activeItem = this._selection.getActiveItem();
80
81 if (activeItem) {
82 if (this._selection.getCheckedItems().length > 0) {
83 this._selection.clearSelection();
84 return false;
85 }
86
87 this._selection.clearActivity();
88 }
89
90 return true;
91 }
92
93 _changeCheckedItemsArray() {
94 if (!this._selection) {
95 return false;
96 }
97 const activeItem = this._selection.getActiveItem();
98 if (activeItem && !this._addSelectionMode) {
99 this._addSelectionMode = activeItem.checked ? 'uncheckItem' : 'checkItem';
100 }
101 this._selection[this._addSelectionMode](activeItem);
102
103 return undefined;
104 }
105}