Skip to content

Instantly share code, notes, and snippets.

@ermouth
Created April 5, 2016 19:27
Show Gist options
  • Save ermouth/c05e836fa701d9cc2d777994eed25b43 to your computer and use it in GitHub Desktop.
Save ermouth/c05e836fa701d9cc2d777994eed25b43 to your computer and use it in GitHub Desktop.
({
"id": "cw.Sys.Diff",
"require": [{
"jsondiffpatch": "lib/diff.js"
}],
"params": {
},
"data": {
"cmd": "",
"title": "Diff",
"doc": {},
"json": [],
"r": {},
"settings": ["nolog"],
"rev": ["-", "-"]
},
"init": function ($o,form) {
var that = this,
d = form.data;
$o.formgen([that.HTML]);
that.Diff = jsondiffpatch.create({
objectHash: function(obj, index) {
if (Object.isArray(obj) && Object.isNumber(obj[0])) {
return obj[0]+"";
} else if (Object.isObject(obj)){
if (obj._id !== void 0) return obj._id+"";
if (obj.id !== void 0) return obj.id+"";
if (obj.name !== void 0) return obj.name+"";
}
return '$$index:' + index;
}
});
$o.then(function(){
if (d.doc._id) that.Req(d.doc._id);
});
},
"ui": {
"#docid": "doc._id",
"input[name=\"settings\"]": "settings",
"#diff": {
"delay": 100,
"bind": function (d) {
var that = this,
delta,
j = d.json,
log = (d.settings.indexOf("nolog")==-1),
a0, a1;
if (d.rev[0]=="-" || d.rev[1]=="-") return '<div class="tac p50 orange">Select revisions</div>';
a0 = log?j[0]:Object.reject(j[0],"log");
a1 = log?j[1]:Object.reject(j[1],"log");
delta = that.Diff.diff(a0, a1);
if (void 0 == delta) return '<div class="tac p50 green">No diff</div>';
if (d.settings.indexOf("unchanged")==-1) jsondiffpatch.formatters.html.hideUnchanged();
else jsondiffpatch.formatters.html.showUnchanged();
return jsondiffpatch.formatters.html.format(delta, a0);
},
"watch": "input[name=\"settings\"],#rev"
},
"#rev0": {
"bind": "rev.0",
"init": function ($o) {
$o.select2();
}
},
"#rev1": {
"bind": "rev.1",
"init": function ($o) {
$o.select2();
}
},
"#proxy": {
"delay": 300,
"bind": function (d) {
var that = this;
if (d.rev[0]!="-" && d.rev[1]!="-") that.Req.debounce(0)(d.doc._id, d.rev);
},
"watch": "#rev0,#rev1"
}
},
"HTML": "<div class=\"fs80 w900 oh\">\n\t<div class=\"fr\">\n\t\t<label class=\"dib\"><input type=\"checkbox\" name=\"settings\" value=\"nolog\"> No .log</label>\n\t\t<label class=\"ml20 dib\"><input type=\"checkbox\" name=\"settings\" value=\"unchanged\"> Show unchanged</label>\n\t</div>\n\t<h3>Compare revisions of _id:<span id=\"docid\"></span></h3>\n\t<div class=\"mt10\">\n\t\t<select id=\"rev0\" class=\"w430 fs110\"><option value=\"-\">Source revision...</option></select><span \n\t\tclass=\"dib w40 tac\">vs</span><select id=\"rev1\" class=\"w430 fs110\"><option value=\"-\">Target revision...</option></select>\n\t</div>\n\t\n\t<div id=\"diff\" class=\"lh120 mt10 fs80\"></div>\n\t<div id=\"proxy\" class=\"hide\"></div>\n</div>",
"style": {
" pre": "max-width:700px;overflow:hidden;text-overflow:ellipsis;white-space: auto;word-break:auto;",
" .jsondiffpatch-delta": "padding-left:0",
" .w430": "width:430px"
},
"Req": function (id,rev) {
var that = this,
d = that.data,
j = d.json,
t = '<option value="{rev}">{txt}</option>',
ctr = 2;
if (!rev) {
that.db.get(id,{revs_info:true, conflicts:true})
.then(function(doc){
j[0] = Object.reject(doc, ["_revs_info","_conflicts"]);
var p = [];
$.extend(true,[],(doc._conflicts||[]))
.forEach(function(e){
p.push(t.assign({rev:e,txt:"× "+e}));
});
$.extend(true,[],doc._revs_info)
.forEach(function(e){
e.txt = e.rev;
if (e.status=="available") p.push(t.assign(e));
});
that.my.find("#rev0").append(p.join(""));
that.my.find("#rev1").append(p.join(""));
});
} else {
[0,1].forEach(function(i){
if (d.r[rev[i]]) {
j[i] = d.r[rev[i]];
_ready();
} else {
that.db.get(id,{rev:rev[i]})
.then(function(doc){
d[doc._rev] = j[i] = doc;
_ready();
});
}
});
}
function _ready(){
ctr-=1;
if (!ctr) that.my.check("#diff");
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment