1
1
mirror of https://github.com/github/semantic.git synced 2024-12-19 04:41:47 +03:00
semantic/prototype/UI/index.html
2015-10-21 16:07:47 -04:00

141 lines
3.9 KiB
HTML

<!doctype html>
<html>
<head>
<title>semantic-diff</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
.insert {
background-color: #eaffea;
outline: 1px solid #c1e9c1;
margin-left: 50%;
}
.delete {
background-color: #ffecec;
outline: 1px solid #f1c0c0;
margin-right: 50%;
}
.replace {
background-color: #eee;
}
</style>
<script type="text/javascript">
function loadJSON(path, callback) {
var request = new XMLHttpRequest();
request.overrideMimeType("application/json");
request.open('GET', '' + path, true);
request.onreadystatechange = function () {
if (request.readyState == 4 && (request.status == "200" || request.status == 0)) {
callback(JSON.parse(request.responseText));
}
};
request.send(null);
}
function wrap(tagName, element) {
if (element == null) { return null; }
var node = document.createElement(tagName);
node.appendChild(element);
return node;
}
function Delete(before) {
this.before = before;
return this;
}
function Insert(after) {
this.after = after;
return this;
}
function Replace(before, after) {
this.before = before;
this.after = after;
return this;
}
function Term(range, syntax) {
this.range = range;
this.syntax = syntax;
return this;
}
function toDOM(view) {
if (view instanceof Delete) {
var element = document.createElement("div");
element.classList.add("delete");
element.appendChild(toDOM(view.before));
return element;
}
if (view instanceof Term) {
var element = document.createElement("div");
element.classList.add("term");
if (view.syntax instanceof Array) {
for (i in view.syntax) {
element.appendChild(toDOM(view.syntax[i]));
}
} else if (view.syntax instanceof Object) {
for (k in view.syntax) {
element.appendChild(toDOM(view.syntax[k]));
}
}
return element;
}
}
function term(json, a, b) {
if (json.extract == null || json.unwrap == null) { return null; }
return new Term(json.extract, syntax(json.unwrap, a, b, term));
}
function patch(patch, a, b) {
if (patch.delete != null) { return new Delete(term(patch.delete, a, b)); }
if (patch.insert != null) { return new Insert(term(patch.insert, a, b)); }
if (patch.replace != null) { return new Replace(term(patch.replace.before, a, b), term(patch.replace.after, a, b)); }
}
function indexed(array, a, b, continuation) {
var out = [];
for (index in array) {
out.push(continuation(array[index], a, b));
}
return out;
}
function keyed(object, a, b, continuation) {
var out = {};
for (key in object) {
out[key] = continuation(object[key], a, b);
}
return out;
}
function syntax(json, a, b, continuation) {
if (json instanceof Array) { return indexed(json, a, b, continuation); }
if (json instanceof Object) { return keyed(json, a, b, continuation); }
return continuation(json, a, b);
}
function diff(json, a, b) {
if (json.pure != null) { return patch(json.pure, a, b); }
if (json.roll != null) { return syntax(json.roll, a, b, diff); }
}
</script>
</head>
<body>
<div id="diff"></div>
<script type="text/javascript">
loadJSON('diff.json', function (json) {
console.log(json.diff);
document.getElementById("diff").appendChild(toDOM(diff(json.diff, json.a, json.b)));
});
</script>
</body>
</html>