-
Notifications
You must be signed in to change notification settings - Fork 3
/
dp-loader.js
193 lines (141 loc) · 5.32 KB
/
dp-loader.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
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
(function () {
var dribbbleProfile, dribbbleId, script,
apiEndpoint = 'https://api.dribbble.com/v1/',
token = '';
//
// Load stylesheet
dpAddStylesheet('//nadikun.com/code/dribbble-profile/style.css'); // style.css
dpAddStylesheet('//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css');
//
// Create top-level element for Dribble Profile
dribbbleProfile = document.createElement('div');
dribbbleProfile.setAttribute('id', 'dribbble-profile');
script = document.querySelector("[data-dribbble-id]");
dribbbleId = script.getAttribute('data-dribbble-id');
script.parentNode.insertBefore(dribbbleProfile, script);
dribbbleProfile.innerHTML = '<div id="dp-left-profile"></div>' +
'<div id="dp-right-shot"></div>';
//
// Get profile details
sendJsonp(apiEndpoint + 'users/' + dribbbleId + '?callback=displayProfile&access_token=' + token, {
callbackName: 'displayProfile',
onSuccess: displayProfile,
onTimeout: displayProfile,
timeout: 5
});
//
// Get the last published shot
sendJsonp(apiEndpoint + 'users/' + dribbbleId + '/shots?callback=displayLastShot&access_token=' + token, {
callbackName: 'displayLastShot',
onSuccess: displayLastShot,
onTimeout: displayLastShot,
timeout: 5
});
//
// Display Profile
function displayProfile(response) {
var profileTop, profileMid, profileBottom, h3, location,
data = response.data;
profileTop = '<div id="dp-profile-top">' +
'<h3>' + data.name + '</h3>' +
'<p><i class="fa fa-map-marker"></i> ' + data.location + '</p>' +
'</div>';
profileMid = '<div id="dp-profile-mid">' +
'<a href="' + data.html_url + '">' +
'<img src="' + data.avatar_url + '" alt="">' +
'</a>' +
'</div>';
profileBottom = '<ul id="dp-profile-bottom">' +
'<li class="dp-stat"><i class="fa fa-dribbble"></i><br><b>' + data.shots_count + '</b></li>' +
'<li class="dp-stat"><i class="fa fa-heart-o"></i><br><b>' + data.likes_received_count + '</b></li>' +
'<li class="dp-stat"><i class="fa fa-hand-spock-o"></i><br><b>' + data.followers_count + '</b></li>' +
'</ul>';
dribbbleBall = '<p id="dp-ball" class="fa fa-dribbble"></p>';
document.getElementById('dp-left-profile').innerHTML = profileTop + profileMid + profileBottom + dribbbleBall;
h3 = document.querySelector('#dp-profile-top h3');
location = document.querySelector('#dp-profile-top p');
//
// If name and/or location are too long,
// reduce their font size
checkNameLength(data, h3);
checkLocationLength(data, location);
}
//
// Display last shot
function displayLastShot(response) {
var lastShotImg, lastShotEl,
lastShotData = response.data[0];
lastShotImg = lastShotData.images.hidpi ? lastShotData.images.hidpi : lastShotData.images.normal;
lastShotEl = '<a href="' + lastShotData.html_url + '">' +
'<img src="' + lastShotImg + '" alt="' + lastShotData.title + '">' +
'</a>';
document.getElementById('dp-right-shot').innerHTML = lastShotEl;
}
//
// Check name and location length
function checkNameLength(response, el) {
var n = response.name.length;
switch (true) {
case (n > 15 && n <= 20):
el.className = el.className + " font-18";
break;
case (n > 20 && n <= 25):
el.className = el.className + " font-16";
break;
case (n > 25 && n <= 30):
el.className = el.className + " font-14";
break;
case (n > 30):
el.className = el.className + " font-10";
break;
default: // do nothing
}
}
//
// Check location length
function checkLocationLength(response, el) {
var l = response.location.length;
switch (true) {
case (l > 20 && l <= 29):
el.className = el.className + " font-12";
break;
case (l > 29 && l <= 40):
el.className = el.className + " font-10";
break;
case (l > 40):
el.className = el.className + " font-8";
break;
default: // do nothing
}
}
// Create stylesheet
function dpAddStylesheet(src) {
var header, dpStylesheet;
header = document.getElementsByTagName('head').item(0);
dpStylesheet = document.createElement('link');
dpStylesheet.rel = 'stylesheet';
dpStylesheet.href = src;
header.appendChild(dpStylesheet);
}
// Send JSONP request
function sendJsonp(src, opt) {
var options = opt || {},
callback_name = options.callbackName || 'callback',
on_success = options.onSuccess || function () {},
on_timeout = options.onTimeout || function () {},
timeout = options.timeout || 10;
var timeout_trigger = window.setTimeout(function () {
window[callback_name] = function () {};
on_timeout();
}, timeout * 1000);
window[callback_name] = function (data) {
window.clearTimeout(timeout_trigger);
on_success(data);
};
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}
})();