/**
* 数据模板数据结构.
* @typedef {array} datatemplatedata
* @example
[ //数据模板数据条目
{
"org": [{
"distinguishedname": "张三@bf007525-99a3-4178-a474-32865bdddec8@i",
"id": "bf007525-99a3-4178-a474-32865bdddec8",
"name": "张三",
"person": "0c828550-d8ab-479e-9880-09a59332f1ed",
"unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
"unitlevelname": "兰德纵横/市场营销部",
"unitname": "市场营销部"
}],
"org_1": [{
"distinguishedname": "张三@bf007525-99a3-4178-a474-32865bdddec8@i",
"id": "bf007525-99a3-4178-a474-32865bdddec8",
"name": "张三",
"person": "0c828550-d8ab-479e-9880-09a59332f1ed",
"unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
"unitlevelname": "兰德纵横/市场营销部",
"unitname": "市场营销部"
}, {
"distinguishedname": "李四@bf007525-99a3-4178-a474-32865bdddec8@i",
"id": "bf007525-99a3-4178-a474-32865bdddec8",
"name": "李四",
"person": "0c828550-d8ab-479e-9880-09a59332f1ed",
"unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
"unitlevelname": "兰德纵横/市场营销部",
"unitname": "市场营销部"
}],
"number": "111",
"textfield": "杭州",
"attachment": [
{
"activityname": "拟稿",
"extension": "jpg",
"id": "9514758e-9e28-4bfe-87d7-824f2811f173",
"lastupdatetime": "2020-12-09 21:48:03",
"length": 452863.0,
"name": "111.jpg",
"person": "李四@lisi@p"
}
]
},
...
]
*/
mwf.xdesktop.requireapp("process.xform", "$module", null, false);
/** @class datatemplate 数据模板组件。自定义结构和样式的多行数据编辑组件。
* @o2cn 数据模板
* @example
* //可以在脚本中获取该组件
* //方法1:
* var datatemplate = this.form.get("name"); //获取组件
* //方法2
* var datatemplate = this.target; //在组件事件脚本中获取
* @extends mwf.xapplication.process.xform.$module
* @o2category formcomponents
* @since v6.2
* @o2range {process|cms|protal}
* @hideconstructor
*/
mwf.xapplication.process.xform.datatemplate = mwf.appdatatemplate = new class(
/** @lends mwf.xapplication.process.xform.datatemplate# */
{
implements: [events],
extends: mwf.app$module,
isedit: false,
options: {
/**
* 所有内容加载后执行(包括异步加载)。
* @event mwf.xapplication.process.xform.datatemplate#afterload
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 每初始化一个条目,但未加载的时候触发,通过this.event可以获取条目对象。
* @event mwf.xapplication.process.xform.datatemplate#beforeloadline
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 每一个条目加载后时候触发,通过this.event可以获取条目对象。
* @event mwf.xapplication.process.xform.datatemplate#afterloadline
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 数据模板改变时触发。通过this.event.lines可以获取修改的条目数组,this.event.type可以获得修改的类型。
*
*
*
*
*
*
*
* this.event.type | 触发类型 | this.event.lines |
---|
addline | 添加一行 | 添加的行数组 |
deleteline | 删除一行 | 删除的行数组 |
deletelines | 删除多行 | 删除的行数组 |
editmodule | 字段值改变时 | this.event.lines为编辑的行数组 this.event.module为修改的字段 |
import | 导入数据后 | 数据模板所有行 |
* @event mwf.xapplication.process.xform.datatemplate#change
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 添加条目时触发。通过this.event.line可以获取对应的条目对象,this.event.ev可以获得事件触发的event。
* @event mwf.xapplication.process.xform.datatemplate#addline
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 删除条目前触发。通过this.event可以获取对应的条目对象。
* @event mwf.xapplication.process.xform.datatemplate#deleteline
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 删除条目后触发。
* @event mwf.xapplication.process.xform.datatemplate#afterdeleteline
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 导出前触发。
* @event mwf.xapplication.process.xform.datatablepc#beforeexport
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 导出excel的时候触发,this.event指向导出的数据,您可以通过修改this.event来修改数据。
* @event mwf.xapplication.process.xform.datatemplate#export
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
* @example
* this.event数据格式如下:
* {
* data : [
* ["姓名","性别","学历","专业","出生日期","毕业日期"], //标题
* [ "张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
* [ "李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
* ], //导出的数据
* colwidtharray : [100, 50, 100, 200, 150, 150], //每列宽度
* title : "xxxx" //导出的excel文件标题
* }
*/
/**
* 导入前触发。
* @event mwf.xapplication.process.xform.datatablepc#beforeimport
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 在导入excel,进行数据校验后触发,this.event指向导入的数据。
* @event mwf.xapplication.process.xform.datatemplate#validimport
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
* @example
* this.event数据格式如下:
* {
* data : [
* {
* "姓名" : "张三",
* "性别" : "男",
* "学历" : "大学本科",
* "专业" : "计算机",
* "出生日期" : "aa01-1-2",
* "毕业日期" : "2019-9-2",
* "errortextlist" : [
* "第5列:aa01-1-2不是正确的日期格式。"
* ] //校验出的错误信息,如果该行数据正确,则无该字段
* }
* ...
* ], //导入的数据
* "validted" : true //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
* }
*/
/**
* 在导入excel,数据校验成功将要设置回数据模板的时候触发,this.event指向整理过的导入数据,格式见{@link datatemplatedata}。
* @event mwf.xapplication.process.xform.datatemplate#import
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
/**
* 在导入excel,数据设置回数据模板以后触发,this.event指向整理过的导入数据,格式见{@link datatemplatedata}。
* @event mwf.xapplication.process.xform.datatemplate#afterimport
* @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zts|组件事件说明}
*/
"moduleevents": ["queryload","postload","load", "afterload",
"beforeloadline", "afterloadline", "change", "addline", "deleteline", "afterdeleteline", "beforeexport",
"beforeimport", "export", "import", "validimport", "afterimport"]
},
initialize: function(node, json, form, options){
this.node = $(node);
this.node.store("module", this);
this.json = json;
this.form = form;
this.field = true;
this.fieldmoduleloaded = false;
},
load: function(){
this._loadmoduleevents();
if (this.fireevent("queryload")){
this._queryloaded();
this._loaduserinterface();
this._loadstyles();
this._loaddomevents();
//this._loadevents();
this._afterloaded();
this.fireevent("afterload");
// this.fireevent("load");
}
},
_loadmergeeditnodebyscript: function(){
if (this.json.sectionmergeeditscript && this.json.sectionmergeeditscript.code) {
var data = this.form.macro.exec(this.json.sectionmergeeditscript.code, this);
this._setbusinessdata( data );
//this._loaduserinterface();
}
},
_loadmergeeditnodebydefault: function(){
var data = this.getsortedsectiondata();
var businessdata = [];
data.each(function(d){
d.data = d.data || [];
businessdata = businessdata.concat( d.data );
});
this._setbusinessdata(businessdata);
//this._loaduserinterface();
},
_loaduserinterface: function(){
// this.fireevent("queryload");
debugger;
this.loading = true;
if( this.issectionmergeedit() ){ //区段合并,删除区段值合并数据后编辑
if( this.json.mergetypeedit === "script" ){
this._loadmergeeditnodebyscript();
}else{
this._loadmergeeditnodebydefault();
}
}
//区段合并展现
this.ismergeread = this.issectionmergeread();
//启用区段且显示所有区段
this.sectionby = this._getsectionby();
this.isshowallsection = this.isallsectionshow();
var iconnode = this.node.getelement("div[o2icon='datatemplate']");
if(iconnode)iconnode.destroy();
// this.editmodules = [];
this.node.setstyle("overflow-x", "auto");
this.node.setstyle("overflow-y", "hidden");
this.editable = !(this.readonly || (this.json.isreadonly === true) || (this.form.json.isreadonly === true));
if( this.ismergeread )this.editable = false;
if (this.editable && this.json.editablescript && this.json.editablescript.code){
this.editable = this.form.macro.exec(((this.json.editablescript) ? this.json.editablescript.code : ""), this);
}
this.getrelativeid();
//允许导入
this.importenable = this.editable && (this.importactionidlist.length > 0) &&
(this.json.impexptype === "impexp" || this.json.impexptype === "imp");
//允许导出
this.exportenable = (this.exportactionidlist.length > 0) && (this.json.impexptype === "impexp" || this.json.impexptype === "exp");
if( this.isshowallsection ){
this.data = this._getallsectiondata();
}else if( this.ismergeread ){
this.data = this.getsectionmergereaddata();
}else{
this.data = this._getvalue();
if( !this._getbusinessdata() ){
this.isnew = true;
this._setvalue(this.data);
}
}
this.linelist = [];
this.sectionlinelist = [];
//this.data为object的时候才有值
// this.linemap = {};
// this.totalmodules = [];
this._loadstyles();
//获取html模板和json模板
this.gettemplate();
if( !this.form.isloaded ){ //如果表单还没加载完成
//通过表单的aftermodulesload事件设置节点外的操作:添加、删除、导入、导出
this.setouteractionevents();
}
//隐藏节点
this.node.getchildren().hide();
this.fireevent("load");
this._loaddatatemplate(function(){
// this._loadimportexportaction();
this.fieldmoduleloaded = true;
this.loading = false;
this.fireevent("postload");
}.bind(this));
},
/*
* @summary 重新加载数据表格。会触发load和postload事件。
* @example
* this.form.get("fieldid").reload(); //重新加载
*/
reload: function( isreloadtemplate ){
this.reloading = true;
// this.editmodules = [];
this.getrelativeid();
this.checkmerge( this.getvalue() );
this.clearsubmodules();
this._loadstyles();
if( isreloadtemplate ){ //重新获取html模板和json模板
this.node.getchildren().setstyle("display", "");
this.gettemplate();
this.node.getchildren().hide();
}
this.linelist = [];
this.sectionlinelist = [];
this.fireevent("load");
this._loaddatatemplate(function(){
// this._loadimportexportaction();
this.reloading = false;
this.fireevent("postload");
}.bind(this));
},
getrelativeid: function(){
this.outeraddactionidlist = (this.json.outeraddactionid || "").split(",");
this.outerdeleteactionidlist = (this.json.outerdeleteactionid || "").split(",");
this.outerselectallidlist = (this.json.outerselectallid || "").split(",");
this.addactionidlist = (this.json.addactionid || "").split(",");
this.deleteactionidlist = (this.json.deleteactionid || "").split(",");
this.sequenceidlist = (this.json.sequenceid || "").split(",");
this.selectorid = this.json.selectorid;
this.importactionidlist = (this.json.importactionid || "").split(",");
this.exportactionidlist = (this.json.exportactionid || "").split(",");
},
gettemplate: function(){
this.templatejson = {};
this.templatehtml = this.node.get("html");
var modulenodes = this.form._getmodulenodes(this.node);
modulenodes.each(function (node) {
if (node.get("mwftype") !== "form") {
var json = this.form._getdomjson(node);
this.templatejson[json.id] = json ;
}
}.bind(this));
},
_loadstyles: function(){
this.node.setstyles(this.json.styles);
this.node.set(this.json.properties);
},
_getouteractionmodules: function( idlist ){ //判断不在数据模板中,但是在表单内的id
var list = [];
idlist.each( function (id) {
var module = this._getmodulebypath(id);
var tid = id.split("..").getlast();
if( !this.templatejson.hasownproperty(tid) && module ){
list.push( module );
}
}.bind(this));
return list;
},
_setouteractionevents: function(){
this.addactionlist = this._getouteractionmodules( [].concat(this.addactionidlist, this.outeraddactionidlist) );
this.addactionlist.each( function (module) {
module.node.addevents({"click": function(e){
this._addline(e);
}.bind(this)});
if( !this.editable )module.node.hide();
}.bind(this));
this.deleteactionlist = this._getouteractionmodules( [].concat( this.outerdeleteactionidlist ) );
this.deleteactionlist.each( function (module) {
module.node.addevents({"click": function(e){
this._deleteselectedline(e);
}.bind(this)});
if( !this.editable )module.node.hide();
}.bind(this));
this.selectalllist = this._getouteractionmodules( this.outerselectallidlist );
this.selectalllist.each( function (module) {
// module.setdata(""); //默认不选中
module.node.addevents({"click": function(e){
this._checkselectall(e);
}.bind(this)});
if( !this.editable )module.node.hide();
}.bind(this));
this.selectallselector = this.selectalllist[0];
if(this.selectallselector){
this.unselectall();
}
this.importactionlist = this._getouteractionmodules( this.importactionidlist );
this.importactionlist.each( function (module) {
module.node.addevents({"click": function(e){
this.importfromexcel();
}.bind(this)});
if( !this.editable )module.node.hide();
}.bind(this));
this.exportactionlist = this._getouteractionmodules( this.exportactionidlist );
this.exportactionlist.each( function (module) {
module.node.addevents({"click": function(e){
this.exporttoexcel();
}.bind(this)})
}.bind(this));
},
setouteractionevents: function(){
this.bindevent = function () {
this._setouteractionevents();
this.fireevent("afterload");
//加载完成以后,删除事件
this.form.removeevent("aftermodulesload", this.bindevent );
}.bind(this);
//去要表单的所有组件加载完成以后再去获取外部组件
this.form.addevent("aftermodulesload", this.bindevent );
},
isshowsectionkey: function(){
return this.json.showsectionkey && this.ismergeread ;
},
isshowsectionby: function(){
return this.json.showsectionby && this.isshowallsection ;
},
issectiondata: function(){ //数据是否经过区段处理
var data = this.getbusinessdatabyid();
if( o2.typeof( data ) === "object" ){
var keys = object.keys(data);
// if( o2.typeof(data[keys[0]]) === "array" ){
// return true;
// }
for( var i=0; i 0 ? this.sectionlinelist[idx-1].node : null;
if( map[data.sectionkey] ) {
sectionline = map[data.sectionkey];
if( !operation || operation === "moveuplist"){
this._injectlinenode( sectionline.node, beforenode );
}
sectionline.setindex( data, idx, isedited, isnew, operation );
//console.log("setindex", sectionline);
}else{
var div = this._injectlinenode( new element("div"), beforenode );
sectionline = this._loadsectionline_editsection(div, data, idx, isedited, isnew );
//console.log("_loadsectionline_editsection", sectionline);
}
if( this.sectionby && this.sectionby === data.sectionkey ){
this.sectionlineedited = sectionline;
}
this.sectionlinelist.push(sectionline);
}.bind(this))
if (callback) callback();
},
_loadsectionline_editsection: function(container, data, index, isedited, isnew){
var sectionline = new mwf.xapplication.process.xform.datatemplate.sectionline(container, this, data, {
index : index,
indextext : (index 1).tostring(),
isnew: isnew,
isedited: typeof(isedited) === "boolean" ? isedited : this.editable,
isdeleteable: this.editable && this.issectionlineeditable(data),
isaddable: this.editable && this.issectionlineeditable(data)
});
// this.fireevent("beforeloadline", [line]);
sectionline.load();
// this.fireevent("afterloadline", [line]);
return sectionline;
},
issectionlineeditable: function(data){
return this.isshowallsection && this.sectionby && this.sectionby === data.sectionkey;
},
_loadsectionlinelist: function(callback, operation){
var map = this.unchangedsectionlinemap || {};
this.datawithsectionkey.each(function(data, idx){
var sectionline;
var isedited = false;
var isnew = false;
var beforenode = idx > 0 ? this.sectionlinelist[idx-1].node : null;
if( map[data.sectionkey] ) {
sectionline = map[data.sectionkey];
if( !operation || operation === "moveuplist"){
this._injectlinenode( sectionline.node, beforenode );
}
//console.log("setindex", sectionline);
sectionline.setindex( data, idx, isedited, isnew, operation );
}else {
//var div = new element("div").inject(this.node);
var div = this._injectlinenode( new element("div"), beforenode );
sectionline = this._loadsectionline(div, data, idx, isedited, isnew );
//console.log("_loadsectionline", sectionline);
}
this.sectionlinelist.push(sectionline);
}.bind(this))
if (callback) callback();
},
_loadsectionline: function(container, data, index, isedited, isnew){
var sectionline = new mwf.xapplication.process.xform.datatemplate.sectionline(container, this, data, {
index : index,
indextext : (index 1).tostring(),
isnew: isnew,
isedited: typeof(isedited) === "boolean" ? isedited : this.editable,
isdeleteable: this.editable,
isaddable: this.editable,
ismergeread: this.ismergeread
});
// this.fireevent("beforeloadline", [line]);
sectionline.load();
// this.fireevent("afterloadline", [line]);
return sectionline;
},
_loadlinelist: function(callback, operation){
var map = this.unchangedlinemap || {};
object.each(map, function (line, idx) {
line.setindex( idx.toint() );
});
this.data.each(function(data, idx){
var idxstr = idx.tostring();
var beforenode = idx > 0 ? this.linelist[idx - 1].node : null;
if( map[idxstr] ){
if( !operation || operation === "moveuplist" ){
this._injectlinenode( map[idxstr].node, beforenode );
}
//console.log("inject", map[idxstr]);
this.linelist.push( map[idxstr] );
}else{
var isnew = this.isnew || (o2.typeof(this.newlineindex) === "number" ? idx === this.newlineindex : false);
var div = this._injectlinenode( new element("div"), beforenode );
var line = this._loadline(div, data, idx, isnew);
//console.log("_loadline", line);
this.linelist.push(line);
}
}.bind(this));
this.newlineindex = null;
this.isnew = false;
if (callback) callback();
},
_loadline: function(container, data, index, isnew){
var line = new mwf.xapplication.process.xform.datatemplate.line(container, this, data, {
index : index,
indextext : (index 1).tostring(),
isedited : this.editable,
isnew : isnew,
ismergeread: this.ismergeread
});
this.fireevent("beforeloadline", [line]);
line.load();
this.fireevent("afterloadline", [line]);
return line;
},
_injectlinenode: function( linenode, beforenode ){
if( beforenode ){
linenode.inject( beforenode, "after");
}else{
linenode.inject( this.node );
}
return linenode;
},
ismax : function(){
var maxcount = this.json.maxcount ? this.json.maxcount.toint() : 0;
if( this.editable && maxcount > 0 ) {
if( this.isshowallsection ){
if( this.sectionlineedited && this.sectionlineedited.linelist.length >= maxcount )return true;
}else{
if( this.linelist.length >= maxcount )return true;
}
}
return false;
},
ismin : function(){
var mincount = this.json.mincount ? this.json.mincount.toint() : 0;
if( this.editable && mincount > 0 ) {
if( this.isshowallsection ){
if( this.sectionlineedited && this.sectionlineedited.linelist.length <= mincount )return true;
}else {
if( this.linelist.length <= mincount )return true;
}
}
return false;
},
_setlinedata: function(line, d){
if( line.sectionline ){
var data = this.getbusinessdatabyid();
var sdata = data[ line.sectionline.sectionkey ];
if( sdata ){
sdata[line.options.indexinsectionline] = d;
this.setallsectiondata( data, false, "setlinedata" );
}
}else{
var index = line.options.index;
var data = this.getinputdata();
data[index] = d;
this.setdata( data, false, "setlinedata" );
}
},
_addline: function(ev, d){
if( this.ismax() ){
var text = mwf.xapplication.process.xform.lp.maxitemcountnotice.replace("{n}",this.json.maxcount);
this.form.notice(text,"info");
return false;
}
var data, index, newline;
if( this.isshowallsection ){
data = this.getbusinessdatabyid();
var sdata = data[ this.sectionby ];
if( !sdata ){
sdata = data[ this.sectionby ] = [];
}
sdata.push(d||{});
index = sdata.length - 1;
this.newlineindex = index;
this.setallsectiondata( data , false, "addline");
newline = this.sectionlineedited.linelist[index];
}else{
data = this.getinputdata();
data.push(d || {});
index = data.length-1;
this.newlineindex = index;
this.setdata( data, false, "addline" );
newline = this.getline(index);
}
this.validationmode();
this.fireevent("addline",[{"line":newline, "ev":ev}]);
this.fireevent("change", [{"lines":[newline], "type":"addline"}]);
return newline;
},
_insertline: function(ev, beforeline){
if( this.ismax() ){
var text = mwf.xapplication.process.xform.lp.maxitemcountnotice.replace("{n}",this.json.maxcount);
this.form.notice(text,"info");
return false;
}
//使用数据驱动
var data, index, newline;
if( this.isshowallsection ){
index = beforeline.options.indexinsectionline 1;
data = this.getbusinessdatabyid();
var sdata = data[ this.sectionby ];
if( !sdata ){
sdata = data[ this.sectionby ] = [];
}
sdata.splice(index, 0, {});
this.newlineindex = index;
this.setallsectiondata( data , false, "insertline");
newline = this.sectionlineedited.linelist[index];
}else {
index = beforeline.options.index 1;
data = this.getinputdata();
data.splice(index, 0, {});
this.newlineindex = index;
this.setdata( data, false, "insertline" );
newline = this.getline( index );
}
this.validationmode();
this.fireevent("addline",[{"line":newline, "ev":ev}]);
this.fireevent("change", [{"lines":[newline], "type":"addline"}]);
return newline;
},
_insertlinebyindex: function(ev, index, d){
if( this.ismax() ){
var text = mwf.xapplication.process.xform.lp.maxitemcountnotice.replace("{n}",this.json.maxcount);
this.form.notice(text,"info");
return false;
}
//使用数据驱动
var data, newline;
if( this.isshowallsection ){
data = this.getbusinessdatabyid();
var sdata = data[ this.sectionby ];
if( !sdata ){
sdata = data[ this.sectionby ] = [];
}
if (sdata.length < index) return null;
sdata.splice(index, 0, d || {});
this.newlineindex = index;
this.setallsectiondata( data , false, "insertline");
line = this.sectionlineedited.linelist[index];
}else {
data = this.getinputdata();
if(data.length < index )return null;
data.splice(index, 0, d||{});
this.newlineindex = index;
this.setdata( data , false, "insertline");
newline = this.getline( index );
}
this.validationmode();
this.fireevent("addline",[{"line":newline, "ev":ev}]);
this.fireevent("change", [{"lines":[newline], "type":"addline"}]);
return newline;
},
_deleteselectedline: function(ev){
var selectedline = this.linelist.filter(function (line) { return line.selected; });
if( selectedline.length === 0 ){
this.form.notice( mwf.xapplication.process.xform.lp.selectitemnotice,"info");
return false;
}
var mincount = this.json.mincount ? this.json.mincount.toint() : 0;
if( mincount > 0 ){
var length;
if( this.isshowallsection && this.sectionlineedited ){
length = this.sectionlineedited.linelist.length;
}else{
length = this.linelist.length;
}
if( length - selectedline.length < mincount ){
var text = mwf.xapplication.process.xform.lp.minitemnotice.replace("{n}", mincount );
this.form.notice(text,"info");
return false;
}
}
var _self = this;
this.form.confirm("warn", ev, mwf.xapplication.process.xform.lp.deletedatagridlinetitle, mwf.xapplication.process.xform.lp.deleteselecteditemnotice, 300, 120, function(){
_self._dellines( selectedline );
this.close();
}, function(){
this.close();
}, null, null, this.form.json.confirmstyle);
},
_dellines: function(lines){
var _self = this;
var saveflag = false;
var data;
if( this.isshowallsection ){
data = this.getbusinessdatabyid();
}else{
data = _self.getinputdata();
}
lines.reverse().each(function(line){
_self.fireevent("deleteline", [line]);
if(line.deleteattachment())saveflag = true;
if( line.sectionline ){
var d = data[ line.sectionline.sectionkey ];
if( d ){
d.splice(line.options.indexinsectionline, 1);
}
}else {
data.splice(line.options.index, 1);
}
_self.fireevent("afterdeleteline");
});
if( this.isshowallsection ){
_self.setallsectiondata(data, false, "deletelines");
}else{
_self.setdata( data , false, "deletelines");
}
this.validationmode();
_self.fireevent("change", [{"lines":lines, "type":"deletelines"}]);
if(saveflag)this.form.saveformdata();
},
_deleteline: function(ev, line){
if( this.ismin() ){
var text = mwf.xapplication.process.xform.lp.minitemcountnotice.replace("{n}", this.json.mincount );
this.form.notice(text,"info");
return false;
}
var _self = this;
this.form.confirm("warn", ev, mwf.xapplication.process.xform.lp.deletedatagridlinetitle, mwf.xapplication.process.xform.lp.deletedatagridline, 300, 120, function(){
_self._delline(line);
this.close();
}, function(){
this.close();
}, null, null, this.form.json.confirmstyle);
},
_delline: function(line){
this.fireevent("deleteline", [line]);
var saveflag = line.deleteattachment();
//使用数据驱动
var data;
if( line.sectionline ){
var data = this.getbusinessdatabyid();
var d = data[ line.sectionline.sectionkey ];
if( d ){
d.splice(line.options.indexinsectionline, 1);
}
this.setallsectiondata( data, false, "deleteline" );
}else{
data = this.getinputdata();
data.splice(line.options.index, 1);
this.setdata( data , false, "deleteline");
}
this.validationmode();
this.fireevent("afterdeleteline");
this.fireevent("change", [{"lines":[line], "type":"deleteline"}]);
if(saveflag)this.form.saveformdata();
},
_checkselectall: function () {
var selectdata = this.selectallselector.getdata();
var selected;
if(o2.typeof(selectdata)==="array"){
selected = selectdata.contains(this.json.outerselectallselectedvalue);
}else{
selected = selectdata === this.json.outerselectallselectedvalue;
}
this.selected = selected;
if( this.isshowallsection && this.sectionlineedited){
this.sectionlineedited.linelist.each(function (line) {
this.selected ? line.select() : line.unselect();
}.bind(this))
}else{
this.linelist.each(function (line) {
this.selected ? line.select() : line.unselect();
}.bind(this))
}
},
selectall: function(){
this.selected = true;
if(this.selectallselector)this.selectallselector.setdata(this.json.outerselectallselectedvalue);
},
unselectall: function(){
this.selected = false;
if( this.selectallselector.getoptionsobj ){
var options = this.selectallselector.getoptionsobj();
var value = "";
var arr = options.valuelist || [];
for( var i=0; i
* 当表单上没有对应组件的时候,可以使用this.data[fieldid] = data赋值。
* @summary 为数据模板赋值。
* @param data{datatemplatedata|promise|array} 必选,数组或promise.
* @example
* this.form.get("fieldid").setdata([]); //赋空值
* @example
* //如果无法确定表单上是否有组件,需要判断
* if( this.form.get('fieldid') ){ //判断表单是否有无对应组件
* this.form.get('fieldid').setdata( data );
* }else{
* this.data['fieldid'] = data;
* }
*@example
* //使用promise
* var field = this.form.get("fieldid");
* var promise = new promise(function(resolve, reject){ //发起异步请求
* var oreq = new xmlhttprequest();
* oreq.addeventlistener("load", function(){ //绑定load事件
* resolve(oreq.responsetext);
* });
* oreq.open("get", "/data.json"); //假设数据存放在data.json
* oreq.send();
* });
* promise.then( function(){
* var data = field.getdata(); //此时由于异步请求已经执行完毕,getdata方法获得data.json的值
* })
* field.setdata( promise );
*/
setdata: function(data, firechange, operation){
if (!data){
data = this._getvalue();
}
this._setdata(data, firechange, operation);
},
_setdata: function(data, firechange, operation){
var p = o2.promiseall(this.data).then(function(v){
this.data = v;
// if (o2.typeof(data)==="object") data = [data];
this.__setdata(data, firechange, operation);
this.modulevalueag = null;
return v;
}.bind(this), function(){
this.modulevalueag = null;
}.bind(this));
this.modulevalueag = p;
if (this.modulevalueag) this.modulevalueag.then(function(){
this.modulevalueag = null;
}.bind(this), function(){
this.modulevalueag = null;
}.bind(this));
},
__setdata: function(data, firechange, operation){
if( this.isshowallsection ){
//兼容外部对编辑当前区段的setdata,内部的setdata不走这里,直接走setallsectiondata
this._seteditedsectiondata(data, firechange, operation);
return;
}else if( this.ismergeread ){
//合并且只读,不允许setdata
throw new error("the data template is merged and read-only, you can use the 'setallsectiondata' method to set all section data");
}
var old;
if(firechange)old = object.clone(this._getbusinessdata() || {});
this._setunchangedlinemap(data, operation);
this._setbusinessdata(data);
this.data = data;
if (this.data){
this.clearsubmodules();
}
this.linelist = [];
this.sectionlinelist = [];
this._loaddatatemplate(function(){
this._setsubdatatemplateouterevents();
if (firechange && json.stringify(old) !== json.stringify(data)) this.fireevent("change");
this.unchangedlinemap = null;
}.bind(this), operation);
},
_setsubdatatemplateouterevents: function(){
//告诉下层的数据模板绑定外部事件
for (var i=0; i
* 1、当使用promise的时候
* 使用异步函数生成器(promise)为组件赋值的时候,用getdata方法立即获取数据,可能返回修改前的值,当promise执行完成以后,会返回修改后的值。
* this.data[fieldid] 立即获取数据,可能获取到异步函数生成器,当promise执行完成以后,会返回修改后的值。
* {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0#eggil|具体差异请查看链接}
* 2、当表单上没有对应组件的时候,可以使用this.data[fieldid]获取值,但是this.form.get('fieldid')无法获取到组件。
* @summary 获取数据模板数据.
* @example
* var data = this.form.get('fieldid').getdata();
*@example
* //如果无法确定表单上是否有组件,需要判断
* var data;
* if( this.form.get('fieldid') ){ //判断表单是否有无对应组件
* data = this.form.get('fieldid').getdata();
* }else{
* data = this.data['fieldid']; //直接从数据中获取字段值
* }
* @example
* //使用promise
* var field = this.form.get("fieldid");
* var promise = new promise(function(resolve, reject){ //发起异步请求
* var oreq = new xmlhttprequest();
* oreq.addeventlistener("load", function(){ //绑定load事件
* resolve(oreq.responsetext);
* });
* oreq.open("get", "/data.json"); //假设数据存放在data.json
* oreq.send();
* });
* promise.then( function(){
* var data = field.getdata(); //此时由于异步请求已经执行完毕,getdata方法获得data.json的值
* })
* field.setdata( promise );
* @return {datatemplatedata}
*/
getdata: function(){
if( this.importer ){
this.importer.destroysimulatemodule();
}
if (this.editable!==false){
// var data = [];
// this.linelist.each(function(line, index){
// data.push(line.getdata())
// });
//
// this.data = data;
//
// this._setbusinessdata(this.data);
//
// return (this.data.length) ? this.data : [];
this.linelist.each(function (line) {
line.computemoduledata("save");
})
return this._getbusinessdata();
}else{
return this._getbusinessdata();
}
},
getinputdata: function(){
if( this.importer ){
this.importer.destroysimulatemodule();
}
if (this.editable!==false){
return this._getbusinessdata();
}else{
return this._getbusinessdata();
}
},
_getsectionby: function(){
if (this.json.section!=="yes"){
return "";
}else {
switch (this.json.sectionby){
case "person":
return layout.desktop.session.user.id;
case "unit":
return (this.form.businessdata.task) ? this.form.businessdata.task.unit : "";
case "activity":
return (this.form.businessdata.work) ? this.form.businessdata.work.activity : "";
case "splitvalue":
return (this.form.businessdata.work) ? this.form.businessdata.work.splitvalue : "";
case "script":
if( this.json.sectionbyscript && this.json.sectionbyscript.code){
return this.form.macro.exec(this.json.sectionbyscript.code, this) || "";
}else{
return "";
}
default:
return "";
}
}
},
createerrornode: function(text){
var node = new element("div");
var iconnode = new element("div", {
"styles": {
"width": "20px",
"height": "20px",
"float": "left",
"background": " center center no-repeat"
}
}).inject(node);
var textnode = new element("div", {
"styles": {
"line-height": "20px",
"margin-left": "20px",
"color": "red",
"word-break": "keep-all"
},
"text": text
}).inject(node);
return node;
},
notvalidationmode: function(text){
if (!this.isnotvalidationmode){
this.isnotvalidationmode = true;
this.node.store("borderstyle", this.node.getstyles("border-left", "border-right", "border-top", "border-bottom"));
this.node.setstyle("border", "1px solid red");
this.errnode = this.createerrornode(text).inject(this.node, "after");
this.shownotvalidationmode(this.node);
var parentnode = this.errnode;
while( parentnode && parentnode.offsetparent === null ){
parentnode = parentnode.getparent();
}
if (parentnode && !parentnode.isintoview()) parentnode.scrollintoview(false);
}
},
shownotvalidationmode: function(node){
var p = node.getparent("div");
if (p){
if (p.get("mwftype") == "tab$content"){
if (p.getparent("div").getstyle("display")=="none"){
var contentareanode = p.getparent("div").getparent("div");
var tabareanode = contentareanode.getprevious("div");
var idx = contentareanode.getchildren().indexof(p.getparent("div"));
var tabnode = tabareanode.getlast().getfirst().getchildren()[idx];
tabnode.click();
p = tabareanode.getparent("div");
}
}
this.shownotvalidationmode(p);
}
},
validationmode: function(){
if (this.isnotvalidationmode){
this.isnotvalidationmode = false;
this.node.setstyles(this.node.retrieve("borderstyle"));
if (this.errnode){
this.errnode.destroy();
this.errnode = null;
}
}
},
validationconfigitem: function(routename, data){
var flag = (data.status=="all") ? true: (routename == data.decision);
if (flag){
var n = this.getdata();
if( o2.typeof(n)==="object"){
var arr = [];
object.each( n, function (d, key) {
if(o2.typeof(d) === "array")arr = arr.concat(d);
});
n = arr;
}
var v = (data.valuetype=="value") ? n : n.length;
switch (data.operateor){
case "isnull":
if (!v){
this.notvalidationmode(data.prompt);
return false;
}
break;
case "notnull":
if (v){
this.notvalidationmode(data.prompt);
return false;
}
break;
case "gt":
if (v>data.value){
this.notvalidationmode(data.prompt);
return false;
}
break;
case "lt":
if (v 0 ? this.linelist[idx - 1].node : null;
if( map[idxstr] ){
if( !operation || operation === "moveuplist" ){
this._injectlinenode( map[idxstr].node, beforenode )
}
//console.log("inject", map[idxstr]);
this.linelist.push( map[idxstr] );
this.template.linelist.push(map[idxstr]);
}else{
var node = this._injectlinenode( new element("div"), beforenode );
var isedited = false, isnew = false;
if( this.options.isedited ){
var dt = this.template;
isnew = dt.isnew || (o2.typeof(dt.newlineindex) === "number" ? idx === dt.newlineindex : false);
isedited = true;
dt.isnew = false;
dt.newlineindex = null;
}
var line = this._loadline( node, d, idx, isedited, isnew );
//console.log("_loadline", line);
this.linelist.push(line);
this.template.linelist.push(line);
}
}.bind(this))
}
},
_loadline: function(container, data, index, isedited, isnew){
var line = new mwf.xapplication.process.xform.datatemplate.line(container, this.template, data, {
indexinsectionline : index,
indexinsectionlinetext : (index 1).tostring(),
index: this.template.linelist.length,
indextext : (this.template.linelist.length 1).tostring(),
isnew: isnew,
isedited: typeof(isedited) === "boolean" ? isedited : this.options.isedited,
isdeleteable: this.options.isdeleteable,
isaddable: this.options.isaddable,
ismergeread: this.options.ismergeread,
sectionkey: this.sectionkey
}, this);
this.template.fireevent("beforeloadline", [line]);
line.load();
this.template.fireevent("afterloadline", [line]);
return line;
},
_injectlinenode: function( linenode, beforenode ){
if( beforenode ){
linenode.inject( beforenode, "after");
}else{
linenode.inject( this.node );
}
return linenode;
},
loadsectionkeynode: function () {
var sectionkeystyles = this.template._parsestyles(this.template.json.sectionkeystyles);
var keynode = new element("div.mwf_sectionkey", {
styles : sectionkeystyles
}).inject( this.node );
this.keynode = keynode;
var separator;
if( this.template.isshowsectionkey() ){
separator = this.template.json.keycontentseparator;
}else{
separator = this.template.json.keycontentseparatorsectionby;
}
this.template.getsectionkeywithmerge( this.data, function (key) {
if( o2.typeof(key) === "string" ){
keynode.set("text", key (separator || ""));
}else{
promise.resolve(key).then(function (k) {
keynode.set("text", k (separator || ""));
}.bind(this))
}
}.bind(this));
},
_setunchangedlinemap: function(data, operation){
var fromoutside = !operation;
var dt = this.template;
var editalbe;
if( dt.isshowallsection ){
editalbe = dt.isshowallsection && dt.sectionby && dt.sectionby === this.sectionkey;
}
var linedatalist = this.linelist.map(function (line) {
if( dt.isshowallsection ){
if( line.options.isedited !== (dt.editable && editalbe) )return "";
if( line.options.isdeleteable !== (dt.editable && editalbe) )return "";
if( line.options.isaddable !== (dt.editable && editalbe) )return "";
}else{
if( line.options.isedited !== dt.editable )return "";
if( line.options.isdeleteable !== dt.editable )return "";
if( line.options.isaddable !== dt.editable )return "";
}
return fromoutside ? json.stringify(line.data) : line.data;
}.bind(this));
var dstr, map = {};
data.each(function (d, idx) {
if(fromoutside)dstr = json.stringify(d);
for (var i = 0; i < this.linelist.length; i ) {
var isequal = fromoutside ? (dstr === linedatalist[i]) : (d === linedatalist[i] );
if ( isequal ) {
map[idx] = this.linelist[i];
linedatalist[i] = "";
break;
}
}
}.bind(this));
this.isunchangedall = data.length === this.linelist.length;
if( this.isunchangedall ){
for( var i=0; i 0;
if( !hasunchangedline ){
if( this.keynode ){
this.keynode.destroy();
this.keynode = null;
}
}
var lines = [];
object.values(map).each(function (d) {
lines = lines.concat(d);
});
for (var i=0; i maxlength) ? sectionkey.substr(sectionkey.length-maxlength, maxlength) : sectionkey;
sectionid = "." sectionid;
}else{
maxlength = 64 - basesite.length;
}
var templateid = this.template.json.id;
templateid = (templateid.length > maxlength) ? templateid.substr(templateid.length-maxlength, maxlength) : templateid;
return templateid sectionid basesite;
},
setevents: function (module, id) {
if( this.template.addactionidlist.contains( id )){
this.addactionlist.push( module );
module.node.addevent("click", function (ev) {
this.template._insertline( ev, this )
}.bind(this))
if( !this.template.editable )module.node.hide();
if( !this.options.isaddable )module.node.hide();
}
if( this.template.deleteactionidlist.contains(id)){
this.deleteactionlist.push( module );
module.node.addevent("click", function (ev) {
this.template._deleteline( ev, this )
}.bind(this))
if( !this.template.editable )module.node.hide();
if( !this.options.isdeleteable )module.node.hide();
}
if( this.template.selectorid === id){
this.selector = module;
// module.setdata(""); //默认不选择
module.node.addevent("click", function (ev) {
this.checkselect();
}.bind(this))
if( !this.template.editable )module.node.hide();
if( !this.options.isdeleteable )module.node.hide();
this.unselect();
}
//???
// if( this.template.importactionidlist.contains(id))this.importactionlist.push( module );
// if( this.template.exportactionidlist.contains(id))this.exportactionlist.push( module );
},
checksequence: function(module, id){
if( this.template.sequenceidlist.contains(id)){
this.sequencenodelist.push( module );
var indextext;
if(
( this.template.isshowsectionkey() && this.template.json.sequencebysection === "section" ) ||
( this.template.isshowsectionby() && this.template.json.sequenceby === "section" )
){
indextext = this.options.indexinsectionlinetext;
}else{
indextext = this.options.indextext;
}
if(this.form.getmoduletype(module) === "label"){
if (module.json.valuetype === "script" && module.json.script && module.json.script.code){
var value = module.form.macro.exec(module.json.script.code, module);
module._setnodetext(value);
}else{
module.node.set("text", indextext );
}
}else if(module.setdata){
module.setdata( indextext );
}
}
},
checkselect: function () {
var selectdata = this.selector.getdata();
var selected;
if(o2.typeof(selectdata)==="array"){
selected = selectdata.contains(this.template.json.selectorselectedvalue);
}else{
selected = selectdata === this.template.json.selectorselectedvalue;
}
this.selected = selected;
},
select: function(){
this.selected = true;
if(this.selector)this.selector.setdata(this.template.json.selectorselectedvalue);
},
unselect: function(){
this.selected = false;
if( this.selector.getoptionsobj ){
var options = this.selector.getoptionsobj();
var value = "";
var arr = options.valuelist || [];
for( var i=0; i -1 ? options.textlist[ idx ] : "") ;
// });
// text = arr.join(", ");
// break;
// case "radio":
// case "select":
// var options = module.getoptionsobj();
// var idx = options.textlist.indexof( value );
// text = idx > -1 ? options.valuelist[ idx ] : "";
// break;
// case "textarea":
// text = value;
// break;
// case "calendar":
// text = value;
// break;
// default:
// text = value;
// break;
// }
// } else if ( json.type === "label" && module.node) {
// text = module.node.get("text");
// }
//
// if( !text && o2.typeof(text) !== "number" ){
// text = "";
// }
//
// exportdata.push( text );
// }
// }.bind(this));
// return exportdata;
// },
isavaliablefield : function(json){
if (["image","button","imageclipper","writingboard","attachment","attachmentdg","label",
"elbutton","elbutton","elcarousel","eldropdown","elicon","eltree"].contains( json.type) )return false; //图片,附件,label不导入导出
return true;
},
getexcelname: function(){
var title;
if( this.template.json.excelname && this.template.json.excelname.code ){
title = this.form.macro.exec(this.template.json.excelname.code, this);
}else{
title = mwf.xapplication.process.xform.lp.datatemplateexportdefaultname;
}
var titlea = title.split(".");
if( ["xls","xlst"].contains( titlea[titlea.length-1].tolowercase() ) ){
titlea.splice( titlea.length-1 );
}
title = titlea.join(".");
return title;
},
getcolwidtharray : function(){
var colwidtharr = [];
this.template.json.excelfieldconfig.each(function (config) {
var json = this.form.json.modulelist[config.field];
if ( !json ){
colwidtharr.push(150);
}else if ( ["org","reader","author","personfield","orgfield"].contains(json.type)) {
colwidtharr.push(340);
} else if (json.type === "address") {
colwidtharr.push(170);
} else if (json.type === "textarea") {
colwidtharr.push(260);
} else if (json.type === "htmleditor") {
colwidtharr.push(500);
} else if (json.type === "tinymceeditor") {
colwidtharr.push(500);
} else if (json.type === "calendar") {
colwidtharr.push(150);
} else {
colwidtharr.push(150);
}
}.bind(this));
return colwidtharr;
},
getdateindexarray : function(){
var dateindexarr = []; //日期格式列下标
this.template.json.excelfieldconfig.each(function (config, i) {
var json = this.form.json.modulelist[config.field];
if (json && json.type === "calendar") {
dateindexarr.push(i);
}
}.bind(this));
return dateindexarr;
},
exportwithimportdatatoexcel : function ( columnlist, importeddata ) {
mwf.require("mwf.widget.mask", null, false);
this.mask = new mwf.widget.mask({ "style": "desktop", "zindex": 50000 });
// 适配移动端
if (layout.mobile) {
this.mask.load();
} else {
this.mask.loadnode(this.form.app.content);
}
this.template.fireevent("beforeexport");
var resultarr = [];
var titlearr = this.template.json.excelfieldconfig.map(function(config){
return config.title;
});
titlearr.push( mwf.xapplication.process.xform.lp.validationinfor );
resultarr.push( titlearr );
// this.template.linelist.each(function (line, index) {
// resultarr.push( this.getlineexportdata(line, index) );
// }.bind(this));
importeddata.each( function( linedata, lineindex ){
var array = [];
columnlist.each( function (obj, i) {
array.push( ( linedata[ obj.text ] || '' ).replace(/
/g, "\n") );
});
array.push( linedata.errortextlistexcel ? linedata.errortextlistexcel.join("\n") : "" );
resultarr.push( array );
}.bind(this));
var colwidtharr = this.getcolwidtharray();
colwidtharr.push(300); //提示信息
var excelname = this.getexcelname();
var arg = {
data : resultarr,
colwidtharray : colwidtharr,
title : excelname,
witherror: true
};
this.template.fireevent("export", [arg]);
new mwf.xapplication.process.xform.datatemplate.excelutils( this.template ).exporttoexcel(
arg.data || resultarr,
arg.title || excelname,
arg.colwidtharray || colwidtharr,
this.getdateindexarray(), //日期格式列下标
null,
function () {
if (this.mask) {
this.mask.hide();
this.mask = null;
}
}.bind(this)
);
}
});
mwf.xapplication.process.xform.datatemplate.importer = new class({
implements: [options, events],
options: {
},
initialize: function (template, options) {
this.setoptions(options);
this.template = template;
this.form = this.template.form;
this.lp = mwf.xapplication.process.xform.lp;
this.columntext = this.lp.importvalidationcolumntext;
this.columntextexcel = this.lp.importvalidationcolumntextexcel;
this.excelutil = new mwf.xapplication.process.xform.datatemplate.excelutils( this.template );
},
isavaliablefield : function(json, module, type){
if (["image","button","imageclipper","writingboard","attachment","attachmentdg","label",
"elbutton","elcarousel","eldropdown","elicon","eltree"].contains( json.type) )return false; //图片,附件,label不导入导出
return true;
},
importfromexcel : function () {
this.template.fireevent("beforeimport");
this.getfieldarray();
var datecolarray = this.getdateindexarray(); //日期列
var orgtitlearray = this.getorgtitlearray();
this.excelutil.upload( datecolarray, function (data) {
mwf.require("mwf.widget.mask", null, false);
this.mask = new mwf.widget.mask({ "style": "desktop", "zindex": 50000 });
// 适配移动端
if (layout.mobile) {
this.mask.load();
} else {
this.mask.loadnode(this.form.app.content);
}
this.importeddata = data;
if( !this.checkcount() )return;
this.loadsimulatemodule();
this.fieldarray.each(function (c) {
c.module = this.importerline.getmodule(c.field)
}.bind(this));
if( orgtitlearray.length > 0 ){
this.listallorgdata( orgtitlearray, function () {
this.checkandimport();
}.bind(this));
}else{
this.checkandimport();
}
}.bind(this));
},
checkandimport: function(){
this.checkdata( function (flag) {
if( !flag ){
this.openerrordlg();
}else{
this.importdata()
}
this.destroysimulatemodule();
}.bind(this));
},
destroysimulatemodule: function(){
if( !this.importerdatatemplate ){
this.form.disallowsaving = false;
return;
}
this.importerdatatemplate.destroy();
this.importerdatatemplate = null;
this.form.disallowsaving = false;
},
loadsimulatemodule: function(){
this.form.disallowsaving = true;
this.importerdatatemplate = new mwf.xapplication.process.xform.datatemplate.importerdatatemplate( this.template );
this.importerdatatemplate.load();
this.importerline = this.importerdatatemplate.addline({});
},
getfieldarray: function(){
//this.loadsimulatemodule();
this.fieldarray = []; //日期格式列下标
this.template.json.excelfieldconfig.each(function (config, i) {
this.fieldarray.push({
"text": config.title,
"field": config.field,
"index": i,
// "module": this.simelatemodulemap[config.field],
"json": this.form.json.modulelist[config.field]
})
}.bind(this));
},
getdateindexarray : function(){
var dateindexarr = []; //日期格式列下标
this.template.json.excelfieldconfig.each(function (config, i) {
var json = this.form.json.modulelist[config.field];
if (json && json.type === "calendar") {
dateindexarr.push(i);
}
}.bind(this));
return dateindexarr;
},
getorgtitlearray : function(){
var orgtitlearr = []; //日期格式列下标
this.template.json.excelfieldconfig.each(function (config, i) {
var json = this.form.json.modulelist[config.field];
if (json && ["org","reader","author","personfield","orgfield"].contains(json.type) ) {
orgtitlearr.push(config.title);
}
}.bind(this));
return orgtitlearr;
},
parseimporteddata: function(){
var data = [];
this.importeddata.each( function( ilinedata ){
var linedata = {};
this.fieldarray.each( function (obj, i) {
var index = obj.index;
var module = obj.module;
var json = obj.json;
var text = obj.text;
var d = ilinedata[text] || "";
var value;
if( d === "" || d === undefined || d === null ){
value = "";
}else{
switch (json.type) {
case "org":
case "reader":
case "author":
case "personfield":
case "orgfield":
var arr = this.stringtoarray(d); //空格,空格
if( arr.length === 0 ){
value = this.getorgdata( d );
}else{
value = [];
arr.each( function(d, idx){
var obj = this.getorgdata( d );
value.push( obj );
}.bind(this));
}
break;
default:
value = d; //d.replace(/
/g,""); //换行符
break;
}
}
linedata[ json.id ] = value;
}.bind(this));
data.push( linedata );
}.bind(this));
return data;
},
stringtoarray: function(string){
return string.replace(/
/g,",").split(/\s*,\s*/g ).filter(function(s){
return !!s;
});
},
importdata: function(){
var data = this.parseddata;
this.template.fireevent("import", [data] );
this.template.setdata( data );
this.template.fireevent("afterimport", [data] );
this.template.fireevent("change", [{lines: this.template.linelist, type : "import"}]);
if (this.mask) {
this.mask.hide();
this.mask = null;
}
this.form.notice( mwf.xapplication.process.xform.lp.importsuccess );
},
openerrordlg : function(){
var edata = this.importeddata;
var _self = this;
var objecttostring = function (obj, type) {
if(!obj)return "";
var arr = [];
object.each(obj, function (value, key) {
if( type === "style" ){
arr.push( key ":" value ";" )
}else{
arr.push( key "='" value "'" )
}
});
return arr.join(" ")
}
var htmlarray = [""];
var titlestyle = objecttostring(this.template.json.impexptabletitlestyles, "style");
htmlarray.push("");
this.fieldarray.each(function (obj, i) {
htmlarray.push( "" );
});
htmlarray.push("");
htmlarray.push("" );
var contentstyles = object.clone( this.template.json.impexptablecontentstyles );
if( !contentstyles[ "border-bottom" ] && !contentstyles[ "border" ] )contentstyles[ "border-bottom" ] = "1px solid #eee";
var contentstyle = objecttostring( object.merge( contentstyles, {"text-align":"left"}) , "style" );
edata.each( function( linedata, lineindex ){
htmlarray.push( "" );
this.fieldarray.each( function (obj, i) {
htmlarray.push( "" ); //换行符
});
htmlarray.push( "" );
htmlarray.push( "" );
}.bind(this));
htmlarray.push( "" obj.text " | " mwf.xapplication.process.xform.lp.validationinfor " |
---|
" ( linedata[ obj.text ] || '' ).replace(/
/g," ") " | " ( linedata.errortextlist ? linedata.errortextlist.join(" ") : "" ) " |
" );
var width = this.template.json.impexpdlgwidth || 1000;
var height = this.template.json.impexpdlgheight || 700;
width = width.toint();
height = height.toint();
var div = new element("div", { style : "padding:10px;", html : htmlarray.join("") });
var dlg = o2.dl.open({
"style" : this.form.json.dialogstyle || "user",
"title": mwf.xapplication.process.xform.lp.importfail,
"content": div,
"offset": {"y": 0},
"ismax": true,
"width": width,
"height": height,
"buttonlist": [
{
"type": "exportwitherror",
"text": mwf.xapplication.process.xform.lp.datagridexport,
"action": function () { _self.exportwithimportdatatoexcel(edata); }
},
{
"type": "cancel",
"text": mwf.lp.process.button.cancel,
"action": function () { dlg.close(); }
}
],
"onpostclose": function(){
dlg = null;
}.bind(this)
});
if (this.mask) {
this.mask.hide();
this.mask = null;
}
},
checkcount: function(){
var idata = this.importeddata;
var lp = mwf.xapplication.process.xform.lp;
var exceeded = false;
var maxcount = this.template.json.maxcount ? this.template.json.maxcount.toint() : 0;
if( maxcount > 0 && idata.length > maxcount )exceeded = true;
var less = false;
var mincount = this.template.json.mincount ? this.template.json.mincount.toint() : 0;
if( mincount > 0 && idata.length < mincount) less = true;
if( exceeded ) {
var text = lp.importtoomanynotice.replace("{n1}", idata.length).replace("{n2}", this.template.json.maxcount);
this.form.notice(text, "error");
return false;
}
if( less ){
var text = lp.importtoofewnotice.replace("{n1}", idata.length).replace("{n2}", this.template.json.mincount );
this.form.notice(text,"error");
return false;
}
return true;
},
checkdata : function( callback){
this.parseddata = this.parseimporteddata();
this.isimportsuccess = true;
this.checklinedata(0, function () {
var arg = {
validted : this.isimportsuccess,
data : this.importeddata
};
this.template.fireevent( "validimport", [arg] );
callback( arg.validted )
}.bind(this));
},
checklinedata: function(lineindex, callback){
if( lineindex < this.importeddata.length ){
this._checklinedata(this.importeddata[lineindex], lineindex, function (flag) {
lineindex ;
if( !flag )this.isimportsuccess = false;
this.checklinedata(lineindex, callback);
}.bind(this));
}else{
if(callback)callback();
}
},
_checklinedata: function(linedata, lineindex, callback){
linedata.errortextlist = linedata.errortextlist || [];
linedata.errortextlistexcel = linedata.errortextlistexcel || [];
var parsedlinedata = (this.parseddata && this.parseddata[lineindex]) ? this.parseddata[lineindex] : [];
this.checkmoduledata(0, linedata, parsedlinedata, function () {
var flag = !linedata.errortextlist.length;
callback(flag);
});
},
checkmoduledata: function(index, linedata, parsedlinedata, callback){
if( index < this.fieldarray.length ){
var result = this._checkmoduledata(this.fieldarray[index], linedata, parsedlinedata);
promise.resolve(result).then(function (flag) {
index ;
this.checkmoduledata(index, linedata, parsedlinedata, callback);
}.bind(this))
}else{
if(callback)callback();
}
},
_checkmoduledata: function (field, linedata, parsedlinedata) {
var index = field.index;
var json = field.json;
var module = field.module;
var text = field.text;
var colinfor = this.columntext.replace( "{n}", index 1 );
var colinforexcel = this.columntextexcel.replace( "{n}", this.excelutil.index2colname( index ) );
var d = linedata[text] || "";
var parsedd = parsedlinedata[json.id] || "";
var lp = this.lp;
var flag = true;
if(d){
switch (json && json.type) {
case "org":
case "reader":
case "author":
case "personfield":
case "orgfield":
var arr = this.stringtoarray(d);
arr.each( function(d, idx){
var obj = this.getorgdata( d );
if( obj.errortext ){
linedata.errortextlist.push( colinfor obj.errortext lp.fullstop );
linedata.errortextlistexcel.push( colinforexcel obj.errortext lp.fullstop );
flag = false;
}
}.bind(this));
break;
case "number":
case "currency":
case "elnumber":
if (isnan(d)){
linedata.errortextlist.push( colinfor d lp.notvalidnumber lp.fullstop );
linedata.errortextlistexcel.push( colinforexcel d lp.notvalidnumber lp.fullstop );
flag = false;
}
break;
case "calendar":
case "eldate":
case "eldatetime":
if( !( isnan(d) && !isnan(date.parse(d) ))){
linedata.errortextlist.push(colinfor d lp.notvaliddate lp.fullstop );
linedata.errortextlistexcel.push( colinforexcel d lp.notvaliddate lp.fullstop );
flag = false;
}
break;
default:
break;
}
}
if (module && module.setdata && json.type !== "address"){
var haserror = false;
if(["org","reader","author","personfield","orgfield"].contains(json.type)){
if(o2.typeof(parsedd)==="array" && parsedd.length){
haserror = parsedd.some(function (item) { return item.errortext; });
flag = false;
}
}
if(!haserror){
module.setexceldata(parsedd);
var ps = [];
if( module.moduleexcelag )ps.push( module.moduleexcelag );
if( module.modulevalueag && !ps.contains(module.modulevalueag) )ps.push( module.modulevalueag );
if( module.moduleselectag && !ps.contains(module.moduleselectag) )ps.push( module.moduleselectag );
return promise.all( ps ).then(function () {
var result = module.validationexcel();
if ( result && result.length ){
linedata.errortextlist.push(colinfor result.join("\n") );
linedata.errortextlistexcel.push( colinforexcel result.join("\n"));
flag = false;
}
parsedlinedata[json.id] = module.getdata();
return flag;
})
}
}
return flag
},
exportwithimportdatatoexcel: function(edata){
var exporter = new mwf.xapplication.process.xform.datatemplate.exporter(this.template);
exporter.exportwithimportdatatoexcel(this.fieldarray, edata)
},
getorgdata : function( str ){
str = str.trim();
var flag = str.substr(str.length-2, 2);
switch (flag.tolowercase()){
case "@i":
return this.identitymap[str] || {"errortext": str mwf.xapplication.process.xform.lp.notexistinsystem };
case "@p":
return this.personmap[str] || {"errortext": str mwf.xapplication.process.xform.lp.notexistinsystem };
case "@u":
return this.unitmap[str] || {"errortext": str mwf.xapplication.process.xform.lp.notexistinsystem };
case "@g":
return this.groupmap[str] || {"errortext": str mwf.xapplication.process.xform.lp.notexistinsystem };
default:
return this.identitymap[str] ||
this.personmap[str] ||
this.unitmap[str] ||
this.groupmap[str] ||
{"errortext": str mwf.xapplication.process.xform.lp.notexistinsystem };
}
},
listallorgdata : function (orgtitlelist, callback) {
var identitylist = [], personlist = [], unitlist = [], grouplist = [];
if( orgtitlelist.length > 0 ){
this.importeddata.each( function( linedata, lineindex ){
// if( lineindex === 0 )return;
orgtitlelist.each( function (title, index) {
if( !linedata[title] )return;
var arr = this.stringtoarray(linedata[title]);
arr.each( function( a ){
a = a.trim();
var flag = a.substr(a.length-2, 2);
switch (flag.tolowercase()){
case "@i":
identitylist.push( a ); break;
case "@p":
personlist.push( a ); break;
case "@u":
unitlist.push( a ); break;
case "@g":
grouplist.push( a ); break;
default:
identitylist.push( a );
personlist.push( a );
unitlist.push( a );
grouplist.push( a );
break;
}
})
}.bind(this))
}.bind(this));
var identityloaded, personloaded, unitloaded, grouploaded;
var check = function () {
if( identityloaded && personloaded && unitloaded && grouploaded ){
if(callback)callback();
}
};
this.identitymap = {};
if( identitylist.length ){
identitylist = identitylist.unique();
o2.actions.load("x_organization_assemble_express").identityaction.listobject({ identitylist : identitylist }, function (json) {
json.data.each( function (d) { this.identitymap[ d.matchkey ] = d; }.bind(this));
identityloaded = true;
check();
}.bind(this))
}else{
identityloaded = true;
check();
}
this.personmap = {};
if( personlist.length ){
personlist = personlist.unique();
o2.actions.load("x_organization_assemble_express").personaction.listobject({ personlist : personlist }, function (json) {
json.data.each( function (d) { this.personmap[ d.matchkey ] = d; }.bind(this));
personloaded = true;
check();
}.bind(this))
}else{
personloaded = true;
check();
}
this.unitmap = {};
if( unitlist.length ){
unitlist = unitlist.unique();
o2.actions.load("x_organization_assemble_express").unitaction.listobject({ unitlist : unitlist }, function (json) {
json.data.each( function (d) { this.unitmap[ d.matchkey ] = d; }.bind(this));
unitloaded = true;
check();
}.bind(this))
}else{
unitloaded = true;
check();
}
this.groupmap = {};
if( grouplist.length ){
grouplist = grouplist.unique();
o2.actions.load("x_organization_assemble_express").groupaction.listobject({ grouplist : grouplist }, function (json) {
json.data.each( function (d) { this.groupmap[ d.matchkey ] = d; }.bind(this));
grouploaded = true;
check();
}.bind(this))
}else{
grouploaded = true;
check();
}
}
}
});
mwf.xdesktop.requireapp("template", "utils.excelutils", null, false);
mwf.xapplication.process.xform.datatemplate.excelutils = new class({
extends: mwf.xapplication.template.utils.excelutils
});