_baseEditor.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.EditorState = undefined;
  4. var _src = require('./../3rdparty/walkontable/src');
  5. var _mixed = require('./../helpers/mixed');
  6. var EditorState = exports.EditorState = {
  7. VIRGIN: 'STATE_VIRGIN', // before editing
  8. EDITING: 'STATE_EDITING',
  9. WAITING: 'STATE_WAITING', // waiting for async validation
  10. FINISHED: 'STATE_FINISHED'
  11. };
  12. function BaseEditor(instance) {
  13. this.instance = instance;
  14. this.state = EditorState.VIRGIN;
  15. this._opened = false;
  16. this._fullEditMode = false;
  17. this._closeCallback = null;
  18. this.init();
  19. }
  20. BaseEditor.prototype._fireCallbacks = function (result) {
  21. if (this._closeCallback) {
  22. this._closeCallback(result);
  23. this._closeCallback = null;
  24. }
  25. };
  26. BaseEditor.prototype.init = function () {};
  27. BaseEditor.prototype.getValue = function () {
  28. throw Error('Editor getValue() method unimplemented');
  29. };
  30. BaseEditor.prototype.setValue = function (newValue) {
  31. throw Error('Editor setValue() method unimplemented');
  32. };
  33. BaseEditor.prototype.open = function () {
  34. throw Error('Editor open() method unimplemented');
  35. };
  36. BaseEditor.prototype.close = function () {
  37. throw Error('Editor close() method unimplemented');
  38. };
  39. BaseEditor.prototype.prepare = function (row, col, prop, td, originalValue, cellProperties) {
  40. this.TD = td;
  41. this.row = row;
  42. this.col = col;
  43. this.prop = prop;
  44. this.originalValue = originalValue;
  45. this.cellProperties = cellProperties;
  46. var invalidActiveElement = !document.activeElement || document.activeElement && document.activeElement.nodeName === void 0;
  47. if (this.instance.view.isMouseDown() && document.activeElement && document.activeElement !== document.body && !invalidActiveElement) {
  48. document.activeElement.blur();
  49. } else if (invalidActiveElement) {
  50. // IE
  51. document.body.focus();
  52. }
  53. this.state = EditorState.VIRGIN;
  54. };
  55. BaseEditor.prototype.extend = function () {
  56. var baseClass = this.constructor;
  57. function Editor() {
  58. baseClass.apply(this, arguments);
  59. }
  60. function inherit(Child, Parent) {
  61. function Bridge() {}
  62. Bridge.prototype = Parent.prototype;
  63. Child.prototype = new Bridge();
  64. Child.prototype.constructor = Child;
  65. return Child;
  66. }
  67. return inherit(Editor, baseClass);
  68. };
  69. BaseEditor.prototype.saveValue = function (value, ctrlDown) {
  70. var selection = void 0;
  71. var tmp = void 0;
  72. // if ctrl+enter and multiple cells selected, behave like Excel (finish editing and apply to all cells)
  73. if (ctrlDown) {
  74. selection = this.instance.getSelected();
  75. if (selection[0] > selection[2]) {
  76. tmp = selection[0];
  77. selection[0] = selection[2];
  78. selection[2] = tmp;
  79. }
  80. if (selection[1] > selection[3]) {
  81. tmp = selection[1];
  82. selection[1] = selection[3];
  83. selection[3] = tmp;
  84. }
  85. } else {
  86. selection = [this.row, this.col, null, null];
  87. }
  88. this.instance.populateFromArray(selection[0], selection[1], value, selection[2], selection[3], 'edit');
  89. };
  90. BaseEditor.prototype.beginEditing = function (initialValue, event) {
  91. if (this.state != EditorState.VIRGIN) {
  92. return;
  93. }
  94. this.instance.view.scrollViewport(new _src.CellCoords(this.row, this.col));
  95. this.instance.view.render();
  96. this.state = EditorState.EDITING;
  97. initialValue = typeof initialValue == 'string' ? initialValue : this.originalValue;
  98. this.setValue((0, _mixed.stringify)(initialValue));
  99. this.open(event);
  100. this._opened = true;
  101. this.focus();
  102. // only rerender the selections (FillHandle should disappear when beginediting is triggered)
  103. this.instance.view.render();
  104. this.instance.runHooks('afterBeginEditing', this.row, this.col);
  105. };
  106. BaseEditor.prototype.finishEditing = function (restoreOriginalValue, ctrlDown, callback) {
  107. var _this = this,
  108. val;
  109. if (callback) {
  110. var previousCloseCallback = this._closeCallback;
  111. this._closeCallback = function (result) {
  112. if (previousCloseCallback) {
  113. previousCloseCallback(result);
  114. }
  115. callback(result);
  116. _this.instance.view.render();
  117. };
  118. }
  119. if (this.isWaiting()) {
  120. return;
  121. }
  122. if (this.state == EditorState.VIRGIN) {
  123. this.instance._registerTimeout(setTimeout(function () {
  124. _this._fireCallbacks(true);
  125. }, 0));
  126. return;
  127. }
  128. if (this.state == EditorState.EDITING) {
  129. if (restoreOriginalValue) {
  130. this.cancelChanges();
  131. this.instance.view.render();
  132. return;
  133. }
  134. var value = this.getValue();
  135. if (this.instance.getSettings().trimWhitespace) {
  136. // We trim only string values
  137. val = [[typeof value === 'string' ? String.prototype.trim.call(value || '') : value]];
  138. } else {
  139. val = [[value]];
  140. }
  141. this.state = EditorState.WAITING;
  142. this.saveValue(val, ctrlDown);
  143. if (this.instance.getCellValidator(this.cellProperties)) {
  144. this.instance.addHookOnce('postAfterValidate', function (result) {
  145. _this.state = EditorState.FINISHED;
  146. _this.discardEditor(result);
  147. });
  148. } else {
  149. this.state = EditorState.FINISHED;
  150. this.discardEditor(true);
  151. }
  152. }
  153. };
  154. BaseEditor.prototype.cancelChanges = function () {
  155. this.state = EditorState.FINISHED;
  156. this.discardEditor();
  157. };
  158. BaseEditor.prototype.discardEditor = function (result) {
  159. if (this.state !== EditorState.FINISHED) {
  160. return;
  161. }
  162. // validator was defined and failed
  163. if (result === false && this.cellProperties.allowInvalid !== true) {
  164. this.instance.selectCell(this.row, this.col);
  165. this.focus();
  166. this.state = EditorState.EDITING;
  167. this._fireCallbacks(false);
  168. } else {
  169. this.close();
  170. this._opened = false;
  171. this._fullEditMode = false;
  172. this.state = EditorState.VIRGIN;
  173. this._fireCallbacks(true);
  174. }
  175. };
  176. /**
  177. * Switch editor into full edit mode. In this state navigation keys don't close editor. This mode is activated
  178. * automatically after hit ENTER or F2 key on the cell or while editing cell press F2 key.
  179. */
  180. BaseEditor.prototype.enableFullEditMode = function () {
  181. this._fullEditMode = true;
  182. };
  183. /**
  184. * Checks if editor is in full edit mode.
  185. *
  186. * @returns {Boolean}
  187. */
  188. BaseEditor.prototype.isInFullEditMode = function () {
  189. return this._fullEditMode;
  190. };
  191. BaseEditor.prototype.isOpened = function () {
  192. return this._opened;
  193. };
  194. BaseEditor.prototype.isWaiting = function () {
  195. return this.state === EditorState.WAITING;
  196. };
  197. BaseEditor.prototype.checkEditorSection = function () {
  198. var totalRows = this.instance.countRows();
  199. var section = '';
  200. if (this.row < this.instance.getSettings().fixedRowsTop) {
  201. if (this.col < this.instance.getSettings().fixedColumnsLeft) {
  202. section = 'top-left-corner';
  203. } else {
  204. section = 'top';
  205. }
  206. } else if (this.instance.getSettings().fixedRowsBottom && this.row >= totalRows - this.instance.getSettings().fixedRowsBottom) {
  207. if (this.col < this.instance.getSettings().fixedColumnsLeft) {
  208. section = 'bottom-left-corner';
  209. } else {
  210. section = 'bottom';
  211. }
  212. } else if (this.col < this.instance.getSettings().fixedColumnsLeft) {
  213. section = 'left';
  214. }
  215. return section;
  216. };
  217. exports.default = BaseEditor;