d2d42fbe772b17bf1794397f0b06c5e4b02fbb12a6d9874587d5ba0619e322dd5f7567bac6425bc704a89d84d539a39f43f01b2a556688ebe9e058643b41bd 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. import Core from './../core';
  4. import { isObject } from './../helpers/object';
  5. /**
  6. * @class RecordTranslator
  7. * @util
  8. */
  9. var RecordTranslator = function () {
  10. function RecordTranslator(hot) {
  11. _classCallCheck(this, RecordTranslator);
  12. this.hot = hot;
  13. }
  14. /**
  15. * Translate physical row index into visual.
  16. *
  17. * @param {Number} row Physical row index.
  18. * @returns {Number} Returns visual row index.
  19. */
  20. _createClass(RecordTranslator, [{
  21. key: 'toVisualRow',
  22. value: function toVisualRow(row) {
  23. return this.hot.runHooks('unmodifyRow', row);
  24. }
  25. /**
  26. * Translate physical column index into visual.
  27. *
  28. * @param {Number} column Physical column index.
  29. * @returns {Number} Returns visual column index.
  30. */
  31. }, {
  32. key: 'toVisualColumn',
  33. value: function toVisualColumn(column) {
  34. return this.hot.runHooks('unmodifyCol', column);
  35. }
  36. /**
  37. * Translate physical coordinates into visual. Can be passed as separate 2 arguments (row, column) or as an object in first
  38. * argument with `row` and `column` keys.
  39. *
  40. * @param {Number|Object} row Physical coordinates or row index.
  41. * @param {Number} [column] Physical column index.
  42. * @returns {Object|Array} Returns an object with visual records or an array if coordinates passed as separate arguments.
  43. */
  44. }, {
  45. key: 'toVisual',
  46. value: function toVisual(row, column) {
  47. var result = void 0;
  48. if (isObject(row)) {
  49. result = {
  50. row: this.toVisualRow(row.row),
  51. column: this.toVisualColumn(row.column)
  52. };
  53. } else {
  54. result = [this.toVisualRow(row), this.toVisualColumn(column)];
  55. }
  56. return result;
  57. }
  58. /**
  59. * Translate visual row index into physical.
  60. *
  61. * @param {Number} row Visual row index.
  62. * @returns {Number} Returns physical row index.
  63. */
  64. }, {
  65. key: 'toPhysicalRow',
  66. value: function toPhysicalRow(row) {
  67. return this.hot.runHooks('modifyRow', row);
  68. }
  69. /**
  70. * Translate visual column index into physical.
  71. *
  72. * @param {Number} column Visual column index.
  73. * @returns {Number} Returns physical column index.
  74. */
  75. }, {
  76. key: 'toPhysicalColumn',
  77. value: function toPhysicalColumn(column) {
  78. return this.hot.runHooks('modifyCol', column);
  79. }
  80. /**
  81. * Translate visual coordinates into physical. Can be passed as separate 2 arguments (row, column) or as an object in first
  82. * argument with `row` and `column` keys.
  83. *
  84. * @param {Number|Object} row Visual coordinates or row index.
  85. * @param {Number} [column] Visual column index.
  86. * @returns {Object|Array} Returns an object with physical records or an array if coordinates passed as separate arguments.
  87. */
  88. }, {
  89. key: 'toPhysical',
  90. value: function toPhysical(row, column) {
  91. var result = void 0;
  92. if (isObject(row)) {
  93. result = {
  94. row: this.toPhysicalRow(row.row),
  95. column: this.toPhysicalColumn(row.column)
  96. };
  97. } else {
  98. result = [this.toPhysicalRow(row), this.toPhysicalColumn(column)];
  99. }
  100. return result;
  101. }
  102. }]);
  103. return RecordTranslator;
  104. }();
  105. export { RecordTranslator };
  106. var identities = new WeakMap();
  107. var translatorSingletons = new WeakMap();
  108. export function registerIdentity(identity, hot) {
  109. identities.set(identity, hot);
  110. }
  111. export function getTranslator(identity) {
  112. var singleton = void 0;
  113. if (!(identity instanceof Core)) {
  114. if (!identities.has(identity)) {
  115. throw Error('Record translator was not registered for this object identity');
  116. }
  117. identity = identities.get(identity);
  118. }
  119. if (translatorSingletons.has(identity)) {
  120. singleton = translatorSingletons.get(identity);
  121. } else {
  122. singleton = new RecordTranslator(identity);
  123. translatorSingletons.set(identity, singleton);
  124. }
  125. return singleton;
  126. }