Datatemplate.js 83.1 KB
Newer Older
U
unknown 已提交
1 2
/**
 * 数据模板数据结构.
U
unknown 已提交
3
 * @typedef {Array} DatatemplateData
U
unknown 已提交
4
 * @example
U
unknown 已提交
5
 [ //数据模板数据条目
U
unknown 已提交
6
 {
U
unknown 已提交
7
           "org": [{
U
unknown 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
                "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"
                }
            ]
        },
U
unknown 已提交
47
 ...
U
unknown 已提交
48
 ]
U
unknown 已提交
49 50
 */
MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
U
unknown 已提交
51
/** @class Datatemplate 数据模板组件。自定义结构和样式的多行数据编辑组件。
52
 * @o2cn 数据模板
U
unknown 已提交
53 54 55
 * @example
 * //可以在脚本中获取该组件
 * //方法1:
U
unknown 已提交
56
 * var datatemplate = this.form.get("name"); //获取组件
U
unknown 已提交
57
 * //方法2
U
unknown 已提交
58
 * var datatemplate = this.target; //在组件事件脚本中获取
U
unknown 已提交
59 60
 * @extends MWF.xApplication.process.Xform.$Module
 * @o2category FormComponents
U
unknown 已提交
61
 * @since v6.2
U
unknown 已提交
62
 * @o2range {Process|CMS|Protal}
U
unknown 已提交
63 64
 * @hideconstructor
 */
