tfp
Version:
A Web UI framework for TaskBuilder
340 lines (285 loc) • 10 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
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 Select = /*#__PURE__*/function (_FormInput) {
(0, _inherits2["default"])(Select, _FormInput);
var _super = _createSuper(Select);
function Select(__tfp, dataModel, parent) {
(0, _classCallCheck2["default"])(this, Select);
return _super.call(this, __tfp, "Select", dataModel, parent);
} //属性
(0, _createClass2["default"])(Select, [{
key: "value",
get: function get() {
if (!this._tfp.isDesigning && this._jqObj) this.dataModel.value = this._jqObj.val();
if (!this.dataModel.value) return "";
return this.dataModel.value;
},
set: function set(value) {
this.dataModel.value = value;
if (this._jqObj && !this._tfp.isDesigning) {
if (this.dataModel.readonly) {
for (var i = 0; i < this.dataModel.options.length; i++) {
if (this.dataModel.options[i].value == value) {
var txt = this.dataModel.options[i].text;
if (!txt) txt = value;
this._jqObj.empty();
this._jqObj.append("<option value=\"" + value + "\">" + txt + "</option>");
return;
}
}
} else {
this._jqObj.val(value);
}
}
if (!this._tfp.isDesigning) {
this.valueOnChange();
this.exeEventHandler("onChange", value);
}
} //属性
}, {
key: "text",
get: function get() {
if (!this.options) return "";
var text = "";
for (var i = 0; i < this.options.length; i++) {
var option = this.options[i];
if (option.value == this.value) {
text = option.text;
if (!option.text && option.text != 0) text = option.value;
break;
}
}
return text;
}
}, {
key: "options",
get: function get() {
return this.dataModel.options;
},
set: function set(value) {
if (this._tfp.isDesigning) {
this.dataModel.options = value;
return;
}
if (this._jqObj && value && Array.isArray(value)) {
this._jqObj.empty();
this.dataModel.options = [];
if (!this.required) this._jqObj.append("<option value=\"\"></option>");
for (var i = 0; i < value.length; i++) {
this.addOption(value[i]);
} //TODO 如果重置选项列表后,没有组件当前值对应的选项,应该把组件值设置为空
}
}
}, {
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: "valueField",
get: function get() {
return this.dataModel.valueField;
},
set: function set(value) {
this.dataModel.valueField = value;
}
}, {
key: "textField",
get: function get() {
return this.dataModel.textField;
},
set: function set(value) {
this.dataModel.textField = value;
}
}, {
key: "showTree",
get: function get() {
return this.dataModel.showTree;
},
set: function set(value) {
this.dataModel.showTree = value ? true : false;
}
}, {
key: "parentIdField",
get: function get() {
return this.dataModel.parentIdField;
},
set: function set(value) {
this.dataModel.parentIdField = value;
}
}, {
key: "rootParentId",
get: function get() {
return this.dataModel.rootParentId;
},
set: function set(value) {
this.dataModel.rootParentId = value;
}
}, {
key: "addOption",
value: function addOption(option) {
if (!option) return;
var opt = option;
if (typeof opt == "string") opt = {
value: option,
text: option
};
if (!this.dataModel.options) this.dataModel.options = [];
this.dataModel.options.push(opt);
var txt = opt.text;
if (!txt) txt = opt.value;
this._jqObj.append("<option value=\"" + opt.value + "\">" + txt + "</option>");
if (opt.value == this.dataModel.value) this._jqObj.val(opt.value);
}
}, {
key: "createTree",
value: function createTree(data) {
this.nodes = {};
this.rootNodes = [];
var nodeIdArr = [];
for (var i = 0; i < data.length; i++) {
var nodeData = data[i];
var node = {};
node.id = nodeData[this.dataModel.valueField];
node.name = nodeData[this.dataModel.textField];
if (!this.dataModel.textField) node.name = nodeData[this.dataModel.valueField];
node.parentId = nodeData[this.dataModel.parentIdField];
node.data = nodeData;
node.childNodes = [];
this.nodes[node.id] = node;
nodeIdArr.push(node.id);
} //生成节点之间的关系
for (var i = 0; i < nodeIdArr.length; i++) {
var nodeId = nodeIdArr[i];
var node = this.nodes[nodeId];
if (node.parentId == this.dataModel.rootParentId) {
this.rootNodes.push(nodeId);
} else {
var parentNode = this.nodes[node.parentId];
if (parentNode) {
parentNode.childNodes.push(nodeId);
}
}
}
this.options = [];
for (var i = 0; i < this.rootNodes.length; i++) {
var node = this.nodes[this.rootNodes[i]];
if (!node.parentId) node.parentId = this.dataModel.rootParentId;
this.createTreeNode(node, "");
}
}
}, {
key: "createTreeNode",
value: function createTreeNode(node, prefix) {
this.addOption({
value: node.id,
text: prefix + node.name
});
for (var i = 0; i < node.childNodes.length; i++) {
var childNode = this.nodes[node.childNodes[i]];
this.createTreeNode(childNode, prefix + "--");
}
}
}, {
key: "bindData",
value: function bindData(data) {
if (isNull(data) || !Array.isArray(data)) return;
if (this.dataModel.showTree && this.dataModel.parentIdField) {
this.createTree(data);
return;
}
var optionsTmp = [];
for (var i = 0; i < data.length; i++) {
var row = data[i];
var option = {};
try {
option.value = this._tfp.replaceDataField(row, "{" + this.dataModel.valueField + "}");
} catch (e) {
console.log(e);
return;
}
if (isNull(option.value)) continue;
if (this.dataModel.textField) {
try {
option.text = this._tfp.replaceDataField(row, "{" + this.dataModel.textField + "}");
} catch (e) {
console.log(e);
return;
}
}
optionsTmp.push(option);
}
this.options = optionsTmp;
}
}, {
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;
}
if (!this.dataModel.valueField) {
alert("请为[" + this.id + "]设置选项值字段格式!");
return;
}
var that = this; //与服务组件建立绑定
if (!serviceCpt.bindCpts) serviceCpt.bindCpts = [];
if (!serviceCpt.bindCpts.contains(this.id)) serviceCpt.bindCpts.push(this.id);
serviceCpt.request(null, function (req, res) {
var data = res[that.dataModel.dataBindingMember];
that.bindData(data);
});
}
}, {
key: "initRuntime",
value: function initRuntime() {
if (this.dataModel.loadDataService) {
this.loadData();
}
var that = this;
this._jqObj.change(function () {
that.dataModel.value = $(this).val();
that.valueOnChange();
that.exeEventHandler("onChange", that.dataModel.value);
});
}
}]);
return Select;
}(_controller.FormInput);
exports["default"] = Select;