mapcomplete/UI/BigComponents/ShareButton.ts

40 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-06-12 02:58:32 +02:00
import BaseUIElement from "../BaseUIElement";
2020-11-22 03:16:56 +01:00
export default class ShareButton extends BaseUIElement {
2021-06-12 02:58:32 +02:00
private _embedded: BaseUIElement;
2021-06-15 01:24:04 +02:00
private _shareData: () => { text: string; title: string; url: string };
2021-06-15 01:24:04 +02:00
constructor(embedded: BaseUIElement, generateShareData: () => {
2020-11-22 03:16:56 +01:00
text: string,
title: string,
url: string
}) {
super();
this._embedded = embedded;
2021-06-15 01:24:04 +02:00
this._shareData = generateShareData;
2021-06-12 02:58:32 +02:00
this.SetClass("share-button")
2020-11-22 03:16:56 +01:00
}
2021-06-12 02:58:32 +02:00
protected InnerConstructElement(): HTMLElement {
const e = document.createElement("button")
e.type = "button"
e.appendChild(this._embedded.ConstructElement())
2021-06-12 02:58:32 +02:00
e.addEventListener('click', () => {
2020-11-22 03:16:56 +01:00
if (navigator.share) {
2021-06-15 01:24:04 +02:00
navigator.share(this._shareData()).then(() => {
2020-11-22 03:16:56 +01:00
console.log('Thanks for sharing!');
})
.catch(err => {
console.log(`Couldn't share because of`, err.message);
});
} else {
console.log('web share not supported');
}
});
2021-06-12 02:58:32 +02:00
return e;
2020-11-22 03:16:56 +01:00
}
2021-06-12 02:58:32 +02:00
2020-11-22 03:16:56 +01:00
}