jellyfish-web/src/elements/emby-progressring/emby-progressring.js

104 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-01-26 15:42:02 -05:00
import './emby-progressring.scss';
2020-09-08 02:43:56 -04:00
import 'webcomponents.js/webcomponents-lite';
2020-11-25 00:17:24 -05:00
import template from './emby-progressring.template.html';
2022-07-03 13:40:58 -04:00
import { getCurrentDateTimeLocale } from '../../scripts/globalize';
/* eslint-disable indent */
2020-07-17 10:33:31 +02:00
const EmbyProgressRing = Object.create(HTMLDivElement.prototype);
EmbyProgressRing.createdCallback = function () {
this.classList.add('progressring');
2022-07-03 13:40:58 -04:00
this.setAttribute('dir', 'ltr');
const instance = this;
2020-11-25 00:17:24 -05:00
instance.innerHTML = template;
2020-11-25 00:17:24 -05:00
if (window.MutationObserver) {
// create an observer instance
const observer = new MutationObserver(function (mutations) {
2021-01-26 22:20:12 -05:00
mutations.forEach(function () {
2020-11-25 00:17:24 -05:00
instance.setProgress(parseFloat(instance.getAttribute('data-progress') || '0'));
2020-07-19 16:15:11 +02:00
});
2020-11-25 00:17:24 -05:00
});
2020-07-19 16:15:11 +02:00
2020-11-25 00:17:24 -05:00
// configuration of the observer:
const config = { attributes: true, childList: false, characterData: false };
2020-07-19 16:15:11 +02:00
2020-11-25 00:17:24 -05:00
// pass in the target node, as well as the observer options
observer.observe(instance, config);
2020-07-19 16:15:11 +02:00
2020-11-25 00:17:24 -05:00
instance.observer = observer;
}
2020-11-25 00:17:24 -05:00
instance.setProgress(parseFloat(instance.getAttribute('data-progress') || '0'));
};
EmbyProgressRing.setProgress = function (progress) {
2018-10-23 01:05:09 +03:00
progress = Math.floor(progress);
let angle;
if (progress < 25) {
angle = -90 + (progress / 100) * 360;
this.querySelector('.animate-0-25-b').style.transform = 'rotate(' + angle + 'deg)';
this.querySelector('.animate-25-50-b').style.transform = 'rotate(-90deg)';
this.querySelector('.animate-50-75-b').style.transform = 'rotate(-90deg)';
this.querySelector('.animate-75-100-b').style.transform = 'rotate(-90deg)';
} else if (progress >= 25 && progress < 50) {
angle = -90 + ((progress - 25) / 100) * 360;
this.querySelector('.animate-0-25-b').style.transform = 'none';
this.querySelector('.animate-25-50-b').style.transform = 'rotate(' + angle + 'deg)';
this.querySelector('.animate-50-75-b').style.transform = 'rotate(-90deg)';
this.querySelector('.animate-75-100-b').style.transform = 'rotate(-90deg)';
} else if (progress >= 50 && progress < 75) {
angle = -90 + ((progress - 50) / 100) * 360;
this.querySelector('.animate-0-25-b').style.transform = 'none';
this.querySelector('.animate-25-50-b').style.transform = 'none';
this.querySelector('.animate-50-75-b').style.transform = 'rotate(' + angle + 'deg)';
this.querySelector('.animate-75-100-b').style.transform = 'rotate(-90deg)';
} else if (progress >= 75 && progress <= 100) {
angle = -90 + ((progress - 75) / 100) * 360;
this.querySelector('.animate-0-25-b').style.transform = 'none';
this.querySelector('.animate-25-50-b').style.transform = 'none';
this.querySelector('.animate-50-75-b').style.transform = 'none';
this.querySelector('.animate-75-100-b').style.transform = 'rotate(' + angle + 'deg)';
}
2022-07-03 13:40:58 -04:00
this.querySelector('.progressring-text').innerHTML = new Intl.NumberFormat(getCurrentDateTimeLocale(), {
style: 'percent',
maximumFractionDigits: 0
}).format(progress / 100);
};
EmbyProgressRing.attachedCallback = function () {
// no-op
};
EmbyProgressRing.detachedCallback = function () {
2020-07-17 10:33:31 +02:00
const observer = this.observer;
if (observer) {
// later, you can stop observing
observer.disconnect();
this.observer = null;
}
};
document.registerElement('emby-progressring', {
2018-10-23 01:05:09 +03:00
prototype: EmbyProgressRing,
extends: 'div'
});
export default EmbyProgressRing;
/* eslint-enable indent */