/
coords.js
135 lines (121 loc) · 3.28 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
/**
* CellCoords holds cell coordinates (row, column) and few method to validate them and retrieve as an array or an object.
*
* @util
*/
class CellCoords {
constructor(row, column) {
/**
* Row index.
*
* @type {number}
*/
this.row = null;
/**
* Column index.
*
* @type {number}
*/
this.col = null;
if (typeof row !== 'undefined' && typeof column !== 'undefined') {
this.row = row;
this.col = column;
}
}
/**
* Checks if given set of coordinates is valid in context of a given Walkontable instance.
*
* @param {Walkontable} wot A Walkontable instance.
* @returns {boolean}
*/
isValid(wot) {
// is it a valid cell index (0 or higher)
if (this.row < 0 || this.col < 0) {
return false;
}
// is selection within total rows and columns
if (this.row >= wot.getSetting('totalRows') || this.col >= wot.getSetting('totalColumns')) {
return false;
}
return true;
}
/**
* Checks if this cell coordinates are the same as cell coordinates given as an argument.
*
* @param {CellCoords} cellCoords Cell coordinates to equal.
* @returns {boolean}
*/
isEqual(cellCoords) {
if (cellCoords === this) {
return true;
}
return this.row === cellCoords.row && this.col === cellCoords.col;
}
/**
* Checks if tested coordinates are positioned in south-east from this cell coordinates.
*
* @param {object} testedCoords Cell coordinates to check.
* @returns {boolean}
*/
isSouthEastOf(testedCoords) {
return this.row >= testedCoords.row && this.col >= testedCoords.col;
}
/**
* Checks if tested coordinates are positioned in north-east from this cell coordinates.
*
* @param {object} testedCoords Cell coordinates to check.
* @returns {boolean}
*/
isNorthWestOf(testedCoords) {
return this.row <= testedCoords.row && this.col <= testedCoords.col;
}
/**
* Checks if tested coordinates are positioned in south-west from this cell coordinates.
*
* @param {object} testedCoords Cell coordinates to check.
* @returns {boolean}
*/
isSouthWestOf(testedCoords) {
return this.row >= testedCoords.row && this.col <= testedCoords.col;
}
/**
* Checks if tested coordinates are positioned in north-east from this cell coordinates.
*
* @param {object} testedCoords Cell coordinates to check.
* @returns {boolean}
*/
isNorthEastOf(testedCoords) {
return this.row <= testedCoords.row && this.col >= testedCoords.col;
}
/**
* Normalizes the coordinates to the nearest valid position. The coordinates that point
* to the 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 the coordinates.
*
* @returns {CellCoords}
*/
clone() {
return new CellCoords(this.row, this.col);
}
/**
* Converts CellCoords to literal object with `row` and `col` properties.
*
* @returns {object} Returns a literal object with `row` and `col` properties.
*/
toObject() {
return {
row: this.row,
col: this.col,
};
}
}
export default CellCoords;