devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
212 lines (210 loc) • 8.32 kB
JavaScript
/**
* DevExtreme (esm/__internal/grids/tree_list/editing/module.js)
* Version: 22.1.9
* Build date: Tue Apr 18 2023
*
* Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import "../module_not_extended/editor_factory";
import $ from "../../../../core/renderer";
import {
isDefined
} from "../../../../core/utils/type";
import {
extend
} from "../../../../core/utils/extend";
import {
Deferred
} from "../../../../core/utils/deferred";
import messageLocalization from "../../../../localization/message";
import errors from "../../../../ui/widget/ui.errors";
import gridCoreUtils from "../../../../ui/grid_core/ui.grid_core.utils";
import {
editingModule
} from "../../../../ui/grid_core/ui.grid_core.editing";
import treeListCore from "../module_core";
const TREELIST_EXPAND_ICON_CONTAINER_CLASS = "dx-treelist-icon-container";
const SELECT_CHECKBOX_CLASS = "dx-select-checkbox";
const DATA_EDIT_DATA_INSERT_TYPE = "insert";
const EditingController = editingModule.controllers.editing.inherit({
_generateNewItem(key) {
const item = this.callBase(key);
item.data = {
key: key
};
item.children = [];
item.level = 0;
item.parentKey = this.option("rootValue");
return item
},
_isProcessedItem: () => true,
_setInsertAfterOrBeforeKey(change, parentKey) {
if (void 0 !== parentKey && parentKey !== this.option("rootValue")) {
change.insertAfterKey = parentKey
} else {
this.callBase.apply(this, arguments)
}
},
_getLoadedRowIndex(items, change) {
const dataController = this.getController("data");
const dataSourceAdapter = dataController.dataSource();
const parentKey = null === dataSourceAdapter || void 0 === dataSourceAdapter ? void 0 : dataSourceAdapter.parentKeyOf(change.data);
if (void 0 !== parentKey && parentKey !== this.option("rootValue")) {
const rowIndex = gridCoreUtils.getIndexByKey(parentKey, items);
if (rowIndex >= 0 && this._dataController.isRowExpanded(parentKey)) {
return rowIndex + 1
}
return -1
}
return this.callBase.apply(this, arguments)
},
_isEditColumnVisible() {
const result = this.callBase.apply(this, arguments);
const editingOptions = this.option("editing");
return result || editingOptions.allowAdding
},
_isDefaultButtonVisible(button, options) {
const result = this.callBase.apply(this, arguments);
const {
row: row
} = options;
if ("add" === button.name) {
return this.allowAdding(options) && row.rowIndex !== this._getVisibleEditRowIndex() && !(row.removed || row.isNewRow)
}
return result
},
_getEditingButtons(options) {
const buttons = this.callBase.apply(this, arguments);
if (!options.column.buttons) {
buttons.unshift(this._getButtonConfig("add", options))
}
return buttons
},
_beforeSaveEditData(change) {
const dataController = this._dataController;
const result = this.callBase.apply(this, arguments);
if (change && "insert" !== change.type) {
const store = null === dataController || void 0 === dataController ? void 0 : dataController.store();
const key = null === store || void 0 === store ? void 0 : store.key();
if (!isDefined(key)) {
throw errors.Error("E1045")
}
}
return result
},
addRowByRowIndex(rowIndex) {
const dataController = this.getController("data");
const row = dataController.getVisibleRows()[rowIndex];
return this.addRow(row ? row.key : void 0)
},
addRow(key) {
if (void 0 === key) {
key = this.option("rootValue")
}
return this.callBase.call(this, key)
},
_addRowCore(data, parentKey, oldEditRowIndex) {
const {
callBase: callBase
} = this;
const rootValue = this.option("rootValue");
const dataController = this.getController("data");
const dataSourceAdapter = dataController.dataSource();
const parentKeyGetter = dataSourceAdapter.createParentIdGetter();
parentKey = parentKeyGetter(data);
if (void 0 !== parentKey && parentKey !== rootValue && !dataController.isRowExpanded(parentKey)) {
const deferred = new Deferred;
dataController.expandRow(parentKey).done(() => {
setTimeout(() => {
callBase.call(this, data, parentKey, oldEditRowIndex).done(deferred.resolve).fail(deferred.reject)
})
}).fail(deferred.reject);
return deferred.promise()
}
return callBase.call(this, data, parentKey, oldEditRowIndex)
},
_initNewRow(options, parentKey) {
const dataController = this.getController("data");
const dataSourceAdapter = dataController.dataSource();
const parentIdSetter = dataSourceAdapter.createParentIdSetter();
parentIdSetter(options.data, parentKey);
return this.callBase.apply(this, arguments)
},
allowAdding(options) {
return this._allowEditAction("allowAdding", options)
},
_needToCloseEditableCell($targetElement) {
return this.callBase.apply(this, arguments) || $targetElement.closest(".dx-treelist-icon-container").length && this.isEditing()
},
getButtonLocalizationNames() {
const names = this.callBase.apply(this);
names.add = "dxTreeList-editingAddRowToNode";
return names
}
});
const originalRowClick = editingModule.extenders.views.rowsView._rowClick;
const originalRowDblClick = editingModule.extenders.views.rowsView._rowDblClick;
const validateClick = function(e) {
const $targetElement = $(e.event.target);
const originalClickHandler = "dxdblclick" === e.event.type ? originalRowDblClick : originalRowClick;
if ($targetElement.closest(".dx-select-checkbox").length) {
return false
}
return !needToCallOriginalClickHandler.call(this, e, originalClickHandler)
};
function needToCallOriginalClickHandler(e, originalClickHandler) {
const $targetElement = $(e.event.target);
if (!$targetElement.closest(".dx-treelist-icon-container").length) {
originalClickHandler.call(this, e);
return true
}
return false
}
const RowsViewExtender = extend({}, editingModule.extenders.views.rowsView, {
_renderCellCommandContent($container, options) {
const editingController = this._editingController;
const isEditRow = options.row && editingController.isEditRow(options.row.rowIndex);
const isEditing = options.isEditing || isEditRow;
if (!isEditing) {
return this.callBase.apply(this, arguments)
}
return false
},
_rowClick(e) {
if (validateClick.call(this, e)) {
this.callBase.apply(this, arguments)
}
},
_rowDblClick(e) {
if (validateClick.call(this, e)) {
this.callBase.apply(this, arguments)
}
}
});
treeListCore.registerModule("editing", {
defaultOptions: () => extend(true, editingModule.defaultOptions(), {
editing: {
texts: {
addRowToNode: messageLocalization.format("dxTreeList-editingAddRowToNode")
}
}
}),
controllers: {
editing: EditingController
},
extenders: {
controllers: extend(true, {}, editingModule.extenders.controllers, {
data: {
changeRowExpand() {
this._editingController.refresh();
return this.callBase.apply(this, arguments)
}
}
}),
views: {
rowsView: RowsViewExtender,
headerPanel: editingModule.extenders.views.headerPanel
}
}
});