-
Notifications
You must be signed in to change notification settings - Fork 0
/
GithubUserLangs.js
296 lines (243 loc) · 8.01 KB
/
GithubUserLangs.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
/*
* GitHubUserLangs.js (https://github.com/kingkong123/github-user-langs)
* version 1.0.0
* jQuery Plugin
* by kingkong123 (http://kingkong123.github.io/)
* MIT License
*/
(function ( $ ) {
$.GithubUserApi = function( options ) {
var $scope = this;
var opts = {}, langs = {};
var userInfo = { success: false, data: {}, message: '' },
allRepos = { success: false, data: [], message: '' },
allLangs = { success: false, process: false, parsedRepo: 0, data: {}, message: '' };
var API_URL = 'https://api.github.com/';
var user_info_uri = 'users/:user';
var user_repos_uri = 'users/:user/repos';
var repo_langs_uri = 'repos/:user/:repo/languages';
var defaultSort = [];
var langRepos = 0;
var defaults = {
user: '',
includedForks: true,
repoType: 'all', // all, owner, public, private, member
repoSort: 'full_name', // created, updated, pushed, full_name
repoDirection: 'asc', // asc, desc
langSort: 'name', // name, size
langDirection: 'asc', // asc, desc
};
var REPO_TYPES = ['all', 'owner', 'public', 'private', 'member'];
var REPO_DEFAULT_SORTS = ['created', 'updated', 'pushed', 'full_name'];
var DIRECTIONS = ['asc', 'desc'];
var REPO_CUSTOM_SORTS = ['forks', 'watchers', 'open_issues', 'size'];
var LANG_SORTS = ['name', 'size'];
var getJson = function(url, callback){
return $.getJSON(url, function(data){}).fail(function(jqxhr, textStatus, error){
if(callback && $.isFunction(callback)){
if(jqxhr.responseJSON.message != null){
return callback({ success: false, message: jqxhr.responseJSON.message });
}else if(error){
return callback({ success: false, message: error });
}
return callback({ success: false, message: 'Fail to connect to server, please retry.' });
}else{
if(jqxhr.responseJSON.message != null){
return console.log(jqxhr.responseJSON.message);
}else if(error){
return console.log(error);
}
return console.log('Fail to connect to server, please retry.');
}
});
}
var getUserInfoURL = function(user){
return API_URL+'users/'+user;
}
var getAllReposURL = function(user){
var getQuery = '';
var getQueryArray = [];
if((opts.repoType != defaults.repoType) && (REPO_TYPES.indexOf(opts.repoType) > -1)){
getQueryArray.push('type='+opts.repoType);
}
if((opts.repoSort != defaults.repoSort) && (REPO_DEFAULT_SORTS.indexOf(opts.repoSort) > -1)){
getQueryArray.push('sort='+opts.repoSort);
}
if((opts.repoDirection != defaults.repoDirection) && (DIRECTIONS.indexOf(opts.repoDirection) > -1)){
getQueryArray.push('direction='+opts.repoDirection);
}else if(DIRECTIONS.indexOf(opts.repoDirection) == -1){
opts.repoDirection = defaults.repoDirection;
}
if(getQueryArray.length > 0){
getQuery = '?'+getQueryArray.join('&');
}
return API_URL+'users/'+user+'/repos'+getQuery;
}
var getRepoLangsURL = function(user, name){
return API_URL+'repos/'+user+'/'+name+'/languages';
}
var removeForkedRepos = function(repos){
var newRepos = [];
$.each(repos, function(idx, val){
if(!val.fork){
newRepos.push(val);
}
});
return newRepos;
}
this.init = function(options){
opts = $.extend( {}, defaults, options );
return this;
};
this.getUserInfo = function(callback){
if(!userInfo.success){
var url = getUserInfoURL(opts.user);
var userInfoJson = getJson(url, callback);
userInfoJson.complete(function(data){
//console.log(data);
if(data.status == 200){
userInfo.success = true;
userInfo.data = data.responseJSON;
if( $.isFunction(callback) ){
return callback(userInfo);
}else{
return { success: false, message: 'Callback function not exists' };
}
}else{
userInfo.message = 'Fail to connect to server, please retry.';
return userInfo;
}
});
}else{
return callback(userInfo.data);
}
}
this.getAllRepos = function(callback1, callback2){
if(!allRepos.success){
var url = getAllReposURL(opts.user);
var allReposJson = getJson(url, (callback2 != null? callback2: callback1));
allReposJson.complete(function(data){
//console.log(data);
if(data.status == 200){
if((opts.repoSort != defaults.repoSort) && (REPO_CUSTOM_SORTS.indexOf(opts.repoSort) > -1)){
data.responseJSON.sort( $scope.customSort(opts.repoSort, opts.repoDirection) );
}
if(!opts.includedForks){
data.responseJSON = removeForkedRepos(data.responseJSON);
}
allRepos.success = true;
allRepos.data = data.responseJSON;
if( $.isFunction(callback1) ){
if( callback2 != null ){
return callback1(callback2);
}else{
return callback1(allRepos);
}
}else{
return { success: false, message: 'Callback function not exists' };
}
}else{
allRepos.message = 'Fail to connect to server, please retry.';
return allRepos;
}
});
}else{
if( callback2 != null ){
return callback1(callback2);
}else{
return callback1(allRepos);
}
}
}
var getAllReposLength = function(){
var i = 0;
if(allRepos.success){
$.each(allRepos.data, function(idx, val){
i++;
});
return i;
}
return 0;
}
var getParsedRepo = function(){
return allLangs.parsedRepo;
}
var updateParsedRepo = function(i){
allLangs.parsedRepo = i;
}
var updateLangs = function(data){
$.each(data, function(idx, val){
//console.log(idx+" - "+val);
if( typeof(langs[idx]) == 'undefined' ){
langs[idx] = val;
}else{
langs[idx] += val;
}
});
//console.log(langs);
}
var checkLangsCompleted = function(callback){
if(getParsedRepo() == langRepos && allLangs.process && !allLangs.success){
var newLang = [];
$.each(langs, function(idx, val){
newLang.push({ name: idx, size: val });
});
allLangs.success = true;
allLangs.data = newLang;
return $scope.getAllLangs(callback);
}
}
this.getAllLangs = function(callback){
if(!allRepos.success){
this.getAllRepos(this.getAllLangs, callback);
}else{
if(!allLangs.success && !allLangs.process){
langRepos = getAllReposLength();
$.each(allRepos.data, function(idx, val){
//console.log(val);
var url = getRepoLangsURL(opts.user, val.name);
var langJson = getJson(url, callback);
langJson.complete(function(data){
if(data.status == 200){
var parsedRepo = getParsedRepo();
updateParsedRepo(parsedRepo +1);
updateLangs(data.responseJSON);
return checkLangsCompleted(callback);
}else{
allLangs.message = 'Fail to connect to server, please retry.';
return callback(allLangs);
}
});
});
allLangs.process = true;
}else if(!allLangs.success && allLangs.process){
return this.getAllLangs(callback);
}else if( allLangs.success && allLangs.process && langRepos == getParsedRepo() ){
if( $.isFunction(callback) ){
if( (LANG_SORTS.indexOf(opts.langSort) > -1) && (DIRECTIONS.indexOf(opts.langDirection) > -1) ){
allLangs.data.sort( $scope.customSort(opts.langSort, opts.langDirection) );
}else if( (LANG_SORTS.indexOf(opts.langSort) == -1) && (DIRECTIONS.indexOf(opts.langDirection) > -1) ){
allLangs.data.sort( $scope.customSort(defaults.langSort, opts.langDirection) );
}else if( (LANG_SORTS.indexOf(opts.langSort) > -1) && (DIRECTIONS.indexOf(opts.langDirection) == -1) ){
allLangs.data.sort( $scope.customSort(opts.langSort, defaults.langDirection) );
}
return callback(allLangs);
}else{
return { success: false, message: 'Callback function not exists' };
}
}
}
}
this.customSort = function(field, direction) {
var sortOrder = 1;
if(direction === "desc") {
sortOrder = -1;
}
return function (a,b) {
var result = (a[field] < b[field]) ? -1 : (a[field] > b[field]) ? 1 : 0;
return result * sortOrder;
}
}
return this.init(options);
};
}( jQuery ));