2020-04-14 11:28:33 +02:00
|
|
|
import { Component, ElementRef, Input } from '@angular/core';
|
|
|
|
import { ConfigService } from '../../../services/config.service';
|
2019-08-21 14:28:50 +02:00
|
|
|
|
2020-02-04 14:28:43 +01:00
|
|
|
interface VoteChoice {
|
|
|
|
votes?: {
|
2020-04-14 11:28:33 +02:00
|
|
|
yes: number;
|
|
|
|
no: number;
|
|
|
|
maybe: number;
|
|
|
|
notAnswered: number;
|
2020-02-04 14:28:43 +01:00
|
|
|
};
|
|
|
|
name?: string;
|
|
|
|
date?: Date;
|
|
|
|
answer: 'yes' | 'no' | 'maybe' | null;
|
2020-04-14 11:28:33 +02:00
|
|
|
simpleAnswer?: boolean;
|
2020-02-04 14:28:43 +01:00
|
|
|
false; // enable if we display only a togglable "yes"
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* each vote choice takes a configuration from the container of all choices.
|
|
|
|
* this component is used to select a date choice, or a name answer
|
|
|
|
*/
|
2019-08-21 14:28:50 +02:00
|
|
|
@Component({
|
2020-02-04 14:28:43 +01:00
|
|
|
selector: 'framadate-voting-choice',
|
|
|
|
templateUrl: './voting-choice.component.html',
|
2020-04-14 11:28:33 +02:00
|
|
|
styleUrls: ['./voting-choice.component.scss'],
|
2019-08-21 14:28:50 +02:00
|
|
|
})
|
2020-02-04 14:28:43 +01:00
|
|
|
export class VotingChoiceComponent {
|
|
|
|
public showChangeChoicebutton = false;
|
|
|
|
@Input() public choice: any;
|
|
|
|
@Input() public choices_count: any;
|
|
|
|
@Input() public choice_id: any;
|
|
|
|
@Input() public poll: any;
|
|
|
|
@Input() public simpleAnswer: boolean = true;
|
|
|
|
@Input() public pollIsSpecialDate: boolean = false;
|
2019-08-21 14:28:50 +02:00
|
|
|
|
2020-04-14 11:28:33 +02:00
|
|
|
constructor(private el: ElementRef, private config: ConfigService) {
|
2020-02-05 15:21:28 +01:00
|
|
|
if (this.poll && this.poll.allowedAnswers) {
|
2020-04-14 11:28:33 +02:00
|
|
|
this.simpleAnswer = this.poll.allowedAnswers.length == 1;
|
2020-02-04 14:28:43 +01:00
|
|
|
}
|
2019-09-09 10:57:08 +02:00
|
|
|
}
|
2019-08-21 14:28:50 +02:00
|
|
|
|
2020-02-04 14:28:43 +01:00
|
|
|
setAnswserTo(newAnswer: 'yes' | 'no' | 'maybe' | null) {
|
|
|
|
if (this.simpleAnswer) {
|
|
|
|
// only toggle yes to no
|
|
|
|
if (this.choice.answer && this.choice.answer === 'yes') {
|
|
|
|
this.choice.answer = 'no';
|
|
|
|
this.config.myTempVoteStack--;
|
|
|
|
} else {
|
2020-02-05 15:21:28 +01:00
|
|
|
this.choice.answer = newAnswer;
|
2020-02-04 14:28:43 +01:00
|
|
|
this.config.myTempVoteStack++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.choice.answer = newAnswer;
|
|
|
|
if (this.choice.answer !== newAnswer) {
|
|
|
|
if (newAnswer == 'maybe' || newAnswer == 'yes') {
|
|
|
|
this.config.myTempVoteStack++;
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-14 11:28:33 +02:00
|
|
|
console.info('same answer as before');
|
2020-02-04 14:28:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.el.nativeElement.blur();
|
|
|
|
}
|
2019-08-21 14:28:50 +02:00
|
|
|
}
|