@@ -50,24 +50,28 @@ function behind_badge(amount) {
50
50
return '<svg xmlns="http://www.w3.org/2000/svg" width="92" height="18" role="img"><title>How far behind of the original repo\'s master branch this fork\'s master branch is</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="r"><rect width="92" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="47" height="18" fill="#555"/><rect x="47" width="45" height="18" fill="' + color + '"/><rect width="92" height="18" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="245" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">behind</text><text x="245" y="130" transform="scale(.1)" fill="#fff" textLength="370">behind</text><text x="685" y="130" transform="scale(.1)" fill="#fff" textLength="' + badge_width ( amount ) + '">' + amount + '</text></g></svg>' ;
51
51
}
52
52
53
- function build_fork_element_html ( table_body , combined_name , num_stars , num_watches , num_forks ) {
54
- table_body . append (
55
- $ ( '<tr>' , { id : extract_username_from_fork ( combined_name ) , class : "useful_forks_repo" } ) . append (
56
- $ ( '<td>' ) . html ( svg_literal_fork + ' <a href=https://github.com/' + combined_name + '>' + combined_name + '</a>' ) ,
57
- $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR ) ,
58
- $ ( '<td>' ) . html ( svg_literal_star + ' x ' + num_stars ) ,
59
- $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR ) ,
60
- $ ( '<td>' ) . html ( svg_literal_eye + ' x ' + num_watches ) ,
61
- $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR ) ,
62
- $ ( '<td>' ) . html ( svg_literal_fork + ' x ' + num_forks )
63
- )
64
- ) ;
65
- }
66
-
67
53
function getElementById_$ ( id ) {
68
54
return $ ( toHtmlId ( id ) ) ;
69
55
}
70
56
57
+ function getTdValue ( rows , index , col ) {
58
+ return rows . item ( index ) . getElementsByTagName ( 'td' ) . item ( col ) . getAttribute ( "value" ) ;
59
+ }
60
+
61
+ /** 'sortColumn' index starts at 0. https://stackoverflow.com/a/37814596/9768291 */
62
+ function sortTable ( table_id , sortColumn ) {
63
+ let tableData = document . getElementById ( table_id ) . getElementsByTagName ( 'tbody' ) . item ( 0 ) ;
64
+ let rows = tableData . getElementsByTagName ( 'tr' ) ;
65
+ for ( let i = 0 ; i < rows . length - 1 ; i ++ ) {
66
+ for ( let j = 0 ; j < rows . length - ( i + 1 ) ; j ++ ) {
67
+ if ( getTdValue ( rows , j , sortColumn ) < getTdValue ( rows , j + 1 , sortColumn ) ) {
68
+ tableData . insertBefore ( rows . item ( j + 1 ) , rows . item ( j ) ) ;
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ /** The secondary request which appends the badges. */
71
75
function commits_count ( request , table_body , fork_username ) {
72
76
return ( ) => {
73
77
const response = JSON . parse ( request . responseText ) ;
@@ -89,12 +93,14 @@ function commits_count(request, table_body, fork_username) {
89
93
}
90
94
}
91
95
96
+ /** To remove erroneous repos. */
92
97
function commits_count_failure ( fork_username ) {
93
98
return ( ) => {
94
99
getElementById_$ ( fork_username ) . remove ( ) ;
95
100
}
96
101
}
97
102
103
+ /** To use the Access Token with a request. */
98
104
function authenticatedRequestHeaderFactory ( url ) {
99
105
let request = new XMLHttpRequest ( ) ;
100
106
request . open ( 'GET' , url ) ;
@@ -103,6 +109,37 @@ function authenticatedRequestHeaderFactory(url) {
103
109
return request ;
104
110
}
105
111
112
+ function onreadystatechangeFactory ( xhr , successFn , failureFn ) {
113
+ return ( ) => {
114
+ if ( xhr . readyState === 4 ) {
115
+ if ( xhr . status === 200 ) {
116
+ successFn ( ) ;
117
+ } else if ( xhr . status === 403 ) {
118
+ console . warn ( 'Looks like the rate-limit was exceeded.' ) ;
119
+ getElementById_$ ( UF_ID_MSG ) . html ( UF_MSG_API_RATE ) ;
120
+ } else {
121
+ console . warn ( 'GitHub API returned status:' , xhr . status ) ;
122
+ failureFn ( ) ;
123
+ }
124
+ } else {
125
+ // Request is still in progress
126
+ }
127
+ } ;
128
+ }
129
+
130
+ /** Dynamically fills the second part of the rows. */
131
+ function build_fork_element_html ( table_body , combined_name , num_stars , num_watches , num_forks ) {
132
+ table_body . append (
133
+ $ ( '<tr>' , { id : extract_username_from_fork ( combined_name ) , class : "useful_forks_repo" } ) . append (
134
+ $ ( '<td>' ) . html ( svg_literal_fork + ' <a href=https://github.com/' + combined_name + '>' + combined_name + '</a>' ) ,
135
+ $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR + svg_literal_star + ' x ' + num_stars ) . attr ( "value" , num_stars ) ,
136
+ $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR + svg_literal_eye + ' x ' + num_watches ) . attr ( "value" , num_watches ) ,
137
+ $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR + svg_literal_fork + ' x ' + num_forks ) . attr ( "value" , num_forks )
138
+ )
139
+ ) ;
140
+ }
141
+
142
+ /** Prepares, appends, and updates dynamically a table row. */
106
143
function add_fork_elements ( forkdata_array , user , repo ) {
107
144
if ( ! forkdata_array || forkdata_array . length === 0 )
108
145
return ;
@@ -129,31 +166,6 @@ function add_fork_elements(forkdata_array, user, repo) {
129
166
}
130
167
}
131
168
132
- function onreadystatechangeFactory ( xhr , successFn , failureFn ) {
133
- return ( ) => {
134
- if ( xhr . readyState === 4 ) {
135
- if ( xhr . status === 200 ) {
136
- successFn ( ) ;
137
- } else if ( xhr . status === 403 ) {
138
- console . warn ( 'Looks like the rate-limit was exceeded.' ) ;
139
- getElementById_$ ( UF_ID_MSG ) . html ( UF_MSG_API_RATE ) ;
140
- } else {
141
- console . warn ( 'GitHub API returned status:' , xhr . status ) ;
142
- failureFn ( ) ;
143
- }
144
- } else {
145
- // Request is still in progress
146
- }
147
- } ;
148
- }
149
-
150
- function add_css ( ) {
151
- let styleSheet = document . createElement ( 'style' ) ;
152
- styleSheet . type = "text/css" ;
153
- styleSheet . innerText = additional_css_literal ;
154
- document . head . appendChild ( styleSheet ) ;
155
- }
156
-
157
169
/** Paginated request. Pages index start at 1. */
158
170
function request_fork_page ( page_number , user , repo ) {
159
171
let request = authenticatedRequestHeaderFactory ( 'https://api.github.com/repos/' + user + '/' + repo + '/forks?sort=stargazers&per_page=' + FORKS_PER_PAGE + '&page=' + page_number )
@@ -176,6 +188,11 @@ function request_fork_page(page_number, user, repo) {
176
188
}
177
189
}
178
190
191
+ /* Half-assed sort. (todo: redo this) */
192
+ $ ( ( ) => {
193
+ sortTable ( UF_ID_TABLE , 1 ) ;
194
+ } ) ;
195
+
179
196
/* Populate the table. */
180
197
add_fork_elements ( response , user , repo ) ;
181
198
} ) ;
@@ -196,7 +213,14 @@ function prepare_display() {
196
213
) ;
197
214
}
198
215
199
- /* Entry point. */
216
+ function add_css ( ) {
217
+ let styleSheet = document . createElement ( 'style' ) ;
218
+ styleSheet . type = "text/css" ;
219
+ styleSheet . innerText = additional_css_literal ;
220
+ document . head . appendChild ( styleSheet ) ;
221
+ }
222
+
223
+ /** Entry point. */
200
224
const pathComponents = window . location . pathname . split ( '/' ) ;
201
225
if ( pathComponents . length >= 3 ) {
202
226
const user = pathComponents [ 1 ] , repo = pathComponents [ 2 ] ;
0 commit comments