U
unknown 已提交
65
MWF.xApplication.process.Xform.Datatemplate = MWF.APPDatatemplate = new Class(
U
unknown 已提交
66 67 68 69 70 71
	/** @lends MWF.xApplication.process.Xform.Datatemplate# */
	{
		Implements: [Events],
		Extends: MWF.APP$Module,
		isEdit: false,
		options: {
U
unknown 已提交
72 73 74 75 76
			/**
			 * 所有内容加载后执行(包括异步加载)。
			 * @event MWF.xApplication.process.Xform.Datatemplate#afterLoad
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
U
unknown 已提交
77
			/**
U
unknown 已提交
78
			 * 每初始化一个条目,但未加载的时候触发,通过this.event可以获取条目对象。
U
unknown 已提交
79 80 81 82
			 * @event MWF.xApplication.process.Xform.Datatemplate#beforeLoadLine
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
U
unknown 已提交
83
			 * 每一个条目加载后时候触发,通过this.event可以获取条目对象。
U
unknown 已提交
84 85 86
			 * @event MWF.xApplication.process.Xform.Datatemplate#afterLoadLine
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
U
unknown 已提交
87 88 89 90 91 92
			/**
			 * 数据模板改变时触发。通过this.event.lines可以获取修改的条目数组,this.event.type可以获得修改的类型。<br/>
			 * <table>
			 *     <tr><th><b>this.event.type</b></th><th><b>触发类型</b></th><th><b>this.event.lines</b></th></tr>
			 *     <tr><td>addline</td><td>添加一行</td><td>添加的行数组</td></tr>
			 *     <tr><td>deleteline</td><td>删除一行</td><td>删除的行数组</td></tr>
U
unknown 已提交
93
			 *     <tr><td>deletelines</td><td>删除多行</td><td>删除的行数组</td></tr>
U
unknown 已提交
94 95 96 97 98 99
			 *     <tr><td>editmodule</td><td>字段值改变时</td><td>this.event.lines为编辑的行数组<br/>this.event.module为修改的字段</td></tr>
			 *     <tr><td>import</td><td>导入数据后</td><td>数据模板所有行</td></tr>
			 * </table>
			 * @event MWF.xApplication.process.Xform.Datatemplate#change
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
U
unknown 已提交
100
			/**
U
unknown 已提交
101
			 * 添加条目时触发。通过this.event.line可以获取对应的条目对象,this.event.ev可以获得事件触发的Event。
U
unknown 已提交
102
			 * @event MWF.xApplication.process.Xform.Datatemplate#addLine
U
unknown 已提交
103 104 105
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
U
unknown 已提交
106
			 * 删除条目前触发。通过this.event可以获取对应的条目对象。
U
unknown 已提交
107
			 * @event MWF.xApplication.process.Xform.Datatemplate#deleteLine
U
unknown 已提交
108 109 110 111
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
			 * 删除条目后触发。
U
unknown 已提交
112
			 * @event MWF.xApplication.process.Xform.Datatemplate#afterDeleteLine
U
unknown 已提交
113 114 115 116
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
			 * 导出excel的时候触发,this.event指向导出的数据,您可以通过修改this.event来修改数据。
U
unknown 已提交
117
			 * @event MWF.xApplication.process.Xform.Datatemplate#export
U
unknown 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 * @example
			 * <caption>this.event数据格式如下:</caption>
			 * {
			 *  	data : [
			 *   		["姓名","性别","学历","专业","出生日期","毕业日期"], //标题
			 *  		[ "张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
			 *  		[ "李四","男","大学专科","数学","1998-1-2","2018-9-2" ]  //第二行数据
			 * 	], //导出的数据
			 *     colWidthArray : [100, 50, 100, 200, 150, 150], //每列宽度
			 *     title : "xxxx" //导出的excel文件标题
			 * }
			 */
			/**
			 * 在导入excel,进行数据校验后触发,this.event指向导入的数据。
U
unknown 已提交
133
			 * @event MWF.xApplication.process.Xform.Datatemplate#validImport
U
unknown 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 * @example
			 * <caption>this.event数据格式如下:</caption>
			 * {
			 *  	data : [
			 *  	   {
			 *  	 	"姓名" : "张三",
			 *  	 	"性别" : "男",
			 *  	 	"学历" : "大学本科",
			 *  	    "专业" : "计算机",
			 *  	    "出生日期" : "aa01-1-2",
			 *  	 	"毕业日期" : "2019-9-2",
			 *  	 	"errorTextList" : [
			 *  	 	    "第5列:aa01-1-2不是正确的日期格式。"
U
unknown 已提交
148
			 *  	 	] //校验出的错误信息,如果该行数据正确,则无该字段
U
unknown 已提交
149 150 151 152 153 154 155
			 *  	 }
			 *  	 ...
			 *     ], //导入的数据
			 *     "validted" : true  //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
			 * }
			 */
			/**
U
unknown 已提交
156
			 * 在导入excel,数据校验成功将要设置回数据模板的时候触发,this.event指向整理过的导入数据,格式见{@link DatatemplateData}。
U
unknown 已提交
157
			 * @event MWF.xApplication.process.Xform.Datatemplate#import
U
unknown 已提交
158 159
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
160 161 162 163 164
			/**
			 * 在导入excel,数据设置回数据模板以后触发,this.event指向整理过的导入数据,格式见{@link DatatemplateData}。
			 * @event MWF.xApplication.process.Xform.Datatemplate#afterImport
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
U
unknown 已提交
165
			"moduleEvents": ["queryLoad","postLoad","load", "afterLoad",
U
unknown 已提交
166
				"beforeLoadLine", "afterLoadLine", "change", "addLine", "deleteLine", "afterDeleteLine","export", "import", "validImport", "afterImport"]
U
unknown 已提交
167 168 169 170 171 172 173 174
		},

		initialize: function(node, json, form, options){
			this.node = $(node);
			this.node.store("module", this);
			this.json = json;
			this.form = form;
			this.field = true;
175
			this.fieldModuleLoaded = false;
U
unknown 已提交
176
		},
177
		load: function(){
U
unknown 已提交
178 179 180
				this._loadModuleEvents();
				if (this.fireEvent("queryLoad")){
					this._queryLoaded();
U
unknown 已提交
181 182 183 184 185 186
					if( this.isSectionMergeEdit() ){ //区段合并,删除区段值合并数据后编辑
						if( this.json.mergeTypeEdit === "script" ){
                        this._loadMergeEditNodeByScript();
                    }else{
                        this._loadMergeEditNodeByDefault();
                    }
U
unknown 已提交
187 188 189 190 191 192 193 194 195 196 197 198
					}else{
						this._loadUserInterface();
					}
					this._loadStyles();
					this._loadDomEvents();
					//this._loadEvents();

					this._afterLoaded();
					this.fireEvent("afterLoad");
					// this.fireEvent("load");
				}
		},
U
unknown 已提交
199 200 201 202 203
		_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();
204
			}
U
unknown 已提交
205
		},
U
unknown 已提交
206
		_loadMergeEditNodeByDefault: function(){
U
unknown 已提交
207 208 209
			var data = this.getSortedSectionData();
			var businessData = [];
			data.each(function(d){
U
unknown 已提交
210 211
				d.data = d.data || [];
				businessData = businessData.concat( d.data );
U
unknown 已提交
212
			});
U
unknown 已提交
213
			this._setBusinessData(businessData);
U
unknown 已提交
214
			this._loadUserInterface();
215
		},
U
unknown 已提交
216
		_loadUserInterface: function(){
217
			// this.fireEvent("queryLoad");
U
unknown 已提交
218
			debugger;
219
			this.loading = true;
U
unknown 已提交
220

U
unknown 已提交
221 222 223 224 225 226 227
			//区段合并展现
			this.isMergeRead = this.isSectionMergeRead();

			//启用区段且显示所有区段
			this.sectionBy = this._getSectionBy();
			this.isShowAllSection = this.isAllSectionShow();

228
			var iconNode = this.node.getElement("div[o2icon='datatemplate']");
229 230
			if(iconNode)iconNode.destroy();

U
unknown 已提交
231 232 233 234
			this.editModules = [];
			this.node.setStyle("overflow-x", "auto");
			this.node.setStyle("overflow-y", "hidden");

U
unknown 已提交
235
			this.editable = !(this.readonly || (this.json.isReadonly === true) || (this.form.json.isReadonly === true));
U
unknown 已提交
236
			if( this.isMergeRead )this.editable = false;
U
unknown 已提交
237 238 239 240
			if (this.editable && this.json.editableScript && this.json.editableScript.code){
				this.editable = this.form.Macro.exec(((this.json.editableScript) ? this.json.editableScript.code : ""), this);
			}

U
unknown 已提交
241
			this.getRelativeId();
U
unknown 已提交
242

U
unknown 已提交
243 244 245 246 247 248 249
			//允许导入
			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");

U
unknown 已提交
250
			debugger;
U
unknown 已提交
251 252 253 254 255 256 257 258 259 260
			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);
				}
U
unknown 已提交
261
			}
U
unknown 已提交
262 263

			this.lineList = [];
U
unknown 已提交
264
			this.sectionlineList = [];
U
unknown 已提交
265

U
unknown 已提交
266 267 268
			//this.data为object的时候才有值
			// this.lineMap = {};

U
unknown 已提交
269 270 271 272
			// this.totalModules = [];
			this._loadStyles();

			//获取html模板和json模板
U
unknown 已提交
273
			this.getTemplate();
U
unknown 已提交
274

U
unknown 已提交
275 276 277 278 279

			if( !this.form.isLoaded ){ //如果表单还没加载完成
				//通过表单的afterModulesLoad事件设置节点外的操作:添加、删除、导入、导出
				this.setOuterActionEvents();
			}
U
unknown 已提交
280

U
unknown 已提交
281 282 283
			//隐藏节点
			this.node.getChildren().hide();

U
unknown 已提交
284
			this.fireEvent("load");
U
unknown 已提交
285 286
			this._loadDataTemplate(function(){
				// this._loadImportExportAction();
287
				this.fieldModuleLoaded = true;
288
				this.loading = false;
U
unknown 已提交
289 290
				this.fireEvent("postLoad");
			}.bind(this));
U
unknown 已提交
291
		},
U
unknown 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304
		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(",");
		},
U
unknown 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317
		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);
U
unknown 已提交
318
			this.node.set(this.json.properties);
U
unknown 已提交
319
		},
U
unknown 已提交
320 321 322
		_getOuterActionModules: function( idList ){ //判断不在数据模板中,但是在表单内的Id
			var list = [];
			idList.each( function (id) {
U
unknown 已提交
323
				var module = this._getModuleByPath(id);
U
unknown 已提交
324
				var tId = id.split("..").getLast();
U
unknown 已提交
325 326
				if( !this.templateJson.hasOwnProperty(tId) && module ){
					list.push( module );
U
unknown 已提交
327 328 329 330 331 332 333 334 335 336 337 338
				}
			}.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));
U
unknown 已提交
339

U
unknown 已提交
340 341 342 343 344 345 346
			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));
U
unknown 已提交
347

U
unknown 已提交
348 349 350 351 352 353 354 355 356 357 358 359
			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();
			}
U
unknown 已提交
360

U
unknown 已提交
361 362 363 364 365 366 367
			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));
U
unknown 已提交
368

U
unknown 已提交
369 370 371 372 373 374 375 376
			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(){
U
unknown 已提交
377

U
unknown 已提交
378 379
			this.bindEvent = function () {
				this._setOuterActionEvents();
U
unknown 已提交
380 381

				this.fireEvent("afterLoad");
U
unknown 已提交
382

U
unknown 已提交
383 384 385
				//加载完成以后,删除事件
				this.form.removeEvent("afterModulesLoad", this.bindEvent );
			}.bind(this);
U
unknown 已提交
386

U
unknown 已提交
387 388
			//去要表单的所有组件加载完成以后再去获取外部组件
			this.form.addEvent("afterModulesLoad", this.bindEvent );
U
unknown 已提交
389
		},
U
unknown 已提交
390 391 392 393 394 395 396 397 398 399
		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);
400 401 402 403 404 405 406 407 408 409
				// if( o2.typeOf(data[keys[0]]) === "array"  ){
				// 	return true;
				// }
				for( var i=0; i<keys.length; i++ ){
					var key = keys[i];
					if( key !== "data" ){
						if( o2.typeOf(data[key]) === "array"  ){
							return true;
						}
					}
U
unknown 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
				}
			}
			return false;
		},
		getAllSectionData: function(){
			var bData = this.getBusinessDataById();
			var flag = false;
			if( !bData ){
				flag = true;
				bData = {};
			}
			if( !bData[this.sectionBy] ){
				flag = true;
				this.isNew = true;
				bData[this.sectionBy] = this.getValue();
			}
			if( flag )this.setBusinessDataById( bData );
			this.dataWithSectionBy = this.getAllSortedSectionData();
			return flag ? this.getBusinessDataById() : bData;
		},
		getAllSortedSectionData: function(){ //获取合并排序后的数据
			var data = this.getBusinessDataById();
			var array = [];
			for( var key in data ){
				array.push({
					sectionKey: key,
					key: key,
					data: data[key]
				})
			}
			if( this.json.sectionDisplaySortScript && this.json.sectionDisplaySortScript.code){
				array.sort( function(a, b){
					this.form.Macro.environment.event = {
						"a": a,
						"b": b
					};
					var flag = this.form.Macro.exec(this.json.sectionDisplaySortScript.code, this);
					this.form.Macro.environment.event = null;
					return flag;
				}.bind(this))
			}
			return array;
		},
		setAllSectionData: function(data, fireChange){
			var old;
			if(fireChange)old = Object.clone(this.getBusinessDataById() || {});

			this.setBusinessDataById(data);
			this.data = data;

			if (this.data){
				this.clearSubModules();
			}

			if (fireChange && JSON.stringify(old) !== JSON.stringify(data)) this.fireEvent("change");

			this.lineList = [];
			this.sectionlineList = [];
U
unknown 已提交
468
			this._loadDataTemplate();
U
unknown 已提交
469 470 471 472 473 474 475
		},
		getSectionMergeReadData: function(){
			switch (this.json.mergeTypeRead) {
				case "dataScript":
					if (this.json.sectionMergeReadDataScript && this.json.sectionMergeReadDataScript.code) {
						return this.form.Macro.exec(this.json.sectionMergeReadDataScript.code, this);
					}else{
U
unknown 已提交
476
						return [];
U
unknown 已提交
477 478 479 480 481 482 483 484 485
					}
				default:
					var sortedData = this.getSortedSectionData();
					if( this.json.showSectionKey ){
						this.dataWithSectionKey = sortedData;
					}
					var data = [];
					//把区段值放在每行的数据里
					sortedData.each(function(d){
U
unknown 已提交
486
						( d.data || [] ).each(function( obj ){
U
unknown 已提交
487 488 489 490 491 492 493
							if( o2.typeOf(obj) === "object" ){
								// obj.sectionKey = d.sectionKey;
								data.push( obj )
							}
						});
						// data = data.concat( d.data.data );
					});
U
unknown 已提交
494
					return data;
U
unknown 已提交
495 496
			}
		},
497 498 499 500
		getDefaultValue: function(){
			var value;
			if (this.json.defaultData && this.json.defaultData.code) value = this.form.Macro.exec(this.json.defaultData.code, this);
			if (value && !value.then) if (o2.typeOf(value)==="object") value = [value];
U
unknown 已提交
501 502 503 504 505 506
			if(!value){
				value = [];
				var count = this.json.defaultCount ? this.json.defaultCount.toInt() : 0;
				for( var i=0; i<count; i++ )value.push({})
			}
			return value;
U
unknown 已提交
507
		},
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
		_getValue: function(){
			if (this.moduleValueAG) return this.moduleValueAG;
			var value = this._getBusinessData();
			if( !value ){
				value = this.getDefaultValue();
			}
			// if (!value){
			// 	if (this.json.defaultData && this.json.defaultData.code) value = this.form.Macro.exec(this.json.defaultData.code, this);
			// 	if (value && !value.then) if (o2.typeOf(value)==="object") value = [value];
			// }
			// if(!value){
			// 	value = [];
			// 	var count = this.json.defaultCount ? this.json.defaultCount.toInt() : 0;
			// 	for( var i=0; i<count; i++ )value.push({})
			// }
			return value;
		},
U
unknown 已提交
525 526 527 528
		getValue: function(){
			return this._getValue();
		},

U
unknown 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		_setValue: function(value){
			if (!!value && o2.typeOf(value.then)=="function"){
				var p = o2.promiseAll(value).then(function(v){
					this.__setValue(v);
				}.bind(this), function(){});
				this.moduleValueAG = p;
				p.then(function(){
					this.moduleValueAG = null;
				}.bind(this), function(){
					this.moduleValueAG = null;
				}.bind(this));
			}else{
				this.moduleValueAG = null;
				this.__setValue(value);
			}
		},
		__setValue: function(value){
			this._setBusinessData(value);
			this.moduleValueAG = null;
			return value;
		},

U
unknown 已提交
551
		_loadDataTemplate: function(callback){
U
unknown 已提交
552 553
			var p = o2.promiseAll(this.data).then(function(v){
				this.data = v;
U
unknown 已提交
554
				// if (o2.typeOf(this.data)=="object") this.data = [this.data];
U
unknown 已提交
555 556 557 558 559 560 561 562 563 564


				if( this.isShowAllSection ){
					this._loadSectionLineList_EditSection(callback)
				}else if( this.isShowSectionKey() ){
					this._loadSectionLineList(callback)
				}else{
				    this._loadLineList(callback);
				}

U
unknown 已提交
565 566 567 568 569 570 571 572 573 574 575 576
				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));
		},
U
unknown 已提交
577 578 579 580 581 582 583 584
		_loadSectionLineList_EditSection: function(callback){
			this.getAllSectionData();
			this.dataWithSectionBy.each(function(data, idx){
				var isEdited = false;
				if( this.isSectionLineEditable( data ) && this.editable ){
					isEdited = true;
				}
				var isNew = false;
U
unknown 已提交
585 586
				var div = new Element("div").inject(this.node);
				var sectionLine = this._loadSectionLine_EditSection(div, data, idx, isEdited, isNew );
U
unknown 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599
				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,
U
unknown 已提交
600 601
				isDeleteable: this.editable && this.isSectionLineEditable(data),
				isAddable: this.editable && this.isSectionLineEditable(data)
U
unknown 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615
			});
			// 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){
			this.dataWithSectionKey.each(function(data, idx){
				var isEdited = false;
				var isNew = false;
U
unknown 已提交
616 617
				var div = new Element("div").inject(this.node);
				var sectionLine = this._loadSectionLine(div, data, idx, isEdited, isNew );
U
unknown 已提交
618 619 620 621 622 623 624 625 626 627
				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,
U
unknown 已提交
628 629
				isDeleteable: this.editable,
				isAddable: this.editable,
U
unknown 已提交
630 631 632 633 634 635 636
				isMergeRead: this.isMergeRead
			});
			// this.fireEvent("beforeLoadLine", [line]);
			sectionLine.load();
			// this.fireEvent("afterLoadLine", [line]);
			return sectionLine;
		},
U
unknown 已提交
637
		_loadLineList: function(callback){
U
unknown 已提交
638 639 640 641 642 643 644
			this.data.each(function(data, idx){
				var isNew = this.isNew || (o2.typeOf(this.newLineIndex) === "number" ? idx === this.newLineIndex : false);
				var div = new Element("div").inject(this.node);
				var line = this._loadLine(div, data, idx, isNew);
				this.lineList.push(line);
			}.bind(this));
			this.newLineIndex = null;
U
unknown 已提交
645
			this.isNew = false;
U
unknown 已提交
646 647
			if (callback) callback();
		},
U
unknown 已提交
648 649 650
		isMax : function(){
			var maxCount = this.json.maxCount ? this.json.maxCount.toInt() : 0;
			if( this.editable && maxCount > 0 ) {
U
unknown 已提交
651 652 653 654 655
				if( this.isShowAllSection ){
					if( this.sectionLineEdited && this.sectionLineEdited.lineList.length >= maxCount )return true;
				}else{
				    if( this.lineList.length >= maxCount )return true;
				}
U
unknown 已提交
656 657 658 659 660 661
			}
			return false;
		},
		isMin : function(){
			var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
			if( this.editable && minCount > 0 ) {
U
unknown 已提交
662 663 664 665 666
				if( this.isShowAllSection ){
					if( this.sectionLineEdited && this.sectionLineEdited.lineList.length <= minCount )return true;
				}else {
				    if( this.lineList.length <= minCount )return true;
				 }
U
unknown 已提交
667 668 669
			}
			return false;
		},
U
unknown 已提交
670
		_loadLine: function(container, data, index, isNew){
U
unknown 已提交
671
			var line = new MWF.xApplication.process.Xform.Datatemplate.Line(container, this, data, {
U
unknown 已提交
672 673
				index : index,
				indexText : (index+1).toString(),
U
unknown 已提交
674
				isEdited : this.editable,
U
unknown 已提交
675 676
				isNew : isNew,
				isMergeRead: this.isMergeRead
U
unknown 已提交
677
			});
U
unknown 已提交
678
			this.fireEvent("beforeLoadLine", [line]);
U
unknown 已提交
679
			line.load();
U
unknown 已提交
680
			this.fireEvent("afterLoadLine", [line]);
U
unknown 已提交
681 682
			return line;
		},
U
unknown 已提交
683
		_setLineData: function(line, d){
U
unknown 已提交
684 685 686 687 688 689 690 691 692
			if( line.sectionLine ){
				var data = this.getBusinessDataById();
				var sdata = data[ line.sectionLine.sectionKey ];
				if( sdata  ){
					sdata[line.options.indexInSectionLine] = d;
					this.setAllSectionData( data );
				}
			}else{
			    var index = line.options.index;
693
			    var data = this.getInputData();
U
unknown 已提交
694 695 696
			    data[index] = d;
			    this.setData( data );
			}
U
unknown 已提交
697
		},
U
unknown 已提交
698
		_addLine: function(ev, d){
U
unknown 已提交
699

U
unknown 已提交
700 701 702 703 704 705
			if( this.isMax() ){
				var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
				this.form.notice(text,"info");
				return false;
			}

U
unknown 已提交
706 707 708 709 710 711 712
            var data, index, newLine;
			if( this.isShowAllSection ){
				data = this.getBusinessDataById();
				var sdata = data[ this.sectionBy ];
				if( !sdata ){
					sdata = data[ this.sectionBy ] = [];
				}
U
unknown 已提交
713

U
unknown 已提交
714 715 716 717 718 719 720
				sdata.push(d||{});
				index = sdata.length - 1;
				this.newLineIndex = index;

				this.setAllSectionData( data );
				newLine = this.sectionLineEdited.lineList[index];
			}else{
721
                data = this.getInputData();
U
unknown 已提交
722 723 724 725 726 727 728

                data.push(d || {});
                index = data.length-1;
                this.newLineIndex = index;
                this.setData( data );
                newLine = this.getLine(index);
			}
U
unknown 已提交
729

730
			this.validationMode();
U
unknown 已提交
731
			this.fireEvent("addLine",[{"line":newLine, "ev":ev}]);
U
unknown 已提交
732
			this.fireEvent("change", [{"lines":[newLine], "type":"addline"}]);
U
unknown 已提交
733 734
			return newLine;
		},
U
unknown 已提交
735
		_insertLine: function(ev, beforeLine){
U
unknown 已提交
736
			if( this.isMax() ){
U
unknown 已提交
737 738
				var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
				this.form.notice(text,"info");
U
unknown 已提交
739 740
				return false;
			}
U
unknown 已提交
741 742 743

			debugger;

U
unknown 已提交
744
			//使用数据驱动
U
unknown 已提交
745 746 747 748 749 750 751 752 753 754 755
			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;
U
unknown 已提交
756

U
unknown 已提交
757 758 759 760
				this.setAllSectionData( data );
				newLine = this.sectionLineEdited.lineList[index];
			}else {
                index = beforeLine.options.index+1;
761
                data = this.getInputData();
U
unknown 已提交
762 763 764 765 766
                data.splice(index, 0, {});
                this.newLineIndex = index;
                this.setData( data );
                newLine = this.getLine( index );
			}
U
unknown 已提交
767

768
			this.validationMode();
U
unknown 已提交
769
			this.fireEvent("addLine",[{"line":newLine, "ev":ev}]);
U
unknown 已提交
770
			this.fireEvent("change", [{"lines":[newLine], "type":"addline"}]);
U
unknown 已提交
771
			return newLine;
U
unknown 已提交
772
		},
U
unknown 已提交
773
		_insertLineByIndex: function(ev, index, d){
U
unknown 已提交
774 775 776 777 778 779
			if( this.isMax() ){
				var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
				this.form.notice(text,"info");
				return false;
			}
			//使用数据驱动
U
unknown 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793
            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 );
				line = this.sectionLineEdited.lineList[index];
			}else {
794
                data = this.getInputData();
U
unknown 已提交
795 796 797 798 799 800
                if(data.length < index )return null;
                data.splice(index, 0, d||{});
                this.newLineIndex = index;
                this.setData( data );
                newLine = this.getLine( index );
			}
U
unknown 已提交
801

802
			this.validationMode();
U
unknown 已提交
803
			this.fireEvent("addLine",[{"line":newLine, "ev":ev}]);
U
unknown 已提交
804
			this.fireEvent("change", [{"lines":[newLine], "type":"addline"}]);
U
unknown 已提交
805
			return newLine;
U
unknown 已提交
806
		},
U
unknown 已提交
807 808 809
		_deleteSelectedLine: function(ev){
			var selectedLine = this.lineList.filter(function (line) { return line.selected; });
			if( selectedLine.length === 0 ){
U
unknown 已提交
810
				this.form.notice( MWF.xApplication.process.Xform.LP.selectItemNotice,"info");
U
unknown 已提交
811 812 813
				return false;
			}
			var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
U
unknown 已提交
814
			if( minCount > 0 ){
U
unknown 已提交
815 816 817 818 819 820 821
				var length;
				if( this.isShowAllSection && this.sectionLineEdited ){
					length = this.sectionLineEdited.lineList.length;
				}else{
					length = this.lineList.length;
				}
				if( length - selectedLine.length < minCount ){
U
unknown 已提交
822 823
					var text = MWF.xApplication.process.Xform.LP.minItemNotice.replace("{n}", minCount );
					this.form.notice(text,"info");
U
unknown 已提交
824 825 826
					return false;
				}
			}
U
unknown 已提交
827
			var _self = this;
U
unknown 已提交
828
			this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, MWF.xApplication.process.Xform.LP.deleteSelectedItemNotice, 300, 120, function(){
U
unknown 已提交
829

U
unknown 已提交
830
				_self._delLines( selectedLine );
U
unknown 已提交
831

U
unknown 已提交
832 833 834 835 836 837 838
				this.close();

			}, function(){
				this.close();
			}, null, null, this.form.json.confirmStyle);

		},
U
unknown 已提交
839
		_delLines: function(lines){
U
unknown 已提交
840
			var _self = this;
U
unknown 已提交
841 842 843 844
			var data;
			if( this.isShowAllSection ){
				data = this.getBusinessDataById();
			}else{
845
				data = _self.getInputData();
U
unknown 已提交
846
			}
U
unknown 已提交
847

U
unknown 已提交
848 849
			var saveFlag = false;

U
unknown 已提交
850
			lines.reverse().each(function(line){
U
unknown 已提交
851
				_self.fireEvent("deleteLine", [line]);
U
unknown 已提交
852

U
unknown 已提交
853 854
				if(line.deleteAttachment())saveFlag = true;

U
unknown 已提交
855 856 857 858 859 860 861 862
				if( line.sectionLine ){
					var d = data[ line.sectionLine.sectionKey ];
					if( d ){
						d.splice(line.options.indexInSectionLine, 1);
					}
				}else {
				    data.splice(line.options.index, 1);
				}
U
unknown 已提交
863

U
unknown 已提交
864
				_self.fireEvent("afterDeleteLine");
U
unknown 已提交
865 866
			});

U
unknown 已提交
867 868 869 870 871
			if( this.isShowAllSection ){
				_self.setAllSectionData(data);
			}else{
			    _self.setData( data );
			}
872
			this.validationMode();
U
unknown 已提交
873 874 875

			_self.fireEvent("change", [{"lines":lines, "type":"deletelines"}]);

U
unknown 已提交
876
			if(saveFlag)this.form.saveFormData();
U
unknown 已提交
877 878 879 880 881 882 883 884 885
		},
		_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(){
U
unknown 已提交
886
				_self._delLine(line);
U
unknown 已提交
887
				this.close();
U
unknown 已提交
888 889 890
			}, function(){
				this.close();
			}, null, null, this.form.json.confirmStyle);
U
unknown 已提交
891
		},
U
unknown 已提交
892
		_delLine: function(line){
U
unknown 已提交
893 894
			this.fireEvent("deleteLine", [line]);

U
unknown 已提交
895
			var saveFlag = line.deleteAttachment();
U
unknown 已提交
896
			//使用数据驱动
U
unknown 已提交
897 898 899 900 901 902 903 904 905
			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 );
			}else{
906
                data = this.getInputData();
U
unknown 已提交
907 908 909
                data.splice(line.options.index, 1);
                this.setData( data );
			}
U
unknown 已提交
910

911
			this.validationMode();
U
unknown 已提交
912
			this.fireEvent("afterDeleteLine");
U
unknown 已提交
913 914 915

			this.fireEvent("change", [{"lines":[line], "type":"deleteline"}]);

U
unknown 已提交
916
			if(saveFlag)this.form.saveFormData();
U
unknown 已提交
917
		},
U
unknown 已提交
918 919 920 921 922 923 924 925
		_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;
			}
U
unknown 已提交
926
			this.selected = selected;
U
unknown 已提交
927 928 929 930 931 932 933 934 935
			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))
			}
U
unknown 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
		},
		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<arr.length; i++ ){
					var v = arr[i];
					if( v !== this.json.outerSelectAllSelectedValue ){
						value = v;
						break;
					}
				}
				this.selectAllSelector.setData(value);
			}else{
				this.selectAllSelector.setData("")
U
unknown 已提交
957 958
			}
		},
U
unknown 已提交
959 960 961 962

		editValidation: function(){
			var flag = true;
			this.editModules.each(function(field, key){
U
unknown 已提交
963
				if (field.json.type!=="sequence" && field.validationMode ){
U
unknown 已提交
964 965 966 967 968 969
					field.validationMode();
					if (!field.validation()) flag = false;
				}
			}.bind(this));
			return flag;
		},
U
unknown 已提交
970
		exportToExcel: function(){
U
unknown 已提交
971 972
			this.exporter = new MWF.xApplication.process.Xform.Datatemplate.Exporter(this);
			this.exporter.exportToExcel();
U
unknown 已提交
973 974
		},
		importFromExcel: function(){
U
unknown 已提交
975 976
			this.importer = new MWF.xApplication.process.Xform.Datatemplate.Importer(this);
			this.importer.importFromExcel();
U
unknown 已提交
977
		},
U
unknown 已提交
978

U
unknown 已提交
979 980 981

		_afterLoaded: function(){
		},
982 983 984 985 986
		// /**
		//  * @summary 重置数据模板的值为默认值或置空。
		//  *  @example
		//  * this.form.get('fieldId').resetData();
		//  */
U
unknown 已提交
987
		resetData: function(){
988 989
			// var value = this.getDefaultValue() || [];
			var value = this.getValue() || [];
990
			this.setData(value);
U
unknown 已提交
991 992 993
		},
		/**当参数为Promise的时候,请查看文档: {@link  https://www.yuque.com/o2oa/ixsnyt/ws07m0|使用Promise处理表单异步}<br/>
		 * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
U
unknown 已提交
994
		 * @summary 为数据模板赋值。
U
unknown 已提交
995
		 * @param data{DatatemplateData|Promise|Array} 必选,数组或Promise.
U
unknown 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
		 * @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){
			if (!data){
				data = this._getValue();
			}
			this._setData(data);
		},
		_setData: function(data){
			var p = o2.promiseAll(this.data).then(function(v){
U
unknown 已提交
1029
				this.data = v;
U
unknown 已提交
1030
				// if (o2.typeOf(data)==="object") data = [data];
U
unknown 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
				this.__setData(data);
				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){
U
unknown 已提交
1045
			// if( typeOf( data ) === "object" && typeOf(data) === "array"  ){
U
unknown 已提交
1046
			this._setBusinessData(data);
U
unknown 已提交
1047 1048 1049
			this.data = data;

			if (this.data){
U
unknown 已提交
1050
				this.clearSubModules();
U
unknown 已提交
1051
			}
U
unknown 已提交
1052 1053

			this.lineList = [];
U
unknown 已提交
1054
			this.sectionlineList = [];
U
unknown 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
			this._loadDataTemplate(function(){
				this._setSubDatatemplateOuterEvents();
			}.bind(this))
		},
		_setSubDatatemplateOuterEvents: function(){
			//告诉下层的数据模板绑定外部事件
			for (var i=0; i<this.lineList.length; i++){
				this.lineList[i].setSubDatatemplateOuterActionEvents();
			}
		},
U
unknown 已提交
1065
		clearSubModules: function(){
U
unknown 已提交
1066 1067 1068 1069 1070 1071 1072 1073
			if( this.sectionlineList && this.sectionlineList.length ){
				for( var i=0; i<this.sectionlineList.length; i++ ){
					this.sectionlineList[i].clearSubModules();
				}
			}else{
                for (var i=0; i<this.lineList.length; i++){
                    this.lineList[i].clearSubModules();
                }
U
unknown 已提交
1074
			}
U
unknown 已提交
1075 1076
		},
		/**
U
unknown 已提交
1077
		 * @summary 判断数据模板是否为空.
U
unknown 已提交
1078 1079 1080 1081 1082 1083 1084
		 * @example
		 * if( this.form.get('fieldId').isEmpty() ){
		 *     this.form.notice('至少需要添加一条数据', 'warn');
		 * }
		 * @return {Boolean} 是否为空
		 */
		isEmpty: function(){
1085
			var data = this.getInputData();
U
unknown 已提交
1086
			if( !data )return true;
U
unknown 已提交
1087
			if( o2.typeOf( data ) === "array" ){
U
unknown 已提交
1088
				return data.length === 0;
U
unknown 已提交
1089
			}
U
unknown 已提交
1090 1091 1092
			if( o2.typeOf( data ) === "object" ){
				return Object.keys(data).length === 0;
			}
U
unknown 已提交
1093 1094
			return false;
		},
U
unknown 已提交
1095 1096
		//api 相关开始
		/**
U
unknown 已提交
1097
		 * 获取对应的条目。
U
unknown 已提交
1098 1099 1100 1101 1102 1103
		 * @param {Number} index 条目序号,从零开始
		 * @return {MWF.xApplication.process.Xform.Datatemplate.Line | Null} 对应的数据模板条目
		 * @example
		 * //获取数据模板“dt1”的第一个条目。
		 * var line = this.form.get("dt1").getLine(0);
		 * //获取第一个条目subject字段的值
1104
		 * var data = line.get("subject").getData();
U
unknown 已提交
1105
		 * //设置subject字段的值
1106
		 * line.get("subject").setData("test1");
U
unknown 已提交
1107 1108 1109 1110 1111 1112
		 */
		getLine: function(index){
			var line = this.lineList[index];
			return line || null;
		},
		/**
U
unknown 已提交
1113
		 * 在数据模板末尾添加条目。
U
unknown 已提交
1114 1115 1116 1117 1118 1119
		 * @param {Object} [data] 添加条目的数据。
		 * @return {MWF.xApplication.process.Xform.Datatemplate.Line} 添加的数据模板条目
		 * @example
		 * var line = this.form.get("dt1").addLine();
		 */
		addLine: function( data ){
U
unknown 已提交
1120
			return this._addLine( null, data );
U
unknown 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		},
		/**
		 * 在数据模板指定位置添加条目。
		 * @param {Number} index 条目序号,从零开始,如果下标超过当前数据模板条目数,插入失败并返回null。
		 * @param {Object} [data] 添加条目的数据。
		 * @return {MWF.xApplication.process.Xform.Datatemplate.Line | Null} 插入的数据模板条目
		 * @example
		 * var line = this.form.get("dt1").insertLine(0);
		 */
		insertLine: function(index, data){
U
unknown 已提交
1131
			return this._insertLineByIndex(null, index, data);
U
unknown 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		},
		/**
		 * 删除指定位置的条目。
		 * @param {Number} index 条目序号,从零开始,如果下标超过当前数据模板条目数,删除失败。
		 * @example
		 * //直接删除第一个条目
		 * this.form.get("dt1").deleteLine(0);
		 */
		deleteLine: function(index, ev){
			var line = this.lineList[index];
			if( !line )return null;
			// if( ev ){
			// 	this._deleteLine(ev, line);
			// }else{
U
unknown 已提交
1146
			this._delLine(line);
U
unknown 已提交
1147 1148
			// }
		},
U
unknown 已提交
1149
		/**
1150
		 * 获取对应表单组件,作用等同于get。
U
unknown 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
		 * @param {Number} index 条目序号,从零开始
		 * @param {String} id 组件标识
		 * @return {FormComponent} 对应表单组件
		 * @example
		 * //获取数据模板“dt1”的第一个条目的subject字段。
		 * var module = this.form.get("dt1").getModule(0, "subject");
		 * //获取subject字段的值
		 * var data = module.getData();
		 * //设置subject字段的值
		 * module.setData("test1");
		 */
		getModule: function(index, id){
			var line = this.lineList[index];
			if( !line )return null;
U
unknown 已提交
1165 1166
			return line.getModule(id);
		},
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
		/**
		 * 获取对应表单组件,作用等同于getModule。
		 * @param {Number} index 条目序号,从零开始
		 * @param {String} id 组件标识
		 * @return {FormComponent} 对应表单组件
		 * @example
		 * //获取数据模板“dt1”的第一个条目的subject字段。
		 * var module = this.form.get("dt1").get(0, "subject");
		 * //获取subject字段的值
		 * var data = module.getData();
		 * //设置subject字段的值
		 * module.setData("test1");
		 */
1180 1181 1182
		get: function(index, id){
			return this.getModule(index, id);
		},
U
unknown 已提交
1183
		//api 相关
U
unknown 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192

		/**
		 * 在脚本中使用 this.data[fieldId] 也可以获取组件值。
		 * 区别如下:<br/>
		 * 1、当使用Promise的时候<br/>
		 * 使用异步函数生成器(Promise)为组件赋值的时候,用getData方法立即获取数据,可能返回修改前的值,当Promise执行完成以后,会返回修改后的值。<br/>
		 * this.data[fieldId] 立即获取数据,可能获取到异步函数生成器,当Promise执行完成以后,会返回修改后的值。<br/>
		 * {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0#EggIl|具体差异请查看链接}<br/>
		 * 2、当表单上没有对应组件的时候,可以使用this.data[fieldId]获取值,但是this.form.get('fieldId')无法获取到组件。
U
unknown 已提交
1193
		 * @summary 获取数据模板数据.
U
unknown 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
		 * @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 );
U
unknown 已提交
1219
		 * @return {DatatemplateData}
U
unknown 已提交
1220 1221
		 */
		getData: function(){
U
unknown 已提交
1222 1223 1224
			if( this.importer ){
				this.importer.destroySimulateModule();
			}
U
unknown 已提交
1225
			if (this.editable!==false){
U
unknown 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
				// 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 : [];
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
				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){
U
unknown 已提交
1249
				return this._getBusinessData();
U
unknown 已提交
1250 1251 1252 1253
			}else{
				return this._getBusinessData();
			}
		},
U
unknown 已提交
1254
		_getSectionBy: function(){
U
unknown 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
			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 "";
				}
			}
		},
U
unknown 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
		createErrorNode: function(text){
			var node = new Element("div");
			var iconNode = new Element("div", {
				"styles": {
					"width": "20px",
					"height": "20px",
					"float": "left",
					"background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) 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);
			}
		},
		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();
U
unknown 已提交
1340 1341 1342 1343 1344 1345 1346
				if( o2.typeOf(n)==="object"){
					var arr = [];
					Object.each( n, function (d, key) {
						if(o2.typeOf(d) === "array")arr = arr.concat(d);
					});
					n = arr;
				}
U
unknown 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
				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<data.value){
							this.notValidationMode(data.prompt);
							return false;
						}
						break;
					case "equal":
						if (v==data.value){
							this.notValidationMode(data.prompt);
							return false;
						}
						break;
					case "neq":
						if (v!=data.value){
							this.notValidationMode(data.prompt);
							return false;
						}
						break;
					case "contain":
						if (v.indexOf(data.value)!=-1){
							this.notValidationMode(data.prompt);
							return false;
						}
						break;
					case "notcontain":
						if (v.indexOf(data.value)==-1){
							this.notValidationMode(data.prompt);
							return false;
						}
						break;
				}
			}
			return true;
		},
		validationConfig: function(routeName, opinion){
			if (this.json.validationConfig){
				if (this.json.validationConfig.length){
					for (var i=0; i<this.json.validationConfig.length; i++) {
						var data = this.json.validationConfig[i];
						if (!this.validationConfigItem(routeName, data)) return false;
					}
				}
				return true;
			}
			return true;
		},
		validation: function(routeName, opinion){
			if (this.isEdit){
				if (!this.editValidation()){
					return false;
				}
			}
			if (!this.validationConfig(routeName, opinion))  return false;

			if (!this.json.validation) return true;
			if (!this.json.validation.code) return true;

			this.currentRouteName = routeName;
			var flag = this.form.Macro.exec(this.json.validation.code, this);
			this.currentRouteName = "";

			if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
U
unknown 已提交
1429
			if (flag.toString()!=="true"){
U
unknown 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
				this.notValidationMode(flag);
				return false;
			}
			return true;
		},
		getAttachmentRandomSite: function(){
			var i = (new Date()).getTime();
			return this.json.id+i;
		}
	});

