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.

223 lines
6.5 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 editpanelnavAdapter(id, args, pageId) {
this.id = id;
this.pageId = pageId;
this.opts = {
dispType: 'group',
editable: false,
groupCaption:[]
};
this.data = {
columns: [], // 列
item: {} //行数据
};
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.curRow
};
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.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.dataSrc.on('onValueChanged', function (ds, e) {
if (e.row === self.data.item) {
self.setValue(e.col.fieldName, e.row.getColumnText(e.col));
}
});
self.dataSrc.on('onPropertyChanged', function (ds, e) {
if (e.propertyName == 'Editable') {
if (ds.getEditable()) {
self.enable();
} else {
self.disable();
}
}
});
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;
}
}
});
//处理编辑受限问题
for (var i = 0; i < dsDetails.length; i++) {
dsDetails[i].on('onFieldChanged', function (ds, e) {
self.checkEditLimit(ds.getParentRelation().master);
});
dsDetails[i].on('onRowDeleted', function (ds, e) {
self.checkEditLimit(ds.getParentRelation().master);
});
dsDetails[i].on('onPropertyChanged', function (ds, e) {
if (e.propertyName == 'Editable') {
if (ds.getEditable()) {
self.enable();
self.checkEditLimit(ds.getParentRelation().master);
} else {
self.disable();
}
}
});
}
}
editpanelnavAdapter.prototype.enable = function () {
if (this.vmodel.$enable) {
this.vmodel.$enable();
}
}
editpanelnavAdapter.prototype.disable = function () {
if (this.vmodel.$disable) {
this.vmodel.$disable();
}
}
editpanelnavAdapter.prototype.refresh = function (row) {
if (this.vmodel.$refresh) {
this.vmodel.$refresh(row);
this.data.item = row;
}
}
editpanelnavAdapter.prototype.checkEditLimit = function (dataSrc) {
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();
}
for (var i = 0; i < dataSrc.columns.length; i++) {
var col = dataSrc.columns[i];
if (col.isEditLimit) {
self.setEditorEditable(col.fieldName, col.isEditable && isEmpty);
}
}
}
editpanelnavAdapter.prototype.setValue = function (fieldName, value) {
if (this.vmodel.$setValue) {
this.vmodel.$setValue(fieldName, value);
}
}
editpanelnavAdapter.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);
}
}
};
editpanelnavAdapter.prototype.addContextMenu = function (fn) {
this._contextMenu = fn;
};
editpanelnavAdapter.prototype.removeContextMenu = function () {
delete this._contextMenu;
};
editpanelnavAdapter.prototype.onRClick = function (e) {
if (this._contextMenu) {
e.preventDefault();
this._contextMenu({
top: e.pageY,
left: e.pageX
});
}
};
editpanelnavAdapter.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;
}
return editpanelnavAdapter;
})