@@ -29,6 +29,18 @@ const UF_TABLE_SEPARATOR = " | ";
2929
3030const FORKS_PER_PAGE = 100 ; // enforced by GitHub API
3131
32+ let REQUESTS_COUNTER = 0 ; // to know when it's over
33+
34+
35+ function allRequestsAreDone ( ) {
36+ return REQUESTS_COUNTER <= 0 ;
37+ }
38+
39+ function checkIfAllRequestsAreDone ( ) {
40+ if ( allRequestsAreDone ( ) ) {
41+ sortTable ( ) ;
42+ }
43+ }
3244
3345function extract_username_from_fork ( combined_name ) {
3446 return combined_name . split ( '/' ) [ 0 ] ;
@@ -54,11 +66,15 @@ function getElementById_$(id) {
5466}
5567
5668function getTdValue ( rows , index , col ) {
57- return rows . item ( index ) . getElementsByTagName ( 'td' ) . item ( col ) . getAttribute ( "value" ) ;
69+ return Number ( rows . item ( index ) . getElementsByTagName ( 'td' ) . item ( col ) . getAttribute ( "value" ) ) ;
70+ }
71+
72+ function sortTable ( ) {
73+ sortTableColumn ( UF_ID_TABLE , 1 ) ;
5874}
5975
6076/** 'sortColumn' index starts at 0. https://stackoverflow.com/a/37814596/9768291 */
61- function sortTable ( table_id , sortColumn ) {
77+ function sortTableColumn ( table_id , sortColumn ) {
6278 let tableData = document . getElementById ( table_id ) . getElementsByTagName ( 'tbody' ) . item ( 0 ) ;
6379 let rows = tableData . getElementsByTagName ( 'tr' ) ;
6480 for ( let i = 0 ; i < rows . length - 1 ; i ++ ) {
@@ -88,16 +104,32 @@ function commits_count(request, table_body, table_row) {
88104 $ ( '<td>' ) . html ( behind_badge ( response . behind_by ) )
89105 )
90106 }
107+
108+ /* Detection of final request. */
109+ REQUESTS_COUNTER -- ;
110+ checkIfAllRequestsAreDone ( ) ;
91111 }
92112}
93113
94114/** To remove erroneous repos. */
95115function commits_count_failure ( table_row ) {
96116 return ( ) => {
97117 table_row . remove ( ) ;
118+
119+ /* Detection of final request. */
120+ REQUESTS_COUNTER -- ;
121+ checkIfAllRequestsAreDone ( ) ;
98122 }
99123}
100124
125+ function clearMsg ( ) {
126+ getElementById_$ ( UF_ID_MSG ) . html ( "" ) ;
127+ }
128+
129+ function getTableBody ( ) {
130+ return getElementById_$ ( UF_ID_TABLE ) . find ( $ ( "tbody" ) ) ;
131+ }
132+
101133/** To use the Access Token with a request. */
102134function authenticatedRequestHeaderFactory ( url ) {
103135 let request = new XMLHttpRequest ( ) ;
@@ -131,7 +163,7 @@ function build_fork_element_html(table_body, combined_name, num_stars, num_watch
131163 const NEW_ROW = $ ( '<tr>' , { id : extract_username_from_fork ( combined_name ) , class : "useful_forks_repo" } ) ;
132164 table_body . append (
133165 NEW_ROW . append (
134- $ ( '<td>' ) . html ( svg_literal_fork + ' <a href=https://github.com/' + combined_name + '>' + combined_name + '</a>' ) ,
166+ $ ( '<td>' ) . html ( svg_literal_fork + ' <a href=https://github.com/' + combined_name + ' target="_blank" rel="noopener noreferrer" >' + combined_name + '</a>' ) ,
135167 $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR + svg_literal_star + ' x ' + num_stars ) . attr ( "value" , num_stars ) ,
136168 $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR + svg_literal_eye + ' x ' + num_watches ) . attr ( "value" , num_watches ) ,
137169 $ ( '<td>' ) . html ( UF_TABLE_SEPARATOR + svg_literal_fork + ' x ' + num_forks ) . attr ( "value" , num_forks )
@@ -145,17 +177,18 @@ function add_fork_elements(forkdata_array, user, repo) {
145177 if ( ! forkdata_array || forkdata_array . length === 0 )
146178 return ;
147179
148- getElementById_$ ( UF_ID_MSG ) . html ( "" ) ;
149- let table_body = getElementById_$ ( UF_ID_TABLE ) . find ( "tbody" ) ;
180+ clearMsg ( ) ;
150181
151- for ( let i = 0 ; i < Math . min ( FORKS_PER_PAGE , forkdata_array . length ) ; ++ i ) {
182+ let table_body = getTableBody ( ) ;
183+ for ( let i = 0 ; i < forkdata_array . length ; ++ i ) {
152184 const elem_ref = forkdata_array [ i ] ;
153185
154186 /* Basic data (stars, watchers, forks). */
155187 const NEW_ROW = build_fork_element_html ( table_body , elem_ref . full_name , elem_ref . stargazers_count , elem_ref . watchers_count , elem_ref . forks_count ) ;
156188
157189 /* Commits diff data (ahead/behind). */
158- let request = authenticatedRequestHeaderFactory ( 'https://api.github.com/repos/' + user + '/' + repo + '/compare/master...' + extract_username_from_fork ( elem_ref . full_name ) + ':master' ) ;
190+ const API_REQUEST_URL = 'https://api.github.com/repos/' + user + '/' + repo + '/compare/master...' + extract_username_from_fork ( elem_ref . full_name ) + ':master' ;
191+ let request = authenticatedRequestHeaderFactory ( API_REQUEST_URL ) ;
159192 request . onreadystatechange = onreadystatechangeFactory ( request , commits_count ( request , table_body , NEW_ROW ) , commits_count_failure ( NEW_ROW ) ) ;
160193 request . send ( ) ;
161194
@@ -168,17 +201,22 @@ function add_fork_elements(forkdata_array, user, repo) {
168201
169202/** Paginated request. Pages index start at 1. */
170203function request_fork_page ( page_number , user , repo ) {
171- let request = authenticatedRequestHeaderFactory ( 'https://api.github.com/repos/' + user + '/' + repo + '/forks?sort=stargazers&per_page=' + FORKS_PER_PAGE + '&page=' + page_number )
204+ const API_REQUEST_URL = 'https://api.github.com/repos/' + user + '/' + repo + '/forks?sort=stargazers&per_page=' + FORKS_PER_PAGE + '&page=' + page_number ;
205+ let request = authenticatedRequestHeaderFactory ( API_REQUEST_URL ) ;
172206 request . onreadystatechange = onreadystatechangeFactory ( request ,
173207 ( ) => {
174208 const response = JSON . parse ( request . responseText ) ;
209+
210+ /* On empty response (repo has not been forked). */
175211 if ( ! response || response . length === 0 ) {
176212 if ( page_number === 1 ) {
177213 getElementById_$ ( UF_ID_MSG ) . html ( UF_MSG_NO_FORKS ) ;
178214 }
179215 return ;
180216 }
181217
218+ REQUESTS_COUNTER += response . length ; // to keep track of when the query ends
219+
182220 /* Pagination (beyond 100 forks). */
183221 const link_header = request . getResponseHeader ( "link" ) ;
184222 if ( link_header ) {
@@ -188,13 +226,14 @@ function request_fork_page(page_number, user, repo) {
188226 }
189227 }
190228
191- /* Half-assed sort. (todo: redo this) */
192- $ ( ( ) => {
193- sortTable ( UF_ID_TABLE , 1 ) ;
194- } ) ;
229+ sortTable ( ) ;
195230
196231 /* Populate the table. */
197232 add_fork_elements ( response , user , repo ) ;
233+ } ,
234+ ( ) => {
235+ getElementById_$ ( UF_ID_MSG ) . html ( UF_MSG_ERROR ) ;
236+ checkIfAllRequestsAreDone ( ) ;
198237 } ) ;
199238 request . send ( ) ;
200239}
0 commit comments