dynamicParameterTree.js
13.7 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
// 右侧的属性表格
var pGridObj;
//当前选中的节点
var selectedNode;
//用于保存已修改的参数集合,在重新加载参数树及提交成功后清空
var changedKeyValues = new Array();
//当前选中的行
var currentRowIndex = null;
var isDebug = false;
// 属性树的列定义
var myColumns = [[
{ field: "name", title: "参数名", width: 100 },
{ field: "value", title: "值", width: 150, formatter: parameterValueFormatter, styler: parameterStyleFormatter },
{ field: "info", title: "说明", width: 150 }
]];
$().ready(function () {
pGridObj = $("#pgrid");
// 加载XML参数,并显示树状结构
loadParameterXml(showParameterTree);
// 保存参数
$("#saveAllChanged").click(submitAllChanges);
});
//加载参数树
function loadParameterXml(onloadSuccessCallback) {
var url = "/do/trafficController/configVal?t=" + new Date().getTime();
var dataUrl = "/do/trafficController/configKey?t=" + new Date().getTime();
if (isDebug) {
url = "xml/VasconfigInfo.xml";
dataUrl = "xml/VasconfigVal.xml";
}
$.get(url, null, function (xml) {
//解释XML
var xmlDoc;
try {
xmlDoc = $.parseXML(xml);
} catch (e) {
$.messager.alert("错误", "解释参数XML错误!(XML格式不正确)", "error");
return;
}
//加载存储设置值的XML
$.ajax(dataUrl, {
async: true,
dataType: "text",
error: function (jqXHR, status, error) {
$.messager.alert("错误", "获取参数值XML出错!", "error");
},
success: function (data, status, jqXHR) {
try {
var valueXml = $.parseXML(data);
} catch (e) {
$.messager.alert("错误", "解释参数XML错误!(XML格式不正确)", "error");
return;
}
if (onloadSuccessCallback) {
onloadSuccessCallback(xmlDoc, valueXml);
}
}
});
}, "text");
}
/**
* 显示参数树
* @param {XmlDocument} xmlDoc
* @param {XmlDocument} valueXml
*/
function showParameterTree(xmlDoc, valueXml) {
var jsonData = convertXmlToJson(xmlDoc, valueXml);
$("#ptree").tree("loadData", jsonData);
}
//参数值格式化函数
function parameterValueFormatter(value, row, index) {
var valueType = row.type;
switch (valueType) {
case "3":
var data = row.editor.options.data;
for (var i = 0; i < data.length; i++) {
if (data[i].id == value)
return data[i].text;
}
return value;
//布尔型,1->开启,0->关闭
case "4":
return value == "1" ? "是" : "否";
default:
return value;
}
}
function parameterStyleFormatter(value, row, index) {
var valueType = row.type;
switch (valueType) {
//布尔型,1->显示为绿色,0->显示为红色
case "4":
return value == "1" ? "color:green" : "color:red";
default:
return "";
}
}
function clickTreeNode(node) {
selectedNode = node;//保存当前选中的节点
if (node.gridData) {
pGridObj.propertygrid("loadData", node.gridData);
}
}
//结果编辑参数
function onEndEditParameter(index, row, changes) {
if (changes && changes.value != null && changes.value != undefined) {
if (window.console) {
console.log(selectedNode.attr.path + "/" + row.name + "=" + changes.value);
}
var key = selectedNode.attr.path + "/" + row.name + "#" + row.type;
//替换'/'为'\\'
key = key.replace(/\//g, "\\");
changedKeyValues[key] = changes.value;//保存参数
}
}
//点击属性表格行
function onClickPropertyRow(index, row) {
currentRowIndex = index;
//开始编辑表格
$("#pgrid").propertygrid("beginEdit", index);
}
//提交所有的修改
function submitAllChanges() {
if (currentRowIndex) {
$("#pgrid").propertygrid("unselectRow", currentRowIndex);
}
setTimeout(submitAllChangesToServer, 200);
}
//提交修改到服务器
function submitAllChangesToServer() {
var dataString = "";
for (var key in changedKeyValues) {
if (dataString.length > 0)
dataString = dataString + "&" + encodeURIComponent(key) + "=" + changedKeyValues[key];
else
dataString = dataString + encodeURIComponent(key) + "=" + changedKeyValues[key];
}
if (dataString.length == 0) {
$.messager.alert("提示", "无需要保存的修改!", "info");
return;
}
//添加securityToken
var securityToken = document.getElementsByName("-xsrf-");
if (securityToken.length > 0) {
dataString += "&-xsrf-=" + securityToken[0].value;
} else {
alert("securityToken为空!不能提交!");
return;
}
//提交到后台接口进行保存
// var saveParameterUrl = "/settings/advanced/save";
var saveParameterUrl = "/do/trafficController/saveConfig";
$.post(saveParameterUrl, dataString, function (data) {
if (!data.error) {
$.messager.alert("结果", "保存失败!原因:" + data.feedback.Msg, "error");
} else {
$.messager.alert("结果", "保存成功!", "info");
//保存成功,清空上次修改的参数
changedKeyValues = new Array();
}
}, "json");
}
//解释参数树为Tree组件要求的Json格式
function convertXmlToJson(xml, xmlValue) {
var jsonData = [];
//如果XMl的节点不为空
var rootNode = $(xml).find("root");
if (rootNode.length > 0) {
var idgetter = getIdGetter();
var treeNode = convertToTreeNode(rootNode[0], idgetter, "");
bindXmlNodeToTreeNode(treeNode, rootNode[0], idgetter, xmlValue);
//添加到Json数据
jsonData = treeNode.children;
}
return jsonData;
}
//用于获取累加的树结点Id
function getIdGetter() {
var id = 1;
return function () {
return id++;
};
}
//绑定Xml结点到Tree结点
function bindXmlNodeToTreeNode(parentTreeNode, xmlNode, idGetter, valueXml) {
var childNodes = getXmlChildNodes(xmlNode);
for (var i = 0; i < childNodes.length; i++) {
var currentNode = childNodes[i];
//判断结点是否是叶节点(把属于右侧的参数拆分出去)
var isEndNode = isEndNodeFun(currentNode);
var treeNode;//当前节点对应的TreeNode
if (isEndNode) {
//把子节点格式化为propertiesGrid的Json格式
var nodePath = parentTreeNode.attr.path + "/" + currentNode.nodeName;
parentTreeNode.addGridData(getGridData(currentNode, valueXml, nodePath));
} else {
treeNode = convertToTreeNode(currentNode, idGetter, parentTreeNode.attr.path);
//循环添加所有的子节点
bindXmlNodeToTreeNode(treeNode, currentNode, idGetter, valueXml);
parentTreeNode.children.push(treeNode);
}
}
}
//转换为PropertyGrid需要的格式
function getGridData(currentNode, valueXml, nodePath) {
var childNodes = getXmlChildNodes(currentNode);
var pType = childNodes[0].text ? childNodes[0].text : childNodes[0].textContent;
var pInfo = childNodes[1].text ? childNodes[1].text : childNodes[1].textContent;
var pDefault = childNodes[2].text ? childNodes[2].text : childNodes[2].textContent;
var pValue = getValueFromValueXml(valueXml, nodePath);
//对数值进行格式化
pDefault = getValidateValue(pType, pDefault);
pValue = getValidateValue(pType, pValue);
pValue = pValue ? pValue : pDefault;//如果当前节点的值没有改变,则取默认值
var pRange = childNodes[3].text ? childNodes[3].text : childNodes[3].textContent;
return {
name: currentNode.nodeName,
value: pValue,
defaultValue: pDefault,
type: pType,
info: pInfo,
group: "default",
editor: getEditor(pType, pRange)
};
}
//从另一个值Xml中获取已经改变的值
function getValueFromValueXml(valueXml, nodePath) {
//使用遍历的方式去查找元素
var isFound = true;
var nodes = nodePath.split("/");
var tempNode = valueXml;
for (var i = 0; i < nodes.length; i++) {
tempNode = getDirectChildByTagName(tempNode, nodes[i]);
if (!tempNode) {
isFound = false;
break;
}
}
if (isFound)
return tempNode.text ? tempNode.text : tempNode.textContent;
else
return null;
}
//根据节点的类型,返回合适的编辑器
/*
pType:
0 - Int
1 - Float
2 - String
3 - Enum 枚举值在 Range字段内
4 - Boolean
5 - Time
6 - Undefined
*/
function getEditor(pType, pRange) {
var editor = {
type: null,
options: {}
};
//最小值,最大值
var min = null;
var max = null;
if (pRange) {
var rangePart = getNumRange(pRange);
min = rangePart[0];
max = rangePart[1];
}
switch (pType) {
case "0":
editor.type = "numberspinner";
if (min)
editor.options.min = parseInt(min);
if (max)
editor.options.max = parseInt(max);
break;
case "1":
editor.type = "numberbox";
if (min)
editor.options.min = parseFloat(min);
if (max)
editor.options.max = parseFloat(max);
editor.options.precision = 4;//浮点数默认4位小数
break;
case "3":
//枚举类型
editor.type = "combobox";
editor.options = {
valueField: "id",
textField: "text",
data: getComboboxData(pRange)
};
break;
case "4":
editor.type = "checkbox";
editor.options.on = 1;
editor.options.off = 0;
break;
case "5":
editor.type = "my97";
editor.options = getTimeOptions(pRange);
break;
case "2":
case "6":
default:
editor.type = "text";
break;
}
return editor;
function getComboboxData(pRangeString) {
var enumRegex = /(-?\d+)-(([0-9a-zA-Z\u4e00-\u9faf]+)(&)?)?/g;//枚举类型的正则表达式
var comboboxData = [];
var match = enumRegex.exec(pRangeString);
while (match != null) {
comboboxData.push({
id: match[1],
text: match.length >= 4 ? match[3] : ""
});
match = enumRegex.exec(pRangeString);
}
return comboboxData;
}
function getNumRange(pRangeString) {
var numRangeRegex = /(-?([0-9]+\.)?[0-9]*)-(([0-9]+\.)?[0-9]*)/g; // 数字范围类型的正则表达式
var data = [];
var match = numRangeRegex.exec(pRangeString);
if (match != null) {
data.push(match[1]);
data.push(match[3]);
}
return data;
}
function getTimeOptions(pRangeString) {
var options = {};
var timeRangeParts;
if (pRange) {
timeRangeParts = pRangeString.split("-");
if (timeRangeParts.length == 2) {
options.maxDate = timeRangeParts[0].split(":")[1].replace(/_/g, ":");
options.minDate = timeRangeParts[1].split(":")[1].replace(/_/g, ":");
options.dateFmt = "HH:mm:ss";
}
}
return options;
}
}
//格式Xml中的值为合法的格式
function getValidateValue(type, value) {
if (value) {
switch (type) {
case "5":
value = value.split(":")[1].replace(/_/g, ":");
break;
default:
}
}
return value;
}
//是否是参数树的叶节点
function isEndNodeFun(currentNode) {
var childNodes = getXmlChildNodes(currentNode);
if (currentNode && childNodes && childNodes.length > 0
&& childNodes[0].nodeName == "AttribType") {
return true;
} else {
return false;
}
}
//获取直系子节点
function getDirectChildByTagName(node, tagName) {
var nodes = getXmlChildNodes(node);
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeName == tagName)
return nodes[i];
}
return null;
}
//获取XML节点的子节点列表,兼容IE及非IE
function getXmlChildNodes(xmlNode) {
//IE下是childNodes,非IE下是children
if (xmlNode.childNodes) {
var currentNode;
//遍历所有的子节点
var validateNodeArray = new Array();
for (var i = 0; i < xmlNode.childNodes.length; i++) {
currentNode = xmlNode.childNodes[i];
if (currentNode.nodeName == "#text") {
continue;
}
validateNodeArray.push(currentNode);
}
return validateNodeArray;
} else {
return xmlNode.children;
}
}
//把一个XMl结点转为TreeNode
function convertToTreeNode(xmlNode, idGetter, parentName) {
return {
id: idGetter(),
text: xmlNode.nodeName,
children: [],
attr: {
path: function () {
if (parentName)
return parentName + "/" + xmlNode.nodeName; //保存根节点到当前节点的路径
else {
return xmlNode.nodeName;
}
}()
},
gridData: {
total: 0,
rows: []
},
addGridData: function (data) {
this.gridData.total++;
this.gridData.rows.push(data);
}
};
}