Datatemplate.js 34.5 KB
Newer Older
U
unknown 已提交
1 2 3 4 5 6
/**
 * 数据模板数据结构.
 * @typedef {Object} DatatemplateData
 * @property {Array} data - 数据网格列表数据
 * @property {Object} total - 统计数据
 * @example
U
unknown 已提交
7
	[ //数据模板数据条目
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 47 48 49
        {
            "org": [{
                "distinguishedName": "张三@bf007525-99a3-4178-a474-32865bdddec8@I",
                "id": "bf007525-99a3-4178-a474-32865bdddec8",
                "name": "张三",
                "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
                "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
                "unitLevelName": "兰德纵横/市场营销部",
                "unitName": "市场营销部"
            }],
            "org_1": [{
                "distinguishedName": "张三@bf007525-99a3-4178-a474-32865bdddec8@I",
                "id": "bf007525-99a3-4178-a474-32865bdddec8",
                "name": "张三",
                "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
                "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
                "unitLevelName": "兰德纵横/市场营销部",
                "unitName": "市场营销部"
            }, {
                "distinguishedName": "李四@bf007525-99a3-4178-a474-32865bdddec8@I",
                "id": "bf007525-99a3-4178-a474-32865bdddec8",
                "name": "李四",
                "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
                "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
                "unitLevelName": "兰德纵横/市场营销部",
                "unitName": "市场营销部"
            }],
            "number": "111",
            "textfield": "杭州",
            "attachment": [
                {
                    "activityName": "拟稿",
                    "extension": "jpg",
                    "id": "9514758e-9e28-4bfe-87d7-824f2811f173",
                    "lastUpdateTime": "2020-12-09 21:48:03",
                    "length": 452863.0,
                    "name": "111.jpg",
                    "person": "李四@lisi@P"
                }
            ]
        },
        ...
U
unknown 已提交
50
    ]
U
unknown 已提交
51 52 53 54 55 56
 */
MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
/** @class Datatemplate 数据模板组件。
 * @example
 * //可以在脚本中获取该组件
 * //方法1:
U
unknown 已提交
57
 * var datatemplate = this.form.get("name"); //获取组件
U
unknown 已提交
58
 * //方法2
U
unknown 已提交
59
 * var datatemplate = this.target; //在组件事件脚本中获取
U
unknown 已提交
60 61 62 63 64
 * @extends MWF.xApplication.process.Xform.$Module
 * @o2category FormComponents
 * @o2range {Process|CMS}
 * @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 78
			/**
			 * 添加条目时触发。通过this.event可以获取对应的tr。
U
unknown 已提交
79
			 * @event MWF.xApplication.process.Xform.Datatemplate#addLine
U
unknown 已提交
80 81 82 83
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
			 * 删除条目前触发。通过this.event可以获取对应的tr。
U
unknown 已提交
84
			 * @event MWF.xApplication.process.Xform.Datatemplate#deleteLine
U
unknown 已提交
85 86 87 88
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
			 * 删除条目后触发。
U
unknown 已提交
89
			 * @event MWF.xApplication.process.Xform.Datatemplate#afterDeleteLine
U
unknown 已提交
90 91 92 93
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
			/**
			 * 导出excel的时候触发,this.event指向导出的数据,您可以通过修改this.event来修改数据。
U
unknown 已提交
94
			 * @event MWF.xApplication.process.Xform.Datatemplate#export
U
unknown 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
			 * @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 已提交
110
			 * @event MWF.xApplication.process.Xform.Datatemplate#validImport
U
unknown 已提交
111 112 113 114 115 116 117 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 : [
			 *  	   {
			 *  	 	"姓名" : "张三",
			 *  	 	"性别" : "男",
			 *  	 	"学历" : "大学本科",
			 *  	    "专业" : "计算机",
			 *  	    "出生日期" : "aa01-1-2",
			 *  	 	"毕业日期" : "2019-9-2",
			 *  	 	"errorTextList" : [
			 *  	 	    "第5列:aa01-1-2不是正确的日期格式。"
			 *  	 	] //校验出的错误信息,如果改行数据正确,则无该字段
			 *  	 }
			 *  	 ...
			 *     ], //导入的数据
			 *     "validted" : true  //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
			 * }
			 */
			/**
U
unknown 已提交
133 134
			 * 在导入excel,数据校验成功将要设置回数据网格的时候触发,this.event指向整理过的导入数据,格式见{@link DatatemplateData}。
			 * @event MWF.xApplication.process.Xform.Datatemplate#import
U
unknown 已提交
135 136
			 * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
			 */
