@@ -3,6 +3,7 @@ import { Card } from "../common/Card.js";
3
3
import { I18n } from "../common/I18n.js" ;
4
4
import { icons , rankIcon } from "../common/icons.js" ;
5
5
import {
6
+ CustomError ,
6
7
clampValue ,
7
8
flexLayout ,
8
9
getCardColors ,
@@ -16,6 +17,8 @@ const CARD_MIN_WIDTH = 287;
16
17
const CARD_DEFAULT_WIDTH = 287 ;
17
18
const RANK_CARD_MIN_WIDTH = 420 ;
18
19
const RANK_CARD_DEFAULT_WIDTH = 450 ;
20
+ const RANK_ONLY_CARD_MIN_WIDTH = 290 ;
21
+ const RANK_ONLY_CARD_DEFAULT_WIDTH = 290 ;
19
22
20
23
/**
21
24
* Create a stats card text item.
@@ -234,11 +237,18 @@ const renderStatsCard = (stats = {}, options = {}) => {
234
237
} ) ,
235
238
) ;
236
239
240
+ if ( statItems . length === 0 && hide_rank ) {
241
+ throw new CustomError (
242
+ "Could not render stats card." ,
243
+ "Either stats or rank are required." ,
244
+ ) ;
245
+ }
246
+
237
247
// Calculate the card height depending on how many items there are
238
248
// but if rank circle is visible clamp the minimum height to `150`
239
249
let height = Math . max (
240
250
45 + ( statItems . length + 1 ) * lheight ,
241
- hide_rank ? 0 : 150 ,
251
+ hide_rank ? 0 : statItems . length ? 150 : 180 ,
242
252
) ;
243
253
244
254
// the lower the user's percentile the better
@@ -253,33 +263,47 @@ const renderStatsCard = (stats = {}, options = {}) => {
253
263
} ) ;
254
264
255
265
const calculateTextWidth = ( ) => {
256
- return measureText ( custom_title ? custom_title : i18n . t ( "statcard.title" ) ) ;
266
+ return measureText (
267
+ custom_title
268
+ ? custom_title
269
+ : statItems . length
270
+ ? i18n . t ( "statcard.title" )
271
+ : i18n . t ( "statcard.ranktitle" ) ,
272
+ ) ;
257
273
} ;
258
274
259
275
/*
260
276
When hide_rank=true, the minimum card width is 270 px + the title length and padding.
261
277
When hide_rank=false, the minimum card_width is 340 px + the icon width (if show_icons=true).
262
278
Numbers are picked by looking at existing dimensions on production.
263
279
*/
264
- const iconWidth = show_icons ? 16 + /* padding */ 1 : 0 ;
280
+ const iconWidth = show_icons && statItems . length ? 16 + /* padding */ 1 : 0 ;
265
281
const minCardWidth =
266
282
( hide_rank
267
283
? clampValue (
268
284
50 /* padding */ + calculateTextWidth ( ) * 2 ,
269
285
CARD_MIN_WIDTH ,
270
286
Infinity ,
271
287
)
272
- : RANK_CARD_MIN_WIDTH ) + iconWidth ;
288
+ : statItems . length
289
+ ? RANK_CARD_MIN_WIDTH
290
+ : RANK_ONLY_CARD_MIN_WIDTH ) + iconWidth ;
273
291
const defaultCardWidth =
274
- ( hide_rank ? CARD_DEFAULT_WIDTH : RANK_CARD_DEFAULT_WIDTH ) + iconWidth ;
292
+ ( hide_rank
293
+ ? CARD_DEFAULT_WIDTH
294
+ : statItems . length
295
+ ? RANK_CARD_DEFAULT_WIDTH
296
+ : RANK_ONLY_CARD_DEFAULT_WIDTH ) + iconWidth ;
275
297
let width = isNaN ( card_width ) ? defaultCardWidth : card_width ;
276
298
if ( width < minCardWidth ) {
277
299
width = minCardWidth ;
278
300
}
279
301
280
302
const card = new Card ( {
281
303
customTitle : custom_title ,
282
- defaultTitle : i18n . t ( "statcard.title" ) ,
304
+ defaultTitle : statItems . length
305
+ ? i18n . t ( "statcard.title" )
306
+ : i18n . t ( "statcard.ranktitle" ) ,
283
307
width,
284
308
height,
285
309
border_radius,
@@ -309,12 +333,16 @@ const renderStatsCard = (stats = {}, options = {}) => {
309
333
* @returns {number } - Rank circle translation value.
310
334
*/
311
335
const calculateRankXTranslation = ( ) => {
312
- const minXTranslation = RANK_CARD_MIN_WIDTH + iconWidth - 70 ;
313
- if ( width > RANK_CARD_DEFAULT_WIDTH ) {
314
- const xMaxExpansion = minXTranslation + ( 450 - minCardWidth ) / 2 ;
315
- return xMaxExpansion + width - RANK_CARD_DEFAULT_WIDTH ;
336
+ if ( statItems . length ) {
337
+ const minXTranslation = RANK_CARD_MIN_WIDTH + iconWidth - 70 ;
338
+ if ( width > RANK_CARD_DEFAULT_WIDTH ) {
339
+ const xMaxExpansion = minXTranslation + ( 450 - minCardWidth ) / 2 ;
340
+ return xMaxExpansion + width - RANK_CARD_DEFAULT_WIDTH ;
341
+ } else {
342
+ return minXTranslation + ( width - minCardWidth ) / 2 ;
343
+ }
316
344
} else {
317
- return minXTranslation + ( width - minCardWidth ) / 2 ;
345
+ return width / 2 + 20 - 10 ;
318
346
}
319
347
} ;
320
348
0 commit comments