_baseEditor.js 6.8 KB

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