U
unknown 已提交
137
			"moduleEvents": ["queryLoad","postLoad","load","afterLoad","addLine", "deleteLine", "afterDeleteLine","export", "import", "validImport"]
U
unknown 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		},

		initialize: function(node, json, form, options){
			this.node = $(node);
			this.node.store("module", this);
			this.json = json;
			this.form = form;
			this.field = true;
		},

		_loadUserInterface: function(){
			this.fireEvent("queryLoad");

			this.editModules = [];
			this.node.setStyle("overflow-x", "auto");
			this.node.setStyle("overflow-y", "hidden");

U
unknown 已提交
155
			this.editable = !(this.readonly || (this.json.isReadonly === true));
U
unknown 已提交
156 157 158 159
			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 已提交
160
			this.getRelativeId();
U
unknown 已提交
161

U
unknown 已提交
162 163 164 165 166 167 168
			//允许导入
			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 已提交
169
			debugger;
U
unknown 已提交
170
			this.data = this._getValue();
U
unknown 已提交
171 172 173

			this.lineList = [];

U
unknown 已提交
174 175 176
			//this.data为object的时候才有值
			// this.lineMap = {};

U
unknown 已提交
177 178 179 180
			// this.totalModules = [];
			this._loadStyles();

			//获取html模板和json模板
U
unknown 已提交
181
			this.getTemplate();
U
unknown 已提交
182 183

			//设置节点外的操作:添加、删除、导入、导出
U
unknown 已提交
184 185 186
			this.setOuterActionsEvents();

			debugger;
U
unknown 已提交
187 188 189 190

			//隐藏节点
			this.node.getChildren().hide();

U
unknown 已提交
191 192 193 194 195
			this._loadDataTemplate(function(){
				// this._loadImportExportAction();
				this.fireEvent("postLoad");
				this.fireEvent("load");
			}.bind(this));
U
unknown 已提交
196
		},
U
unknown 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209
		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 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222
		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 已提交
223
			this.node.set(this.json.properties);
U
unknown 已提交
224
		},
U
unknown 已提交
225
		setOuterActionsEvents: function(){
U
unknown 已提交
226

U
unknown 已提交
227
			//判断不在数据模板中,但是在表单内的Id
U
unknown 已提交
228 229 230
			var getModules = function (idList) {
				var list = [];
				idList.each( function (id) {
U
unknown 已提交
231
					if( !this.templateJson.hasOwnProperty(id) && this.form.all[id] ){
U
unknown 已提交
232 233 234 235
						list.push( this.form.all[id] );
					}
				}.bind(this));
				return list;
U
unknown 已提交
236 237 238
			}.bind(this);

			this.bindEvent = function () {
U
unknown 已提交
239
				this.addActionList = getModules( [].concat(this.addActionIdList, this.outerAddActionIdList) );
U
unknown 已提交
240 241
				this.addActionList.each( function (module) {
					module.node.addEvents({"click": function(e){
U
unknown 已提交
242 243 244
						this._addLine(e);
					}.bind(this)});
					if( !this.editable )module.node.hide();
U
unknown 已提交
245
				}.bind(this));
U
unknown 已提交
246

U
unknown 已提交
247
				this.deleteActionList = getModules( [].concat( this.outerDeleteActionIdList ) );
U
unknown 已提交
248 249
				this.deleteActionList.each( function (module) {
					module.node.addEvents({"click": function(e){
U
unknown 已提交
250 251 252
						this._deleteSelectedLine(e);
					}.bind(this)});
					if( !this.editable )module.node.hide();
U
unknown 已提交
253
				}.bind(this));
U
unknown 已提交
254

U
unknown 已提交
255 256
				this.selectAllList = getModules( this.outerSelectAllIdList );
				this.selectAllList.each( function (module) {
U
unknown 已提交
257
					// module.setData(""); //默认不选中
U
unknown 已提交
258
					module.node.addEvents({"click": function(e){
U
unknown 已提交
259 260 261
						this._checkSelectAll(e);
					}.bind(this)});
					if( !this.editable )module.node.hide();
U
unknown 已提交
262
				}.bind(this));
U
unknown 已提交
263
				this.selectAllSelector = this.selectAllList[0];
U
unknown 已提交
264 265 266
				if(this.selectAllSelector){
					this.unselectAll();
				}
U
unknown 已提交
267

U
unknown 已提交
268 269 270
				this.importActionList = getModules( this.importActionIdList );
				this.importActionList.each( function (module) {
					module.node.addEvents({"click": function(e){
U
unknown 已提交
271 272 273
						this.importFromExcel();
					}.bind(this)});
					if( !this.editable )module.node.hide();
U
unknown 已提交
274
				}.bind(this));
U
unknown 已提交
275

U
unknown 已提交
276 277 278 279 280 281
				this.exportActionList = getModules( this.exportActionIdList );
				this.exportActionList.each( function (module) {
					module.node.addEvents({"click": function(e){
							this.exportToExcel();
						}.bind(this)})
				}.bind(this));
U
unknown 已提交
282 283

				this.fireEvent("afterLoad");
U
unknown 已提交
284 285 286
				//加载完成以后,删除事件
				this.form.removeEvent("afterModulesLoad", this.bindEvent );
			}.bind(this);
U
unknown 已提交
287

U
unknown 已提交
288 289
			//去要表单的所有组件加载完成以后再去获取外部组件
			this.form.addEvent("afterModulesLoad", this.bindEvent );
U
unknown 已提交
290 291 292
		},
		_getValue: function(){
			if (this.moduleValueAG) return this.moduleValueAG;
U
unknown 已提交
293
			var value = this._getBusinessData();
U
unknown 已提交
294 295
			if (!value){
				if (this.json.defaultData && this.json.defaultData.code) value = this.form.Macro.exec(this.json.defaultData.code, this);
U
unknown 已提交
296
				if (!value.then) if (o2.typeOf(value)==="object") value = [value];
U
unknown 已提交
297
			}
U
unknown 已提交
298
			return value || [];
U
unknown 已提交
299 300 301 302 303
		},
		getValue: function(){
			return this._getValue();
		},

