tfp
Version:
A Web UI framework for TaskBuilder
1,510 lines (1,286 loc) • 46.1 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _controller = require("../controller.js");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
/**
* 数据集组件
* @param {[type]} dataModel [description]
*/
var DataSet = /*#__PURE__*/function (_VisibleComponent) {
(0, _inherits2["default"])(DataSet, _VisibleComponent);
var _super = _createSuper(DataSet);
function DataSet(__tfp, dataModel, parent) {
var _this;
(0, _classCallCheck2["default"])(this, DataSet);
_this = _super.call(this, __tfp, "DataSet", dataModel, parent);
if (_this._tfp.isDesigning && (!_this.dataModel.columns || _this.dataModel.columns.length == 0)) {
//默认创建1行3列
_this.dataModel.columns = [{
id: "col1",
name: "第1列",
type: "text",
width: "60px"
}, {
id: "col2",
name: "第2列",
type: "text",
width: "60px"
}, {
id: "col3",
name: "第3列",
type: "text",
width: "60px"
}];
}
return _this;
} //属性
(0, _createClass2["default"])(DataSet, [{
key: "loadDataService",
get: function get() {
return this.dataModel.loadDataService;
},
set: function set(value) {
this.dataModel.loadDataService = value;
} //属性
}, {
key: "dataBindingMember",
get: function get() {
return this.dataModel.dataBindingMember;
},
set: function set(value) {
this.dataModel.dataBindingMember = value;
}
}, {
key: "columns",
get: function get() {
return this.dataModel.columns;
},
set: function set(value) {
this.dataModel.columns = value;
if (this._jqObj) {
this.reRenderBody();
}
}
}, {
key: "rows",
get: function get() {
return this.getRows({
checkRequired: false
});
},
set: function set(value) {
this.dataModel.rows = value;
if (this._jqObj) {
this.reRenderBody();
}
}
}, {
key: "keyCol",
get: function get() {
return this.dataModel.keyCol;
},
set: function set(value) {
this.dataModel.keyCol = value;
if (this._jqObj) {//
}
}
}, {
key: "showAddButton",
get: function get() {
return this.dataModel.showAddButton;
},
set: function set(value) {
this.dataModel.showAddButton = value ? true : false;
if (this._jqObj) {
if (this.dataModel.showAddButton) {
$(".tfp-dataset-header").css("height", "70px");
$(".tfp-dataset-list").css("top", "71px");
$(".tfp-dataset-toolbar").show();
$(".tfp-dataset-button-add").show();
} else {
$(".tfp-dataset-button-add").hide();
if (!this.dataModel.showAddFromDialogButton) {
$(".tfp-dataset-toolbar").hide();
$(".tfp-dataset-header").css("height", "30px");
$(".tfp-dataset-list").css("top", "31px");
}
}
}
}
}, {
key: "addButtonText",
get: function get() {
if (!this.dataModel.addButtonText) this.dataModel.addButtonText = "添加";
return this.dataModel.addButtonText;
},
set: function set(value) {
this.dataModel.addButtonText = value;
if (this._jqObj) {
$(".tfp-dataset-button-add").html(value);
}
}
}, {
key: "showAddFromDialogButton",
get: function get() {
return this.dataModel.showAddFromDialogButton;
},
set: function set(value) {
this.dataModel.showAddFromDialogButton = value ? true : false;
if (this._jqObj) {
if (this.dataModel.showAddFromDialogButton) {
$(".tfp-dataset-header").css("height", "70px");
$(".tfp-dataset-list").css("top", "71px");
$(".tfp-dataset-toolbar").show();
$(".tfp-dataset-button-add-from-dialog").show();
} else {
$(".tfp-dataset-button-add-from-dialog").hide();
if (!this.dataModel.showAddButton) {
$(".tfp-dataset-toolbar").hide();
$(".tfp-dataset-header").css("height", "30px");
$(".tfp-dataset-list").css("top", "31px");
}
}
}
}
}, {
key: "addFromDialogButtonText",
get: function get() {
if (!this.dataModel.addFromDialogButtonText) this.dataModel.addFromDialogButtonText = "弹窗选择";
return this.dataModel.addFromDialogButtonText;
},
set: function set(value) {
this.dataModel.addFromDialogButtonText = value;
if (this._jqObj) {
$(".tfp-dataset-button-add-from-dialog").html(value);
}
}
}, {
key: "addFromDialogPath",
get: function get() {
return this.dataModel.addFromDialogPath;
},
set: function set(value) {
this.dataModel.addFromDialogPath = value;
}
}, {
key: "addFromDialogWidth",
get: function get() {
return this.dataModel.addFromDialogWidth;
},
set: function set(value) {
this.dataModel.addFromDialogWidth = value;
}
}, {
key: "addFromDialogHeight",
get: function get() {
return this.dataModel.addFromDialogHeight;
},
set: function set(value) {
this.dataModel.addFromDialogHeight = value;
}
}, {
key: "showCheckbox",
get: function get() {
return this.dataModel.showCheckbox;
},
set: function set(value) {
this.dataModel.showCheckbox = value ? true : false;
if (this._jqObj) {
this.reRenderBody();
}
}
}, {
key: "enableDelete",
get: function get() {
return this.dataModel.enableDelete;
},
set: function set(value) {
this.dataModel.enableDelete = value ? true : false;
if (this._jqObj) {
this.reRenderBody();
}
}
}, {
key: "reRenderBody",
value: function reRenderBody() {
this.dataModel.components = [];
this._jqObj.find(".tfp-dataset-titlerow").remove();
this._jqObj.find(".tfp-dataset-header").append(this.getTitleHtml());
this._jqObj.find(".tfp-dataset-list").remove();
this._jqObj.append(this.getBodyHtml());
this.bindEventHandler(); //this.initRuntime();
}
}, {
key: "getHeaderHtml",
value: function getHeaderHtml() {
var indent = this.getHtmlIndent();
var headerHeight = 0;
var showToolBar = false;
if (this.dataModel.showAddButton || this.dataModel.showAddFromDialogButton) {
showToolBar = true;
headerHeight = 40;
}
var html = indent + "\t\t<div class=\"tfp-dataset-toolbar\"";
if (!showToolBar) html += " style=\"display:none;\"";
html += ">\r\n";
html += indent + "\t\t\t<div class=\"tfp-dataset-button-add\"";
if (!this.dataModel.showAddButton) html += " style=\"display:none;\"";
html += ">" + this.addButtonText + "</div>\r\n";
html += indent + "\t\t\t<div class=\"tfp-dataset-button-add-from-dialog\"";
if (!this.dataModel.showAddFromDialogButton) html += " style=\"display:none;\"";
html += ">" + this.addFromDialogButtonText + "</div>\r\n";
html += indent + "\t\t</div>\r\n";
html += indent + "\t<div class=\"tfp-dataset-header\" style=\"top:" + headerHeight + "px;\">\r\n";
html += this.getTitleHtml();
html += indent + "\t</div>\r\n";
return html;
}
}, {
key: "getTitleHtml",
value: function getTitleHtml() {
var indent = this.getHtmlIndent();
var html = indent + "\t\t<div class=\"tfp-dataset-titlerow\">"; // style=\"width:{rowWidth};\">\r\n";
this.rowWidth = 6;
if (this.dataModel.enableDelete) this.rowWidth += 30;
if (this.dataModel.columns) {
if (this.dataModel.showCheckbox) {
this.rowWidth += 24;
html += indent + "\t\t\t<div style=\"flex-basis:24px; text-align: center;\">" + "<input type=\"checkbox\" class=\"tfp-checkbox tfp-dataset-checkall\" " + "style=\"margin-top:7px;\" onclick=\"" + this.id + ".checkAllOnClick(this)\" /></div>\r\n";
}
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i];
html += indent + "\t\t\t<div";
if (col.hidden) {
html += " style='display:none;'";
} else {
if (col.width) {
html += " style='flex-basis:" + (col.width + "").replace("px", "") + "px;'";
this.rowWidth += parseInt((col.width + '').replace("px", ""));
} else {
if (col.type.toLowerCase() == "switch") {
html += " style='flex-basis:40px;'";
this.rowWidth += 40;
} else {
html += " style='flex-basis:100px;'";
this.rowWidth += 100;
}
}
}
var colName = col.id;
if (col.name) colName = col.name;
html += ">" + colName + "</div>\r\n";
}
if (this.dataModel.enableDelete) {
html += indent + "\t\t\t<div style=\"flex-basis:30px;\"></div>\r\n";
}
} //html = html.replace("{rowWidth}", (this.rowWidth+6)+"px");
html += indent + "\t\t</div>\r\n";
return html;
}
}, {
key: "getBodyHtml",
value: function getBodyHtml() {
var headerHeight = 30;
if (this.dataModel.showAddButton || this.dataModel.showAddFromDialogButton) {
headerHeight = 70;
}
var indent = this.getHtmlIndent();
var html = indent + "\t<div class=\"tfp-dataset-list\" style=\"top:" + (headerHeight + 1) + "px;\">\r\n";
if (this.dataModel.columns) {
if (this.dataModel.rows && this.dataModel.rows.length > 0) {
for (var i = 0; i < this.dataModel.rows.length; i++) {
html += this.getRowHtml(i, this.dataModel.rows[i], indent, this.rowWidth);
}
} else if (this._tfp.isDesigning) {
//设计时添加三行空行
html += this.getRowHtml(0, null, indent, this.rowWidth);
html += this.getRowHtml(1, null, indent, this.rowWidth);
html += this.getRowHtml(2, null, indent, this.rowWidth);
}
}
html += indent + "\t</div>\r\n";
return html;
}
}, {
key: "getRowWidth",
value: function getRowWidth() {
var rowWidth = 6;
if (this.dataModel.enableDelete) rowWidth += 30;
if (this.dataModel.columns) {
if (this.dataModel.showCheckbox) rowWidth += 30;
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i];
if (col.width) {
rowWidth += parseInt((col.width + '').replace("px", ""));
} else {
if (col.type.toLowerCase() == "switch") {
rowWidth += 40;
} else {
rowWidth += 100;
}
}
}
}
return rowWidth;
}
/**
* 添加数据集行
* @param {[type]} data [description]
*/
}, {
key: "getRowHtml",
value: function getRowHtml(rowIndex, rowData, indent, rowWidth) {
if (!this.dataModel.columns) return "";
var htmlStr = indent + "\t\t<div class=\"tfp-dataset-row\" >\r\n"; //+"style=\"width:"+ this._tfp.formatPx(rowWidth) +";\">\r\n";
if (this.dataModel.showCheckbox) {
htmlStr += indent + "\t\t\t<div style=\"flex-basis:24px; text-align: center;\">" + "<input type=\"checkbox\" class=\"tfp-checkbox tfp-dataset-checkbox\" " + "style=\"margin-top:6px;\" /></div>\r\n";
}
this.newCpts = [];
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i];
htmlStr += indent + "\t\t\t<div style=\"";
if (col.hidden) htmlStr += "display:none;";
if (col.width) {
htmlStr += "flex-basis:" + this._tfp.formatPx(col.width) + ";";
} else {
if (col.type.toLowerCase() == "switch") {
htmlStr += "flex-basis:40px;";
} else {
htmlStr += "flex-basis:100px;";
}
}
if (col.type.toLowerCase() == "date" || col.type.toLowerCase() == "datetime") {
var cptId = this.id + "_" + col.type.toLowerCase() + "_" + rowIndex + "_" + i;
htmlStr += "border:0px;\" id=\"" + cptId + "\" class=\"tfp-" + col.type.toLowerCase() + "\"";
var cptDateTime = {
id: cptId,
type: col.type.toLowerCase() == "date" ? "Date" : "DateTime"
};
if (this.dataModel.onCellDataChange) {
cptDateTime.onChange = this.dataModel.onCellDataChange.substr(0, this.dataModel.onCellDataChange.indexOf("(")) + "(" + rowIndex + ", " + i + ")";
}
this.dataModel.components.push(cptDateTime);
this.newCpts.push(cptDateTime);
} else {
htmlStr += "\"";
}
htmlStr += ">";
if (col.type.toLowerCase() == "select" && !col.readonly) {
htmlStr += "<select>";
if (!col.required) htmlStr += "<option value=\"\"></option>";
if (col.options) {
var options = [];
if (Array.isArray(col.options)) {
for (var j = 0; j < col.options.length; j++) {
var option = col.options[j];
if (typeof option == "string") {
options.push({
value: option
});
} else if (Object.prototype.toString.call(option) === '[object Object]') {
options.push(option);
}
}
} else if (typeof col.options == "string") {
var arr = (col.options + '').split(",");
for (var j = 0; j < arr.length; j++) {
options.push({
value: arr[j]
});
}
}
for (var j = 0; j < options.length; j++) {
var _option = options[j];
htmlStr += "<option value='" + _option.value + "'";
if (rowData && rowData[col.id] == _option.value || col["default"] == _option.value && (!rowData || !rowData[col.id])) htmlStr += " selected";
var text = _option.value;
if (_option.text) text = _option.text;
htmlStr += ">" + text + "</option>";
}
}
htmlStr += "</select>";
} else if (col.type.toLowerCase() == "switch" && !col.readonly) {
var checked = false;
if (rowData && rowData[col.id] + "" == "true" || col["default"] && (!rowData || isNull(rowData[col.id]))) checked = true;
htmlStr += "<div class=\"switch\" value=\"" + checked + "\" style=\"height: 16px;\"";
if (this._tfp.isRuntime) htmlStr += " onclick=\"" + this.id + ".switchOnClick(this)\"";
htmlStr += ">";
htmlStr += "<div";
if (checked) htmlStr += " style=\"float: right; background-color:#0099ff;\"";
htmlStr += "></div>";
htmlStr += "</div>";
} else {
var iptType = "text";
if (col.type.toLowerCase() == "int") {
iptType = "number";
} else if (col.type.toLowerCase() == "password") {
iptType = "password";
}
htmlStr += "<input type=\"" + iptType + "\""; //if(col.onchange) htmlStr += " onchange='"+col.onchange+"'";
if (this._tfp.isDesigning || col.readonly) htmlStr += " readonly";
if (col.readonly && this._tfp.curPage.bgColorMode == "light") htmlStr += " style=\"background-color:#EEEEEE;\"";
var cellValue = "";
if (rowData && !isNull(rowData[col.id])) {
cellValue = rowData[col.id];
} else if (col["default"]) {
cellValue = col["default"];
}
if (typeof cellValue == "string" && cellValue.indexOf("\"") >= 0) {
htmlStr += " value='" + cellValue + "'";
} else {
htmlStr += " value=\"" + cellValue + "\"";
}
htmlStr += " />";
}
htmlStr += "</div>\r\n";
}
var delIconUrl = this._tfp.rootPath + "/src/components/dataset/images/delete-blue-24.png";
if (this.dataModel.enableDelete) {
htmlStr += indent + "\t\t\t<div style=\"flex-basis:30px;\">" + "<img src='" + delIconUrl + "' title=\"删除\"";
if (this._tfp.isRuntime) htmlStr += " onclick=\"" + this.id + ".deleteRow(this)\"";
htmlStr += " /></div>\r\n";
}
htmlStr += indent + "\t\t</div>\r\n";
return htmlStr;
}
/**
* 点击开关时
* @param {[type]} divSwitch [description]
* @return {[type]} [description]
*/
}, {
key: "switchOnClick",
value: function switchOnClick(divSwitch) {
if ($(divSwitch).find("div").css("float") == "left") {
$(divSwitch).find("div").css("float", "right");
$(divSwitch).find("div").css("background-color", "#0099ff");
$(divSwitch).attr("value", "true");
} else {
$(divSwitch).find("div").css("float", "left");
if (this._tfp.curPage.bgColorMode == "dark") {
$(divSwitch).find("div").css("background-color", "#666666");
} else {
$(divSwitch).find("div").css("background-color", "#999999");
}
$(divSwitch).attr("value", "false");
}
var rowIndex = $(divSwitch).closest(".tfp-dataset-row").index();
var colIndex = $(divSwitch).parent().index();
if (this.dataModel.showCheckbox) colIndex--;
var cellData = $(divSwitch).attr("value");
var that = this;
if (this.dataModel.onCellClick) {
$(ipt).click(function () {
try {
eval(that.dataModel.onCellClick);
} catch (err) {
console.log(err);
}
});
}
if (this.dataModel.onCellDataChange) {
try {
eval(that.dataModel.onCellDataChange);
} catch (err) {
console.error(err);
}
}
}
/**
* 执行计算表达式
* @param {[type]} colR [description]
* @param {[type]} rowData [description]
* @param {[type]} curRowDiv [description]
* @param {[type]} colIndex [description]
* @return {[type]} [description]
*/
}, {
key: "exeFormula",
value: function exeFormula(colR, rowData, curRowDiv, colIndex) {
var colRFormula = this._tfp.replaceDataField(rowData, colR.formula);
var formulaVal = null;
try {
formulaVal = eval(colRFormula);
} catch (error) {
console.error(error.message);
return;
}
if (colR.type == "text" && (colR.dataType == "int" || colR.dataType == "float" || colR.dataType == "money")) {
if (colR.dataType == "int") {
formulaVal = parseInt(formulaVal);
} else if (colR.dataType == "float") {
formulaVal = parseFloat(formulaVal);
} else if (colR.dataType == "money") {
formulaVal = this._tfp.formatMoney(formulaVal);
}
}
var colRDivIndex = colIndex;
if (this.dataModel.showCheckbox) colRDivIndex++;
curRowDiv.children().eq(colRDivIndex).find("input").val(formulaVal);
this._tfp.iptValueOnChange(this);
}
/**
* 添加新行:该方法已废除,请使用addRow
* @param {[type]} data [description]
*/
}, {
key: "addNewRow",
value: function addNewRow(data) {
this.addRow(data);
}
}, {
key: "bindScrollEvent",
value: function bindScrollEvent() {
var divHeader = this._jqObj.find(".tfp-dataset-header").get(0);
var divDataList = this._jqObj.find(".tfp-dataset-list").get(0);
divDataList.scrollTop = 0;
divDataList.addEventListener('scroll', function () {
divHeader.scrollLeft = divDataList.scrollLeft;
});
}
}, {
key: "bindEventHandler",
value: function bindEventHandler() {
this.bindScrollEvent();
var that = this;
this._jqObj.find(".tfp-dataset-row").each(function () {
var rowEl = $(this);
that.bindRowEventHandler(rowEl, rowEl.index());
});
if (this.dataModel.components) {
for (var i = 0; i < this.dataModel.components.length; i++) {
var cdm = this.dataModel.components[i];
tfp.parseCpt(cdm, this);
var cpt = tfp.components[cdm.id];
cpt.initRuntime();
}
}
this.isBindEventHandler = true;
}
}, {
key: "bindRowEventHandler",
value: function bindRowEventHandler(rowEl, rowIndex) {
var _this2 = this;
var that = this; //挂载单元格事件
var _loop = function _loop() {
var col = _this2.dataModel.columns[i];
var colIndex = i;
if (_this2.dataModel.showCheckbox) colIndex++;
rowEl.children().eq(colIndex).find("input").each(function () {
that.bindCellEventHandler(this, rowIndex, colIndex);
});
rowEl.children().eq(colIndex).find("select").each(function () {
that.bindCellEventHandler(this, rowIndex, colIndex);
});
rowEl.children().eq(colIndex).find(".switch").each(function () {
that.bindCellEventHandler(this, rowIndex, colIndex);
});
};
for (var i = 0; i < this.dataModel.columns.length; i++) {
_loop();
}
}
/**
* 绑定单元格输入项事件
* @param {[type]} ipt [description]
* @return {[type]} [description]
*/
}, {
key: "bindCellEventHandler",
value: function bindCellEventHandler(ipt, rowIndex, colIndex) {
var that = this;
var col = this.dataModel.columns[colIndex]; //如果是开关,则不在此处理,而是在开关单独的点击事件函数里处理
//如果是只读,不绑定事件
if (!col || col.type.toLowerCase() == "switch") return;
var cellData = $(ipt).val(); //单击事件
if (this.dataModel.onCellClick) {
$(ipt).click(function () {
try {
eval(that.dataModel.onCellClick);
} catch (err) {
console.log(err);
}
});
} //双击事件
if (this.dataModel.onCellDblClick) {
$(ipt).dblclick(function () {
try {
eval(that.dataModel.onCellDblClick);
} catch (err) {
console.log(err);
}
});
} //获得焦点事件
if (this.dataModel.onCellFocus) {
$(ipt).focus(function () {
try {
eval(that.dataModel.onCellFocus);
} catch (err) {
console.log(err);
}
});
}
if (col.readonly) return; //如果是下拉列表,要单独处理数据变化和失去焦点事件
if (col.type.toLowerCase() == "select") {
if (this.dataModel.onCellDataChange || this.calcCols && this.calcCols[col.id]) {
$(ipt).change(function () {
try {
eval(that.dataModel.onCellDataChange);
} catch (err) {
console.error(err);
}
that.formulaCell(this, col);
});
}
if (this.dataModel.onCellBlur) {
$(ipt).blur(function () {
try {
eval(that.dataModel.onCellBlur);
} catch (err) {
console.error(err);
}
});
}
} //最后再处理单行输入框的失去焦点事件
//因为单行输入框DOM元素没有数据变化事件
if (col.type.toLowerCase() == "text" || col.type.toLowerCase() == "password") {
$(ipt).blur(function () {
if (that.dataModel.onCellBlur) {
try {
eval(that.dataModel.onCellBlur);
} catch (err) {
console.log(err);
}
}
if (that.dataModel.onCellDataChange) {
try {
eval(that.dataModel.onCellDataChange);
} catch (err) {
console.error(err);
}
}
that.formulaCell(this, col);
});
if (that.dataModel.onCellKeyPress) {
$(ipt).keypress(function () {
try {
eval(that.dataModel.onCellKeyPress);
} catch (err) {
console.error(err);
}
});
}
}
}
}, {
key: "formulaCell",
value: function formulaCell(ipt, col) {
if (!(this.calcCols && this.calcCols[col.id])) return; //获得当前行的数据
var curRowDiv = $(ipt).parent().parent();
var rowDataTmp = this.getRow(curRowDiv.index(), {
autoFormat: true
}).row;
var arr = this.calcCols[col.id];
for (var j = 0; j < arr.length; j++) {
for (var k = 0; k < this.dataModel.columns.length; k++) {
var colR = this.dataModel.columns[k];
if (arr[j] == colR.id && colR.formula) {
this.exeFormula(colR, rowDataTmp, curRowDiv, k);
break;
}
}
}
}
/**
* 添加行
* @param {[type]} data [description]
*/
}, {
key: "addRow",
value: function addRow(data) {
var dataList = this._jqObj.find(".tfp-dataset-list");
var rowIndex = dataList.find(".tfp-dataset-row").length;
var rowData = data;
if (!rowData) rowData = {};
if (this.dataModel.onBeforeAddRow) {
try {
eval(this.dataModel.onBeforeAddRow);
} catch (err) {
console.log(err);
}
}
this.rows.push(rowData);
var rowWidth = this.getRowWidth();
var indent = this.getHtmlIndent();
var rowHtml = this.getRowHtml(rowIndex, data, indent + "\t\t", rowWidth);
dataList.append(rowHtml);
if (this.newCpts && this.newCpts.length > 0) {
for (var i = 0; i < this.newCpts.length; i++) {
var cdm = this.newCpts[i];
tfp.parseCpt(cdm, this);
var cpt = tfp.components[cdm.id];
cpt.initRuntime();
}
}
this.newCpts = [];
var lastRow = dataList.find(".tfp-dataset-row").last();
if (lastRow.find("input").length > 0) {
lastRow.find("input").eq(0).focus();
} else if (lastRow.find("select").length > 0) {
lastRow.find("select").eq(0).focus();
}
if (this.dataModel.onAfterAddRow) {
try {
eval(this.dataModel.onAfterAddRow);
} catch (err) {
console.log(err);
}
}
this.bindRowEventHandler(lastRow, rowIndex);
/*let that = this;
//挂载单元格事件
for(var i=0;i<this.dataModel.columns.length;i++) {
let col = this.dataModel.columns[i];
let colIndex = i;
if(this.dataModel.showCheckbox) colIndex++;
lastRow.children().eq(colIndex).find("input").each(function() {
that.bindCellEventHandler(this, rowIndex, colIndex);
});
lastRow.children().eq(colIndex).find("select").each(function() {
that.bindCellEventHandler(this, rowIndex, colIndex);
});
lastRow.children().eq(colIndex).find(".switch").each(function() {
that.bindCellEventHandler(this, rowIndex, colIndex);
});
}*/
}
/**
* 通过点击删除图片删除行
* @param {[type]} img [description]
* @return {[type]} [description]
*/
}, {
key: "deleteRow",
value: function deleteRow(img) {
this.removeRow($(img).parent().parent().index());
}
/**
* 删除行
* @param {[type]} rowIndex [description]
* @return {[type]} [description]
*/
}, {
key: "removeRow",
value: function removeRow(rowIndex) {
var rowData = this.rows[rowIndex];
if (this.dataModel.onBeforeRemoveRow) {
try {
eval(this.dataModel.onBeforeRemoveRow);
} catch (err) {
console.log(err);
}
}
this._jqObj.find(".tfp-dataset-row").eq(rowIndex).remove();
if (this.dataModel.onRemoveRow) {
try {
eval(this.dataModel.onRemoveRow);
} catch (err) {
console.log(err);
}
}
if (this.dataModel.onAfterRemoveRow) {
try {
eval(this.dataModel.onAfterRemoveRow);
} catch (err) {
console.log(err);
}
}
}
/**
* 点击全选框后
* @param {[type]} cbk [description]
* @return {[type]} [description]
*/
}, {
key: "checkAllOnClick",
value: function checkAllOnClick(cbk) {
var dataList = this._jqObj.find(".tfp-dataset-list");
dataList.find(".tfp-dataset-checkbox").each(function () {
$(this).get(0).checked = cbk.checked;
});
}
/**
* 全部选中
* @return {[type]} [description]
*/
}, {
key: "checkAll",
value: function checkAll() {
if (!this.dataModel.showCheckbox) return;
var dataList = this._jqObj.find(".tfp-dataset-list");
dataList.find(".tfp-dataset-checkbox").each(function () {
$(this).get(0).checked = true;
});
}
/**
* 取消全选
* @return {[type]} [description]
*/
}, {
key: "unCheckAll",
value: function unCheckAll() {
if (!this.dataModel.showCheckbox) return;
var dataList = this._jqObj.find(".tfp-dataset-list");
dataList.find(".tfp-dataset-checkbox").each(function () {
$(this).get(0).checked = false;
});
}
/**
* 选中指定键值的行
* @param {[type]} keys [description]
* @return {[type]} [description]
*/
}, {
key: "checkRows",
value: function checkRows(keys) {
if (!this.dataModel.showCheckbox || !this.dataModel.keyCol || !keys) return;
if (typeof keys == "string") keys = keys.split(",");
var keyColIndex = 0;
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i];
if (col.id == this.dataModel.keyCol) {
keyColIndex = i + 1;
break;
}
}
if (keyColIndex == 0) return;
var dataList = this._jqObj.find(".tfp-dataset-list");
dataList.find(".tfp-dataset-row").each(function () {
var keyColDiv = $(this).children("div").eq(keyColIndex);
var key = keyColDiv.find("input").val();
if (keys.contains(key)) $(this).find(".tfp-dataset-checkbox").get(0).checked = true;
});
}
/**
* 获得选中的行
* @param {Function} cb [description]
* @return {[type]} [description]
*/
}, {
key: "getCheckedRows",
value: function getCheckedRows(cb) {
if (cb) {
this.getRows({
onlyGetChecked: true,
checkRequired: true
}, cb);
return;
}
return this.getRows({
onlyGetChecked: true,
checkRequired: true
});
}
/**
* 获得指定单元格的数据
* @param {[type]} rowIndex [description]
* @param {[type]} _colIndex [description]
* @return {[type]} [description]
*/
}, {
key: "getCellData",
value: function getCellData(rowIndex, _colIndex) {
var dataList = this._jqObj.find(".tfp-dataset-list");
var rowDiv = dataList.find(".tfp-dataset-row").eq(rowIndex);
var colIndex = _colIndex;
if (this.dataModel.showCheckbox) colIndex++;
var val = null;
var colDiv = rowDiv.children("div").eq(colIndex);
var col = this.dataModel.columns[_colIndex];
var colType = col.type.toLowerCase();
if (colType == "select") {
val = colDiv.find(colType).val();
} else if (colType == "switch") {
val = false;
if (colDiv.find(".switch").attr("value") == "true") val = true;
} else {
val = colDiv.find("input").val();
}
if (val == "") {
if (colType == "text" && (col.dataType == "int" || col.dataType == "float" || col.dataType == "money")) {
if (col.dataType == "int") {
val = 0;
} else {
val = 0.00;
}
}
} else {
if (colType == "text" && (col.dataType == "int" || col.dataType == "float" || col.dataType == "money")) {
if (col.dataType == "int") {
val = parseInt(val);
} else {
val = parseFloat(val);
}
}
}
return val;
}
/**
* 设置指定单元格的值
* @param {[type]} rowIndex [description]
* @param {[type]} _colIndex [description]
* @param {[type]} val [description]
*/
}, {
key: "setCellData",
value: function setCellData(rowIndex, _colIndex, val) {
var dataList = this._jqObj.find(".tfp-dataset-list");
var rowDiv = dataList.find(".tfp-dataset-row").eq(rowIndex);
var colIndex = _colIndex;
if (this.dataModel.showCheckbox) colIndex++;
var colDiv = rowDiv.children("div").eq(colIndex);
var col = this.dataModel.columns[_colIndex];
var colType = col.type.toLowerCase();
if (colType == "switch") {
if (val + "" == "true") {
colDiv.find("div").css("float", "right");
colDiv.find("div").css("background-color", "#0099ff");
colDiv.attr("value", "true");
} else {
colDiv.find("div").css("float", "left");
if (this._tfp.curPage.bgColorMode == "dark") {
colDiv.find("div").css("background-color", "#666666");
} else {
colDiv.find("div").css("background-color", "#999999");
}
colDiv.attr("value", "false");
}
} else if (colType == "select") {
colDiv.find("select").val(val);
} else {
colDiv.find("input").val(val);
}
}
/**
* 设置行数据
* @param {[type]} rowIndex [description]
* @param {[type]} rowData [description]
*/
}, {
key: "setRow",
value: function setRow(rowIndex, rowData) {
if (!rowData || !((0, _typeof2["default"])(rowData) == "object")) return;
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i];
var colIndex = i;
if (this.dataModel.showCheckbox) colIndex++;
if (col.id in rowData) {
this.setCellData(rowIndex, colIndex, rowData[col.id]);
}
}
}
/**
* 获得指定行的div对象
* @param {[type]} rowIndex [description]
* @return {[type]} [description]
*/
}, {
key: "getRowDiv",
value: function getRowDiv(rowIndex) {
var dataList = this._jqObj.find(".tfp-dataset-list");
return dataList.find(".tfp-dataset-row").eq(rowIndex);
}
/**
* 获得指定单元格的div对象
* @param {[type]} rowIndex [description]
* @param {[type]} _colIndex [description]
* @return {[type]} [description]
*/
}, {
key: "getCellDiv",
value: function getCellDiv(rowIndex, _colIndex) {
var dataList = this._jqObj.find(".tfp-dataset-list");
var rowDiv = dataList.find(".tfp-dataset-row").eq(rowIndex);
var colIndex = _colIndex;
if (this.dataModel.showCheckbox) colIndex++;
return rowDiv.children("div").eq(colIndex);
}
/**
* 获得指定行的数据
* @param {[type]} index [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
}, {
key: "getRow",
value: function getRow(index, options) {
var checkRequired = false;
var onlyGetChecked = false;
var autoFormat = false;
if (options) {
if (options.checkRequired) checkRequired = true;
if (options.onlyGetChecked) onlyGetChecked = true;
if (options.autoFormat) autoFormat = true;
}
var dataList = this._jqObj.find(".tfp-dataset-list");
var rowDiv = dataList.find(".tfp-dataset-row").eq(index);
var row = {};
var checked = false;
if (this.dataModel.showCheckbox && rowDiv.find(".tfp-dataset-checkbox").get(0).checked) {
checked = true;
row.checked = true;
}
if (onlyGetChecked && !checked) return {
row: null,
isCheckOk: true
};
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i];
var colIndex = i;
if (this.dataModel.showCheckbox) colIndex++;
var val = null;
var colDiv = rowDiv.children("div").eq(colIndex);
var colType = col.type.toLowerCase();
if (colType == "select") {
val = colDiv.find(colType).val();
} else if (colType == "switch") {
val = false;
if (colDiv.find(".switch").attr("value") == "true") val = true;
} else {
val = colDiv.find("input").val();
}
if (val == "") {
if (col.required && checkRequired) {
var colName = col.id;
if (col.name) colName = col.name;
this._tfp.showMsg(colName + "不能为空!");
if (colType == "text" || colType == "select" || colType == "password" || colType == "number" || colType == "date" || colType == "datetime") {
colDiv.find(colType).focus();
}
return {
row: null,
isCheckOk: false
};
} else {
if (colType == "text" && autoFormat && (col.dataType == "int" || col.dataType == "float" || col.dataType == "money")) {
if (col.dataType == "int") {
val = 0;
} else {
val = 0.00;
}
}
}
} else {
if (colType == "text" && autoFormat && (col.dataType == "int" || col.dataType == "float" || col.dataType == "money")) {
if (col.dataType == "int") {
val = parseInt(val);
} else {
val = parseFloat(val);
}
}
row[col.id] = val;
}
}
return {
row: row,
isCheckOk: true
};
}
/**
* 获得所有行
* @param {[type]} options [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
}, {
key: "getRows",
value: function getRows(options, cb) {
var checkRequired = false;
var onlyGetChecked = false;
if (options) {
if (options.checkRequired) checkRequired = true;
if (options.onlyGetChecked) onlyGetChecked = true;
}
if (onlyGetChecked && !this.dataModel.showCheckbox) return [];
var that = this;
var rows = [];
var isCheckOk = true;
var dataList = this._jqObj.find(".tfp-dataset-list");
dataList.find(".tfp-dataset-row").each(function (index) {
var ret = that.getRow(index, options);
if (!ret.isCheckOk) {
isCheckOk = false;
return false;
}
if (ret.row) rows.push(ret.row);
});
if (!isCheckOk) return;
if (cb) {
cb(rows);
} else {
return rows;
}
}
/**
* 求指定列的合计值
* @param {[type]} colName [description]
* @return {[type]} [description]
*/
}, {
key: "sum",
value: function sum(colId) {
var ret = 0;
var colIndex = 0;
var dataType = "text";
for (var i = 0; i < this.columns.length; i++) {
var col = this.columns[i];
if (col.id == colId) {
colIndex = i;
if (this.dataModel.showCheckbox) colIndex++;
if (col.dataType) dataType = col.dataType;
break;
}
}
var that = this;
var dataList = this._jqObj.find(".tfp-dataset-list");
dataList.find(".tfp-dataset-row").each(function (index) {
var cellVal = $(this).children().eq(colIndex).find("input").val().trim().replaceAll(",", "");
if (cellVal != "") {
if (dataType == "int") {
if (isInt(cellVal)) ret += parseInt(cellVal);
} else if (!isNaN(cellVal)) {
ret += parseFloat(cellVal);
}
}
});
if (dataType == "money") {
ret = this._tfp.formatMoney(ret);
}
return ret;
}
/**
* 绑定数据
* @param {[type]} data [description]
* @return {[type]} [description]
*/
}, {
key: "bindData",
value: function bindData(data) {
this.rows = data;
}
/**
* 加载数据
* @return {[type]} [description]
*/
}, {
key: "loadData",
value: function loadData() {
if (!this.dataModel.loadDataService) {
//alert("请为["+this.id+"]设置加载数据服务!");
return;
}
var serviceCpt = this._tfp.get(this.dataModel.loadDataService);
if (!serviceCpt) {
alert("ID为[" + this.dataModel.loadDataService + "]的组件不存在!");
return;
}
if (!this.dataModel.dataBindingMember) {
alert("请为[" + this.id + "]设置数据绑定成员!");
return;
}
var that = this; //与服务组件建立绑定
if (!serviceCpt.bindCpts) serviceCpt.bindCpts = [];
if (!serviceCpt.bindCpts.contains(this.id)) serviceCpt.bindCpts.push(this.id);
serviceCpt.status = 0;
serviceCpt.request(null, function (req, res) {
var data = res[that.dataModel.dataBindingMember];
if (!data) return;
if (data.pageSize && data.rows) {
that.bindData(data.rows);
} else {
that.bindData(data);
}
});
}
}, {
key: "initDesigning",
value: function initDesigning() {
this.bindScrollEvent();
}
/**
* 初始化运行时
* @return {[type]} [description]
*/
}, {
key: "initRuntime",
value: function initRuntime() {
var that = this; //绑定添加按钮的点击事件
this._jqObj.find(".tfp-dataset-button-add").click(function () {
that.addRow();
}); //绑定打开对话框添加按钮的点击事件
this._jqObj.find(".tfp-dataset-button-add-from-dialog").click(function () {
if (isNull(that.dataModel.addFromDialogPath)) {
that._tfp.showMsg("请设置弹窗页面路径!");
return;
}
var width = "640px";
var height = "480px";
if (that.dataModel.addFromDialogWidth) width = that.dataModel.addFromDialogWidth + "";
if (width.indexOf("px") < 0) width += "px";
if (that.dataModel.addFromDialogHeight) height = that.dataModel.addFromDialogHeight + "";
if (height.indexOf("px") < 0) height += "px";
that._tfp.openDialog(that.dataModel.addFromDialogButtonText, that.dataModel.addFromDialogPath, width, height, null, function (ret) {
if (ret) {
if (Array.isArray(ret)) {
for (var _i = 0; _i < ret.length; _i++) {
that.addRow(ret[_i]);
}
} else {
that.addRow(ret);
}
}
});
}); //要在这里判断一下,否则有些情况会造成重复绑定事件
if (!this.isBindEventHandler) {
this.bindEventHandler();
}
this.loadData(); //设置计算列关联的列,以便当关联列的数据发生变化时,自动执行计算列的值
if (this.dataModel.columns) {
//存在关联计算的列
this.calcCols = {};
for (var i = 0; i < this.dataModel.columns.length; i++) {
var col = this.dataModel.columns[i]; //如果是计算列
if (col.formula) {
var arr = col.formula.match(/\{[\w]+\}/g);
if (arr.length > 0) {
for (var j = 0; j < arr.length; j++) {
for (var k = 0; k < this.dataModel.columns.length; k++) {
var colR = this.dataModel.columns[k]; //如果当前列与计算列有关联,则标示为计算关联列
if ("{" + colR.id + "}" == arr[j]) {
if (!this.calcCols[colR.id]) {
this.calcCols[colR.id] = [];
}
if (!this.calcCols[colR.id].contains(col.id)) this.calcCols[colR.id].push(col.id);
}
}
}
}
}
}
}
}
}]);
return DataSet;
}(_controller.VisibleComponent);
exports["default"] = DataSet;