class IconButton extends Button
{
constructor({ layoutDelegate = null, cssClassName = "", iconName = "" } = {})
{
super(layoutDelegate);
this.element.classList.add("icon");
if (!!cssClassName)
this.element.classList.add(cssClassName);
this._image = null;
this._iconName = "";
if (!!iconName)
this.iconName = iconName;
}
get iconName()
{
return this._iconName;
}
set iconName(iconName)
{
if (this._iconName === iconName)
return;
if (this._image)
this._image.removeEventListener("load", this);
this._image = iconService.imageForIconNameAndLayoutTraits(iconName, this.layoutTraits);
this._iconName = iconName;
if (this._image.complete)
this._updateImage();
else
this._image.addEventListener("load", this);
}
get on()
{
return this.element.classList.contains("on");
}
set on(flag) {
this.element.classList.toggle("on", flag);
}
handleEvent(event)
{
if (event.type === "load" && event.target === this._image)
this._imageDidLoad();
else
super.handleEvent(event);
}
layout()
{
super.layout();
this.element.style.webkitMaskImage = `url(${this._image.src})`;
this.element.style.webkitMaskSize = `${this.width}px ${this.height}px`;
}
_imageDidLoad()
{
this._image.removeEventListener("load", this);
this._updateImage();
}
_updateImage()
{
this.width = this._image.width / window.devicePixelRatio;
this.height = this._image.height / window.devicePixelRatio;
this.needsLayout = true;
}
}