forked from NaiboWang/EasySpider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowChart_CN.js
619 lines (600 loc) · 23.2 KB
/
FlowChart_CN.js
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//处理表现层
let nodeList = Array(); //所有新生成的节点全部存储在这里,并且有唯一索引号,所有的定位均通过index进行,即将图保存下来了
let root = {
index: 0, //在nodeList中的索引号
id: 0,
parentId: 0,
type: -1,
option: 0,
title: "root",
sequence: [],
parameters: {
history: 1,
tabIndex: 0,
useLoop: false, //是否使用循环中的元素
xpath: "", //xpath
wait: 0,
waitType: 0,
},
isInLoop: false, //是否处于循环内
};
nodeList.push(root);
let queue = new Array();
let actionSequence = new Array(); //存储图结构,每个元素为在nodelist里面的索引值,下面的id和pid根据此数组进行索引,然后再在nodelist里找
let nowNode = null; //存储现在所在的节点
let vueData = { nowNodeIndex: 0 }; //存储目前所在节点的索引号,不能直接使用变量而需要用对象包起来
let option = 0; //工具箱选项
let title = "";
let parameterNum = 1; //记录目前的参数个数
//处理逻辑层
let app = new Vue({
el: '#app',
data: {
list: { nl: nodeList },
index: vueData,
nodeType: 0, // 当前元素的类型
nowNode: null, // 用来临时存储元素的节点
loopType: -1, //点击循环时候用来循环选项
useLoop: false, //记录是否使用循环内元素
nowArrow: { "position": -1, "pId": 0, "num": 0 },
paras: { "parameters": [] }, //提取数据的参数列表
TClass: -1, //条件分支的条件类别
paraIndex: 0, //当前参数的index
XPaths: "", //xpath列表
},
watch: {
nowArrow: { //变量发生变化的时候进行一些操作
deep: true,
handler: function(newVal, oldVal) {
let arrlist = document.getElementsByClassName("arrow");
if (oldVal != null) {
for (let i = 0; i < arrlist.length; i++) {
if (arrlist[i].getAttribute("position") == oldVal["position"] &&
arrlist[i].getAttribute("pid") == oldVal["pId"]) {
arrlist[i].style.backgroundColor = ""; // 时刻指示现在应该插入的节点的位置
break;
}
}
}
for (let i = 0; i < arrlist.length; i++) {
if (arrlist[i].getAttribute("position") == newVal["position"] &&
arrlist[i].getAttribute("pid") == newVal["pId"]) {
arrlist[i].style.backgroundColor = "lavender"; // 时刻指示现在应该插入的节点的位置
break;
}
}
}
},
loopType: {
handler: function(newVal, oldVal) {
this.nowNode["parameters"]["loopType"] = newVal;
}
},
TClass: {
handler: function(newVal, oldVal) {
this.nowNode["parameters"]["class"] = newVal;
}
},
useLoop: {
handler: function(newVal, oldVal) {
this.nowNode["parameters"]["useLoop"] = newVal;
}
},
paras: {
handler: function(newVal, oldVal) {
this.nowNode["parameters"]["paras"] = newVal["parameters"];
}
},
},
methods: {
getCookies: function() { //获取cookies
let command = new WebSocket("ws:https://localhost:"+getUrlParam("wsport"))
command.onopen = function() {
let message = {
type: 7, //消息类型,0代表连接操作
};
this.send(JSON.stringify(message));
};
},
changeXPaths: function (XPaths){
let result = "";
for (let i = 0; i < XPaths.length; i++) {
result += XPaths[i] + "\n";
}
this.XPaths = result;
},
addPara: function() { //添加参数
this.nowNode["parameters"]["paras"].push({
"nodeType": 0,
"contentType": 0,
"relative": false,
"name": "自定义参数_" + this.nowNode["parameters"]["paras"].length.toString(),
"desc": "",
"extractType": 0,
"relativeXPath": "//body",
"recordASField": 1,
"allXPaths": [],
"exampleValues": [
{
"num": 0,
"value": "自定义字段"
}
],
"default": "",
"beforeJS": "",
"beforeJSWaitTime": 0,
"JS": "",
"paraType": "text",
"JSWaitTime": 0,
"afterJS": "",
"afterJSWaitTime": 0,
"downloadPic": 0
});
this.paraIndex = this.nowNode["parameters"]["paras"].length - 1;
},
modifyParas: function(i) { //修改第i个参数
this.paraIndex = i;
console.log(this.paras);
},
deleteParas: function(i) { //删除第i个参数
this.nowNode["parameters"]["paras"].splice(i, 1);
//如果参数删除完了,就把提取数据也删掉
if (this.nowNode["parameters"]["paras"].length == 0) {
deleteElement();
}
},
upParas: function(i) { //上移第i个参数
if (i != 0) {
let t = this.nowNode["parameters"]["paras"].splice(i, 1)[0];
this.nowNode["parameters"]["paras"].splice(i - 1, 0, t);
}
},
downParas: function(i) { //下移第i个参数
if (i != this.nowNode["parameters"]["paras"].length - 1) {
let t = this.nowNode["parameters"]["paras"].splice(i, 1)[0];
this.nowNode["parameters"]["paras"].splice(i + 1, 0, t);
}
},
getType: function(nodeType, contentType) { //根据类型得到字段名称
if (contentType == 2) {
return "InnerHTML";
} else if (contentType == 3) {
return "OuterHTML";
}
if (nodeType == 2) {
return "链接地址";
} else if (nodeType == 1) {
return "链接文本";
} else if (nodeType == 4) {
return "图片地址";
} else {
return "文本";
}
}
}
})
//深复制
function DeepClone(obj) {
if (obj === null || typeof obj !== 'object') return obj;
let cpObj = obj instanceof Array ? [] : {};
for (let key in obj) cpObj[key] = DeepClone(obj[key]);
return cpObj;
}
// 根据元素类型返回不同元素的样式
function newNode(node) {
id = node["id"];
title = node["title"];
type = node["type"];
if (type == 0) //顺序
{
return `<div class="sequence"><div class="node clk" data="${id}" dataType=${type} id = "${id}" position=${node["position"]} pId=${node["parentId"]}>
<div >
<p>${title}</p>
</div>
</div>
<p class="arrow" position=${node["position"]} data = "${id}" pId=${node["parentId"]}>↓</p></div>`;
} else if (type == 1) //循环
{
return `<div class="loop clk" data="${id}" dataType=${type} id = "${id}" position=${node["position"]} pId=${node["parentId"]}>
<p style="background:#d6d6d6;text-align:left;padding:2px">${title}</p>
<p class="arrow" position=-1 data = "${id}" pId=${id}>↓</p>
</div>
<p class="arrow" data = "${id}" position=${node["position"]} pId=${node["parentId"]}>↓</p></div>`;
} else if (type == 2) //判断
{
return `<div class="loop clk" dataType=${type} data="${id}" position=${node["position"]} pId=${node["parentId"]}>
<p style="background:#d6d6d6;text-align:left;padding:2px">${title}</p>
<p class="branchAdd" data="${id}">点击此处在最左边增加条件分支</p>
<div class="judge" id = "${id}">
</div></div>
<p class="arrow" data = "${id}" position=${node["position"]} pId=${node["parentId"]}>↓</p></div>`;
} else //判断分支
{
return `<div class="branch clk" dataType=${type} data="${id}" position=${node["position"]} pId=${node["parentId"]}>
<p style="background:#d6d6d6;text-align:left;padding:2px">${title}</p>
<p data = "${id}" class="arrow" position=-1 pId=${id}>↓</p>
<div id = "${id}">
</div></div>`;
}
}
function elementMousedown(e) {
if (e.button == 2) //右键点击
{
if (nowNode != null) {
nowNode.style.borderColor = "skyblue";
}
nowNode = this;
vueData.nowNodeIndex = actionSequence[this.getAttribute("data")];
this.style.borderColor = "blue";
handleElement(); //处理元素
}
e.stopPropagation(); //防止冒泡
}
function branchMouseDown(e) {
if (e.button == 2) //右键点击
{
let judgeId = this.getAttribute('data');
let l = nodeList.length;
let t = {
index: l,
id: 0,
parentId: 0,
type: 3,
option: 10,
title: "条件分支",
sequence: [],
isInLoop: false,
};
addParameters(t)
nodeList.push(t);
nodeList[actionSequence[judgeId]]["sequence"].splice(0, 0, t.index);
refresh();
app._data.nowArrow = { "position": -1, "pId": t["id"], "num": 0 };
$("#" + t["id"]).click();
}
e.stopPropagation(); //防止冒泡
}
function arrowMouseDown(e) {
if (e.button == 2) //右键点击
{
if (option != 0) {
app._data.nowArrow = { "position": this.getAttribute('position'), "pId": this.getAttribute('pId'), "num": 0 };
}
toolBoxKernel(e);
}
}
//增加分支点击事件
function branchClick(e) {
let judgeId = this.getAttribute('data');
let l = nodeList.length;
let t = {
index: l,
id: 0,
parentId: 0,
type: 3,
option: 10,
title: "条件分支",
sequence: [],
isInLoop: false,
};
addParameters(t);
nodeList.push(t);
nodeList[actionSequence[judgeId]]["sequence"].splice(0, 0, t.index);
refresh();
app._data.nowArrow = { "position": -1, "pId": t["id"], "num": 0 };
$("#" + t["id"]).click();
e.stopPropagation(); //防止冒泡
}
//元素点击事件
function elementClick(e) {
if (nowNode != null) {
nowNode.style.borderColor = "skyblue";
}
nowNode = this;
vueData.nowNodeIndex = actionSequence[this.getAttribute("data")];
this.style.borderColor = "blue";
handleElement(); //处理元素
e.stopPropagation(); //防止冒泡
}
//箭头点击事件
function arrowClick(e) {
if (option != 0) {
app._data.nowArrow = { "position": this.getAttribute('position'), "pId": this.getAttribute('pId'), "num": 0 };
}
toolBoxKernel(e);
}
//增加元素函数
function addElement(op, para) {
option = op;
if (option == 1) { //打开网页选项
title = "打开网页";
} else {
title = $(".options")[option - 1].innerHTML; //获取新增操作名称
}
toolBoxKernel(null, para);
}
// 工具箱操作函数
function toolBoxKernel(e, para = null) {
if (option == 13) { //调整锚点
// let tarrow = DeepClone(app.$data.nowArrow);
// refresh();
// app._data.nowArrow =tarrow;
} else if (option == 11) { //复制操作
if (nowNode == null) {
e.stopPropagation(); //防止冒泡
} else if (nowNode.getAttribute("dataType") > 0) {
alert("循环和判断、条件分支不可复制!");
e.stopPropagation(); //防止冒泡
} else {
let position = parseInt(nowNode.getAttribute('position'));
let pId = nowNode.getAttribute('pId');
let tt = nodeList[nodeList[actionSequence[pId]]["sequence"][position]]; //在相应位置添加新元素
t = DeepClone(tt); //浅复制元素
let l = nodeList.length;
t.index = l;
nodeList.push(t);
let position2 = parseInt(app._data.nowArrow['position']);
let pId2 = app._data.nowArrow['pId'];
nodeList[actionSequence[pId2]]["sequence"].splice(position2 + 1, 0, t.index); //在相应位置添加新元素
refresh(); //重新渲染页面
app._data.nowArrow = { "position": t["position"], "pId": t["parentId"], "num": 0 };
$("#" + t["id"]).click(); //复制后点击复制后的元素
e.stopPropagation(); //防止冒泡
}
} else if (option == 10) { //剪切操作
if (nowNode == null) {
e.stopPropagation(); //防止冒泡
} else if ($(nowNode).is(".branch")) {
alert("判断分支不可移动!");
e.stopPropagation(); //防止冒泡
} else {
let position = parseInt(nowNode.getAttribute('position'));
let pId = nowNode.getAttribute('pId');
let position2 = parseInt(app._data.nowArrow['position']);
let pId2 = app._data.nowArrow['pId'];
let id = nowNode.getAttribute('data');
let pidt = pId2;
let move = true;
console.log(pidt, id);
while (pidt != 0) {
if (pidt == id) {
move = false;
break;
}
pidt = nodeList[actionSequence[pidt]]["parentId"];
}
if (move) //如果自己要移动到自己节点里就不允许移动
{
let element = nodeList[actionSequence[pId]]["sequence"].splice(position, 1); //在相应位置删除元素
if (pId == pId2 && position < position2) //如果要移动的位置属于同一层并且是从前往后移动,注意需要控制数组插入位置向前错位
{
position2--;
}
console.log(element);
nodeList[actionSequence[pId2]]["sequence"].splice(position2 + 1, 0, element[0]); //在相应位置添加新元素
refresh(); //重新渲染页面
console.log(nodeList[element[0]]);
app._data.nowArrow = { "position": nodeList[element[0]]["position"], "pId": nodeList[element[0]]["parentId"], "num": 0 };
$("#" + nodeList[element[0]]["id"]).click();
} else {
alert("自己不能移动到自己的节点里!");
}
e.stopPropagation(); //防止冒泡
}
} else if (option > 0) { //新增操作
let l = nodeList.length;
let t = {
id: 0,
index: l,
parentId: 0,
type: 0,
option: option,
title: title,
sequence: [],
isInLoop: false,
};
nodeList.push(t);
if (option == 8) //循环
{
t["type"] = 1;
} else if (option == 9) //判断
{
t["type"] = 2;
// 增加两个分支
let nt = {
id: 0,
parentId: 0,
index: l + 1,
type: 3,
option: 10,
title: "条件分支",
sequence: [],
isInLoop: false,
};
let nt2 = {
id: 0,
parentId: 0,
index: l + 2,
type: 3,
option: 10,
title: "条件分支",
sequence: [],
isInLoop: false,
};
t["sequence"].push(nt.index);
t["sequence"].push(nt2.index);
nodeList.push(nt)
nodeList.push(nt2);
addParameters(nt); //增加选项的默认参数
addParameters(nt2); //增加选项的默认参数
}
let position = parseInt(app._data.nowArrow['position']);
let pId = app._data.nowArrow['pId'];
nodeList[actionSequence[pId]]["sequence"].splice(position + 1, 0, t.index); //在相应位置添加新元素
refresh(); //重新渲染页面
//下面是确定添加元素之后下一个要插入的节点的位置
app._data.nowArrow = { "position": t["position"], "pId": t["parentId"], "num": 0 };
addParameters(t); //增加选项的默认参数
if (para != null) {
modifyParameters(t, para);
}
if (option == 8) //循环情况下应插入在循环里面
{
app._data.nowArrow = { "position": -1, "pId": t["id"], "num": 0 };
$("#" + t["id"]).click();
} else if (option == 9) //判断插入到第一个判断条件中
{
app._data.nowArrow = { "position": -1, "pId": nt["id"], "num": 0 };
$("#" + nt["id"]).click();
} else {
$("#" + t["id"]).click();
}
if (e != null)
e.stopPropagation(); //防止冒泡
option = 0;
return t;
}
option = 0;
}
$(".options").mousedown(function() {
option = parseInt(this.getAttribute("data"));
title = this.innerHTML;
if (option >= 10 && option <= 12 && (nowNode == null || nowNode.getAttribute("id") == 0)) {
alert("目前未选中元素");
} else if (option == 12) {
deleteElement();
$(".options")[12].click();
}
});
function bindEvents() {
// 清空原来的listener然后再添加新的listener
//以下绑定了左右键的行为
let rect = document.getElementsByClassName('clk');
for (let i = 0, rule; rule = rect[i++];) {
rule.removeEventListener('mousedown', elementMousedown);
rule.addEventListener('mousedown', elementMousedown);
rule.removeEventListener('click', elementClick);
rule.addEventListener('click', elementClick);
}
let arr = document.getElementsByClassName('arrow');
for (let i = 0, rule; rule = arr[i++];) {
rule.removeEventListener('click', arrowClick);
rule.addEventListener('click', arrowClick);
rule.removeEventListener('mousedown', arrowMouseDown);
rule.addEventListener('mousedown', arrowMouseDown);
}
let branch = document.getElementsByClassName('branchAdd');
for (let i = 0, rule; rule = branch[i++];) {
rule.removeEventListener('click', branchClick);
rule.addEventListener('click', branchClick);
rule.removeEventListener('mousedown', branchMouseDown);
rule.addEventListener('mousedown', branchMouseDown);
}
}
//重新画图
function refresh(nowArrowReset = true) {
$("#0").empty();
$("#0").append(`<div style="border-radius: 50%;width: 40px;height: 40px;border:solid;border-color:seagreen;margin:5px auto;background-color:lightcyan;margin-top:20px">
<p style="font-size: 24px!important;text-align: center;margin-left: 6px;font-family:'Times New Roman'">▶</p>
</div>
<p id="firstArrow" class="arrow" position=-1 pId=0>↓</p>`);
actionSequence.splice(0);
queue.splice(0);
let idd = 1;
queue.push(0);
actionSequence.push(0);
while (queue.length != 0) {
let nd = queue.shift(); //取出父元素并建立对子元素的链接
for (let i = 0; i < nodeList[nd].sequence.length; i++) {
nodeList[nodeList[nd].sequence[i]].parentId = nodeList[nd].id;
nodeList[nodeList[nd].sequence[i]]["position"] = i;
nodeList[nodeList[nd].sequence[i]].id = idd++;
//检测元素是否位于循环内
if (nodeList[nd].option == 8 || nodeList[nd].isInLoop) {
nodeList[nodeList[nd].sequence[i]].isInLoop = true;
} else {
nodeList[nodeList[nd].sequence[i]].isInLoop = false;
}
queue.push(nodeList[nd].sequence[i]);
actionSequence.push(nodeList[nd].sequence[i]);
}
}
if (nowArrowReset) //如果要重置锚点位置
{
app._data.nowArrow = { "position": nodeList[0].sequence.length - 1, "pId": 0, "num": 0 }; //设置默认要添加的位置是元素流程最开头处
}
//第一个元素不渲染
for (let i = 1; i < actionSequence.length; i++) {
let parentId = nodeList[actionSequence[i]]["parentId"];
$("#" + parentId).append(newNode(nodeList[actionSequence[i]]));
}
bindEvents();
}
function deleteElement() {
if (nowNode.getAttribute("id") == 0) {
alert("当前未选中元素!"); //root
return;
}
// if (nodeList[actionSequence[nowNode.getAttribute("data")]]["option"] == 1) {
// alert("打开网页操作不可删除!");
// return;
// }
let position = parseInt(nowNode.getAttribute('position'));
let pId = nowNode.getAttribute('pId');
let tnode = nodeList[actionSequence[pId]]["sequence"].splice(position, 1); //在相应位置删除元素
//循环的标记已经被删除的元素,因为删除循环后,循环内的元素也会
let queue = new Array();
queue.push(tnode[0]);
while (queue.length > 0) {
let index = queue.shift();
nodeList[index]["id"] = -1; //标记服务已被删除
for (let i = 0; i < nodeList[index]["sequence"].length; i++) {
queue.push(nodeList[index]["sequence"][i]);
}
}
app._data["nowNode"] = null;
app._data["nodeType"] = 0;
vueData.nowNodeIndex = 0;
if (nowNode.getAttribute("datatype") == 3) { //如果删掉的是条件分支的话
pId = nowNode.parentNode.parentNode.getAttribute('pId');
position = nowNode.parentNode.parentNode.getAttribute('position');
}
app.$data.nowArrow = { position: position - 1, "pId": pId, "num": 0 }; //删除元素后锚点跳转到当前元素的上一个节点
refresh(false); //重新渲染页面
nowNode = null; //取消选择
}
document.oncontextmenu = function() {
return false;
} //屏蔽右键菜单
//删除元素
document.onkeydown = function(e) {
if (nowNode != null && e.keyCode == 46) {
// if (confirm("确定要删除元素吗?")) {
deleteElement();
// }
} else { //ctrl+s保存服务
let currKey = 0;
currKey = e.keyCode || e.which || e.charCode;
if (currKey == 83 && (e.ctrlKey || e.metaKey)) {
$('#save').click();
return true;
} else if (currKey == 116) {
location.reload();
} else if (currKey == 123) {
console.log("打开devtools")
let command = new WebSocket("ws:https://localhost:"+getUrlParam("wsport"))
command.onopen = function() {
let message = {
type: 6, //消息类型,0代表连接操作
};
this.send(JSON.stringify(message));
};
}
}
}
function inputDelete(e) {
if (e.keyCode == 46) {
e.stopPropagation(); //输入框按delete应该正常运行
//Electron中如果有alert或者confirm,执行后会卡死输入框,所以最好不要用
}
}