-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathgithubinfowidget.js
More file actions
79 lines (73 loc) · 3.95 KB
/
githubinfowidget.js
File metadata and controls
79 lines (73 loc) · 3.95 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
(function($) {
function widget(element, options, callback) {
this.element = element, this.options = options, this.callback = $.isFunction(callback) ? callback : $.noop
}
widget.prototype = function() {
function getCommits(user, repo, branch, callback) {
$.ajax({
url: "https://api.github.com/repos/" + user + "/" + repo + "/commits?sha=" + branch,
dataType: "jsonp",
success: callback
})
}
function _widgetRun(widget) {
if (!widget.options) return widget.element.append('<span class="error">Options for widget are not set.</span>'), void 0;
var callback = widget.callback,
element = widget.element,
user = widget.options.user,
repo = widget.options.repo,
branch = widget.options.branch,
last = void 0 === widget.options.last ? 0 : widget.options.last,
limitMessage = void 0 === widget.options.limitMessageTo ? 0 : widget.options.limitMessageTo
element.append("<p>Widget intitalization, please wait...</p>"), getCommits(user, repo, branch, function(data) {
function itemClass(current, totalCommits) {
return 0 === current ? 'class="first"' : current === totalCommits - 1 ? 'class="last"' : ""
}
function avatar(user) {
return '<img class="github-avatar" src="'+user.avatar_url+'"/>'
}
function author(login) {
return '<a class="github-user" href="https://github.com/' + login + '">' + login + "</a>"
}
function message(commitMessage, sha) {
var originalCommitMessage = commitMessage;
return limitMessage > 0 && commitMessage.length > limitMessage && (commitMessage = commitMessage.substr(0, limitMessage) + "..."), '"<a class="github-commit" title="' + originalCommitMessage + '" href="https://github.com/' + user + "/" + repo + "/commit/" + sha + '">' + commitMessage + '</a>"'
}
function replaceHtmlTags(message) {
return message.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """)
}
function when(commitDate) {
var commitTime = new Date(commitDate).getTime(),
todayTime = (new Date).getTime(),
differenceInDays = Math.floor((todayTime - commitTime) / 864e5);
if (0 === differenceInDays) {
var differenceInHours = Math.floor((todayTime - commitTime) / 36e5);
if (0 === differenceInHours) {
var differenceInMinutes = Math.floor((todayTime - commitTime) / 6e5);
return 0 === differenceInMinutes ? "just now" : "about " + differenceInMinutes + " minutes ago"
}
return "about " + differenceInHours + " hours ago"
}
return 1 == differenceInDays ? "yesterday" : differenceInDays + " days ago"
}
var commits = data.data,
totalCommits = commits.length > last ? last : commits.length;
element.empty();
for (var list = $('<ul class="github-commits-list">').appendTo(element), c = 0; totalCommits > c; c++) {
var commit = commits[c];
list.append("<li " + itemClass(c, totalCommits) + " >" + " " + (null !== commit.author ? avatar(commit.author) : "") + " " + (null !== commit.author ? author(commit.author.login) : commit.commit.committer.name) + " committed " + message(replaceHtmlTags(commit.commit.message), commit.sha) + " " + when(commit.commit.committer.date) + "</li>")
}
callback(element)
})
}
return {
run: function() {
_widgetRun(this)
}
}
}(), $.fn.githubInfoWidget = function(options, callback) {
return this.each(function() {
new widget($(this), options, callback).run()
}), this
}
})(jQuery);