2012-07-09 20:48:27 +00:00
|
|
|
var id = 1;
|
|
|
|
|
2012-07-10 15:06:18 +00:00
|
|
|
// Extract query parameters
|
|
|
|
var params = function(query) {
|
|
|
|
var search = /([^&=]+)=?([^&]*)/g,
|
|
|
|
pl = /\+/g,
|
|
|
|
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
|
|
|
|
ret = {};
|
2012-07-09 20:48:27 +00:00
|
|
|
|
2012-07-10 15:06:18 +00:00
|
|
|
while (match = search.exec(query))
|
|
|
|
{
|
|
|
|
ret[decode(match[1])] = decode(match[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}(document.location.search.substring(1));
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
var settings = {
|
|
|
|
wrap: true,
|
|
|
|
tab_width: 4,
|
|
|
|
};
|
2012-07-10 15:06:18 +00:00
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
if ('settings' in params)
|
|
|
|
{
|
|
|
|
settings = $.merge(settings, JSON.parse(params.setttings));
|
|
|
|
}
|
2012-07-10 15:06:18 +00:00
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
var templates = {};
|
|
|
|
|
|
|
|
function create_template(name, bindmap)
|
2012-07-10 15:06:18 +00:00
|
|
|
{
|
2012-07-11 12:04:38 +00:00
|
|
|
templ = $('#templates').children('.' + name);
|
2012-07-10 15:06:18 +00:00
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
if (templ.length != 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
templ = $(templ[0]);
|
|
|
|
|
|
|
|
props = [];
|
|
|
|
|
|
|
|
$.each(bindmap, function (key, callback) {
|
|
|
|
props.push({
|
|
|
|
elements: templ.find(key),
|
|
|
|
callback: callback
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
templates[name] = {
|
|
|
|
template: templ,
|
|
|
|
props: props,
|
|
|
|
execute: function (context) {
|
|
|
|
$.each(this.props, function (i, val) {
|
|
|
|
$.each(val.elements, function (i, e) {
|
|
|
|
var ee = $(e);
|
|
|
|
|
|
|
|
retval = val.callback.call(context, ee);
|
|
|
|
|
|
|
|
if (typeof(retval) == 'undefined')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-12 12:07:28 +00:00
|
|
|
if (retval.nodeType || retval.jquery)
|
|
|
|
{
|
|
|
|
ee.replace(retval);
|
|
|
|
}
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
if (typeof(retval) == 'string')
|
|
|
|
{
|
|
|
|
ee.text(retval);
|
|
|
|
}
|
|
|
|
else if ('text' in retval)
|
|
|
|
{
|
|
|
|
ee.text(retval.text);
|
|
|
|
}
|
|
|
|
else if ('html' in retval)
|
|
|
|
{
|
|
|
|
ee.html(retval.html);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.template.clone();
|
2012-07-10 15:06:18 +00:00
|
|
|
}
|
2012-07-11 12:04:38 +00:00
|
|
|
};
|
2012-07-10 15:06:18 +00:00
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
return templates[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_template(name, context)
|
|
|
|
{
|
|
|
|
return templates[name].execute(context);
|
2012-07-10 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function diff_file(file)
|
|
|
|
{
|
2012-07-12 12:07:28 +00:00
|
|
|
var f = $('<div/>');
|
2012-07-10 15:06:18 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < file.hunks.length; ++i)
|
|
|
|
{
|
|
|
|
var h = file.hunks[i];
|
2012-07-12 12:07:28 +00:00
|
|
|
var ht = run_template('hunk', {file: file, hunk: h});
|
2012-07-11 12:04:38 +00:00
|
|
|
|
|
|
|
var table = ht.children('table');
|
|
|
|
|
|
|
|
if (settings.wrap)
|
|
|
|
{
|
|
|
|
table.addClass('wrapped');
|
|
|
|
}
|
|
|
|
|
|
|
|
var cold = h.range.old.start;
|
|
|
|
var cnew = h.range.new.start;
|
2012-07-10 15:06:18 +00:00
|
|
|
|
|
|
|
for (var j = 0; j < h.lines.length; ++j)
|
|
|
|
{
|
|
|
|
var l = h.lines[j];
|
|
|
|
var o = String.fromCharCode(l.type);
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
var oldtd = $('<td/>', {'class': 'gutter'});
|
|
|
|
var newtd = $('<td/>', {'class': 'gutter'});
|
|
|
|
|
|
|
|
var row = $('<tr/>');
|
2012-07-10 15:06:18 +00:00
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
switch (o)
|
2012-07-10 15:06:18 +00:00
|
|
|
{
|
2012-07-11 12:04:38 +00:00
|
|
|
case ' ':
|
|
|
|
row.addClass('context');
|
|
|
|
|
|
|
|
oldtd.text(cold);
|
|
|
|
newtd.text(cnew);
|
|
|
|
|
|
|
|
cold++;
|
|
|
|
cnew++;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
row.addClass('added');
|
|
|
|
|
|
|
|
newtd.text(cnew);
|
|
|
|
cnew++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
row.addClass('removed');
|
|
|
|
|
|
|
|
oldtd.text(cold);
|
|
|
|
cold++;
|
|
|
|
break;
|
2012-07-10 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
var texttd = $('<td/>').text(l.content);
|
2012-07-10 15:06:18 +00:00
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
texttd.html(texttd.html().replace(/\t/g, '<span class="tab" style="width: ' + settings.tab_width + 'ex">\t</span>'));
|
|
|
|
|
|
|
|
row.append(oldtd).append(newtd).append(texttd);
|
|
|
|
table.append(row);
|
2012-07-10 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
|
2012-07-10 15:06:18 +00:00
|
|
|
f.append(ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
function write_diff(content, res)
|
2012-07-10 15:06:18 +00:00
|
|
|
{
|
2012-07-11 12:04:38 +00:00
|
|
|
for (var i = 0; i < res.length; ++i)
|
2012-07-10 15:06:18 +00:00
|
|
|
{
|
2012-07-11 12:04:38 +00:00
|
|
|
var df = diff_file(res[i]);
|
|
|
|
|
|
|
|
content.append(df);
|
2012-07-10 15:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
function write_commit(content, commit)
|
|
|
|
{
|
|
|
|
var c = run_template('commit', commit);
|
|
|
|
content.append(c);
|
|
|
|
}
|
|
|
|
|
2012-07-10 15:06:18 +00:00
|
|
|
function update_diff()
|
|
|
|
{
|
2012-07-09 20:48:27 +00:00
|
|
|
var r = new XMLHttpRequest();
|
|
|
|
|
2012-07-10 15:06:18 +00:00
|
|
|
r.onload = function(e) {
|
|
|
|
j = JSON.parse(r.responseText);
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
var content = $('#diff');
|
|
|
|
content.empty();
|
|
|
|
|
2012-07-11 06:44:23 +00:00
|
|
|
if ('commit' in j)
|
|
|
|
{
|
2012-07-11 12:04:38 +00:00
|
|
|
write_commit(content, j.commit);
|
2012-07-11 06:44:23 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 12:04:38 +00:00
|
|
|
write_diff(content, j.diff);
|
2012-07-09 20:48:27 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 07:53:07 +00:00
|
|
|
var t = (new Date()).getTime()
|
|
|
|
|
|
|
|
r.open("GET", "gitg-internal:/diff/?t=" + t + "&viewid=" + params.viewid);
|
2012-07-09 20:48:27 +00:00
|
|
|
r.send();
|
|
|
|
}
|
2012-07-11 12:04:38 +00:00
|
|
|
|
2012-07-12 12:07:28 +00:00
|
|
|
function date_to_string(d)
|
|
|
|
{
|
|
|
|
var t = ((new Date()).getTime() - d.getTime()) / 1000.0;
|
|
|
|
|
|
|
|
if (t < 1)
|
|
|
|
{
|
|
|
|
return "Less than a second ago";
|
|
|
|
}
|
|
|
|
else if (t < 60)
|
|
|
|
{
|
|
|
|
return "Less than a minute ago";
|
|
|
|
}
|
|
|
|
else if (t < 600)
|
|
|
|
{
|
|
|
|
return "Less than 10 minutes ago";
|
|
|
|
}
|
|
|
|
else if (t < 1800)
|
|
|
|
{
|
|
|
|
return "Half an hour ago";
|
|
|
|
}
|
|
|
|
else if (t < 3600)
|
|
|
|
{
|
|
|
|
return "One hour ago";
|
|
|
|
}
|
|
|
|
else if (t < 3600 * 12)
|
|
|
|
{
|
|
|
|
var tt = Math.round(t / 3600)
|
|
|
|
return tt + " hours ago";
|
|
|
|
}
|
|
|
|
else if (t < 3600 * 24)
|
|
|
|
{
|
|
|
|
return "One day ago";
|
|
|
|
}
|
|
|
|
else if (t < 3600 * 24 * 6)
|
|
|
|
{
|
2012-07-17 07:52:22 +00:00
|
|
|
return Math.round(t / (3600 * 24)) + " days ago";
|
2012-07-12 12:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return d.toLocaleString();
|
|
|
|
}
|
2012-07-11 12:04:38 +00:00
|
|
|
|
2012-07-12 12:07:28 +00:00
|
|
|
addEventListener('DOMContentLoaded', function () {
|
2012-07-11 12:04:38 +00:00
|
|
|
create_template("hunk", {
|
2012-07-12 12:07:28 +00:00
|
|
|
'.filepath': function () {
|
|
|
|
var f = this.file.file;
|
|
|
|
|
|
|
|
if (f.new.path)
|
|
|
|
{
|
|
|
|
return f.new.path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return f.old.path;
|
|
|
|
}
|
|
|
|
},
|
2012-07-11 12:04:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
create_template("commit", {
|
2012-07-12 12:07:28 +00:00
|
|
|
'.author': function () {
|
|
|
|
var name = $('<span/>', {'class': 'author name'}).text(this.author.name);
|
|
|
|
var a = $('<a/>', {href: this.author.email}).text(this.author.email);
|
|
|
|
|
|
|
|
return {html: $('<span/>').append(name).append(' <').append(a).append('>')};
|
|
|
|
},
|
2012-07-11 12:04:38 +00:00
|
|
|
'.date': function () {
|
|
|
|
var d = new Date();
|
|
|
|
d.setTime(this.author.time * 1000);
|
2012-07-12 12:07:28 +00:00
|
|
|
return {text: date_to_string(d)};
|
|
|
|
},
|
|
|
|
'.subject': function () {
|
|
|
|
return this.subject;
|
|
|
|
},
|
|
|
|
'.message': function () {
|
|
|
|
return this.message;
|
|
|
|
},
|
|
|
|
'.sha1': function () {
|
|
|
|
return this.id;
|
2012-07-11 12:04:38 +00:00
|
|
|
},
|
|
|
|
'.avatar': function (e) {
|
|
|
|
var h = this.author.email_md5;
|
|
|
|
|
2012-07-12 12:07:28 +00:00
|
|
|
var robo = 'http://robohash.org/' + h + '.png?size=80x80';
|
|
|
|
|
2012-07-21 11:50:13 +00:00
|
|
|
e.attr('src', 'http://www.gravatar.com/avatar/' + h + '?d=' + encodeURIComponent(robo) + '&s=50');
|
2012-07-11 12:04:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
// vi:ts=4
|