U
unknown 已提交
1441

U
unknown 已提交
1442
MWF.xApplication.process.Xform.Datatemplate.SectionLine =  new Class({
U
unknown 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
	Implements: [Options, Events],
	options: {
		isNew: false,
		isEdited: true, //是否正在编辑
		isDeleteable: true, //能否被删除
		isAddable: true, //能否添加
		isMergeRead: false, //合并阅读
		index: 0,
		indexText: "0"
	},
U
unknown 已提交
1453
	initialize: function (node, template, data, options) {
U
unknown 已提交
1454
		this.setOptions(options);
U
unknown 已提交
1455 1456
		this.node = node;
		this.template = template;
U
unknown 已提交
1457
		this.data = data;
U
unknown 已提交
1458
		this.form = this.template.form;
U
unknown 已提交
1459 1460 1461 1462
		this.lineList = [];
		this.sectionKey = this.data.sectionKey;
	},
	load: function () {
U
unknown 已提交
1463
		if( this.template.isShowSectionKey() || this.template.isShowSectionBy() ){
U
unknown 已提交
1464 1465 1466
			this.loadSectionKeyNode();
		}

U
unknown 已提交
1467 1468
		if( this.data.data ){
			( this.data.data || [] ).each(function(d, idx){
U
unknown 已提交
1469
				if( !d )return;
U
unknown 已提交
1470
				var div = new Element("div").inject(this.node);
U
unknown 已提交
1471 1472
				var isEdited = false, isNew = false;
				if( this.options.isEdited ){
U
unknown 已提交
1473
					var dt = this.template;
U
unknown 已提交
1474
					isNew = dt.isNew || (o2.typeOf(dt.newLineIndex) === "number" ? idx === dt.newLineIndex : false);
U
unknown 已提交
1475
					isEdited = true;
U
unknown 已提交
1476 1477 1478
					dt.isNew = false;
					dt.newLineIndex = null;
				}
U
unknown 已提交
1479
				var line = this._loadLine( div, d, idx, isEdited, isNew );
U
unknown 已提交
1480
				this.lineList.push(line);
U
unknown 已提交
1481
				this.template.lineList.push(line);
U
unknown 已提交
1482 1483 1484 1485
			}.bind(this));
		}
	},
	_loadLine: function(container, data, index, isEdited, isNew){
U
unknown 已提交
1486
		var line = new MWF.xApplication.process.Xform.Datatemplate.Line(container, this.template, data, {
U
unknown 已提交
1487 1488
			indexInSectionLine : index,
			indexInSectionLineText : (index+1).toString(),
U
unknown 已提交
1489 1490
			index: this.template.lineList.length,
			indexText : (this.template.lineList.length + 1).toString(),
U
unknown 已提交
1491 1492 1493 1494 1495 1496 1497
			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);
U
unknown 已提交
1498
		this.template.fireEvent("beforeLoadLine", [line]);
U
unknown 已提交
1499
		line.load();
U
unknown 已提交
1500
		this.template.fireEvent("afterLoadLine", [line]);
U
unknown 已提交
1501 1502 1503
		return line;
	},
	loadSectionKeyNode: function () {
U
unknown 已提交
1504 1505
		var sectionKeyStyles = this.template._parseStyles(this.template.json.sectionKeyStyles);
		var keyNode = new Element("div.mwf_sectionkey", {
U
unknown 已提交
1506
			styles : sectionKeyStyles
U
unknown 已提交
1507
		}).inject( this.node );
U
unknown 已提交
1508 1509
		this.keyNode = keyNode;
		var separator;
U
unknown 已提交
1510 1511
		if( this.template.isShowSectionKey() ){
			separator = this.template.json.keyContentSeparator;
U
unknown 已提交
1512
		}else{
U
unknown 已提交
1513
			separator = this.template.json.keyContentSeparatorSectionBy;
U
unknown 已提交
1514
		}
U
unknown 已提交
1515
		this.template.getSectionKeyWithMerge( this.data, function (key) {
U
unknown 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
			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));
	},
	clearSubModules: function(){
		if( this.keyNode ){
			this.keyNode.destroy();
			this.keyNode = null;
		}
		for (var i=0; i<this.lineList.length; i++){
			this.lineList[i].clearSubModules();
		}
	}
});



U
unknown 已提交
1538 1539 1540
MWF.xApplication.process.Xform.Datatemplate.Line =  new Class({
	Implements: [Options, Events],
	options: {
U
unknown 已提交
1541
		isNew: false,
U
unknown 已提交
1542
		isEdited : true,
U
unknown 已提交
1543 1544
		isAddable: true,
		isDeleteable: true,
U
unknown 已提交
1545
		index : 0,
U
unknown 已提交
1546 1547 1548 1549
		indexText : "0",
		indexInSectionLine: 0,
		indexInSectionLineText : "0",
		sectionKey: ""
U
unknown 已提交
1550
	},
U
unknown 已提交
1551
	initialize: function (node, template, data, options, sectionLine) {
U
unknown 已提交
1552 1553 1554 1555 1556 1557 1558

		this.setOptions(options);

		this.node = node;
		this.template = template;
		this.data = data;
		this.form = this.template.form;
U
unknown 已提交
1559
		this.sectionLine = sectionLine;
U
unknown 已提交
1560

U
unknown 已提交
1561 1562
		this.modules = [];
		this.all = {};
U
unknown 已提交
1563
		this.all_templateId = {};
U
unknown 已提交
1564 1565 1566

		this.fields = [];
		this.allField = {};
U
unknown 已提交
1567
		this.allField_templateId = {};
U
unknown 已提交
1568 1569 1570 1571 1572 1573 1574 1575

		this.addActionList = [];
		this.deleteActionList = [];
		this.sequenceNodeList = [];
		this.selector = null;
		this.importActionList = [];
		this.exportActionList = [];

U
unknown 已提交
1576
		this.subDatatemplateModuleList = [];
U
unknown 已提交
1577
	},
U
unknown 已提交
1578
	load: function(){
U
unknown 已提交
1579 1580
		this.node.set("html", this.template.templateHtml);
		var moduleNodes = this.form._getModuleNodes(this.node);
U
unknown 已提交
1581 1582

		//拆分状态
1583
		var sectionKey = this.options.sectionKey || this.template.sectionBy;
U
unknown 已提交
1584

U
unknown 已提交
1585 1586 1587 1588
		moduleNodes.each(function (node) {
			if (node.get("MWFtype") !== "form") {
				var _self = this;

U
unknown 已提交
1589 1590 1591 1592
				var tJson = this.form._getDomjson(node);
				if( tJson ){
					var json = Object.clone(tJson);

U
unknown 已提交
1593 1594
					if( !this.options.isEdited )json.isReadonly = true;

U
unknown 已提交
1595 1596
					var templateJsonId = json.id;

U
unknown 已提交
1597 1598

					var index = this.options.index;
U
unknown 已提交
1599
					var id;
U
unknown 已提交
1600
					if( this.template.isShowAllSection ){
U
unknown 已提交
1601 1602
					    id = this.template.json.id + ".." + sectionKey + ".." + this.options.indexInSectionLine + ".." + json.id;
				    }else if( sectionKey ){
U
unknown 已提交
1603 1604
						id = this.template.json.id + ".." + sectionKey + ".."+ index + ".." + json.id;
					}else{
U
unknown 已提交
1605
						id = this.template.json.id + ".." + index + ".." + json.id;
U
unknown 已提交
1606
					}
U
unknown 已提交
1607

U
unknown 已提交
1608
					json.id = id;
U
unknown 已提交
1609
					node.set("id", id);
U
unknown 已提交
1610

U
unknown 已提交
1611
					if( json.type==="Attachment" || json.type==="AttachmentDg" ){
U
unknown 已提交
1612
						json.type = "AttachmentDg";
U
unknown 已提交
1613
						json.ignoreSite = true;
U
unknown 已提交
1614
						json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
U
unknown 已提交
1615 1616
					}

U
unknown 已提交
1617 1618 1619
					if (this.form.all[id]) this.form.all[id] = null;
					if (this.form.forms[id])this.form.forms[id] = null;

1620
					var module = this.form._loadModule(json, node, function () {
U
unknown 已提交
1621 1622 1623 1624 1625 1626 1627
						if( _self.options.isMergeRead ){
                            this.field = false; //不希望保存数据
                            this._getBusinessData = function(){
                                return _self.data[templateJsonId];
                            };
                            this._setBusinessData = function () {};
					    }
1628
						if( _self.widget )this.widget = _self.widget;
1629 1630
						this.parentLine = _self;
						this.parentDatatemplate = _self.template;
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651

						//只读方法值在页面加载的时候或者new的时候计算一下
						if( this.json.compute === "show" ){
							var needComputeShow = false;
							if( _self.template.loading ) {
								needComputeShow = true;
							}else if( _self.options.isNew && !_self.reloading ){
								needComputeShow = true;
							}
							if( !needComputeShow ){
								this.json.compute = "create"; //
								if( this.options.moduleEvents && this.options.moduleEvents.length ){ //恢复compute
									var eventName = ( this.options.moduleEvents || [] ).contains("afterLoad") ? "afterLoad" : "load";
									var resetCompute = function () {
										this.json.compute = "show";
										this.removeEvent( eventName, resetCompute );
									}.bind(this)
									this.addEvent(eventName, resetCompute);
								}
							}
						}
1652 1653 1654
					});
					if(!module.parentLine)module.parentLine = this;
					if(!module.parentDatatemplate)module.parentDatatemplate = this.template;
U
unknown 已提交
1655 1656 1657 1658 1659

					if( json.type==="Attachment" || json.type==="AttachmentDg" ){
						module.addEvent("change", function(){
							_self.form.saveFormData();
						}.bind(this))
U
unknown 已提交
1660 1661
					}else if( json.type==="Datatemplate" ){
						this.subDatatemplateModuleList.push(module);
U
unknown 已提交
1662 1663
					}

U
unknown 已提交
1664 1665
					this.form.modules.push(module);

U
unknown 已提交
1666 1667
					this.modules.push(module);
					this.all[id] = module;
U
unknown 已提交
1668
					this.all_templateId[templateJsonId] = module;
U
unknown 已提交
1669

U
unknown 已提交
1670
					if (module.field) {
1671 1672 1673
						// if(this.data.hasOwnProperty(templateJsonId)){
						// 	module.setData(this.data[templateJsonId]);
						// }
U
unknown 已提交
1674
						this.allField[id] = module;
U
unknown 已提交
1675
						this.allField_templateId[templateJsonId] = module;
U
unknown 已提交
1676
						this.fields.push( module );
U
unknown 已提交
1677 1678 1679 1680

						module.addEvent("change", function(){
							this.template.fireEvent("change", [{lines: [this], type: "editmodule", module: module}]);
						}.bind(this))
U
unknown 已提交
1681
					}
U
unknown 已提交
1682

U
unknown 已提交
1683
					this.setEvents(module, templateJsonId);
U
unknown 已提交
1684 1685 1686

				}
			}
U
unknown 已提交
1687
		}.bind(this));
U
unknown 已提交
1688 1689 1690 1691 1692

		if(this.options.isNew){
			this.data = this.getData();
			this.options.isNew = false;
		}
U
unknown 已提交
1693
	},
U
unknown 已提交
1694 1695 1696 1697 1698 1699 1700 1701
	setSubDatatemplateOuterActionEvents: function(){
		this.subDatatemplateModuleList.each(function(module){
			//绑定下级模板事件
			module._setOuterActionEvents();
			//让下级数据模板再去绑定下级模板外部事件
			module._setSubDatatemplateOuterEvents();
		})
	},
1702 1703 1704
	getIndex: function(){
		return this.options.index;
	},
U
unknown 已提交
1705 1706 1707
	getModule: function(templateJsonId){
		return this.all_templateId[templateJsonId];
	},
1708 1709 1710
	get: function(templateJsonId){
		return this.all_templateId[templateJsonId];
	},
U
unknown 已提交
1711
	getAttachmentSite: function(json, templateJsonId, sectionKey){
U
unknown 已提交
1712 1713
		//确保site最长为64,否则后台会报错

U
unknown 已提交
1714
		var index = this.options.index;
U
unknown 已提交
1715 1716 1717 1718

		var baseSite;
		baseSite =  "." + index + "."  + (json.site || templateJsonId);

U
unknown 已提交
1719
		var maxLength;
U
unknown 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728
		var sectionId = "";
		if( sectionKey ){
			maxLength = Math.floor((63 - baseSite.length)/2 );

			sectionId = (sectionKey.length > maxLength) ? sectionKey.substr(sectionKey.length-maxLength, maxLength) : sectionKey;
			sectionId = "." + sectionId;
		}else{
			maxLength = 64 - baseSite.length;
		}
U
unknown 已提交
1729 1730 1731 1732

		var templateId = this.template.json.id;
		templateId = (templateId.length > maxLength) ? templateId.substr(templateId.length-maxLength, maxLength) : templateId;

U
unknown 已提交
1733
		return templateId + sectionId + baseSite;
U
unknown 已提交
1734
	},
U
unknown 已提交
1735 1736 1737 1738
	setEvents: function (module, id) {
		if( this.template.addActionIdList.contains( id )){
			this.addActionList.push( module );
			module.node.addEvent("click", function (ev) {
U
unknown 已提交
1739
				this.template._insertLine( ev, this )
U
unknown 已提交
1740
			}.bind(this))
U
unknown 已提交
1741
			if( !this.template.editable )module.node.hide();
U
unknown 已提交
1742
			if( !this.options.isAddable )module.node.hide();
U
unknown 已提交
1743
		}
U
unknown 已提交
1744 1745 1746 1747 1748 1749

		if( this.template.deleteActionIdList.contains(id)){
			this.deleteActionList.push( module );
			module.node.addEvent("click", function (ev) {
				this.template._deleteLine( ev, this )
			}.bind(this))
U
unknown 已提交
1750
			if( !this.template.editable )module.node.hide();
U
unknown 已提交
1751
			if( !this.options.isDeleteable )module.node.hide();
U
unknown 已提交
1752 1753
		}

U
unknown 已提交
1754 1755
		if( this.template.selectorId === id){
			this.selector = module;
U
unknown 已提交
1756
			// module.setData(""); //默认不选择
U
unknown 已提交
1757 1758 1759
			module.node.addEvent("click", function (ev) {
				this.checkSelect();
			}.bind(this))
U
unknown 已提交
1760
			if( !this.template.editable )module.node.hide();
U
unknown 已提交
1761
			if( !this.options.isDeleteable )module.node.hide();
U
unknown 已提交
1762
			this.unselect();
U
unknown 已提交
1763
		}
U
unknown 已提交
1764

U
unknown 已提交
1765 1766
		if( this.template.sequenceIdList.contains(id)){
			this.sequenceNodeList.push( module );
U
unknown 已提交
1767 1768 1769 1770 1771 1772 1773 1774 1775
			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;
			}
U
unknown 已提交
1776
			if(this.form.getModuleType(module) === "label"){
U
unknown 已提交
1777
				module.node.set("text", indexText );
1778 1779
			}else if(module.setData){
				module.setData( indexText );
U
unknown 已提交
1780 1781
			}
		}
U
unknown 已提交
1782

U
unknown 已提交
1783 1784 1785
		//???
		// if( this.template.importActionIdList.contains(id))this.importActionList.push( module );
		// if( this.template.exportActionIdList.contains(id))this.exportActionList.push( module );
U
unknown 已提交
1786

U
unknown 已提交
1787 1788
	},
	checkSelect: function () {
U
unknown 已提交
1789 1790 1791 1792 1793 1794
		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;
U
unknown 已提交
1795
		}
U
unknown 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
		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<arr.length; i++ ){
				var v = arr[i];
				if( v !== this.template.json.selectorSelectedValue ){
					value = v;
					break;
				}
			}
			this.selector.setData(value);
		}else{
			this.selector.setData("")
		}
