-
Notifications
You must be signed in to change notification settings - Fork 3k
/
coords.js
173 lines (158 loc) · 4.52 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
/* 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;
/**
* @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 a given Walkontable instance.
*
* The `row` index:
* - Can't be negative.
* - Can't be higher than the total number of rows in the Walkontable instance.
*
* The `col` index:
* - Can't be negative.
* - Can't be higher than the total number of columns in the Walkontable instance.
*
* @param {Walkontable} wot A Walkontable instance.
* @returns {boolean} `true`: The coordinates are valid.
*/
isValid(wot) {
// check if the row and column indexes are valid (0 or higher)
if (this.row < 0 || this.col < 0) {
return false;
}
// check if the selection fits in the total of rows and columns
if (this.row >= wot.getSetting('totalRows') || this.col >= wot.getSetting('totalColumns')) {
return false;
}
return true;
}
/**
* Checks if another set of coordinates (`cellCoords`)
* is equal to the coordinates in your `CellCoords` instance.
*
* @param {CellCoords} cellCoords Coordinates to check.
* @returns {boolean}
*/
isEqual(cellCoords) {
if (cellCoords === this) {
return true;
}
return this.row === cellCoords.row && this.col === cellCoords.col;
}
/**
* 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;
}
/**
* 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;