/
coords.js
244 lines (220 loc) · 6.33 KB
/
coords.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @description
*
* The `CellCoords` class holds the coordinates (`row`, `col`) of a single cell.
*
* It also contains methods for validating the coordinates
* and retrieving them as an object.
*
* To import the `CellCoords` class:
*
* ```js
* import Handsontable, { CellCoords } from '/handsontable';
*
* // or, using modules
* import Handsontable, { CellCoords } from '/handsontable/base';
* ```
*/
class CellCoords {
/**
* A visual row index.
*
* @type {number}
*/
row = null;
/**
* A visual column index.
*
* @type {number}
*/
col = null;
/**
* A flag which determines if the coordinates run in RTL mode.
*
* @type {boolean}
*/
#isRtl = false;
constructor(row, column, isRtl = false) {
this.#isRtl = isRtl;
if (typeof row !== 'undefined' && typeof column !== 'undefined') {
this.row = row;
this.col = column;
}
}
/**
* Checks if the coordinates in your `CellCoords` instance are valid
* in the context of given table parameters.
*
* The `row` index:
* - Must be an integer.
* - Must be higher than the number of column headers in the table.
* - Must be lower than the total number of rows in the table.
*
* The `col` index:
* - Must be an integer.
* - Must be higher than the number of row headers in the table.
* - Must be lower than the total number of columns in the table.
*
* @param {object} [tableParams] An object with a defined table size.
* @param {number} [tableParams.countRows=0] The total number of rows.
* @param {number} [tableParams.countCols=0] The total number of columns.
* @param {number} [tableParams.countRowHeaders=0] A number of row headers.
* @param {number} [tableParams.countColHeaders=0] A number of column headers.
* @returns {boolean} `true`: The coordinates are valid.
*/
isValid(tableParams) {
const { countRows, countCols, countRowHeaders, countColHeaders } = {
countRows: 0,
countCols: 0,
countRowHeaders: 0,
countColHeaders: 0,
...tableParams,
};
if (!Number.isInteger(this.row) || !Number.isInteger(this.col)) {
return false;
}
if (this.row < -countColHeaders || this.col < -countRowHeaders) {
return false;
}
if (this.row >= countRows || this.col >= countCols) {
return false;
}
return true;
}
/**
* Checks if another set of coordinates (`coords`)
* is equal to the coordinates in your `CellCoords` instance.
*
* @param {CellCoords} coords Coordinates to check.
* @returns {boolean}
*/
isEqual(coords) {
if (coords === this) {
return true;
}
return this.row === coords.row && this.col === coords.col;
}
/**
* Checks if the coordinates point to the headers range. If one of the axis (row or col) point to
* the header (negative value) then method returns `true`.
*
* @returns {boolean}
*/
isHeader() {
return !this.isCell();
}
/**
* Checks if the coordinates point to the cells range. If all axis (row and col) point to
* the cell (positive value) then method returns `true`.
*
* @returns {boolean}
*/
isCell() {
return this.row >= 0 && this.col >= 0;
}
/**
* Checks if the coordinates runs in RTL mode.
*
* @returns {boolean}
*/
isRtl() {
return this.#isRtl;
}
/**
* Checks if another set of coordinates (`testedCoords`)
* is south-east of the coordinates in your `CellCoords` instance.
*
* @param {CellCoords} testedCoords Coordinates to check.
* @returns {boolean}
*/
isSouthEastOf(testedCoords) {
return this.row >= testedCoords.row &&
(this.#isRtl ? this.col <= testedCoords.col : this.col >= testedCoords.col);
}
/**
* Checks if another set of coordinates (`testedCoords`)
* is north-west of the coordinates in your `CellCoords` instance.
*
* @param {CellCoords} testedCoords Coordinates to check.
* @returns {boolean}
*/
isNorthWestOf(testedCoords) {
return this.row <= testedCoords.row &&
(this.#isRtl ? this.col >= testedCoords.col : this.col <= testedCoords.col);
}
/**
* Checks if another set of coordinates (`testedCoords`)
* is south-west of the coordinates in your `CellCoords` instance.
*
* @param {CellCoords} testedCoords Coordinates to check.
* @returns {boolean}
*/
isSouthWestOf(testedCoords) {
return this.row >= testedCoords.row &&
(this.#isRtl ? this.col >= testedCoords.col : this.col <= testedCoords.col);
}
/**
* Checks if another set of coordinates (`testedCoords`)
* is north-east of the coordinates in your `CellCoords` instance.
*
* @param {CellCoords} testedCoords Coordinates to check.
* @returns {boolean}
*/
isNorthEastOf(testedCoords) {
return this.row <= testedCoords.row &&
(this.#isRtl ? this.col <= testedCoords.col : this.col >= testedCoords.col);
}
/**
* Normalizes the coordinates in your `CellCoords` instance to the nearest valid position.
*
* Coordinates that point to headers (negative values) are normalized to `0`.
*
* @returns {CellCoords}
*/
normalize() {
this.row = this.row === null ? this.row : Math.max(this.row, 0);
this.col = this.col === null ? this.col : Math.max(this.col, 0);
return this;
}
/**
* Assigns the coordinates from another `CellCoords` instance (or compatible literal object)
* to your `CellCoords` instance.
*
* @param {CellCoords | { row: number | undefined, col: number | undefined }} coords The CellCoords
* instance or compatible literal object.
* @returns {CellCoords}
*/
assign(coords) {
if (Number.isInteger(coords?.row)) {
this.row = coords.row;
}
if (Number.isInteger(coords?.col)) {
this.col = coords.col;
}
if (coords instanceof CellCoords) {
this.#isRtl = coords.isRtl();
}
return this;
}
/**
* Clones your `CellCoords` instance.
*
* @returns {CellCoords}
*/
clone() {
return new CellCoords(this.row, this.col, this.#isRtl);
}
/**
* Converts your `CellCoords` instance into an object literal with `row` and `col` properties.
*
* @returns {{row: number, col: number}} An object literal with `row` and `col` properties.
*/
toObject() {
return {
row: this.row,
col: this.col,
};
}
}
export default CellCoords;