handsontableEditor.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _unicode = require('./../helpers/unicode');
  4. var _object = require('./../helpers/object');
  5. var _element = require('./../helpers/dom/element');
  6. var _event = require('./../helpers/dom/event');
  7. var _textEditor = require('./textEditor');
  8. var _textEditor2 = _interopRequireDefault(_textEditor);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var HandsontableEditor = _textEditor2.default.prototype.extend();
  11. /**
  12. * @private
  13. * @editor HandsontableEditor
  14. * @class HandsontableEditor
  15. * @dependencies TextEditor
  16. */
  17. HandsontableEditor.prototype.createElements = function () {
  18. _textEditor2.default.prototype.createElements.apply(this, arguments);
  19. var DIV = document.createElement('DIV');
  20. DIV.className = 'handsontableEditor';
  21. this.TEXTAREA_PARENT.appendChild(DIV);
  22. this.htContainer = DIV;
  23. this.assignHooks();
  24. };
  25. HandsontableEditor.prototype.prepare = function (td, row, col, prop, value, cellProperties) {
  26. _textEditor2.default.prototype.prepare.apply(this, arguments);
  27. var parent = this;
  28. var options = {
  29. startRows: 0,
  30. startCols: 0,
  31. minRows: 0,
  32. minCols: 0,
  33. className: 'listbox',
  34. copyPaste: false,
  35. autoColumnSize: false,
  36. autoRowSize: false,
  37. readOnly: true,
  38. fillHandle: false,
  39. afterOnCellMouseDown: function afterOnCellMouseDown(_, coords) {
  40. var value = this.getSourceData(coords.row, coords.col);
  41. // if the value is undefined then it means we don't want to set the value
  42. if (value !== void 0) {
  43. parent.setValue(value);
  44. }
  45. parent.instance.destroyEditor();
  46. }
  47. };
  48. if (this.cellProperties.handsontable) {
  49. (0, _object.extend)(options, cellProperties.handsontable);
  50. }
  51. this.htOptions = options;
  52. };
  53. var onBeforeKeyDown = function onBeforeKeyDown(event) {
  54. if ((0, _event.isImmediatePropagationStopped)(event)) {
  55. return;
  56. }
  57. var editor = this.getActiveEditor();
  58. var innerHOT = editor.htEditor.getInstance();
  59. var rowToSelect;
  60. var selectedRow;
  61. if (event.keyCode == _unicode.KEY_CODES.ARROW_DOWN) {
  62. if (!innerHOT.getSelected() && !innerHOT.flipped) {
  63. rowToSelect = 0;
  64. } else if (innerHOT.getSelected()) {
  65. if (innerHOT.flipped) {
  66. rowToSelect = innerHOT.getSelected()[0] + 1;
  67. } else if (!innerHOT.flipped) {
  68. selectedRow = innerHOT.getSelected()[0];
  69. var lastRow = innerHOT.countRows() - 1;
  70. rowToSelect = Math.min(lastRow, selectedRow + 1);
  71. }
  72. }
  73. } else if (event.keyCode == _unicode.KEY_CODES.ARROW_UP) {
  74. if (!innerHOT.getSelected() && innerHOT.flipped) {
  75. rowToSelect = innerHOT.countRows() - 1;
  76. } else if (innerHOT.getSelected()) {
  77. if (innerHOT.flipped) {
  78. selectedRow = innerHOT.getSelected()[0];
  79. rowToSelect = Math.max(0, selectedRow - 1);
  80. } else {
  81. selectedRow = innerHOT.getSelected()[0];
  82. rowToSelect = selectedRow - 1;
  83. }
  84. }
  85. }
  86. if (rowToSelect !== void 0) {
  87. if (rowToSelect < 0 || innerHOT.flipped && rowToSelect > innerHOT.countRows() - 1) {
  88. innerHOT.deselectCell();
  89. } else {
  90. innerHOT.selectCell(rowToSelect, 0);
  91. }
  92. if (innerHOT.getData().length) {
  93. event.preventDefault();
  94. (0, _event.stopImmediatePropagation)(event);
  95. editor.instance.listen();
  96. editor.TEXTAREA.focus();
  97. }
  98. }
  99. };
  100. HandsontableEditor.prototype.open = function () {
  101. this.instance.addHook('beforeKeyDown', onBeforeKeyDown);
  102. _textEditor2.default.prototype.open.apply(this, arguments);
  103. if (this.htEditor) {
  104. this.htEditor.destroy();
  105. }
  106. this.htEditor = new Handsontable(this.htContainer, this.htOptions);
  107. if (this.cellProperties.strict) {
  108. this.htEditor.selectCell(0, 0);
  109. this.TEXTAREA.style.visibility = 'hidden';
  110. } else {
  111. this.htEditor.deselectCell();
  112. this.TEXTAREA.style.visibility = 'visible';
  113. }
  114. (0, _element.setCaretPosition)(this.TEXTAREA, 0, this.TEXTAREA.value.length);
  115. };
  116. HandsontableEditor.prototype.close = function () {
  117. this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
  118. this.instance.listen();
  119. _textEditor2.default.prototype.close.apply(this, arguments);
  120. };
  121. HandsontableEditor.prototype.focus = function () {
  122. this.instance.listen();
  123. _textEditor2.default.prototype.focus.apply(this, arguments);
  124. };
  125. HandsontableEditor.prototype.beginEditing = function (initialValue) {
  126. var onBeginEditing = this.instance.getSettings().onBeginEditing;
  127. if (onBeginEditing && onBeginEditing() === false) {
  128. return;
  129. }
  130. _textEditor2.default.prototype.beginEditing.apply(this, arguments);
  131. };
  132. HandsontableEditor.prototype.finishEditing = function (isCancelled, ctrlDown) {
  133. if (this.htEditor && this.htEditor.isListening()) {
  134. // if focus is still in the HOT editor
  135. this.instance.listen(); // return the focus to the parent HOT instance
  136. }
  137. if (this.htEditor && this.htEditor.getSelected()) {
  138. var value = this.htEditor.getInstance().getValue();
  139. if (value !== void 0) {
  140. // if the value is undefined then it means we don't want to set the value
  141. this.setValue(value);
  142. }
  143. }
  144. return _textEditor2.default.prototype.finishEditing.apply(this, arguments);
  145. };
  146. HandsontableEditor.prototype.assignHooks = function () {
  147. var _this = this;
  148. this.instance.addHook('afterDestroy', function () {
  149. if (_this.htEditor) {
  150. _this.htEditor.destroy();
  151. }
  152. });
  153. };
  154. exports.default = HandsontableEditor;