Retrospring/app/javascript/retrospring/controllers/inbox_sharing_controller.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
static targets = ['twitter', 'bluesky', 'tumblr', 'telegram', 'other', 'custom', 'clipboard'];
declare readonly twitterTarget: HTMLAnchorElement;
declare readonly blueskyTarget: HTMLAnchorElement;
declare readonly tumblrTarget: HTMLAnchorElement;
2023-02-19 11:58:47 -08:00
declare readonly telegramTarget: HTMLAnchorElement;
declare readonly customTarget: HTMLAnchorElement;
declare readonly otherTarget: HTMLButtonElement;
declare readonly clipboardTarget: HTMLButtonElement;
declare readonly hasCustomTarget: boolean;
static values = {
config: Object,
autoClose: Boolean
};
declare readonly configValue: Record<string, string>;
declare readonly autoCloseValue: boolean;
2023-02-05 11:18:51 -08:00
connect(): void {
if (this.autoCloseValue) {
this.twitterTarget.addEventListener('click', () => this.close());
this.blueskyTarget.addEventListener('click', () => this.close());
this.tumblrTarget.addEventListener('click', () => this.close());
2023-02-19 11:58:47 -08:00
this.telegramTarget.addEventListener('click', () => this.close());
this.otherTarget.addEventListener('click', () => this.closeAfterShare());
this.clipboardTarget.addEventListener('click', () => this.closeAfterCopyToClipboard());
if (this.hasCustomTarget) {
this.customTarget.addEventListener('click', () => this.close());
}
}
}
2023-02-05 11:18:51 -08:00
configValueChanged(value: Record<string, string>): void {
if (Object.keys(value).length === 0) {
return;
}
this.element.classList.remove('d-none');
this.twitterTarget.href = this.configValue['twitter'];
this.blueskyTarget.href = this.configValue['bluesky'];
this.tumblrTarget.href = this.configValue['tumblr'];
2023-02-19 11:58:47 -08:00
this.telegramTarget.href = this.configValue['telegram'];
if (this.hasCustomTarget) {
this.customTarget.href = `${this.customTarget.href}${this.configValue['custom']}`;
}
}
close(): void {
(this.element.closest(".inbox-entry")).remove();
}
closeAfterShare(): void {
this.otherTarget.addEventListener('retrospring:shared', () => this.close());
}
closeAfterCopyToClipboard(): void {
this.clipboardTarget.addEventListener('retrospring:copied', () => this.close());
}
}