U
unknown 已提交
1819
	},
U
unknown 已提交
1820
	reload: function(){
1821
		this.reloading = true;
U
unknown 已提交
1822 1823 1824 1825 1826 1827 1828 1829
		for(var key in this.all){
			var module = this.all[key];
			this.form.modules.erase(module);
			if (this.form.all[key]) delete this.form.all[key];
			if (this.form.forms[key])delete this.form.forms[key];
		}
		this.node.empty();
		this.load();
1830
		this.reloading = false;
U
unknown 已提交
1831
	},
U
unknown 已提交
1832
	clearSubModules: function () { //把module清除掉
U
unknown 已提交
1833 1834
		for(var key in this.all){
			var module = this.all[key];
U
unknown 已提交
1835
			//如果嵌套数据模板或者数据表格,还要清除掉下级
U
unknown 已提交
1836
			if(module.clearSubModules)module.clearSubModules();
1837
			if( module.json && (module.json.type==="TinyMCEEditor" || module.json.type==="Htmleditor"))module.destroy();
U
unknown 已提交
1838 1839 1840 1841 1842 1843
			this.form.modules.erase(module);
			if (this.form.all[key]) delete this.form.all[key];
			if (this.form.forms[key])delete this.form.forms[key];
		}
		this.node.destroy();
	},
