You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

define(function (require, exports, module) {
function EditPanelAdapter(id, args, pageId) {
this.id = id;
this.pageId = pageId;
this.opts = {
dispType: 'group',
editable: false
};
this.data = {
columns: [], // 列
item: {}, //行数据
groups: []
};
this.actions = {};
this.vmodel = {};
this.state = 'browse';
this.allowEdit = false;//不应该定义在opts里???
_init(this, args);
}
function _init(self, args) {
//如果是DataSource,进行相关数据提取和事件操作
if (args && args.data && args.data.isDataSource) {
_initDs(self, args);
}
if (self.actions.hasOwnProperty('onRClick')) {
var fn = self.actions.onRClick;
self.actions.onRClick = function (e) {
self.onRClick(e);
fn(e);
}
} else {
self.actions.onRClick = function (e) {
self.onRClick(e);
}
}
_.extend(self, args);
}
function _initDs(self, args) {
var dsDetails = null;
self.dataSrc = args.data;
dsDetails = self.dataSrc.getDetails();
self.title = self.dataSrc.uiObjName;
var _columns = _.filter(self.dataSrc.columns, function(item) {
return (item.procParaType == Store.Enums.ProcParaType.NONE ||
!item.procParaType);
});
args.data = {
columns: _columns,
item: self.dataSrc.currentRow,
headers: (self.dataSrc.headers || '').split(',')
};
self.dataSrc.on('onCurrentChanged', function (ds, e) {
self.refresh(e.newRow);
self.state = e.newRow && e.newRow.dataRowState == Store.Enums.DataRowState.ADDED ? 'add' : 'edit';
}, self);
self.dataSrc.on('onCollectChanged', function (ds, e) {
if (e.action == Store.Enums.CollectionChangedAction.RESET) {
self.refresh(ds.currentRow);
}
if (e.action == Store.Enums.CollectionChangedAction.REPLACE && e.row == self.data.item) {
self.refresh(e.row);
}
}, self);
self.dataSrc.on('onValueChanged', function (ds, e) {
if (e.row === self.data.item) {
self.setValue(e.col.fieldName, e.row.getColumnText(e.col));
}
}, self);
self.dataSrc.on('onPropertyChanged', function (ds, e) {
if (e.propertyName == 'Editable') {
if (ds.getEditable()) {
self.enable();
} else {
self.disable();
}
}
}, self);
self.dataSrc.on('commitEdit', function (ds, e) {
var currentRow = ds.currentRow;
var columns = ds.columns;
var editors = self.vmodel.$editors;
for (var i = 0; i < editors.length; i++) {
var editor = editors[i];
e.isCancel = !currentRow.setColumnText(editor.fieldName, editor.getValue());
if (e.isCancel) {
return;
}
}
}, self);
//处理编辑受限问题
for (var i = 0; i < dsDetails.length; i++) {
dsDetails[i].on('onFieldChanged', function (ds, e) {
if (ds.rows.length > 1) return;
if (!e.col.isEmptyField) return;
self.checkEditLimit(ds.getParentRelation().master);
}, self);
dsDetails[i].on('onRowDeleted', function (ds, e) {
if (ds.rows.length > 0) return;
self.checkEditLimit(ds.getParentRelation().master);
}, self);
dsDetails[i].on('onRowAdded', function (ds, e) {
if (ds.rows.length > 1) return;
self.checkEditLimit(ds.getParentRelation().master);
}, self);
dsDetails[i].on('onPropertyChanged', function (ds, e) {
if (e.propertyName == 'Editable') {
if (ds.getEditable()) {
self.enable();
self.checkEditLimit(ds.getParentRelation().master);
if (self.firstFocusField) {
self.setEditorEditable(self.firstFocusField, true);
}
} else {
self.disable();
}
}
}, self);
dsDetails[i].on('onCollectChanged', function (ds, e) {
self.checkEditLimit(ds.getParentRelation().master);
}, self);
}
}
EditPanelAdapter.prototype.enable = function () {
if (this.vmodel.$enable) {
this.vmodel.$enable();
}
}
EditPanelAdapter.prototype.disable = function () {
if (this.vmodel.$disable) {
this.vmodel.$disable();
}
}
EditPanelAdapter.prototype.refresh = function (row) {
if (this.vmodel.$refresh) {
this.vmodel.$refresh(row);
this.data.item = row;
}
}
EditPanelAdapter.prototype.checkEditLimit = function (dataSrc) {
if (!dataSrc.getEditable()) { return }
var self = this;
var details = dataSrc.getDetails();
if (details.length == 0) {
return;
}
var isEmpty = true;
for (var j = 0; j < details.length; j++) {
isEmpty = isEmpty && details[j].isEmpty();
}
this.firstFocusField = null;
for (var i = 0; i < dataSrc.columns.length; i++) {
var col = dataSrc.columns[i];
if (col.isEditLimit) {
self.setEditorEditable(col.fieldName, col.isEditable && isEmpty && dataSrc.allowEdit);
}
//查找应该获取焦点的第一个editor并将其设置焦点
if (!this.firstFocusField &&
dataSrc.allowEdit &&
!col.isPrimaryKey &&
( (col.isEditable && !col.isEditLimit)
|| (col.isEditable && col.isEditLimit && isEmpty) )
&& _.where(self.vmodel.$editors, {fieldName: col.fieldName})[0] ){
this.firstFocusField = col.fieldName;
}
}
// self.setEditorEditable(firstFocusField, true);
}
EditPanelAdapter.prototype.setValue = function (fieldName, value) {
if (this.vmodel.$setValue) {
this.vmodel.$setValue(fieldName, value);
}
}
EditPanelAdapter.prototype.setEditorEditable = function (colName, editable) {
if (this.vmodel && this.vmodel.$editors) {
var editor = _.where(this.vmodel.$editors, {fieldName: colName})[0];
if (editor) {
editor.disable(!editable);
}
}
};
EditPanelAdapter.prototype.addContextMenu = function (fn) {
this._contextMenu = fn;
};
EditPanelAdapter.prototype.removeContextMenu = function () {
delete this._contextMenu;
};
EditPanelAdapter.prototype.onRClick = function (e) {
if (this._contextMenu) {
e.preventDefault();
this._contextMenu({
top: e.pageY,
left: e.pageX
});
}
};
EditPanelAdapter.prototype.getColumnEditable = function (fieldName) {
//非数据源,编辑可用
if (!this.dataSrc) return true;
//数据源不允许编辑,编辑不可用
if (!this.dataSrc.allowEdit) return false;
//通过列的ReadOnly来决定是否可以编辑
var col = this.dataSrc.getColumn(fieldName);
if (col.getReadOnly()) return false;
//主键不允许编辑
if (this.state == 'edit' && col.isPrimaryKey) return false;
return true;
}
EditPanelAdapter.prototype.destroy = function () {
let self = this
self.dataSrc.unmount(self)
let dsDetails = self.dataSrc.getDetails();
//处理编辑受限问题
for (var i = 0; i < dsDetails.length; i++) {
dsDetails[i].unmount(self)
}
}
return EditPanelAdapter;
})