123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- import { CellCoords } from './../3rdparty/walkontable/src';
- import { stringify } from './../helpers/mixed';
- export var EditorState = {
- VIRGIN: 'STATE_VIRGIN', // before editing
- EDITING: 'STATE_EDITING',
- WAITING: 'STATE_WAITING', // waiting for async validation
- FINISHED: 'STATE_FINISHED'
- };
- function BaseEditor(instance) {
- this.instance = instance;
- this.state = EditorState.VIRGIN;
- this._opened = false;
- this._fullEditMode = false;
- this._closeCallback = null;
- this.init();
- }
- BaseEditor.prototype._fireCallbacks = function (result) {
- if (this._closeCallback) {
- this._closeCallback(result);
- this._closeCallback = null;
- }
- };
- BaseEditor.prototype.init = function () {};
- BaseEditor.prototype.getValue = function () {
- throw Error('Editor getValue() method unimplemented');
- };
- BaseEditor.prototype.setValue = function (newValue) {
- throw Error('Editor setValue() method unimplemented');
- };
- BaseEditor.prototype.open = function () {
- throw Error('Editor open() method unimplemented');
- };
- BaseEditor.prototype.close = function () {
- throw Error('Editor close() method unimplemented');
- };
- BaseEditor.prototype.prepare = function (row, col, prop, td, originalValue, cellProperties) {
- this.TD = td;
- this.row = row;
- this.col = col;
- this.prop = prop;
- this.originalValue = originalValue;
- this.cellProperties = cellProperties;
- var invalidActiveElement = !document.activeElement || document.activeElement && document.activeElement.nodeName === void 0;
- if (this.instance.view.isMouseDown() && document.activeElement && document.activeElement !== document.body && !invalidActiveElement) {
- document.activeElement.blur();
- } else if (invalidActiveElement) {
- // IE
- document.body.focus();
- }
- this.state = EditorState.VIRGIN;
- };
- BaseEditor.prototype.extend = function () {
- var baseClass = this.constructor;
- function Editor() {
- baseClass.apply(this, arguments);
- }
- function inherit(Child, Parent) {
- function Bridge() {}
- Bridge.prototype = Parent.prototype;
- Child.prototype = new Bridge();
- Child.prototype.constructor = Child;
- return Child;
- }
- return inherit(Editor, baseClass);
- };
- BaseEditor.prototype.saveValue = function (value, ctrlDown) {
- var selection = void 0;
- var tmp = void 0;
- // if ctrl+enter and multiple cells selected, behave like Excel (finish editing and apply to all cells)
- if (ctrlDown) {
- selection = this.instance.getSelected();
- if (selection[0] > selection[2]) {
- tmp = selection[0];
- selection[0] = selection[2];
- selection[2] = tmp;
- }
- if (selection[1] > selection[3]) {
- tmp = selection[1];
- selection[1] = selection[3];
- selection[3] = tmp;
- }
- } else {
- selection = [this.row, this.col, null, null];
- }
- this.instance.populateFromArray(selection[0], selection[1], value, selection[2], selection[3], 'edit');
- };
- BaseEditor.prototype.beginEditing = function (initialValue, event) {
- if (this.state != EditorState.VIRGIN) {
- return;
- }
- this.instance.view.scrollViewport(new CellCoords(this.row, this.col));
- this.instance.view.render();
- this.state = EditorState.EDITING;
- initialValue = typeof initialValue == 'string' ? initialValue : this.originalValue;
- this.setValue(stringify(initialValue));
- this.open(event);
- this._opened = true;
- this.focus();
- // only rerender the selections (FillHandle should disappear when beginediting is triggered)
- this.instance.view.render();
- this.instance.runHooks('afterBeginEditing', this.row, this.col);
- };
- BaseEditor.prototype.finishEditing = function (restoreOriginalValue, ctrlDown, callback) {
- var _this = this,
- val;
- if (callback) {
- var previousCloseCallback = this._closeCallback;
- this._closeCallback = function (result) {
- if (previousCloseCallback) {
- previousCloseCallback(result);
- }
- callback(result);
- _this.instance.view.render();
- };
- }
- if (this.isWaiting()) {
- return;
- }
- if (this.state == EditorState.VIRGIN) {
- this.instance._registerTimeout(setTimeout(function () {
- _this._fireCallbacks(true);
- }, 0));
- return;
- }
- if (this.state == EditorState.EDITING) {
- if (restoreOriginalValue) {
- this.cancelChanges();
- this.instance.view.render();
- return;
- }
- var value = this.getValue();
- if (this.instance.getSettings().trimWhitespace) {
- // We trim only string values
- val = [[typeof value === 'string' ? String.prototype.trim.call(value || '') : value]];
- } else {
- val = [[value]];
- }
- this.state = EditorState.WAITING;
- this.saveValue(val, ctrlDown);
- if (this.instance.getCellValidator(this.cellProperties)) {
- this.instance.addHookOnce('postAfterValidate', function (result) {
- _this.state = EditorState.FINISHED;
- _this.discardEditor(result);
- });
- } else {
- this.state = EditorState.FINISHED;
- this.discardEditor(true);
- }
- }
- };
- BaseEditor.prototype.cancelChanges = function () {
- this.state = EditorState.FINISHED;
- this.discardEditor();
- };
- BaseEditor.prototype.discardEditor = function (result) {
- if (this.state !== EditorState.FINISHED) {
- return;
- }
- // validator was defined and failed
- if (result === false && this.cellProperties.allowInvalid !== true) {
- this.instance.selectCell(this.row, this.col);
- this.focus();
- this.state = EditorState.EDITING;
- this._fireCallbacks(false);
- } else {
- this.close();
- this._opened = false;
- this._fullEditMode = false;
- this.state = EditorState.VIRGIN;
- this._fireCallbacks(true);
- }
- };
- /**
- * Switch editor into full edit mode. In this state navigation keys don't close editor. This mode is activated
- * automatically after hit ENTER or F2 key on the cell or while editing cell press F2 key.
- */
- BaseEditor.prototype.enableFullEditMode = function () {
- this._fullEditMode = true;
- };
- /**
- * Checks if editor is in full edit mode.
- *
- * @returns {Boolean}
- */
- BaseEditor.prototype.isInFullEditMode = function () {
- return this._fullEditMode;
- };
- BaseEditor.prototype.isOpened = function () {
- return this._opened;
- };
- BaseEditor.prototype.isWaiting = function () {
- return this.state === EditorState.WAITING;
- };
- BaseEditor.prototype.checkEditorSection = function () {
- var totalRows = this.instance.countRows();
- var section = '';
- if (this.row < this.instance.getSettings().fixedRowsTop) {
- if (this.col < this.instance.getSettings().fixedColumnsLeft) {
- section = 'top-left-corner';
- } else {
- section = 'top';
- }
- } else if (this.instance.getSettings().fixedRowsBottom && this.row >= totalRows - this.instance.getSettings().fixedRowsBottom) {
- if (this.col < this.instance.getSettings().fixedColumnsLeft) {
- section = 'bottom-left-corner';
- } else {
- section = 'bottom';
- }
- } else if (this.col < this.instance.getSettings().fixedColumnsLeft) {
- section = 'left';
- }
- return section;
- };
- export default BaseEditor;
|