DataSet_wev8.js
3.98 KB
1
2
3
4
5
6
7
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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);
});