1844 1845 1846 1847 1848 1849 1850 1851 1852
	computeModuleData: function( when ){
		for( var key in this.allField){
			var module = this.allField[key];
			var id = key.split("..").getLast();
			if( module.json.compute === when ){
				this.data[id] = module.getData();
			}
		}
	},
U
unknown 已提交
1853
	getData: function () {
U
unknown 已提交
1854
		var data = this.data;
U
unknown 已提交
1855
		for( var key in this.allField){
U
unknown 已提交
1856
			var module = this.allField[key];
U
unknown 已提交
1857
			var id = key.split("..").getLast();
1858
			if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ) {
U
unknown 已提交
1859
				data[id] = module._getBusinessData();
1860 1861
			}else if( module.json.compute === "save" && module.getInputData ){
				data[id] = module.getInputData();
U
unknown 已提交
1862 1863 1864
			}else{
				data[id] = module.getData();
			}
U
unknown 已提交
1865 1866
		}
		return data;
U
unknown 已提交
1867 1868 1869
	},
	setData: function (data) {
		this.template._setLineData(this, data);
U
unknown 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
	},
	deleteAttachment: function(){
		var saveFlag = false;
		for( var key in this.allField){
			var module = this.allField[key];
			if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
				var array = module._getBusinessData();
				(array || []).each(function(d){
					saveFlag = true;
					this.form.workAction.deleteAttachment(d.id, this.form.businessData.work.id);
				}.bind(this))
			}
		}
		return saveFlag;
