-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
diff_switch.js
74 lines (74 loc) · 3.85 KB
/
diff_switch.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
var enableDiffSwitch = function(moduleConfig) {
var diff_table;
var diff_div = document.getElementById('diff');
var diff_switch = document.createElement('a');
diff_switch.className = 'diff-switch';
diff_switch.text = moduleConfig.i18n.diffSideBySide;
diff_switch.href = '#';
diff_switch.addEventListener('click', function(event) {
event.preventDefault();
if (!document.getElementById('diff-table')) {
diff_table = document.createElement('table');
diff_table.id = 'diff-table';
var previous_tag_name, diff_row;
Array.prototype.forEach.call(diff_div.children[0].children, function(item, i) {
if (!diff_row || diff_row.children.length == 2 || item.children[0].tagName == "SPAN") {
if (diff_row && diff_row.children.length == 1) {
if (previous_tag_name == "INS") {
diff_row.insertBefore(document.createElement('td'), diff_row.children[0]);
} else {
diff_row.appendChild(document.createElement('td'));
}
}
diff_row = document.createElement('tr');
diff_table.appendChild(diff_row);
previous_tag_name = "";
}
if (!previous_tag_name || item.children[0].tagName == "SPAN" || (item.children[0].tagName == "DEL" && previous_tag_name == "INS") || (item.children[0].tagName == "INS" && previous_tag_name == "DEL")) {
var diff_col = document.createElement('td');
diff_col.innerHTML = item.innerHTML;
diff_col.className = 'diff-col-' + item.children[0].tagName.toLowerCase();
diff_row.appendChild(diff_col);
if (item.children[0].tagName == "SPAN") {
diff_row.appendChild(diff_col.cloneNode(true));
}
} else if (previous_tag_name == item.children[0].tagName) {
if (previous_tag_name == "INS") {
diff_row.insertBefore(document.createElement('td'), diff_row.children[0]);
} else {
diff_row.appendChild(document.createElement('td'));
}
var diff_col = document.createElement('td');
diff_col.innerHTML = item.innerHTML;
diff_col.className = 'diff-col-' + item.children[0].tagName.toLowerCase();
diff_row = document.createElement('tr');
diff_row.appendChild(diff_col);
diff_table.appendChild(diff_row);
}
previous_tag_name = item.children[0].tagName;
});
if (diff_row && diff_row.children.length == 1) {
if (previous_tag_name == "INS") {
diff_row.insertBefore(document.createElement('td'), diff_row.children[0]);
} else {
diff_row.appendChild(document.createElement('td'));
}
}
diff_div.parentNode.appendChild(diff_table);
diff_div.style = 'display: none';
diff_switch.text = moduleConfig.i18n.diffList;
diff_switch.className = 'diff-switch diff-switch-list';
} else if (diff_table.style.length) {
diff_div.style = 'display: none';
diff_table.style = '';
diff_switch.text = moduleConfig.i18n.diffList;
diff_switch.className = 'diff-switch diff-switch-list';
} else {
diff_div.style = '';
diff_table.style = 'display: none';
diff_switch.text = moduleConfig.i18n.diffSideBySide;
diff_switch.className = 'diff-switch';
}
});
diff_div.parentNode.insertBefore(diff_switch, diff_div);
}