-
Notifications
You must be signed in to change notification settings - Fork 9
/
coderbits.js
142 lines (132 loc) · 4.21 KB
/
coderbits.js
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
;(function(window, document, undefined) {
"use strict";
var addCommas = function(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
};
var buildSummary = function(data) {
var content = '';
content += '<div id="coderbits-summary">';
content += '<div id="coderbits-avatar">';
content += '<a href="' + data.link + '" target="_parent">';
content += '<img alt="' + data.name + '" class="avatar" src="https://secure.gravatar.com/avatar/' + data.gravatar_hash + '?d=https%3A%2F%2Fcoderbits.com%2Fimages%2Fgravatar.png&r=PG&s=48">';
content += '</a>';
content += '</div>';
content += '<div id="coderbits-overview">';
content += '<h1>';
content += '<a href="' + data.link + '" target="_parent" title="' + data.name + '">' + data.name + '</a>';
content += '</h1>';
content += '<p>';
content += '<span>' + data.title + '</span>';
content += '</p>';
content += '</div>';
content += '</div>';
return content;
};
var buildSkills = function(data) {
var content = '', items;
content += '<div id="coderbits-skills">';
var area = ["Skills", "Interests", "Traits", "Areas"];
(function() {
for(var i = 0; i < area.length; i++) {
var current = "top_" + area[i].toLowerCase();
if(data[current].length > 0) {
content += "<h2>Top " + area[i] + "</h2>";
items = data[current];
return true;
}
}
})();
content += '<p>';
for(var i = 0; i < items.length; i++) {
content += items[i].name;
if(i < items.length - 1) {
content += ', ';
}
}
content += '</p>';
content += '</div>';
return content;
};
var buildStats = function(data) {
var total = 0;
for(var i = 0; i < data.badges.length; i++) {
if(data.badges[i].earned) {
total++
}
}
var content = '';
content += '<div id="coderbits-stats">';
content += '<ul id="coderbits-stats-list">';
content += '<li>';
content += '<p>';
content += '<strong>' + addCommas(total) + '</strong>';
content += '<span> badges</span>';
content += '</p>';
content += '<p>';
content += '<strong>' + addCommas(data.follower_count) + '</strong>';
content += '<span> followers</span>';
content += '</p>';
content += '</li>';
content += '<li class="last">';
content += '<p>';
content += '<strong>' + addCommas(data.views) + '</strong>';
content += '<span> views</span>';
content += '</p>';
content += '<p>';
content += '<strong>' + addCommas(data.following_count) + '</strong>';
content += '<span> friends</span>';
content += '</p>';
content += '</li>';
content += '</ul>';
content += '</div>';
return content;
};
var buildBadges = function(data) {
var content = '', count = 0, total = 0;
content += '<div id="coderbits-awards">';
content += '<p id="coderbits-badges">'
for(var i = 0; i < data.badges.length; i++) {
var badge = data.badges[i];
if(badge.earned) {
total++
if(count < 11 && badge.level === 1) {
content += '<img src="' + badge.image_link + '" title="' + badge.name + ' - ' + badge.description + '" alt="' + badge.level + ' bit ' + badge.name + ' - ' + badge.description + '">';
count++
}
}
}
content += '<a href="' + data.link + '/badges">view all ' + total + '</a>';
content += '</p>';
content += '</div>';
return content;
};
var request = function(url) {
var script = document.getElementsByTagName("script")[0];
var handler = document.createElement("script");
handler.src = url;
script.parentNode.insertBefore(handler, script);
};
var global = "coderbits", element = document.getElementById(global);
if(element) {
window[global] = function(data) {
var content = '';
content += buildSummary(data);
content += buildSkills(data);
content += buildStats(data);
content += buildBadges(data);
document.getElementById(global).innerHTML = content;
delete window[global];
};
var username = element.getAttribute("data-coderbits-username"),
safeUsername = username.replace(/[('"){};!@#%&*]/gi, '');
request("https://coderbits.com/" + safeUsername + ".json?callback=" + global);
}
})(window, document);