U
unknown 已提交
1884
	}
U
unknown 已提交
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
});

MWF.xApplication.process.Xform.Datatemplate.Exporter = new Class({
	Implements: [Options, Events],
	options: {
	},
	initialize: function (template, options) {

		this.setOptions(options);

		this.template = template;
		this.form = this.template.form;

	},
	exportToExcel : function () {
		var resultArr = [];
		var titleArr = this.template.json.excelFieldConfig.map(function(config){
			return config.title;
		});
		resultArr.push( titleArr );

U
unknown 已提交
1906

U
unknown 已提交
1907 1908 1909 1910
		this.template.lineList.each(function (line, index) {
			resultArr.push( this.getLineExportData(line, index) );
		}.bind(this));

U
unknown 已提交
1911 1912 1913
		var colWidthArr = this.getColWidthArray();
		var excelName = this.getExcelName();

U
unknown 已提交
1914 1915 1916
		var arg = {
			data : resultArr,
			colWidthArray : colWidthArr,
U
unknown 已提交
1917
			title : excelName
U
unknown 已提交
1918
		};
U
unknown 已提交
1919
		this.template.fireEvent("export", [arg]);
U
unknown 已提交
1920

U
unknown 已提交
1921
		new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template ).exportToExcel(
U
unknown 已提交
1922
			arg.data || resultArr,
U
unknown 已提交
1923
			arg.title || excelName,
U
unknown 已提交
1924
			arg.colWidthArray || colWidthArr,
U
unknown 已提交
1925 1926
			this.getDateIndexArray()  //日期格式列下标
		);
U
unknown 已提交
1927 1928 1929 1930 1931 1932 1933 1934
	},
	getLineExportData: function(line, index ){
		var exportData = [];
		this.template.json.excelFieldConfig.each(function (config) {

			var module = line.all_templateId[config.field];
			var json = module ? module.json : "";

U
unknown 已提交
1935
			if ( !module || !json || !this.isAvaliableField( json ) ) {
U
unknown 已提交
1936 1937 1938 1939 1940
				exportData.push("");
			}else{
				var value = module.getData();
				var text = "";

U
unknown 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985

				if( value ){
					switch (module.json.type) {
						case "Org":
						case "Reader":
						case "Author":
						case "Personfield":
						case "Orgfield":
							if (o2.typeOf(value) === "array") {
								var textArray = [];
								value.each(function (item) {
									if (o2.typeOf(item) === "object") {
										textArray.push(item.distinguishedName);
									} else {
										textArray.push(item);
									}
								}.bind(this));
								text = textArray.join(", \n");
							} else if (o2.typeOf(value) === "object") {
								text = value.distinguishedName;
							} else {
								text = value;
							}
							break;
						case "Combox":
						case "Address":
							text = o2.typeOf(value) === "array" ? value.join(", ") : value;
							break;
						case "Checkbox":
							var options = module.getOptionsObj();
							var value = o2.typeOf(value) === "array" ? value : [value];
							var arr = [];
							value.each( function( a, i ){
								var idx = options.valueList.indexOf( a );
								arr.push( idx > -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":
U
unknown 已提交
1986
							text = value;
U
unknown 已提交
1987 1988 1989 1990 1991 1992 1993
							break;
						case "Calendar":
							text = value;
							break;
						default:
							text = value;
							break;
U
unknown 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
					}
				} else if ( json.type === "Label" && module.node) {
					text = module.node.get("text");
				}

				if( !text && o2.typeOf(text) !== "number" ){
					text = "";
				}

				exportData.push( text );
			}
U
unknown 已提交
2005
		}.bind(this));
U
unknown 已提交
2006
		return exportData;
U
unknown 已提交
2007
	},
U
unknown 已提交
2008
	isAvaliableField : function(json){
U
unknown 已提交
2009
		if (["Image","Button","ImageClipper","WritingBoard","Attachment","AttachmentDg","Label"].contains( json.type) )return false; //图片,附件,Label不导入导出
U
unknown 已提交
2010 2011 2012 2013
		return true;
	},
	getExcelName: function(){
		var title;
U
unknown 已提交
2014 2015
		if( this.template.json.excelName && this.template.json.excelName.code ){
			title = this.form.Macro.exec(this.template.json.excelName.code, this);
U
unknown 已提交
2016
		}else{
U
unknown 已提交
2017
			title = MWF.xApplication.process.Xform.LP.datatemplateExportDefaultName;
U
unknown 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
		}
		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);
U
tiny  
unknown 已提交
2040 2041
			} else if (json.type === "TinyMCEEditor") {
				colWidthArr.push(500);
U
unknown 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
			} 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 ) {

		var resultArr = [];
U
unknown 已提交
2064 2065 2066
		var titleArr = this.template.json.excelFieldConfig.map(function(config){
			return config.title;
		});
U
unknown 已提交
2067 2068 2069
		titleArr.push( MWF.xApplication.process.Xform.LP.validationInfor );
		resultArr.push( titleArr );

U
unknown 已提交
2070 2071 2072 2073 2074

		// this.template.lineList.each(function (line, index) {
		// 	resultArr.push( this.getLineExportData(line, index) );
		// }.bind(this));

U
unknown 已提交
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
		importedData.each( function( lineData, lineIndex ){
			var array = [];
			columnList.each( function (obj, i) {
				array.push( ( lineData[ obj.text ] || '' ).replace(/&#10;/g, "\n") );
			});
			array.push( lineData.errorTextListExcel ? lineData.errorTextListExcel.join("\n") : ""  );

			resultArr.push( array );
		}.bind(this));

U
unknown 已提交
2085 2086 2087 2088
		var colWidthArr = this.getColWidthArray();
		colWidthArr.push(300); //提示信息

		var excelName = this.getExcelName();
U
unknown 已提交
2089

U
unknown 已提交
2090 2091 2092
		var arg = {
			data : resultArr,
			colWidthArray : colWidthArr,
U
unknown 已提交
2093 2094
			title : excelName,
			withError: true
U
unknown 已提交
2095
		};
U
unknown 已提交
2096
		this.template.fireEvent("export", [arg]);
U
unknown 已提交
2097

U
unknown 已提交
2098
		new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template ).exportToExcel(
U
unknown 已提交
2099
			arg.data || resultArr,
U
unknown 已提交
2100
			arg.title || excelName,
U
unknown 已提交
2101
			arg.colWidthArray || colWidthArr,
U
unknown 已提交
2102 2103
			this.getDateIndexArray()  //日期格式列下标
		);
U
unknown 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
	}
});

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;

	},
