forked from benjamine/jsondiffpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.js
265 lines (236 loc) · 8.23 KB
/
html.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
var base = require('./base');
var BaseFormatter = base.BaseFormatter;
var HtmlFormatter = function HtmlFormatter() {};
HtmlFormatter.prototype = new BaseFormatter();
HtmlFormatter.prototype.typeFormattterErrorFormatter = function(context, err) {
context.out('<pre class="jsondiffpatch-error">' + err + '</pre>');
};
HtmlFormatter.prototype.formatValue = function(context, value) {
context.out('<pre>' + JSON.stringify(value, null, 2) + '</pre>');
};
HtmlFormatter.prototype.formatTextDiffString = function(context, value) {
var lines = this.parseTextDiff(value);
context.out('<ul class="jsondiffpatch-textdiff">');
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
context.out('<li>' +
'<div class="jsondiffpatch-textdiff-location">' +
'<span class="jsondiffpatch-textdiff-line-number">' +
line.location.line +
'</span>' +
'<span class="jsondiffpatch-textdiff-char">' +
line.location.chr +
'</span>' +
'</div>' +
'<div class="jsondiffpatch-textdiff-line">');
var pieces = line.pieces;
for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
var piece = pieces[pieceIndex];
context.out('<span class="jsondiffpatch-textdiff-' + piece.type + '">' +
piece.text + '</span>');
}
context.out('</div></li>');
}
context.out('</ul>');
};
var adjustArrows = function jsondiffpatchHtmlFormatterAdjustArrows(node) {
node = node || document;
var getElementText = function(el) {
return el.textContent || el.innerText;
};
var eachByQuery = function(el, query, fn) {
var elems = el.querySelectorAll(query);
for (var i = 0, l = elems.length; i < l; i++) {
fn(elems[i]);
}
};
var eachChildren = function(el, fn) {
for (var i = 0, l = el.children.length; i < l; i++) {
fn(el.children[i], i);
}
};
eachByQuery(node, '.jsondiffpatch-arrow', function(arrow) {
var arrowParent = arrow.parentNode;
var svg = arrow.children[0],
path = svg.children[1];
svg.style.display = 'none';
var destination = getElementText(arrowParent.querySelector('.jsondiffpatch-moved-destination'));
var container = arrowParent.parentNode;
var destinationElem;
eachChildren(container, function(child) {
if (child.getAttribute('data-key') === destination) {
destinationElem = child;
}
});
if (!destinationElem) {
return;
}
try {
var distance = destinationElem.offsetTop - arrowParent.offsetTop;
svg.setAttribute('height', Math.abs(distance) + 6);
arrow.style.top = (-8 + (distance > 0 ? 0 : distance)) + 'px';
var curve = distance > 0 ?
'M30,0 Q-10,' + Math.round(distance / 2) + ' 26,' + (distance - 4) :
'M30,' + (-distance) + ' Q-10,' + Math.round(-distance / 2) + ' 26,4';
path.setAttribute('d', curve);
svg.style.display = '';
} catch (err) {
return;
}
});
};
HtmlFormatter.prototype.rootBegin = function(context, type, nodeType) {
var nodeClass = 'jsondiffpatch-' + type +
(nodeType ? ' jsondiffpatch-child-node-type-' + nodeType : '');
context.out('<div class="jsondiffpatch-delta ' + nodeClass + '">');
};
HtmlFormatter.prototype.rootEnd = function(context) {
context.out('</div>' + (context.hasArrows ?
('<script type="text/javascript">setTimeout(' +
adjustArrows.toString() +
',10);</script>') : ''));
};
HtmlFormatter.prototype.nodeBegin = function(context, key, leftKey, type, nodeType) {
var nodeClass = 'jsondiffpatch-' + type +
(nodeType ? ' jsondiffpatch-child-node-type-' + nodeType : '');
context.out('<li class="' + nodeClass + '" data-key="' + leftKey + '">' +
'<div class="jsondiffpatch-property-name">' + leftKey + '</div>');
};
HtmlFormatter.prototype.nodeEnd = function(context) {
context.out('</li>');
};
/* jshint camelcase: false */
HtmlFormatter.prototype.format_unchanged = function(context, delta, left) {
if (typeof left === 'undefined') {
return;
}
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, left);
context.out('</div>');
};
HtmlFormatter.prototype.format_movedestination = function(context, delta, left) {
if (typeof left === 'undefined') {
return;
}
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, left);
context.out('</div>');
};
HtmlFormatter.prototype.format_node = function(context, delta, left) {
// recurse
var nodeType = (delta._t === 'a') ? 'array' : 'object';
context.out('<ul class="jsondiffpatch-node jsondiffpatch-node-type-' + nodeType + '">');
this.formatDeltaChildren(context, delta, left);
context.out('</ul>');
};
HtmlFormatter.prototype.format_added = function(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, delta[0]);
context.out('</div>');
};
HtmlFormatter.prototype.format_modified = function(context, delta) {
context.out('<div class="jsondiffpatch-value jsondiffpatch-left-value">');
this.formatValue(context, delta[0]);
context.out('</div>' +
'<div class="jsondiffpatch-value jsondiffpatch-right-value">');
this.formatValue(context, delta[1]);
context.out('</div>');
};
HtmlFormatter.prototype.format_deleted = function(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, delta[0]);
context.out('</div>');
};
HtmlFormatter.prototype.format_moved = function(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatValue(context, delta[0]);
context.out('</div><div class="jsondiffpatch-moved-destination">' + delta[1] + '</div>');
// draw an SVG arrow from here to move destination
context.out(
/*jshint multistr: true */
'<div class="jsondiffpatch-arrow" style="position: relative; left: -34px;">\
<svg width="30" height="60" style="position: absolute; display: none;">\
<defs>\
<marker id="markerArrow" markerWidth="8" markerHeight="8" refx="2" refy="4"\
orient="auto" markerUnits="userSpaceOnUse">\
<path d="M1,1 L1,7 L7,4 L1,1" style="fill: #339;" />\
</marker>\
</defs>\
<path d="M30,0 Q-10,25 26,50" style="stroke: #88f; stroke-width: 2px; fill: none;\
stroke-opacity: 0.5; marker-end: url(#markerArrow);"></path>\
</svg>\
</div>');
context.hasArrows = true;
};
HtmlFormatter.prototype.format_textdiff = function(context, delta) {
context.out('<div class="jsondiffpatch-value">');
this.formatTextDiffString(context, delta[0]);
context.out('</div>');
};
/* jshint camelcase: true */
var showUnchanged = function(show, node, delay) {
var el = node || document.body;
var prefix = 'jsondiffpatch-unchanged-';
var classes = {
showing: prefix + 'showing',
hiding: prefix + 'hiding',
visible: prefix + 'visible',
hidden: prefix + 'hidden',
};
var list = el.classList;
if (!list) {
return;
}
if (!delay) {
list.remove(classes.showing);
list.remove(classes.hiding);
list.remove(classes.visible);
list.remove(classes.hidden);
if (show === false) {
list.add(classes.hidden);
}
return;
}
if (show === false) {
list.remove(classes.showing);
list.add(classes.visible);
setTimeout(function() {
list.add(classes.hiding);
}, 10);
} else {
list.remove(classes.hiding);
list.add(classes.showing);
list.remove(classes.hidden);
}
var intervalId = setInterval(function() {
adjustArrows(el);
}, 100);
setTimeout(function() {
list.remove(classes.showing);
list.remove(classes.hiding);
if (show === false) {
list.add(classes.hidden);
list.remove(classes.visible);
} else {
list.add(classes.visible);
list.remove(classes.hidden);
}
setTimeout(function() {
list.remove(classes.visible);
clearInterval(intervalId);
}, delay + 400);
}, delay);
};
var hideUnchanged = function(node, delay) {
return showUnchanged(false, node, delay);
};
exports.HtmlFormatter = HtmlFormatter;
exports.showUnchanged = showUnchanged;
exports.hideUnchanged = hideUnchanged;
var defaultInstance;
exports.format = function(delta, left) {
if (!defaultInstance) {
defaultInstance = new HtmlFormatter();
}
return defaultInstance.format(delta, left);
};