/
coords.js
113 lines (102 loc) · 2.79 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
/**
* 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;
}
/**
* Converts CellCoords to literal object with `row` and `col` properties.
*
* @return {Object} Returns a literal object with `row` and `col` properties.
*/
toObject() {
return {
row: this.row,
col: this.col,
};
}
}
export default CellCoords;