2
2
const GITHUB_API = 'https://api.github.com/graphql'
3
3
const GITHUB_API_V3 = 'https://api.github.com/repos/'
4
4
const REPO_STATS_CLASS = 'numbers-summary'
5
+ const REPO_REFRESH_STATS_QUERY = '.repository-content .Box .Details ul'
5
6
const REPO_SIZE_ID = 'addon-repo-size'
6
7
const SIZE_KILO = 1024
7
8
const UNITS = [ 'B' , 'KiB' , 'MiB' , 'GiB' , 'TiB' , 'PiB' , 'EiB' , 'ZiB' , 'YiB' ]
@@ -119,13 +120,23 @@ const injectRepoSize = async () => {
119
120
const repoInfo = getRepoInfo ( window . location . pathname . substring ( 1 ) )
120
121
121
122
if ( repoInfo != null ) {
123
+ let statsElt , isRefresh = false
122
124
const statsCol = document . getElementsByClassName ( REPO_STATS_CLASS )
123
125
124
- if ( statsCol . length !== 1 ) {
125
- return
126
+ if ( statsCol . length === 0 ) {
127
+ // maybe we're on the design refresh
128
+ const statsRow = document . querySelector ( REPO_REFRESH_STATS_QUERY )
129
+ if ( statsRow == null ) {
130
+ // can't find any element to add our stats element, we stop here
131
+ return
132
+ }
133
+
134
+ isRefresh = true
135
+ statsElt = statsRow
136
+ } else {
137
+ statsElt = statsCol [ 0 ]
126
138
}
127
139
128
- const statsElt = statsCol [ 0 ]
129
140
const repoSizeElt = document . getElementById ( REPO_SIZE_ID )
130
141
131
142
// nothing to do if we already have the size displayed
@@ -156,8 +167,8 @@ const injectRepoSize = async () => {
156
167
}
157
168
158
169
const humanSize = getHumanFileSize ( repoSize * 1024 )
159
-
160
- createSizeWrapperElement ( statsElt , createSizeElements ( humanSize ) )
170
+ const sizeElt = isRefresh ? createRepoRefreshSizeElements ( humanSize ) : createSizeElements ( humanSize )
171
+ createSizeWrapperElement ( statsElt , createSizeElements ( humanSize ) , isRefresh )
161
172
}
162
173
}
163
174
@@ -180,7 +191,19 @@ const createSizeElements = repoSizeHuman => {
180
191
return [ size , whiteSpace , unitText ]
181
192
}
182
193
183
- const createSizeWrapperElement = async ( parent , children ) => {
194
+ const createRepoRefreshSizeElements = repoSizeHuman => {
195
+ const size = document . createElement ( 'strong' )
196
+ const sizeText = document . createTextNode ( repoSizeHuman . size )
197
+ size . appendChild ( sizeText )
198
+
199
+ const whiteSpace = document . createTextNode ( ' ' )
200
+
201
+ const unitText = document . createTextNode ( repoSizeHuman . unit )
202
+
203
+ return [ size , whiteSpace , unitText ]
204
+ }
205
+
206
+ const createSizeWrapperElement = async ( parent , children , isRefresh = false ) => {
184
207
const storedToken = await getStoredSetting ( TOKEN_KEY )
185
208
let tokenInfo = '' , tokenPlaceholder = ''
186
209
if ( storedToken ) {
@@ -201,7 +224,7 @@ const createSizeWrapperElement = async (parent, children) => {
201
224
li . innerHTML = `
202
225
<details id="${ MODAL_ID } -size-stat-wrapper" class="details-reset details-overlay details-overlay-dark">
203
226
<summary>
204
- <a href="#" id="${ MODAL_ID } -size-stat-content">
227
+ <a id="${ MODAL_ID } -size-stat-content" ${ isRefresh ? 'class="link-gray-dark no-underline d-inline-block"' : '' } >
205
228
<svg class="octicon octicon-database" height="16" width="14" viewBox="0 0 14 16" aria-hidden="true" version="1.1"><path d="M6,15 C2.69,15 0,14.1 0,13 L0,11 C0,10.83 0.09,10.66 0.21,10.5 C0.88,11.36 3.21,12 6,12 C8.79,12 11.12,11.36 11.79,10.5 C11.92,10.66 12,10.83 12,11 L12,13 C12,14.1 9.31,15 6,15 L6,15 Z M6,11 C2.69,11 0,10.1 0,9 L0,7 C0,6.89 0.04,6.79 0.09,6.69 L0.09,6.69 C0.12,6.63 0.16,6.56 0.21,6.5 C0.88,7.36 3.21,8 6,8 C8.79,8 11.12,7.36 11.79,6.5 C11.84,6.56 11.88,6.63 11.91,6.69 L11.91,6.69 C11.96,6.79 12,6.9 12,7 L12,9 C12,10.1 9.31,11 6,11 L6,11 Z M6,7 C2.69,7 0,6.1 0,5 L0,4 L0,3 C0,1.9 2.69,1 6,1 C9.31,1 12,1.9 12,3 L12,4 L12,5 C12,6.1 9.31,7 6,7 L6,7 Z M6,2 C3.79,2 2,2.45 2,3 C2,3.55 3.79,4 6,4 C8.21,4 10,3.55 10,3 C10,2.45 8.21,2 6,2 L6,2 Z" fill-rule="evenodd"></path></svg>
206
229
</a>
207
230
</summary>
@@ -239,7 +262,6 @@ const createSizeWrapperElement = async (parent, children) => {
239
262
parent . appendChild ( li )
240
263
241
264
const elt = document . getElementById ( `${ MODAL_ID } -size-stat-content` )
242
- elt . setAttribute ( 'href' , '#' )
243
265
elt . addEventListener ( 'click' , askForToken )
244
266
elt . appendChild ( document . createTextNode ( ' ' ) )
245
267
@@ -248,6 +270,10 @@ const createSizeWrapperElement = async (parent, children) => {
248
270
249
271
const form = document . getElementById ( `${ MODAL_ID } -form` )
250
272
form . addEventListener ( 'submit' , saveToken )
273
+
274
+ if ( isRefresh ) {
275
+ li . className = 'ml-3 d-none d-md-block'
276
+ }
251
277
252
278
children . forEach ( c => elt . appendChild ( c ) )
253
279
}
0 commit comments