mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Add toPercent utility for compatibility
This commit is contained in:
parent
b008f2db30
commit
9cc6ca66e8
2 changed files with 25 additions and 4 deletions
|
@ -2,6 +2,7 @@ import './emby-progressring.scss';
|
||||||
import 'webcomponents.js/webcomponents-lite';
|
import 'webcomponents.js/webcomponents-lite';
|
||||||
import template from './emby-progressring.template.html';
|
import template from './emby-progressring.template.html';
|
||||||
import { getCurrentDateTimeLocale } from '../../scripts/globalize';
|
import { getCurrentDateTimeLocale } from '../../scripts/globalize';
|
||||||
|
import { toPercent } from '../../utils/number.ts';
|
||||||
|
|
||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
|
|
||||||
|
@ -72,10 +73,7 @@ import { getCurrentDateTimeLocale } from '../../scripts/globalize';
|
||||||
this.querySelector('.animate-75-100-b').style.transform = 'rotate(' + angle + 'deg)';
|
this.querySelector('.animate-75-100-b').style.transform = 'rotate(' + angle + 'deg)';
|
||||||
}
|
}
|
||||||
|
|
||||||
this.querySelector('.progressring-text').innerHTML = new Intl.NumberFormat(getCurrentDateTimeLocale(), {
|
this.querySelector('.progressring-text').innerHTML = toPercent(progress / 100, getCurrentDateTimeLocale());
|
||||||
style: 'percent',
|
|
||||||
maximumFractionDigits: 0
|
|
||||||
}).format(progress / 100);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbyProgressRing.attachedCallback = function () {
|
EmbyProgressRing.attachedCallback = function () {
|
||||||
|
|
23
src/utils/number.ts
Normal file
23
src/utils/number.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
function toLocaleStringSupportsOptions() {
|
||||||
|
return !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of a number formatted as a perentage.
|
||||||
|
* @param {number} value The value as a number.
|
||||||
|
* @returns {string} The value formatted as a percentage.
|
||||||
|
*/
|
||||||
|
export function toPercent(value: number | null | undefined, locale: string): string {
|
||||||
|
if (value == null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toLocaleStringSupportsOptions()) {
|
||||||
|
return value.toLocaleString(locale, {
|
||||||
|
style: 'percent',
|
||||||
|
maximumFractionDigits: 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${Math.round(value * 100)}%`;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue