50cd9555e9004573be43aba52fbeb928f597990d5c06d482b99f76f2e22b58fb164fa0ad5cac152fa444838ac576abe5f787a0ab3f47e91a40152580803439 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * CellCoords holds cell coordinates (row, column) and few method to validate them and
  3. * retrieve as an array or an object
  4. *
  5. * @class CellCoords
  6. */
  7. class CellCoords {
  8. /**
  9. * @param {Number} row Row index
  10. * @param {Number} col Column index
  11. */
  12. constructor(row, col) {
  13. if (typeof row !== 'undefined' && typeof col !== 'undefined') {
  14. this.row = row;
  15. this.col = col;
  16. } else {
  17. this.row = null;
  18. this.col = null;
  19. }
  20. }
  21. /**
  22. * Checks if given set of coordinates is valid in context of a given Walkontable instance
  23. *
  24. * @param {Walkontable} wotInstance
  25. * @returns {Boolean}
  26. */
  27. isValid(wotInstance) {
  28. // is it a valid cell index (0 or higher)
  29. if (this.row < 0 || this.col < 0) {
  30. return false;
  31. }
  32. // is selection within total rows and columns
  33. if (this.row >= wotInstance.getSetting('totalRows') || this.col >= wotInstance.getSetting('totalColumns')) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. /**
  39. * Checks if this cell coords are the same as cell coords given as a parameter
  40. *
  41. * @param {CellCoords} cellCoords
  42. * @returns {Boolean}
  43. */
  44. isEqual(cellCoords) {
  45. if (cellCoords === this) {
  46. return true;
  47. }
  48. return this.row === cellCoords.row && this.col === cellCoords.col;
  49. }
  50. /**
  51. * Checks if tested coordinates are positioned in south-east from this cell coords
  52. *
  53. * @param {Object} testedCoords
  54. * @returns {Boolean}
  55. */
  56. isSouthEastOf(testedCoords) {
  57. return this.row >= testedCoords.row && this.col >= testedCoords.col;
  58. }
  59. /**
  60. * Checks if tested coordinates are positioned in north-east from this cell coords
  61. *
  62. * @param {Object} testedCoords
  63. * @returns {Boolean}
  64. */
  65. isNorthWestOf(testedCoords) {
  66. return this.row <= testedCoords.row && this.col <= testedCoords.col;
  67. }
  68. /**
  69. * Checks if tested coordinates are positioned in south-west from this cell coords
  70. *
  71. * @param {Object} testedCoords
  72. * @returns {Boolean}
  73. */
  74. isSouthWestOf(testedCoords) {
  75. return this.row >= testedCoords.row && this.col <= testedCoords.col;
  76. }
  77. /**
  78. * Checks if tested coordinates are positioned in north-east from this cell coords
  79. *
  80. * @param {Object} testedCoords
  81. * @returns {Boolean}
  82. */
  83. isNorthEastOf(testedCoords) {
  84. return this.row <= testedCoords.row && this.col >= testedCoords.col;
  85. }
  86. }
  87. export default CellCoords;