U
unknown 已提交
2119
	isAvaliableField : function(json, module, type){
U
unknown 已提交
2120
		if (["Image","Button","ImageClipper","WritingBoard","Attachment","AttachmentDg","Label"].contains( json.type) )return false; //图片,附件,Label不导入导出
U
unknown 已提交
2121 2122 2123
		return true;
	},
	importFromExcel : function () {
U
unknown 已提交
2124 2125 2126
		var fieldArray = this.getFieldArray();
		var dateColArray = this.getDateIndexArray(); //日期列
		var orgTitleArray = this.getOrgTitleArray();
U
unknown 已提交
2127

U
unknown 已提交
2128
		new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template ).upload( dateColArray, function (data) {
U
unknown 已提交
2129 2130

			var checkAndImport = function () {
U
unknown 已提交
2131
				if( !this.checkCount(data) )return;
U
unknown 已提交
2132 2133
				if( !this.checkData( fieldArray, data ) ){
					this.openErrorDlg( fieldArray, data );
U
unknown 已提交
2134
				}else{
U
unknown 已提交
2135
					this.importData( fieldArray, data )
U
unknown 已提交
2136
				}
U
unknown 已提交
2137
				this.destroySimulateModule();
U
unknown 已提交
2138 2139
			}.bind(this);

U
unknown 已提交
2140 2141
			if( orgTitleArray.length > 0 ){
				this.listAllOrgData( orgTitleArray, data, function () {
U
unknown 已提交
2142 2143 2144 2145 2146 2147 2148 2149 2150
					checkAndImport();
				}.bind(this));
			}else{
				checkAndImport();
			}


		}.bind(this));
	},
U
unknown 已提交
2151
	destroySimulateModule: function(){
U
unknown 已提交
2152
		if( !this.simelateModuleMap )return;
U
unknown 已提交
2153 2154 2155 2156 2157 2158 2159 2160
		var keys = Object.keys(this.simelateModuleMap);
		keys.each(function (key, i) {
			var module = this.simelateModuleMap[key];
			if( module ){
				var id = module.json.id;
				if( this.form.businessData.data.hasOwnProperty(id) )delete this.form.businessData.data[id];
				delete this.simelateModuleMap[key];
			}
U
unknown 已提交
2161
		}.bind(this))
U
unknown 已提交
2162 2163 2164 2165 2166 2167
		this.simelateModuleMap = null;

		if(this.simulateNode){
			this.simulateNode.destroy();
			this.simulateNode = null;
		}
U
unknown 已提交
2168 2169
	},
	loadSimulateModule: function(){
U
unknown 已提交
2170 2171 2172
		if( this.simelateModuleMap ){
			this.destroySimulateModule();
		}
U
unknown 已提交
2173 2174 2175
		//加载模拟字段
		this.simelateModuleMap = {};
		this.simulateNode = new Element("div").inject(this.template.node);
U
unknown 已提交
2176
		this.simulateNode.hide();
U
unknown 已提交
2177 2178 2179 2180 2181
		this.simulateNode.set("html", this.template.templateHtml);
		var moduleNodes = this.form._getModuleNodes(this.simulateNode);
		moduleNodes.each(function (node) {
			if (node.get("MWFtype") !== "form") {
				var _self = this;
U
unknown 已提交
2182

U
unknown 已提交
2183 2184 2185
				var tJson = this.form._getDomjson(node);
				if( tJson && this.isAvaliableField(tJson) ){
					var json = Object.clone(tJson);
U
unknown 已提交
2186

U
unknown 已提交
2187
					var templateJsonId = json.id;
U
unknown 已提交
2188

U
unknown 已提交
2189 2190 2191 2192 2193 2194
					json.id = "dtSimulate_"+json.id;
					node.set("id", json.id);

					if (!MWF["APP" + json.type]) {
						MWF.xDesktop.requireApp("process.Xform", json.type, null, false);
					}
U
unknown 已提交
2195
					var module = new MWF["APP" + json.type](node, json, this.form);
U
unknown 已提交
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238

					this.simelateModuleMap[templateJsonId] = module;

					module.load();

				}
			}
		}.bind(this));
	},
	getFieldArray: function(){
		this.loadSimulateModule();
		var fieldArray = []; //日期格式列下标
		this.template.json.excelFieldConfig.each(function (config, i) {
			fieldArray.push({
				"text": config.title,
				"field": config.field,
				"index": i,
				"module": this.simelateModuleMap[config.field],
				"json": this.form.json.moduleList[config.field]
			})
		}.bind(this));
		return fieldArray;
	},
	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;
	},
U
unknown 已提交
2239
	parseImportedData: function(fieldArray, idata){
U
unknown 已提交
2240
		var data = [];
U
unknown 已提交
2241

U
unknown 已提交
2242
		idata.each( function( ilineData ){
U
unknown 已提交
2243 2244
			var lineData = {};

U
unknown 已提交
2245
			fieldArray.each( function (obj, i) {
U
unknown 已提交
2246 2247
				var index = obj.index;
				var module = obj.module;
U
unknown 已提交
2248
				var json = obj.json;
U
unknown 已提交
2249 2250
				var text = obj.text;

U
unknown 已提交
2251
				var d = ilineData[text] || "";
U
unknown 已提交
2252 2253

				var value;
U
unknown 已提交
2254 2255 2256 2257 2258 2259 2260 2261 2262
				if( d === "" || d === undefined || d === null ){
					value = "";
				}else{
					switch (json.type) {
						case "Org":
						case "Reader":
						case "Author":
						case "Personfield":
						case "Orgfield":
2263
							var arr = this.stringToArray(d); //空格,空格
U
unknown 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
							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;
						case "Combox":
						case "Address":
2276
							arr = this.stringToArray(d);
U
unknown 已提交
2277 2278 2279
							value = arr.length === 0  ? arr[0] : arr;
							break;
						case "Checkbox":
2280
							arr = this.stringToArray(d);
U
unknown 已提交
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
							var options = module.getOptionsObj();
							arr.each( function( a, i ){
								var idx = options.textList.indexOf( a );
								arr[ i ] = idx > -1 ? options.valueList[ idx ] : a;
							});
							value = arr.length === 1  ? arr[0] : arr;
							break;
						case "Radio":
						case "Select":
							value = d.replace(/&#10;/g,""); //换行符&#10;
							var options = module.getOptionsObj();
							var idx = options.textList.indexOf( value );
							value = idx > -1 ? options.valueList[ idx ] : value;
							break;
						case "Textarea":
							value = d.replace(/&#10;/g,"\n"); //换行符&#10;
							break;
						case "Calendar":
							value = d.replace(/&#10;/g,""); //换行符&#10;
2300
							if( value && (new Date(value).isValid()) ){
U
unknown 已提交
2301 2302 2303 2304 2305 2306 2307
								var format;
								if (!json.format){
									if (json.selectType==="datetime" || json.selectType==="time"){
										format = (json.selectType === "time") ? "%H:%M" : (Locale.get("Date").shortDate + " " + "%H:%M")
									}else{
										format = Locale.get("Date").shortDate;
									}
U
unknown 已提交
2308
								}else{
U
unknown 已提交
2309
									format = json.format;
U
unknown 已提交
2310
								}
U
unknown 已提交
2311
								value = Date.parse( value ).format( format );
U
unknown 已提交
2312
							}
U
unknown 已提交
2313 2314 2315 2316 2317
							break;
						default:
							value = d.replace(/&#10;/g,""); //换行符&#10;
							break;
					}
U
unknown 已提交
2318 2319
				}

U
unknown 已提交
2320
				lineData[ json.id ] = value;
U
unknown 已提交
2321 2322 2323

			}.bind(this));

U
unknown 已提交
2324 2325
			data.push( lineData );

U
unknown 已提交
2326 2327
		}.bind(this));

U
unknown 已提交
2328
		return data;
U
unknown 已提交
2329
	},
2330 2331 2332 2333 2334
	stringToArray: function(string){
		return string.replace(/&#10;/g,",").split(/\s*,\s*/g ).filter(function(s){
			return !!s;
		});
	},
U
unknown 已提交
2335 2336 2337 2338
	importData: function(fieldArray, idata){

		var data = this.parseImportedData(fieldArray, idata);

U
unknown 已提交
2339
		this.template.fireEvent("import", [data] );
U
unknown 已提交
2340

U
unknown 已提交
2341
		this.template.setData( data );
2342 2343 2344

		this.template.fireEvent("afterImport", [data] );

U
unknown 已提交
2345 2346
		this.template.fireEvent("change", [{lines: this.template.lineList, type : "import"}]);

U
unknown 已提交
2347 2348 2349
		this.form.notice( MWF.xApplication.process.Xform.LP.importSuccess );

	},
U
unknown 已提交
2350
	openErrorDlg : function(fieldArray, eData){
U
unknown 已提交
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
		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 +"'" )
				}
			})
U
unknown 已提交
2363
			return arr.join(" ")
U
unknown 已提交
2364 2365
		}

U
unknown 已提交
2366
		var htmlArray = ["<table "+ objectToString( this.template.json.impExpTableProperties ) +" style='"+objectToString( this.template.json.impExpTableStyles, "style" )+"'>"];
U
unknown 已提交
2367

U
unknown 已提交
2368
		var titleStyle = objectToString(this.template.json.impExpTableTitleStyles, "style");
U
unknown 已提交
2369 2370
		htmlArray.push("<tr>");
		fieldArray.each(function (obj, i) {
U
unknown 已提交
2371 2372
			htmlArray.push( "<th style='"+titleStyle+"'>"+obj.text+"</th>" );
		});
U
unknown 已提交
2373 2374
		htmlArray.push("<th style='"+titleStyle+"'> "+MWF.xApplication.process.Xform.LP.validationInfor +"</th>");
		htmlArray.push("</tr>" );
U
unknown 已提交
2375

U
unknown 已提交
2376
		var contentStyles = Object.clone( this.template.json.impExpTableContentStyles );
U
unknown 已提交
2377 2378 2379
		if( !contentStyles[ "border-bottom" ] && !contentStyles[ "border" ] )contentStyles[ "border-bottom" ] = "1px solid #eee";
		var contentStyle = objectToString( Object.merge( contentStyles, {"text-align":"left"}) , "style" );

