382a15665a4031ed30caa805e6439fabed425a847ab6d5d93ba7db861ec6bd7a98c2ea2e0271713b0e5236fe4f39ffb6e4e146a726087f7fd0d94592eab31e 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import Core from './../core';
  2. import {isObject} from './../helpers/object';
  3. /**
  4. * @class RecordTranslator
  5. * @util
  6. */
  7. class RecordTranslator {
  8. constructor(hot) {
  9. this.hot = hot;
  10. }
  11. /**
  12. * Translate physical row index into visual.
  13. *
  14. * @param {Number} row Physical row index.
  15. * @returns {Number} Returns visual row index.
  16. */
  17. toVisualRow(row) {
  18. return this.hot.runHooks('unmodifyRow', row);
  19. }
  20. /**
  21. * Translate physical column index into visual.
  22. *
  23. * @param {Number} column Physical column index.
  24. * @returns {Number} Returns visual column index.
  25. */
  26. toVisualColumn(column) {
  27. return this.hot.runHooks('unmodifyCol', column);
  28. }
  29. /**
  30. * Translate physical coordinates into visual. Can be passed as separate 2 arguments (row, column) or as an object in first
  31. * argument with `row` and `column` keys.
  32. *
  33. * @param {Number|Object} row Physical coordinates or row index.
  34. * @param {Number} [column] Physical column index.
  35. * @returns {Object|Array} Returns an object with visual records or an array if coordinates passed as separate arguments.
  36. */
  37. toVisual(row, column) {
  38. let result;
  39. if (isObject(row)) {
  40. result = {
  41. row: this.toVisualRow(row.row),
  42. column: this.toVisualColumn(row.column),
  43. };
  44. } else {
  45. result = [this.toVisualRow(row), this.toVisualColumn(column)];
  46. }
  47. return result;
  48. }
  49. /**
  50. * Translate visual row index into physical.
  51. *
  52. * @param {Number} row Visual row index.
  53. * @returns {Number} Returns physical row index.
  54. */
  55. toPhysicalRow(row) {
  56. return this.hot.runHooks('modifyRow', row);
  57. }
  58. /**
  59. * Translate visual column index into physical.
  60. *
  61. * @param {Number} column Visual column index.
  62. * @returns {Number} Returns physical column index.
  63. */
  64. toPhysicalColumn(column) {
  65. return this.hot.runHooks('modifyCol', column);
  66. }
  67. /**
  68. * Translate visual coordinates into physical. Can be passed as separate 2 arguments (row, column) or as an object in first
  69. * argument with `row` and `column` keys.
  70. *
  71. * @param {Number|Object} row Visual coordinates or row index.
  72. * @param {Number} [column] Visual column index.
  73. * @returns {Object|Array} Returns an object with physical records or an array if coordinates passed as separate arguments.
  74. */
  75. toPhysical(row, column) {
  76. let result;
  77. if (isObject(row)) {
  78. result = {
  79. row: this.toPhysicalRow(row.row),
  80. column: this.toPhysicalColumn(row.column),
  81. };
  82. } else {
  83. result = [this.toPhysicalRow(row), this.toPhysicalColumn(column)];
  84. }
  85. return result;
  86. }
  87. }
  88. export {RecordTranslator};
  89. const identities = new WeakMap();
  90. const translatorSingletons = new WeakMap();
  91. export function registerIdentity(identity, hot) {
  92. identities.set(identity, hot);
  93. }
  94. export function getTranslator(identity) {
  95. let singleton;
  96. if (!(identity instanceof Core)) {
  97. if (!identities.has(identity)) {
  98. throw Error('Record translator was not registered for this object identity');
  99. }
  100. identity = identities.get(identity);
  101. }
  102. if (translatorSingletons.has(identity)) {
  103. singleton = translatorSingletons.get(identity);
  104. } else {
  105. singleton = new RecordTranslator(identity);
  106. translatorSingletons.set(identity, singleton);
  107. }
  108. return singleton;
  109. }