-
Notifications
You must be signed in to change notification settings - Fork 207
/
buttons.js
354 lines (315 loc) · 12.2 KB
/
buttons.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*!
* github-buttons v2.14.0
* (c) 2020 なつき
* @license BSD-2-Clause
*/
(function () {
'use strict';
var document = window.document;
var location = document.location;
var Math = window.Math;
var HTMLElement = window.HTMLElement;
var XMLHttpRequest = window.XMLHttpRequest;
var buttonClass = 'github-button';
var iframeURL = 'https://' + (/* istanbul ignore next */ 'buttons.github.io') + '/buttons.html';
var domain = 'github.com';
var apiBaseURL = 'https://api.' + domain;
var useXHR = XMLHttpRequest && 'prototype' in XMLHttpRequest && 'withCredentials' in XMLHttpRequest.prototype;
var useShadowDOM = useXHR && HTMLElement && 'attachShadow' in HTMLElement.prototype && !('prototype' in HTMLElement.prototype.attachShadow);
var stringify = function (obj, sep, eq, encodeURIComponent) {
if (sep == null) {
sep = '&';
}
if (eq == null) {
eq = '=';
}
if (encodeURIComponent == null) {
encodeURIComponent = window.encodeURIComponent;
}
var params = [];
for (var name in obj) {
var value = obj[name];
if (value != null) {
params.push(encodeURIComponent(name) + eq + encodeURIComponent(value));
}
}
return params.join(sep)
};
var parse = function (str, sep, eq, decodeURIComponent) {
if (sep == null) {
sep = '&';
}
if (eq == null) {
eq = '=';
}
if (decodeURIComponent == null) {
decodeURIComponent = window.decodeURIComponent;
}
var obj = {};
var params = str.split(sep);
for (var i = 0, len = params.length; i < len; i++) {
var entry = params[i];
if (entry !== '') {
var ref = entry.split(eq);
obj[decodeURIComponent(ref[0])] = (ref[1] != null ? decodeURIComponent(ref.slice(1).join(eq)) : undefined);
}
}
return obj
};
var onEvent = function (target, eventName, func) {
/* istanbul ignore else: IE lt 9 */
if (target.addEventListener) {
target.addEventListener(eventName, func, false);
} else {
target.attachEvent('on' + eventName, func);
}
};
var offEvent = function (target, eventName, func) {
/* istanbul ignore else: IE lt 9 */
if (target.removeEventListener) {
target.removeEventListener(eventName, func, false);
} else {
target.detachEvent('on' + eventName, func);
}
};
var onceEvent = function (target, eventName, func) {
var callback = function () {
offEvent(target, eventName, callback);
return func.apply(this, arguments)
};
onEvent(target, eventName, callback);
};
var onceReadyStateChange = /* istanbul ignore next: IE lt 9 */ function (target, regex, func) {
var eventName = 'readystatechange';
var callback = function () {
if (regex.test(target.readyState)) {
offEvent(target, eventName, callback);
return func.apply(this, arguments)
}
};
onEvent(target, eventName, callback);
};
var createElementInDocument = function (document) {
return function (tag, props, children) {
var el = document.createElement(tag);
if (props != null) {
for (var prop in props) {
var val = props[prop];
if (val != null) {
if (el[prop] != null) {
el[prop] = val;
} else {
el.setAttribute(prop, val);
}
}
}
}
if (children != null) {
for (var i = 0, len = children.length; i < len; i++) {
var child = children[i];
el.appendChild(typeof child === 'string' ? document.createTextNode(child) : child);
}
}
return el
}
};
var createElement = createElementInDocument(document);
var dispatchOnce = function (func) {
var onceToken;
return function () {
if (!onceToken) {
onceToken = 1;
func.apply(this, arguments);
}
}
};
var defer = function (func) {
/* istanbul ignore else */
if (document.readyState === 'complete' || /* istanbul ignore next: IE lt 11 */ (document.readyState !== 'loading' && !document.documentElement.doScroll)) {
setTimeout(func);
} else {
if (document.addEventListener) {
var callback = dispatchOnce(func);
onceEvent(document, 'DOMContentLoaded', callback);
onceEvent(window, 'load', callback);
} else {
onceReadyStateChange(document, /m/, func);
}
}
};
var queues = {};
var fetch = function (url, func) {
var queue = queues[url] || (queues[url] = []);
if (queue.push(func) > 1) {
return
}
var callback = dispatchOnce(function () {
delete queues[url];
while ((func = queue.shift())) {
func.apply(null, arguments);
}
});
if (useXHR) {
var xhr = new XMLHttpRequest();
onEvent(xhr, 'abort', callback);
onEvent(xhr, 'error', callback);
onEvent(xhr, 'load', function () {
var data;
try {
data = JSON.parse(this.responseText);
} catch (error) {
callback(error);
return
}
callback(this.status !== 200, data);
});
xhr.open('GET', url);
xhr.send();
} else {
var contentWindow = this || window;
contentWindow._ = function (json) {
contentWindow._ = null;
callback(json.meta.status !== 200, json.data);
};
var script = createElementInDocument(contentWindow.document)('script', {
async: true,
src: url + (url.indexOf('?') !== -1 ? '&' : '?') + 'callback=_'
});
var onloadend = /* istanbul ignore next: IE lt 9 */ function () {
if (contentWindow._) {
contentWindow._({
meta: {}
});
}
};
onEvent(script, 'load', onloadend);
onEvent(script, 'error', onloadend);
/* istanbul ignore if: IE lt 9 */
if (script.readyState) {
onceReadyStateChange(script, /de|m/, onloadend);
}
contentWindow.document.getElementsByTagName('head')[0].appendChild(script);
}
};
var render = function (root, options, func) {
var createElement = createElementInDocument(root.ownerDocument);
var widget = createElement('ul', {className: 'github-facts'});
root.appendChild(widget);
var hostname = options.hostname.replace(/\.$/, '');
if (hostname.length < domain.length || ('.' + hostname).substring(hostname.length - domain.length) !== ('.' + domain)) {
options.removeAttribute('href');
func(root);
return
}
var path = (' /' + options.pathname).split(/\/+/);
if (((hostname === domain || hostname === 'gist.' + domain) && path[3] === 'archive') ||
(hostname === domain && path[3] === 'releases' && (path[4] === 'download' || (path[4] === 'latest' && path[5] === 'download'))) ||
(hostname === 'codeload.' + domain)) {
options.target = '_top';
}
var api = path[2] ? '/repos/' + path[1] + '/' + path[2] : '/users/' + path[1];
fetch.call(this, apiBaseURL + api, function (error, json) {
if (!error) {
var stargazers_count = json['stargazers_count'];
widget.appendChild(createElement('li', {
className: 'github-fact'
}, [
('' + stargazers_count).replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' Stars'
]));
var forks_count = json['forks_count'];
widget.appendChild(createElement('li', {
className: 'github-fact'
}, [
('' + forks_count).replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' Forks'
]));
}
func(root);
});
};
var devicePixelRatio = window.devicePixelRatio || /* istanbul ignore next */ 1;
var ceilPixel = function (px) {
return (devicePixelRatio > 1 ? Math.ceil(Math.round(px * devicePixelRatio) / devicePixelRatio * 2) / 2 : Math.ceil(px)) || 0
};
var get = function (el) {
var width = el.offsetWidth;
var height = el.offsetHeight;
if (el.getBoundingClientRect) {
var boundingClientRect = el.getBoundingClientRect();
width = Math.max(width, ceilPixel(boundingClientRect.width));
height = Math.max(height, ceilPixel(boundingClientRect.height));
}
return [width, height]
};
var set = function (el, size) {
el.style.width = size[0] + 'px';
el.style.height = size[1] + 'px';
};
var render$1 = function (options, func) {
if (options == null || func == null) {
return
}
if (useShadowDOM) {
var host = createElement('div', {className: 'github-repository'});
render(host, options, function () {
func(host);
});
} else {
var iframe = createElement('iframe', {
src: 'javascript:0',
title: options.title || undefined,
allowtransparency: true,
scrolling: 'no',
frameBorder: 0
});
set(iframe, [0, 0]);
iframe.style.border = 'none';
var callback = function () {
var contentWindow = iframe.contentWindow;
var body;
try {
body = contentWindow.document.body;
} catch (_) /* istanbul ignore next: IE 11 */ {
document.body.appendChild(iframe.parentNode.removeChild(iframe));
return
}
offEvent(iframe, 'load', callback);
render.call(contentWindow, body, options, function (widget) {
var size = get(widget);
iframe.parentNode.removeChild(iframe);
onceEvent(iframe, 'load', function () {
set(iframe, size);
});
iframe.src = iframeURL + '#' + (iframe.name = stringify(options));
func(iframe);
});
};
onEvent(iframe, 'load', callback);
document.body.appendChild(iframe);
}
};
if (location.protocol + '//' + location.host + location.pathname === iframeURL) {
render(document.body, parse(window.name || location.hash.replace(/^#/, '')), function () { });
} else {
defer(function () {
var ref = document.querySelectorAll
? document.querySelectorAll('a.' + buttonClass)
: (function () {
var results = [];
var ref = document.getElementsByTagName('a');
for (var i = 0, len = ref.length; i < len; i++) {
if ((' ' + ref[i].className + ' ').replace(/[ \t\n\f\r]+/g, ' ').indexOf(' ' + buttonClass + ' ') !== -1) {
results.push(ref[i]);
}
}
return results
})();
for (var i = 0, len = ref.length; i < len; i++) {
(function (anchor) {
render$1(anchor, function (el) {
anchor.appendChild(el);
});
})(ref[i]);
}
});
}
}());