U
unknown 已提交
2380
		eData.each( function( lineData, lineIndex ){
U
unknown 已提交
2381 2382

			htmlArray.push( "<tr>" );
U
unknown 已提交
2383
			fieldArray.each( function (obj, i) {
U
unknown 已提交
2384 2385 2386 2387 2388 2389 2390 2391
				htmlArray.push( "<td style='"+contentStyle+"'>"+ ( lineData[ obj.text ] || '' ).replace(/&#10;/g,"<br/>") +"</td>" ); //换行符&#10;
			});
			htmlArray.push( "<td style='"+contentStyle+"'>"+( lineData.errorTextList ? lineData.errorTextList.join("<br/>") : "" )+"</td>" );
			htmlArray.push( "</tr>" );

		}.bind(this));
		htmlArray.push( "</table>" );

U
unknown 已提交
2392 2393 2394 2395 2396
		var width = this.template.json.impExpDlgWidth || 1000;
		var height = this.template.json.impExpDlgHeight || 700;
		width = width.toInt();
		height = height.toInt();

U
unknown 已提交
2397 2398 2399 2400 2401 2402 2403
		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,
U
unknown 已提交
2404 2405
			"width": width,
			"height": height,
U
unknown 已提交
2406 2407 2408 2409
			"buttonList": [
				{
					"type": "exportWithError",
					"text": MWF.xApplication.process.Xform.LP.datagridExport,
U
unknown 已提交
2410
					"action": function () { _self.exportWithImportDataToExcel(fieldArray, eData); }
U
unknown 已提交
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
				},
				{
					"type": "cancel",
					"text": MWF.LP.process.button.cancel,
					"action": function () { dlg.close(); }
				}
			],
			"onPostClose": function(){
				dlg = null;
			}.bind(this)
		});

	},
U
unknown 已提交
2424 2425 2426 2427
	exportWithImportDataToExcel: function(fieldArray, eData){
		var exporter = new MWF.xApplication.process.Xform.Datatemplate.Exporter(this.template);
		exporter.exportWithImportDataToExcel(fieldArray, eData)
	},
U
unknown 已提交
2428 2429 2430 2431 2432
	checkCount: function(idata){
		var lp = MWF.xApplication.process.Xform.LP;

		var exceeded = false;
		var maxCount = this.template.json.maxCount ? this.template.json.maxCount.toInt() : 0;
2433
		if( maxCount > 0 && idata.length > maxCount )exceeded = true;
U
unknown 已提交
2434 2435 2436

		var less = false;
		var minCount = this.template.json.minCount ? this.template.json.minCount.toInt() : 0;
2437
		if( minCount > 0 && idata.length < minCount) less = true;
U
unknown 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451

		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;
	},
U
unknown 已提交
2452
	checkData : function( fieldArray, idata ){
U
unknown 已提交
2453 2454 2455 2456 2457
		var flag = true;

		var lp = MWF.xApplication.process.Xform.LP;
		var columnText =  lp.importValidationColumnText;
		var columnTextExcel = lp.importValidationColumnTextExcel;
U
unknown 已提交
2458
		var excelUtil = new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template );
U
unknown 已提交
2459

U
unknown 已提交
2460
		var parsedData = this.parseImportedData(fieldArray, idata, true);
U
unknown 已提交
2461 2462

		idata.each( function(lineData, lineIndex){
U
unknown 已提交
2463 2464 2465 2466

			var errorTextList = [];
			var errorTextListExcel = [];

U
unknown 已提交
2467 2468
			var parsedLineData = (parsedData && parsedData[lineIndex]) ? parsedData[lineIndex] : [];

U
unknown 已提交
2469
			fieldArray.each( function (obj, i) {
U
unknown 已提交
2470
				var index = obj.index;
U
unknown 已提交
2471
				var json = obj.json;
U
unknown 已提交
2472 2473 2474
				var module = obj.module;
				var text = obj.text;

U
unknown 已提交
2475 2476
				var colInfor = columnText.replace( "{n}", index+1 );
				var colInforExcel = columnTextExcel.replace( "{n}", excelUtil.index2ColName( index ) );
U
unknown 已提交
2477 2478

				var d = lineData[text] || "";
U
unknown 已提交
2479
				var parsedD = parsedLineData[json.id] || "";
U
unknown 已提交
2480

U
unknown 已提交
2481 2482 2483 2484 2485 2486 2487 2488
				if(d){

					switch (json && json.type) {
						case "Org":
						case "Reader":
						case "Author":
						case "Personfield":
						case "Orgfield":
2489
							var arr = this.stringToArray(d);
U
unknown 已提交
2490 2491 2492
							arr.each( function(d, idx){
								var obj = this.getOrgData( d );
								if( obj.errorText ){
U
unknown 已提交
2493 2494
									errorTextList.push( colInfor + obj.errorText + lp.fullstop );
									errorTextListExcel.push( colInforExcel + obj.errorText + lp.fullstop );
U
unknown 已提交
2495 2496 2497 2498
								}
							}.bind(this));
							break;
						case "Number":
2499
							if (isNaN(d)){
U
unknown 已提交
2500 2501
								errorTextList.push( colInfor + d + lp.notValidNumber + lp.fullstop );
								errorTextListExcel.push( colInforExcel + d + lp.notValidNumber + lp.fullstop );
U
unknown 已提交
2502
							}
U
unknown 已提交
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
							break;
						case "Calendar":
							if( !( isNaN(d) && !isNaN(Date.parse(d) ))){
								errorTextList.push(colInfor + d + lp.notValidDate + lp.fullstop );
								errorTextListExcel.push( colInforExcel + d + lp.notValidDate + lp.fullstop );
							}
							break;
						default:
							break;
					}
U
unknown 已提交
2513
				}
U
unknown 已提交
2514
				if (module && module.setData && json.type !== "Address"){
U
unknown 已提交
2515
					var hasError = false;
U
unknown 已提交
2516
					if(["Org","Reader","Author","Personfield","Orgfield"].contains(json.type)){
U
unknown 已提交
2517 2518
						if(o2.typeOf(parsedD)==="array" && parsedD.length){
							hasError = parsedD.some(function (item) { return item.errorText; })
U
unknown 已提交
2519 2520
						}
					}
U
unknown 已提交
2521 2522 2523 2524 2525 2526 2527 2528
					if(!hasError){
						module.setData(parsedD);
						module.validationMode();
						if (!module.validation() && module.errNode){
							errorTextList.push(colInfor + module.errNode.get("text"));
							errorTextListExcel.push( colInforExcel + module.errNode.get("text"));
							module.errNode.destroy();
						}
U
unknown 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
					}
				}
			}.bind(this));

			if(errorTextList.length>0){
				lineData.errorTextList = errorTextList;
				lineData.errorTextListExcel = errorTextListExcel;
				flag = false;
			}

		}.bind(this));

		var arg = {
			validted : flag,
U
unknown 已提交
2543
			data : idata
U
unknown 已提交
2544
		};
U
unknown 已提交
2545
		this.template.fireEvent( "validImport", [arg] );
U
unknown 已提交
2546 2547 2548

		return arg.validted;
	},
U
unknown 已提交
2549
	getOrgData : function( str ){
U
unknown 已提交
2550 2551 2552 2553
		str = str.trim();
		var flag = str.substr(str.length-2, 2);
		switch (flag.toLowerCase()){
			case "@i":
U
unknown 已提交
2554
				return this.identityMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
U
unknown 已提交
2555
			case "@p":
U
unknown 已提交
2556
				return this.personMap[str] || {"errorText":  str + MWF.xApplication.process.Xform.LP.notExistInSystem };
U
unknown 已提交
2557
			case "@u":
U
unknown 已提交
2558
				return this.unitMap[str] ||  {"errorText":  str + MWF.xApplication.process.Xform.LP.notExistInSystem };
U
unknown 已提交
2559
			case "@g":
U
unknown 已提交
2560
				return this.groupMap[str] ||  {"errorText":  str + MWF.xApplication.process.Xform.LP.notExistInSystem };
U
unknown 已提交
2561
			default:
U
unknown 已提交
2562 2563 2564 2565
				return this.identityMap[str] ||
					this.personMap[str] ||
					this.unitMap[str] ||
					this.groupMap[str] ||
U
unknown 已提交
2566 2567 2568 2569
					{"errorText":  str + MWF.xApplication.process.Xform.LP.notExistInSystem };

		}
	},
U
unknown 已提交
2570
	listAllOrgData : function (orgTitleList, iData, callback) {
U
unknown 已提交
2571 2572
		var identityList = [], personList = [], unitList = [], groupList = [];
		if( orgTitleList.length > 0 ){
U
unknown 已提交
2573
			iData.each( function( lineData, lineIndex ){
U
unknown 已提交
2574 2575 2576 2577 2578 2579
				// if( lineIndex === 0 )return;

				orgTitleList.each( function (title, index) {

					if( !lineData[title] )return;

2580
					var arr = this.stringToArray(lineData[title]);
U
unknown 已提交
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
					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;
						}
					})
2601 2602
				}.bind(this))
			}.bind(this));
U
unknown 已提交
2603 2604 2605 2606 2607 2608 2609
			var identityLoaded, personLoaded, unitLoaded, groupLoaded;
			var check = function () {
				if( identityLoaded && personLoaded && unitLoaded && groupLoaded ){
					if(callback)callback();
				}
			};

U
unknown 已提交
2610
			this.identityMap = {};
U
unknown 已提交
2611
			if( identityList.length ){
U
unknown 已提交
2612
				identityList = identityList.unique();
U
unknown 已提交
2613
				o2.Actions.load("x_organization_assemble_express").IdentityAction.listObject({ identityList : identityList }, function (json) {
U
unknown 已提交
2614
					json.data.each( function (d) { this.identityMap[ d.matchKey ] = d; }.bind(this));
U
unknown 已提交
2615 2616 2617 2618 2619 2620 2621 2622
					identityLoaded = true;
					check();
				}.bind(this))
			}else{
				identityLoaded = true;
				check();
			}

U
unknown 已提交
2623
			this.personMap = {};
U
unknown 已提交
2624
			if( personList.length ){
U
unknown 已提交
2625
				personList = personList.unique();
U
unknown 已提交
2626
				o2.Actions.load("x_organization_assemble_express").PersonAction.listObject({ personList : personList }, function (json) {
U
unknown 已提交
2627
					json.data.each( function (d) { this.personMap[ d.matchKey ] = d; }.bind(this));
U
unknown 已提交
2628 2629 2630 2631 2632 2633 2634 2635
					personLoaded = true;
					check();
				}.bind(this))
			}else{
				personLoaded = true;
				check();
			}

U
unknown 已提交
2636
			this.unitMap = {};
U
unknown 已提交
2637
			if( unitList.length ){
U
unknown 已提交
2638
				unitList = unitList.unique();
U
unknown 已提交
2639
				o2.Actions.load("x_organization_assemble_express").UnitAction.listObject({ unitList : unitList }, function (json) {
U
unknown 已提交
2640
					json.data.each( function (d) { this.unitMap[ d.matchKey ] = d; }.bind(this));
U
unknown 已提交
2641 2642 2643 2644 2645 2646 2647 2648
					unitLoaded = true;
					check();
				}.bind(this))
			}else{
				unitLoaded = true;
				check();
			}

U
unknown 已提交
2649
			this.groupMap = {};
U
unknown 已提交
2650
			if( groupList.length ){
U
unknown 已提交
2651
				groupList = groupList.unique();
U
unknown 已提交
2652
				o2.Actions.load("x_organization_assemble_express").GroupAction.listObject({ groupList : groupList }, function (json) {
U
unknown 已提交
2653
					json.data.each( function (d) { this.groupMap[ d.matchKey ] = d; }.bind(this));
U
unknown 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
					groupLoaded = true;
					check();
				}.bind(this))
			}else{
				groupLoaded = true;
				check();
			}
		}
	}
});

U
unknown 已提交
2665
MWF.xDesktop.requireApp("Template", "utils.ExcelUtils", null, false);
U
unknown 已提交
2666
MWF.xApplication.process.Xform.Datatemplate.ExcelUtils = new Class({
U
unknown 已提交
2667
	Extends: MWF.xApplication.Template.utils.ExcelUtils
U
unknown 已提交
2668
});
U
unknown 已提交
2669