U
unknown 已提交
304
		_loadDataTemplate: function(callback){
U
unknown 已提交
305 306
			var p = o2.promiseAll(this.data).then(function(v){
				this.data = v;
U
unknown 已提交
307
				// if (o2.typeOf(this.data)=="object") this.data = [this.data];
U
unknown 已提交
308
				this._loadLineList(callback);
U
unknown 已提交
309 310 311 312 313 314 315 316 317 318 319 320
				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 已提交
321
		_loadLineList: function(callback){
U
unknown 已提交
322 323 324 325 326 327 328 329 330 331 332 333
			if(o2.typeOf(this.data)==="object"){ //区段合并后显示
				var index = 0;
				Object.each(this.data, function (list, sectionKey) {
					debugger;
					list.each(function(data, idx){
						var div = new Element("div").inject(this.node);
						var line = this._loadLine(div, data, index, this.editable, idx, sectionKey);
						this.lineList.push(line);
						index++;
					}.bind(this));
				}.bind(this))
			}else if(this._getBusinessData() && this.data){
U
unknown 已提交
334
				this.data.each(function(data, idx){
U
unknown 已提交
335
					var div = new Element("div").inject(this.node);
U
unknown 已提交
336 337
					var line = this._loadLine(div, data, idx );
					this.lineList.push(line);
U
unknown 已提交
338
				}.bind(this));
U
unknown 已提交
339
			}else if( this.editable ){ //如果是第一次编辑
U
unknown 已提交
340 341 342 343 344 345
				var count = this.json.defaultCount ? this.json.defaultCount.toInt() : 0;
				for( var i=0; i<count; i++ ){
					var div = new Element("div").inject(this.node);
					var line = this._loadLine(div, {}, i );
					this.lineList.push(line);
				}
U
unknown 已提交
346 347 348
			}
			if (callback) callback();
		},
U
unknown 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362
		isMax : function(){
			var maxCount = this.json.maxCount ? this.json.maxCount.toInt() : 0;
			if( this.editable && maxCount > 0 ) {
				if( this.lineList.length >= maxCount )return true;
			}
			return false;
		},
		isMin : function(){
			var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
			if( this.editable && minCount > 0 ) {
				if( this.lineList.length <= minCount )return true;
			}
			return false;
		},
U
unknown 已提交
363
		_loadLine: function(container, data, index, isEdited, indexInSection, sectionKey){
U
unknown 已提交
364
			var line = new MWF.xApplication.process.Xform.Datatemplate.Line(container, this, data, {
U
unknown 已提交
365 366
				index : index,
				indexText : (index+1).toString(),
U
unknown 已提交
367 368
				indexInSection: indexInSection,
				sectionKey: sectionKey,
U
unknown 已提交
369 370 371
				isEdited : typeOf(isEdited) === "boolean" ? isEdited : this.editable
			});
			line.load();
U
unknown 已提交
372 373 374
			return line;
		},
		_addLine: function(ev){
U
unknown 已提交
375
			if( this.isMax() ){
U
unknown 已提交
376
				this.form.notice("最多允许添加"+this.json.maxCount+"","info");
U
unknown 已提交
377 378
				return false;
			}
U
unknown 已提交
379 380 381
			var index = this.lineList.length;
			var div = new Element("div").inject(this.node);
			var line = this._loadLine(div, {}, index );
U
unknown 已提交
382
			this.lineList.push(line);
U
unknown 已提交
383
			this.fireEvent("addLine", [line, ev]);
U
unknown 已提交
384
		},
U
unknown 已提交
385 386
		_insertLine: function(ev, beforeLine){
			debugger;
U
unknown 已提交
387
			if( this.isMax() ){
U
unknown 已提交
388
				this.form.notice("最多允许添加"+this.json.maxCount+"","info");
U
unknown 已提交
389 390
				return false;
			}
U
unknown 已提交
391 392
			//使用数据驱动
			var index = beforeLine.options.index+1;
U
unknown 已提交
393 394 395 396 397 398 399 400 401 402

			var data;
			if( beforeLine.options.sectionKey ){ //区段合并后的数据
				data = this.getData();
				var sectionData = data[beforeLine.options.sectionKey];
				sectionData.splice(beforeLine.options.indexInSection+1, 0, {});
			}else{
				data = this.getData();
				data.splice(index, 0, {});
			}
U
unknown 已提交
403 404
			this.setData( data );
			this.fireEvent("addLine",[this.lineList[index], ev]);
U
unknown 已提交
405
		},
U
unknown 已提交
406
		_deleteSelectedLine: function(ev){
U
unknown 已提交
407
			debugger;
U
unknown 已提交
408 409 410 411 412 413
			var selectedLine = this.lineList.filter(function (line) { return line.selected; });
			if( selectedLine.length === 0 ){
				this.form.notice("请先选择条目","info");
				return false;
			}
			var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
U
unknown 已提交
414
			if( minCount > 0 ){
U
unknown 已提交
415 416
				if( this.lineList.length - selectedLine.length < minCount ){
					this.form.notice("最少需要保留"+minCount+"项,删除后的条目小于需保留的条目,请检查","info");
U
unknown 已提交
417 418 419
					return false;
				}
			}
U
unknown 已提交
420 421 422 423 424
			var _self = this;
			this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, "确定要删除选中的条目", 300, 120, function(){
				selectedLine.each(function(line){
					_self.fireEvent("deleteLine", [line]);

U
unknown 已提交
425 426 427 428 429 430 431 432 433 434
					var data;
					if( line.options.sectionKey ){ //区段合并后的数据
						data = _self.getData();
						var sectionData = data[line.options.sectionKey];
						sectionData.splice(line.options.indexInSection, 1);
					}else{
						var index = line.options.index;
						data = _self.getData();
						data.splice(index, 1);
					}
U
unknown 已提交
435 436 437 438 439 440 441 442 443 444 445 446
					_self.setData( data );

					_self.fireEvent("afterDeleteLine");
				});

				this.close();

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

		},
U
unknown 已提交
447
		_deleteLine: function(ev, line){
U
unknown 已提交
448
			if( this.isMin() ){
U
unknown 已提交
449
				this.form.notice("请最少保留"+this.json.minCount+"","info");
U
unknown 已提交
450 451
				return false;
			}
U
unknown 已提交
452 453 454
			var _self = this;
			this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, MWF.xApplication.process.Xform.LP.deleteDatagridLine, 300, 120, function(){
				_self.fireEvent("deleteLine", [line]);
U
unknown 已提交
455

U
unknown 已提交
456
				//使用数据驱动
U
unknown 已提交
457 458 459 460 461 462 463 464 465 466
				var data;
				if( line.options.sectionKey ){ //区段合并后的数据
					data = _self.getData();
					var sectionData = data[line.options.sectionKey];
					sectionData.splice(line.options.indexInSection, 1);
				}else {
					var index = line.options.index;
					data = _self.getData();
					data.splice(index, 1);
				}
U
unknown 已提交
467 468
				_self.setData( data );
				this.close();
U
unknown 已提交
469

U
unknown 已提交
470 471 472 473
				_self.fireEvent("afterDeleteLine");
			}, function(){
				this.close();
			}, null, null, this.form.json.confirmStyle);
U
unknown 已提交
474
		},
U
unknown 已提交
475
		_checkSelectAll: function () {
U
unknown 已提交
476
			debugger;
U
unknown 已提交
477 478 479 480 481 482 483
			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 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
			this.selected = selected;
			this.lineList.each(function (line) {
				this.selected ? line.select() : line.unselect();
			}.bind(this))
		},
		selectAll: function(){
			this.selected = true;
			if(this.selectAllSelector)this.selectAllSelector.setData(this.json.outerSelectAllSelectedValue);
		},
		unselectAll: function(){
			debugger;
			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 已提交
510 511
			}
		},
U
unknown 已提交
512 513 514 515

		editValidation: function(){
			var flag = true;
			this.editModules.each(function(field, key){
U
unknown 已提交
516
				if (field.json.type!=="sequence" && field.validationMode ){
U
unknown 已提交
517 518 519 520 521 522 523
					field.validationMode();
					if (!field.validation()) flag = false;
				}
			}.bind(this));
			return flag;
		},

U
unknown 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537

		_afterLoaded: function(){
		},
		/**
		 * @summary 重置数据网格的值为默认值或置空。
		 *  @example
		 * this.form.get('fieldId').resetData();
		 */
		resetData: function(){
			this.setData(this._getValue());
		},
		/**当参数为Promise的时候,请查看文档: {@link  https://www.yuque.com/o2oa/ixsnyt/ws07m0|使用Promise处理表单异步}<br/>
		 * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
		 * @summary 为数据网格赋值。
U
unknown 已提交
538
		 * @param data{DatatemplateData|Promise|Array} 必选,数组或Promise.
U
unknown 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
		 * @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 已提交
572
				this.data = v;
U
unknown 已提交
573
				// if (o2.typeOf(data)==="object") data = [data];
U
unknown 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
				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){
			// if( typeOf( data ) === "object" && typeOf(data.data) === "array"  ){
			this._setBusinessData(data);
U
unknown 已提交
590 591 592 593 594
			this.data = data;

			if (this.data){
				for (var i=0; i<this.lineList.length; i++){
					this.lineList[i].clear();
U
unknown 已提交
595 596
				}
			}
U
unknown 已提交
597 598 599

			this.lineList = [];
			this._loadDataTemplate()
U
unknown 已提交
600 601 602 603 604 605 606 607 608 609 610 611
		},
		/**
		 * @summary 判断数据网格是否为空.
		 * @example
		 * if( this.form.get('fieldId').isEmpty() ){
		 *     this.form.notice('至少需要添加一条数据', 'warn');
		 * }
		 * @return {Boolean} 是否为空
		 */
		isEmpty: function(){
			var data = this.getData();
			if( !data )return true;
U
unknown 已提交
612 613
			if( o2.typeOf( data ) === "array" ){
				return data.data.length === 0;
U
unknown 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
			}
			return false;
		},

		/**
		 * 在脚本中使用 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')无法获取到组件。
		 * @summary 获取数据网格数据.
		 * @example
		 * var data = this.form.get('fieldId').getData();
		 *@example
		 *  //如果无法确定表单上是否有组件,需要判断
		 *  var data;
		 *  if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
		 *      data = this.form.get('fieldId').getData();
		 *  }else{
		 *      data = this.data['fieldId']; //直接从数据中获取字段值
		 *  }
		 *  @example
		 *  //使用Promise
		 *  var field = this.form.get("fieldId");
		 *  var promise = new Promise(function(resolve, reject){ //发起异步请求
		 *    var oReq = new XMLHttpRequest();
		 *    oReq.addEventListener("load", function(){ //绑定load事件
		 *      resolve(oReq.responseText);
		 *    });
		 *    oReq.open("GET", "/data.json"); //假设数据存放在data.json
		 *    oReq.send();
		 *  });
		 *  promise.then( function(){
		 *    var data = field.getData(); //此时由于异步请求已经执行完毕,getData方法获得data.json的值
		 * })
		 *  field.setData( promise );
U
unknown 已提交
652
		 * @return {DatatemplateData}
U
unknown 已提交
653 654
		 */
		getData: function(){
U
unknown 已提交
655
			if (this.editable!==false){
U
unknown 已提交
656
				debugger;
U
unknown 已提交
657 658 659 660 661 662 663 664 665 666 667
				// 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 : [];
				return this._getBusinessData();
U
unknown 已提交
668 669 670 671
			}else{
				return this._getBusinessData();
			}
		},
U
unknown 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
		_getSectionKey: function(){
			if (this.json.section!=="yes"){
				return "";
			}else {
				switch (this.json.sectionBy){
					case "person":
						return layout.desktop.session.user.id;
					case "unit":
						return (this.form.businessData.task) ? this.form.businessData.task.unit : "";
					case "activity":
						return (this.form.businessData.work) ? this.form.businessData.work.activity : "";
					case "splitValue":
						return (this.form.businessData.work) ? this.form.businessData.work.splitValue : "";
					case "script":
						if( this.json.sectionByScript && this.json.sectionByScript.code){
							return this.form.Macro.exec(this.json.sectionByScript.code, this) || "";
						}else{
							return "";
						}
					default:
						return "";
				}
			}
		},
U
unknown 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
		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();
				if( typeOf(n)==="object" && JSON.stringify(n) === JSON.stringify({data:[]}) )n = "";
				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;
		},
		/**
		 * @summary 根据组件的校验设置进行校验。
		 *  @param {String} [routeName] - 可选,路由名称.
		 *  @example
		 *  if( !this.form.get('fieldId').validation() ){
		 *      return false;
		 *  }
		 *  @return {Boolean} 是否通过校验
		 */
		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;
			if (flag.toString()!="true"){
				this.notValidationMode(flag);
				return false;
			}
			return true;
		},
		getAttachmentRandomSite: function(){
			var i = (new Date()).getTime();
			return this.json.id+i;
		}

	});

