-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.html
More file actions
130 lines (111 loc) · 2.87 KB
/
demo.html
File metadata and controls
130 lines (111 loc) · 2.87 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
<!DOCTYPE html>
<html>
<head>
<title>ArrayDiff Demo</title>
<meta charset="utf8"/>
<style>
body {
font-family: sans-serif;
margin: 1em;
}
p {
line-height: 1.5;
margin: 2em 0;
}
table {
border-collapse: collapse;
line-height: 2;
}
th {
padding: 0 1em 0 0;
text-align: left;
}
input {
display: block;
font-family: inherit;
font-size: inherit;
height: 2em;
margin: 0;
padding: 0 0.5em;
width: 16em;
}
button {
display: block;
margin: 1em 0;
width: 100%;
}
td {
border: 1px solid #000;
min-width: 2em;
padding: 0;
text-align: center;
}
.added {background-color: #cfc}
.removed {background-color: #fcc}
.old .added {color: #9c9}
.new .removed {color: #c99}
</style>
<script src="ArrayDiffElement.js"></script>
<script src="ArrayDiff.js"></script>
<script>
window.onload = function() {
var table = document.getElementsByTagName('table')[0],
newInput = document.getElementById('new'),
oldInput = document.getElementById('old');
function clearTable() {
var cells = document.getElementsByTagName('td');
while (cells.length > 0) {
cells[0].parentNode.removeChild(cells[0]);
}
}
function addCell(row, content, className) {
var element = document.createElement('td');
element.textContent = content;
element.className = className;
table.rows[row].appendChild(element);
}
function getDiff() {
clearTable();
var arrayDiff = new ArrayDiff(newInput.value, oldInput.value);
for (var i = 0; i < arrayDiff.diff.length; i++) {
if (arrayDiff.diff[i].added()) {
addCell(0, '∅', 'added');
addCell(1, '+', 'added');
addCell(2, arrayDiff.diff[i].item, 'added');
}
else if (arrayDiff.diff[i].removed()) {
addCell(0, arrayDiff.diff[i].item, 'removed');
addCell(1, '−', 'removed');
addCell(2, '∅', 'removed');
}
else {
addCell(0, arrayDiff.diff[i].item, '');
addCell(1, '↓', '');
addCell(2, arrayDiff.diff[i].item, '');
}
}
}
newInput.onkeyup = getDiff;
oldInput.onkeyup = getDiff;
getDiff();
};
</script>
</head>
<body>
<h1>ArrayDiff Demo</h1>
<p>ArrayDiff computes the difference between two JavaScript arrays (as well as any other object that uses numeric subscripts, such as a string or DOMNodeList) by finding the longest common subsequence between them. The result can be used to visualize the transformation of one array to another, as demonstrated below.</p>
<table>
<tr class="old">
<th><label for="old">Old</label></th>
<th><input type="text" id="old" value="nematode knowledge"/></th>
</tr>
<tr>
<th colspan="2"></th>
</tr>
<tr class="new">
<th><label for="new">New</label></th>
<th><input type="text" id="new" value="empty bottle"/></th>
</tr>
</table>
</body>
</html>