9ac398f54a0d9bdee4559f73185dfd3a8d60f7161e529d886092654da9b49e71ba65e3618d119ba74d322890a1b552c22c7507bd8fc067e89e133d80cdf678 4.9 KB

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