Sengi-Windows-MacOS-Linux/src/app/components/create-status/poll-editor/poll-editor.component.ts

101 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-08-06 08:53:24 +02:00
import { Component, Input, OnInit, SimpleChanges } from '@angular/core';
2019-08-25 03:28:04 +02:00
import { faPlus } from "@fortawesome/free-solid-svg-icons";
2019-08-24 06:55:01 +02:00
import { PollEntry } from './poll-entry/poll-entry.component';
2019-08-25 03:28:04 +02:00
import { PollParameters } from '../../../services/mastodon.service';
2023-08-06 08:53:24 +02:00
import { Poll } from '../../../services/models/mastodon.interfaces';
2019-08-24 06:55:01 +02:00
@Component({
2019-08-24 06:55:01 +02:00
selector: 'app-poll-editor',
templateUrl: './poll-editor.component.html',
styleUrls: ['./poll-editor.component.scss']
})
export class PollEditorComponent implements OnInit {
faPlus = faPlus;
2019-08-24 07:38:48 +02:00
private entryUuid: number = 0;
2019-08-24 06:55:01 +02:00
entries: PollEntry[] = [];
delayChoice: Delay[] = [];
selectedId: string;
private multiSelected: boolean;
2019-08-24 06:55:01 +02:00
2023-08-06 08:53:24 +02:00
@Input() oldPoll: Poll;
2019-08-24 06:55:01 +02:00
constructor() {
this.entries.push(new PollEntry(this.getEntryUuid(), this.multiSelected));
this.entries.push(new PollEntry(this.getEntryUuid(), this.multiSelected));
2019-08-24 06:55:01 +02:00
this.delayChoice.push(new Delay(60 * 5, "5 minutes"));
this.delayChoice.push(new Delay(60 * 30, "30 minutes"));
this.delayChoice.push(new Delay(60 * 60, "1 hour"));
this.delayChoice.push(new Delay(60 * 60 * 6, "6 hours"));
this.delayChoice.push(new Delay(60 * 60 * 24, "1 day"));
this.delayChoice.push(new Delay(60 * 60 * 24 * 3, "3 days"));
this.delayChoice.push(new Delay(60 * 60 * 24 * 7, "7 days"));
this.delayChoice.push(new Delay(60 * 60 * 24 * 15, "15 days"));
this.delayChoice.push(new Delay(60 * 60 * 24 * 30, "30 days"));
this.selectedId = this.delayChoice[4].id;
}
2019-08-24 06:55:01 +02:00
ngOnInit() {
2019-08-24 06:55:01 +02:00
}
2023-08-06 08:53:24 +02:00
ngOnChanges(changes: SimpleChanges): void {
if (changes['oldPoll']) {
this.loadPollParameters(this.oldPoll);
}
}
2019-08-24 07:38:48 +02:00
private getEntryUuid(): number {
this.entryUuid++;
return this.entryUuid;
}
2019-08-24 06:55:01 +02:00
addEntry(): boolean {
this.entries.push(new PollEntry(this.getEntryUuid(), this.multiSelected));
2019-08-24 06:55:01 +02:00
return false;
}
2019-08-24 07:38:48 +02:00
2023-08-06 08:53:24 +02:00
removeElement(entry: PollEntry) {
2019-08-24 07:38:48 +02:00
this.entries = this.entries.filter(x => x.id != entry.id);
}
toogleMulti() {
this.multiSelected = !this.multiSelected;
this.entries.forEach((e: PollEntry) => {
e.isMulti = this.multiSelected;
});
}
2019-08-25 03:28:04 +02:00
getPollParameters(): PollParameters {
let params = new PollParameters();
params.expires_in = this.delayChoice.find(x => x.id === this.selectedId).delayInSeconds;
params.multiple = this.multiSelected;
params.options = this.entries.map(x => x.label);
params.hide_totals = false;
return params;
}
2023-08-06 08:53:24 +02:00
private loadPollParameters(poll: Poll) {
2023-08-20 03:33:43 +02:00
if(!this.oldPoll) return;
2023-08-06 08:53:24 +02:00
const isMulti = poll.multiple;
this.entries.length = 0;
for (let o of poll.options) {
const entry = new PollEntry(this.getEntryUuid(), isMulti);
entry.label = o.title;
this.entries.push(entry);
}
}
}
2019-08-24 06:55:01 +02:00
class Delay {
constructor(public delayInSeconds: number, public label: string) {
this.id = delayInSeconds.toString();
}
id: string;
}