mapcomplete/UI/ShareButton.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-11-22 03:16:56 +01:00
import {UIElement} from "./UIElement";
export default class ShareButton extends UIElement{
private _embedded: UIElement;
private _shareData: { text: string; title: string; url: string };
constructor(embedded: UIElement, shareData: {
text: string,
title: string,
url: string
}) {
super();
this._embedded = embedded;
this._shareData = shareData;
}
InnerRender(): string {
2020-11-22 03:50:09 +01:00
return `<button type="button" class="share-button" id="${this.id}">${this._embedded.Render()}</button>`
2020-11-22 03:16:56 +01:00
}
protected InnerUpdate(htmlElement: HTMLElement) {
super.InnerUpdate(htmlElement);
const self= this;
htmlElement.addEventListener('click', () => {
if (navigator.share) {
navigator.share(self._shareData).then(() => {
console.log('Thanks for sharing!');
})
.catch(err => {
console.log(`Couldn't share because of`, err.message);
});
} else {
console.log('web share not supported');
}
});
}
}