Files
pole-book/server/node_modules/media-chrome/dist/media-time-display.js

245 lines
7.6 KiB
JavaScript

var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
var _slot;
import { MediaTextDisplay } from "./media-text-display.js";
import {
getBooleanAttr,
getNumericAttr,
getOrInsertCSSRule,
setBooleanAttr,
setNumericAttr
} from "./utils/element-utils.js";
import { globalThis } from "./utils/server-safe-globals.js";
import { formatAsTimePhrase, formatTime } from "./utils/time.js";
import { MediaUIAttributes } from "./constants.js";
import { nouns } from "./labels/labels.js";
const Attributes = {
REMAINING: "remaining",
SHOW_DURATION: "showduration",
NO_TOGGLE: "notoggle"
};
const CombinedAttributes = [
...Object.values(Attributes),
MediaUIAttributes.MEDIA_CURRENT_TIME,
MediaUIAttributes.MEDIA_DURATION,
MediaUIAttributes.MEDIA_SEEKABLE
];
const ButtonPressedKeys = ["Enter", " "];
const DEFAULT_TIMES_SEP = " / ";
const formatTimesLabel = (el, { timesSep = DEFAULT_TIMES_SEP } = {}) => {
var _a, _b;
const showRemaining = el.hasAttribute(Attributes.REMAINING);
const showDuration = el.hasAttribute(Attributes.SHOW_DURATION);
const currentTime = (_a = el.mediaCurrentTime) != null ? _a : 0;
const [, seekableEnd] = (_b = el.mediaSeekable) != null ? _b : [];
let endTime = 0;
if (Number.isFinite(el.mediaDuration)) {
endTime = el.mediaDuration;
} else if (Number.isFinite(seekableEnd)) {
endTime = seekableEnd;
}
const timeLabel = showRemaining ? formatTime(0 - (endTime - currentTime)) : formatTime(currentTime);
if (!showDuration)
return timeLabel;
return `${timeLabel}${timesSep}${formatTime(endTime)}`;
};
const DEFAULT_MISSING_TIME_PHRASE = "video not loaded, unknown time.";
const updateAriaValueText = (el) => {
var _a;
const currentTime = el.mediaCurrentTime;
const [, seekableEnd] = (_a = el.mediaSeekable) != null ? _a : [];
let endTime = null;
if (Number.isFinite(el.mediaDuration)) {
endTime = el.mediaDuration;
} else if (Number.isFinite(seekableEnd)) {
endTime = seekableEnd;
}
if (currentTime == null || endTime === null) {
el.setAttribute("aria-valuetext", DEFAULT_MISSING_TIME_PHRASE);
return;
}
const showRemaining = el.hasAttribute(Attributes.REMAINING);
const showDuration = el.hasAttribute(Attributes.SHOW_DURATION);
const currentTimePhrase = showRemaining ? formatAsTimePhrase(0 - (endTime - currentTime)) : formatAsTimePhrase(currentTime);
if (!showDuration) {
el.setAttribute("aria-valuetext", currentTimePhrase);
return;
}
const totalTimePhrase = formatAsTimePhrase(endTime);
const fullPhrase = `${currentTimePhrase} of ${totalTimePhrase}`;
el.setAttribute("aria-valuetext", fullPhrase);
};
class MediaTimeDisplay extends MediaTextDisplay {
constructor() {
super();
__privateAdd(this, _slot, void 0);
__privateSet(this, _slot, this.shadowRoot.querySelector("slot"));
__privateGet(this, _slot).innerHTML = `${formatTimesLabel(this)}`;
}
static get observedAttributes() {
return [...super.observedAttributes, ...CombinedAttributes, "disabled"];
}
connectedCallback() {
const { style } = getOrInsertCSSRule(
this.shadowRoot,
":host(:hover:not([notoggle]))"
);
style.setProperty("cursor", "pointer");
style.setProperty(
"background",
"var(--media-control-hover-background, rgba(50 50 70 / .7))"
);
if (!this.hasAttribute("disabled")) {
this.enable();
}
this.setAttribute("role", "progressbar");
this.setAttribute("aria-label", nouns.PLAYBACK_TIME());
const keyUpHandler = (evt) => {
const { key } = evt;
if (!ButtonPressedKeys.includes(key)) {
this.removeEventListener("keyup", keyUpHandler);
return;
}
this.toggleTimeDisplay();
};
this.addEventListener("keydown", (evt) => {
const { metaKey, altKey, key } = evt;
if (metaKey || altKey || !ButtonPressedKeys.includes(key)) {
this.removeEventListener("keyup", keyUpHandler);
return;
}
this.addEventListener("keyup", keyUpHandler);
});
this.addEventListener("click", this.toggleTimeDisplay);
super.connectedCallback();
}
toggleTimeDisplay() {
if (this.noToggle) {
return;
}
if (this.hasAttribute("remaining")) {
this.removeAttribute("remaining");
} else {
this.setAttribute("remaining", "");
}
}
disconnectedCallback() {
this.disable();
super.disconnectedCallback();
}
attributeChangedCallback(attrName, oldValue, newValue) {
if (CombinedAttributes.includes(attrName)) {
this.update();
} else if (attrName === "disabled" && newValue !== oldValue) {
if (newValue == null) {
this.enable();
} else {
this.disable();
}
}
super.attributeChangedCallback(attrName, oldValue, newValue);
}
enable() {
this.tabIndex = 0;
}
disable() {
this.tabIndex = -1;
}
// Own props
/**
* Whether to show the remaining time
*/
get remaining() {
return getBooleanAttr(this, Attributes.REMAINING);
}
set remaining(show) {
setBooleanAttr(this, Attributes.REMAINING, show);
}
/**
* Whether to show the duration
*/
get showDuration() {
return getBooleanAttr(this, Attributes.SHOW_DURATION);
}
set showDuration(show) {
setBooleanAttr(this, Attributes.SHOW_DURATION, show);
}
/**
* Disable the default behavior that toggles between current and remaining time
*/
get noToggle() {
return getBooleanAttr(this, Attributes.NO_TOGGLE);
}
set noToggle(noToggle) {
setBooleanAttr(this, Attributes.NO_TOGGLE, noToggle);
}
// Props derived from media UI attributes
/**
* Get the duration
*/
get mediaDuration() {
return getNumericAttr(this, MediaUIAttributes.MEDIA_DURATION);
}
set mediaDuration(time) {
setNumericAttr(this, MediaUIAttributes.MEDIA_DURATION, time);
}
/**
* The current time in seconds
*/
get mediaCurrentTime() {
return getNumericAttr(this, MediaUIAttributes.MEDIA_CURRENT_TIME);
}
set mediaCurrentTime(time) {
setNumericAttr(this, MediaUIAttributes.MEDIA_CURRENT_TIME, time);
}
/**
* Range of values that can be seeked to.
* An array of two numbers [start, end]
*/
get mediaSeekable() {
const seekable = this.getAttribute(MediaUIAttributes.MEDIA_SEEKABLE);
if (!seekable)
return void 0;
return seekable.split(":").map((time) => +time);
}
set mediaSeekable(range) {
if (range == null) {
this.removeAttribute(MediaUIAttributes.MEDIA_SEEKABLE);
return;
}
this.setAttribute(MediaUIAttributes.MEDIA_SEEKABLE, range.join(":"));
}
update() {
const timesLabel = formatTimesLabel(this);
updateAriaValueText(this);
if (timesLabel !== __privateGet(this, _slot).innerHTML) {
__privateGet(this, _slot).innerHTML = timesLabel;
}
}
}
_slot = new WeakMap();
if (!globalThis.customElements.get("media-time-display")) {
globalThis.customElements.define("media-time-display", MediaTimeDisplay);
}
var media_time_display_default = MediaTimeDisplay;
export {
Attributes,
media_time_display_default as default
};