-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbootstrap-github-widget.js
More file actions
194 lines (165 loc) · 7.62 KB
/
bootstrap-github-widget.js
File metadata and controls
194 lines (165 loc) · 7.62 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
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
/* ========================================================================
* Bootstrap Github Widget: bootsrap-github-widget.js v1.0.0
* ========================================================================
* Copyright 2014 Kevin Gillieron <kevin.gillieron@gw-computing.net>
* Licensed under BSD 3-clauses
* (https://github.com/gilliek/bootstrap-github-widget/blob/master/LICENSE)
* ======================================================================== */
(function ($) {
'use strict';
// GITHUB WIDGET CLASS DEFINITION
// ==============================
$.githubWidget = function(element, options) {
// defaults options
var defaults = {
user: '',
widget: 'repos',
title: 'auto',
body: '',
footer: '',
extrainfo: false, // require font-awesome
limit: 5
}
var plugin = this;
plugin.settings = {};
var $element = $(element),
element = element;
var fillElement = function(title, body, content, footer) {
var panel = '<div class="panel panel-default">';
panel += '<div class="panel-heading">' + title + '</div>';
panel += body != '' ? '<div class="panel-body">' + body + '</div>' : '';
panel += content;
panel += footer != '' ? '<div class="panel-footer" style="text-align: right;">' + footer + '</div>' : '';
panel += '</div>';
$element.html(panel);
};
var githubURL = function() {
switch (plugin.settings.widget) {
case 'repos':
return 'https://github.com/' + plugin.settings.user + '?tab=repositories';
case 'gists':
return 'https://gist.github.com/' + plugin.settings.user;
default:
return '#';
}
};
var autoTitle = function() {
if (plugin.settings.title !== 'auto') { return plugin.settings.title; }
return 'GitHub ' + plugin.settings.widget;
}
var autoBody = function(owner, numberOf) {
if (plugin.settings.body !== 'auto') { return plugin.settings.body; }
return '<div class="media">\
<a class="pull-left" href="https://github.com/' + plugin.settings.user + '">\
<img class="media-object"\
src="' + owner.avatar_url + '"\
width="64"\
height="64"\
alt="' + plugin.settings.user + '" />\
</a>\
<div class="media-body">\
<h4 class="media-heading">' + plugin.settings.user + '</h4>\
' + numberOf + ' ' + plugin.settings.widget + ' \
(<a href="' + githubURL() + '">see all</a>)\
</div>\
</div>';
};
var autoFooter = function(footer) {
if (plugin.settings.footer !== 'auto') { return plugin.settings.footer; }
return '<a href="https://github.com/' + plugin.settings.user + '">'
+ plugin.settings.user +
'</a> <span class="text-muted">@ GitHub</span>';
};
var repoInfo = function(repo) {
if (!plugin.settings.extrainfo) { return ''; }
return '<span class="pull-right">\
<i class="fa fa-eye"></i> ' + repo.watchers_count + ' \
<i class="fa fa-star"></i> ' + repo.stargazers_count + ' \
<i class="fa fa-code-fork"></i> ' + repo.forks + '\
</span>';
};
var gistInfo = function(gist) {
if (!plugin.settings.extrainfo) { return ''; }
return '<span class="pull-right">\
<i class="fa fa-eye"></i> ' + repo.watchers_count + ' \
<i class="fa fa-star"></i> ' + repo.stargazers_count + ' \
<i class="fa fa-code-fork"></i> ' + repo.forks + '\
</span>';
};
var fetchFromGithub = function() {
var user = plugin.settings.user;
var action = plugin.settings.widget;
$.ajax({
url: 'https://api.github.com/users/' + user + '/' + action + '?sort=pushed',
type: 'GET',
dataType: 'json',
success: function(data) {
var title = autoTitle();
var body = autoBody(data[0]['owner'], data.length);
var footer = autoFooter();
var limit = plugin.settings.limit;
var content = '<ul class="list-group">';
var total = 0;
$.each(data, function(k, v) {
if (limit > 0 && total >= limit) {
return;
}
content += '<li class="list-group-item">';
switch (action) {
case 'repos':
content += '<a href="' + v.html_url + '">' + v.full_name + '</a> \
' + repoInfo(v) + '\
<br/>' + v.description;
break;
case 'gists':
var gistName = v.owner.login + "/" + Object.keys(v.files)[0];
content += '<a href="' + v.html_url + '">' + gistName + '</a> \
' + gistInfo(v) + '\
<br/>' + v.description;
break;
}
total++;
});
content += '</ul>';
fillElement(title, body, content, footer);
}
});
}
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
fetchFromGithub();
}
plugin.init();
}
// GITHUB WIDGET PLUGIN DEFINITION
// ===============================
$.fn.githubWidget = function(options) {
return this.each(function() {
var plugin = new $.githubWidget(this, options);
});
}
// GITHUB WIDGET DATA API
// ======================
$(function() {
$.each($('[data-toggle="github-widget"]'), function() {
var inputUser = $(this).data('user');
var inputWidget = $(this).data('widget');
var inputTitle = $(this).data('title');
var inputBody = $(this).data('body');
var inputFooter = $(this).data('footer');
var inputExtraInfo = $(this).data('extrainfo');
var inputLimit = $(this).data('limit');
if (inputUser !== undefined) {
var options = {};
options.user = inputUser;
options.widget = (inputWidget !== undefined) ? inputWidget : 'repos';
options.title = (inputTitle !== undefined) ? inputTitle: 'auto';
options.body = (inputBody !== undefined) ? inputBody : '';
options.footer = (inputFooter !== undefined) ? inputFooter : '';
options.extrainfo = (inputExtraInfo !== undefined) ? Boolean(inputExtraInfo) : false;
options.limit = (inputLimit !== undefined) ? parseInt(inputLimit) : 5;
var plugin = new $.githubWidget(this, options);
}
});
});
})(jQuery);