DataSet_wev8.js 3.98 KB
define(['mUtil', "Component", "mApi/other"], function (mUtil, Component) {
	var comp = function (options) {

		Component.super(this, options);

		this.type = "DataSet";

		var vm = this.viewModel = {};

		var valueObj = {}, _ds = {}, logObj = {};
		this.mounted = function () {
			var that = this;
			var $abbr = that.$el;
			var loadByTrigger = $abbr.attr("data-initialized");
			$abbr.attr("data-initialized", true);
			require(["lazyImgHelper"]);
			
			if(vm.apiConfig){
				var d = $.Deferred();
				require(["apiHelper"], function(apiHelper){
					apiHelper.callApi(vm.apiConfig, {}, function(result){
						d.resolve();
						var formats = vm.apiConfig.response.formats;
						var datas = formats.DATAS ? mUtil.getKeyValue(formats.DATAS, result) : result;
						vm.name && (valueObj[vm.name] = datas);
						loadByTrigger && that.refreshDataSetVars();
					}, function(errMsg){
						d.resolve();
					});
				});
				return d;
			}else{
				var url = mUtil.getActionUrl(this.type, {action:"parseData", "mec_id": this.id}, this.pageid);
				return mUtil.getJSON(url, function(result){
					valueObj = result.data.value;
					logObj = result.data.log;
					if(logObj) {
						mUtil.log(logObj.logNode, logObj.content, logObj.status);
					}
					loadByTrigger && that.refreshDataSetVars();
				});
			}
		};
		
		this.replace = function(text, original){
			if(!valueObj){return text;}

			for(var name in valueObj){
				var regExp = new RegExp("{("+name+"(\\.(\\w+(\\[\\d+\\])?))+)((\\[id\\])|(\\[text\\]))?}", "g");
				
				var regmatchStrs = text.match(regExp);
			    if(!regmatchStrs) continue;
				
				regmatchStrs.map(function(str){
					if(str.indexOf("[id]")> -1) text = text.replace(str,str.replace("[id]", "_original"));
					if(str.indexOf("[text]")> -1) text = text.replace(str,str.replace("[text]", ""));
				});

			    var data = valueObj[name];
                if(mUtil.isArray(data)){
                    data = data[0] || {};
                }
				if(vm.apiConfig){
					var matchArr, matchDeep = 50, count = 0, dataObj = {};
					dataObj[name] = data;
					try{
						while(matchArr = regExp.exec(text)){
						    var exprValue = eval("dataObj."+matchArr[1]);
						    if(mUtil.isObject(exprValue) || mUtil.isArray(exprValue)){
							    exprValue = JSON.stringify(exprValue);
							}
							!mUtil.isEmpty(exprValue) && (text = text.replace(matchArr[0], exprValue));
							if(++count > matchDeep) break;
						}
					}catch(e){
						mUtil.console.error(e);
					}
				}else{
					for(var k in data){//original为true:表单来源浏览框、附件、选择框在$d使用原始id数据替换
						var datak = original ? data[k + "_original"] || data[k] : data[k];
                        !mUtil.isEmpty(datak) && (text = text.replace(new RegExp("{"+name+"\\."+k+"}", "gi"), mUtil.replaceAttachUrl(datak)));
	                }
				}
				text = text.replace(regExp, "");
            }
			
			return text;
		};

		this.refreshDataSetVars = function() {
			var that = this;
			var needParseVar = false,
				$page = that.$container;

			$("wev-dataset[data-key^='" + vm.name + ".']", $page).each(function() {
				var $ds = $(this);
				var v = that.replace("{" + $ds.attr("data-key") + "}");
				(v != $ds.html()) && $ds.html(v);
			});
			
			$("wev-dataset[data-key*='{" + vm.name +"__ds__']", $page).each(function() {
				var key = $(this).attr("data-key").replace(/__ds__/g, ".").replace(/'/g, "\"");
				var v = mUtil.replaceValAndVarParser("$" + that.replace(key) + ";");
				$(this).html(v);
				needParseVar = true;
			});
			
			$("wev-dataset[data-key='html']", $page).each(function() {
				var $ds = $(this);
				var datahtm = $ds.attr("data-html").replace(/__ds__/g, ".").replace(/'/g, "\"");
				var v = that.replace(datahtm);
				if(v.indexOf("$m.") != -1){
					v = mUtil.replaceValAndVarParser(v);
					needParseVar = true;
				}
				(v != $ds.html()) && $ds.html(v);
			});
			
			mUtil.renderVarParser(needParseVar);
		};
		this.getData = function(){
		    return valueObj[vm.name];
		};
	};

	return Component.init(comp);
});