MWF.xApplication.process.Xform.Datatemplate.Line =  new Class({
	Implements: [Options, Events],
	options: {
		isEdited : true,
		index : 0,
U
unknown 已提交
868 869 870
		indexText : "0",
		indexInSection: null, //区段合并后数据的data[sectionKey][index]
		sectionKey: null //区段合并后数据的key
U
unknown 已提交
871 872 873 874 875 876 877 878 879 880
	},
	initialize: function (node, template, data, options) {

		this.setOptions(options);

		this.node = node;
		this.template = template;
		this.data = data;
		this.form = this.template.form;

U
unknown 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894
		this.modules = [];
		this.all = {};

		this.fields = [];
		this.allField = {};

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


U
unknown 已提交
895
	},
U
unknown 已提交
896
	load: function(){
U
unknown 已提交
897 898
		this.node.set("html", this.template.templateHtml);
		var moduleNodes = this.form._getModuleNodes(this.node);
U
unknown 已提交
899 900 901
		//this.options.sectionKey 为区段合并后的数据
		//this.template._getSectionKey() 为当前在区段状态下
		var sectionKey = this.options.sectionKey || this.template._getSectionKey();
U
unknown 已提交
902 903 904 905
		moduleNodes.each(function (node) {
			if (node.get("MWFtype") !== "form") {
				var _self = this;

U
unknown 已提交
906 907 908 909
				var tJson = this.form._getDomjson(node);
				if( tJson ){
					var json = Object.clone(tJson);

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

U
unknown 已提交
912 913 914 915 916 917 918 919 920 921
					var templateJsonId = json.id;

					var id;
					var index = this.options.indexInSection || this.options.index;
					if( sectionKey ){
						id = this.template.json.id + ".." + sectionKey + ".."+ index + ".." + json.id;
					}else{
						id = this.template.json.id + ".." + index + ".." + json.id;
					}
					json.id = id;
U
unknown 已提交
922
					node.set("id", id);
U
unknown 已提交
923

U
unknown 已提交
924
					if( json.type==="Attachment" || json.type==="AttachmentDg" ){
U
unknown 已提交
925
						json.type = "AttachmentDg";
U
unknown 已提交
926
						json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
U
unknown 已提交
927 928
					}

U
unknown 已提交
929 930 931 932
					if (this.form.all[id]) this.form.all[id] = null;
					if (this.form.forms[id])this.form.forms[id] = null;

					var module = this.form._loadModule(json, node, function () {});
U
unknown 已提交
933 934
					this.form.modules.push(module);

U
unknown 已提交
935 936
					this.modules.push(module);
					this.all[id] = module;
U
unknown 已提交
937

U
unknown 已提交
938
					if (module.field) {
U
unknown 已提交
939 940 941
						if(this.data.hasOwnProperty(templateJsonId)){
							module.setData(this.data[templateJsonId]);
						}
U
unknown 已提交
942 943 944
						this.allField[id] = module;
						this.fields.push( module );
					}
U
unknown 已提交
945

U
unknown 已提交
946
					this.setEvents(module, templateJsonId);
U
unknown 已提交
947 948 949

				}
			}
U
unknown 已提交
950 951
		}.bind(this));
	},
U
unknown 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
	getAttachmentSite: function(json, templateJsonId, sectionKey){
		//确保site最长为64,否则后台会报错

		var index = this.options.indexInSection || this.options.index;

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

		var maxLength;
		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;
		}

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

		return templateId + sectionId + baseSite;

		// if( json.site ){
		// 	if( sectionKey ){
		// 		site = this.template.json.id + "." + sectionKey + "."+this.options.index + "." + json.site;
		// 	}else{
		// 		site = this.template.json.id + "."+this.options.index + "." + json.site;
		// 	}
		// }else{
		// 	if( sectionKey ){
		// 		site = this.template.json.id + "." + sectionKey + "."+this.options.index + "." + json.id;
		// 	}else{
		// 		site = this.template.json.id + "."+this.options.index + "." + json.id;
		// 	}
		// }
		// json.site = site;
	},
U
unknown 已提交
991 992 993 994
	setEvents: function (module, id) {
		if( this.template.addActionIdList.contains( id )){
			this.addActionList.push( module );
			module.node.addEvent("click", function (ev) {
U
unknown 已提交
995
				this.template._insertLine( ev, this )
U
unknown 已提交
996
			}.bind(this))
U
unknown 已提交
997
			if( !this.template.editable )module.node.hide();
U
unknown 已提交
998
		}
U
unknown 已提交
999 1000 1001 1002 1003 1004

		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 已提交
1005
			if( !this.template.editable )module.node.hide();
U
unknown 已提交
1006 1007
		}

U
unknown 已提交
1008 1009
		if( this.template.selectorId === id){
			this.selector = module;
U
unknown 已提交
1010
			// module.setData(""); //默认不选择
U
unknown 已提交
1011 1012 1013
			module.node.addEvent("click", function (ev) {
				this.checkSelect();
			}.bind(this))
U
unknown 已提交
1014 1015
			if( !this.template.editable )module.node.hide();
			this.unselect();
U
unknown 已提交
1016
		}
U
unknown 已提交
1017

U
unknown 已提交
1018 1019 1020 1021 1022 1023 1024 1025
		if( this.template.sequenceIdList.contains(id)){
			this.sequenceNodeList.push( module );
			if(this.form.getModuleType(module) === "label"){
				module.node.set("text", this.options.indexText );
			}else{
				module.set( this.options.indexText );
			}
		}
U
unknown 已提交
1026

U
unknown 已提交
1027 1028 1029
		//???
		// if( this.template.importActionIdList.contains(id))this.importActionList.push( module );
		// if( this.template.exportActionIdList.contains(id))this.exportActionList.push( module );
U
unknown 已提交
1030

U
unknown 已提交
1031 1032
	},
	checkSelect: function () {
U
unknown 已提交
1033 1034 1035 1036 1037 1038
		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 已提交
1039
		}
U
unknown 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
		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 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	},
	clear: function () { //把module清除掉
		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.destroy();
	},
	getData: function () {
		var data = {};
		for( var key in this.allField){
U
unknown 已提交
1076
			var module = this.allField[key];
U
unknown 已提交
1077
			var id = key.split("..").getLast();
U
unknown 已提交
1078
			if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
U
unknown 已提交
1079 1080 1081 1082
				data[id] = module._getBusinessData();
			}else{
				data[id] = module.getData();
			}
U
unknown 已提交
1083 1084
		}
		return data;
U
unknown 已提交
1085 1086
	}
});