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 __privateMethod = (obj, member, method) => {
__accessCheck(obj, member, "access private method");
return method;
};
var _mediaController, _clickListener, _positionTooltip, _keyupListener, _keydownListener, _setupTooltip, setupTooltip_fn;
import { MediaStateReceiverAttributes } from "./constants.js";
import {
getOrInsertCSSRule,
getStringAttr,
setStringAttr
} from "./utils/element-utils.js";
import { globalThis, document } from "./utils/server-safe-globals.js";
const Attributes = {
TOOLTIP_PLACEMENT: "tooltipplacement"
};
const template = document.createElement("template");
template.innerHTML = /*html*/
`
`;
class MediaChromeButton extends globalThis.HTMLElement {
constructor(options = {}) {
var _a;
super();
// Called when we know the tooltip is ready / defined
__privateAdd(this, _setupTooltip);
__privateAdd(this, _mediaController, void 0);
this.preventClick = false;
this.tooltipEl = null;
this.tooltipContent = "";
__privateAdd(this, _clickListener, (e) => {
if (!this.preventClick) {
this.handleClick(e);
}
setTimeout(__privateGet(this, _positionTooltip), 0);
});
__privateAdd(this, _positionTooltip, () => {
var _a, _b;
(_b = (_a = this.tooltipEl) == null ? void 0 : _a.updateXOffset) == null ? void 0 : _b.call(_a);
});
// NOTE: There are definitely some "false positive" cases with multi-key pressing,
// but this should be good enough for most use cases.
__privateAdd(this, _keyupListener, (e) => {
const { key } = e;
if (!this.keysUsed.includes(key)) {
this.removeEventListener("keyup", __privateGet(this, _keyupListener));
return;
}
if (!this.preventClick) {
this.handleClick(e);
}
});
__privateAdd(this, _keydownListener, (e) => {
const { metaKey, altKey, key } = e;
if (metaKey || altKey || !this.keysUsed.includes(key)) {
this.removeEventListener("keyup", __privateGet(this, _keyupListener));
return;
}
this.addEventListener("keyup", __privateGet(this, _keyupListener), { once: true });
});
if (!this.shadowRoot) {
this.attachShadow({ mode: "open" });
const buttonHTML = template.content.cloneNode(true);
this.nativeEl = buttonHTML;
let slotTemplate = options.slotTemplate;
if (!slotTemplate) {
slotTemplate = document.createElement("template");
slotTemplate.innerHTML = `${options.defaultContent || ""}`;
}
if (options.tooltipContent) {
buttonHTML.querySelector('slot[name="tooltip-content"]').innerHTML = (_a = options.tooltipContent) != null ? _a : "";
this.tooltipContent = options.tooltipContent;
}
this.nativeEl.appendChild(slotTemplate.content.cloneNode(true));
this.shadowRoot.appendChild(buttonHTML);
}
this.tooltipEl = this.shadowRoot.querySelector("media-tooltip");
}
static get observedAttributes() {
return [
"disabled",
Attributes.TOOLTIP_PLACEMENT,
MediaStateReceiverAttributes.MEDIA_CONTROLLER
];
}
enable() {
this.addEventListener("click", __privateGet(this, _clickListener));
this.addEventListener("keydown", __privateGet(this, _keydownListener));
this.tabIndex = 0;
}
disable() {
this.removeEventListener("click", __privateGet(this, _clickListener));
this.removeEventListener("keydown", __privateGet(this, _keydownListener));
this.removeEventListener("keyup", __privateGet(this, _keyupListener));
this.tabIndex = -1;
}
attributeChangedCallback(attrName, oldValue, newValue) {
var _a, _b, _c, _d, _e;
if (attrName === MediaStateReceiverAttributes.MEDIA_CONTROLLER) {
if (oldValue) {
(_b = (_a = __privateGet(this, _mediaController)) == null ? void 0 : _a.unassociateElement) == null ? void 0 : _b.call(_a, this);
__privateSet(this, _mediaController, null);
}
if (newValue && this.isConnected) {
__privateSet(this, _mediaController, (_c = this.getRootNode()) == null ? void 0 : _c.getElementById(newValue));
(_e = (_d = __privateGet(this, _mediaController)) == null ? void 0 : _d.associateElement) == null ? void 0 : _e.call(_d, this);
}
} else if (attrName === "disabled" && newValue !== oldValue) {
if (newValue == null) {
this.enable();
} else {
this.disable();
}
} else if (attrName === Attributes.TOOLTIP_PLACEMENT && this.tooltipEl && newValue !== oldValue) {
this.tooltipEl.placement = newValue;
}
__privateGet(this, _positionTooltip).call(this);
}
connectedCallback() {
var _a, _b, _c;
const { style } = getOrInsertCSSRule(this.shadowRoot, ":host");
style.setProperty(
"display",
`var(--media-control-display, var(--${this.localName}-display, inline-flex))`
);
if (!this.hasAttribute("disabled")) {
this.enable();
}
this.setAttribute("role", "button");
const mediaControllerId = this.getAttribute(
MediaStateReceiverAttributes.MEDIA_CONTROLLER
);
if (mediaControllerId) {
__privateSet(
this,
_mediaController,
// @ts-ignore
(_a = this.getRootNode()) == null ? void 0 : _a.getElementById(mediaControllerId)
);
(_c = (_b = __privateGet(this, _mediaController)) == null ? void 0 : _b.associateElement) == null ? void 0 : _c.call(_b, this);
}
globalThis.customElements.whenDefined("media-tooltip").then(() => __privateMethod(this, _setupTooltip, setupTooltip_fn).call(this));
}
disconnectedCallback() {
var _a, _b;
this.disable();
(_b = (_a = __privateGet(this, _mediaController)) == null ? void 0 : _a.unassociateElement) == null ? void 0 : _b.call(_a, this);
__privateSet(this, _mediaController, null);
this.removeEventListener("mouseenter", __privateGet(this, _positionTooltip));
this.removeEventListener("focus", __privateGet(this, _positionTooltip));
this.removeEventListener("click", __privateGet(this, _clickListener));
}
get keysUsed() {
return ["Enter", " "];
}
/**
* Get or set tooltip placement
*/
get tooltipPlacement() {
return getStringAttr(this, Attributes.TOOLTIP_PLACEMENT);
}
set tooltipPlacement(value) {
setStringAttr(this, Attributes.TOOLTIP_PLACEMENT, value);
}
/**
* @abstract
* @argument {Event} e
*/
handleClick(e) {
}
// eslint-disable-line
}
_mediaController = new WeakMap();
_clickListener = new WeakMap();
_positionTooltip = new WeakMap();
_keyupListener = new WeakMap();
_keydownListener = new WeakMap();
_setupTooltip = new WeakSet();
setupTooltip_fn = function() {
this.addEventListener("mouseenter", __privateGet(this, _positionTooltip));
this.addEventListener("focus", __privateGet(this, _positionTooltip));
this.addEventListener("click", __privateGet(this, _clickListener));
const initialPlacement = this.tooltipPlacement;
if (initialPlacement && this.tooltipEl) {
this.tooltipEl.placement = initialPlacement;
}
};
if (!globalThis.customElements.get("media-chrome-button")) {
globalThis.customElements.define("media-chrome-button", MediaChromeButton);
}
var media_chrome_button_default = MediaChromeButton;
export {
MediaChromeButton,
media_chrome_button_default as default
};