2020-02-04 16:25:42 +01:00
|
|
|
import { Pipe, PipeTransform } from '@angular/core';
|
|
|
|
|
2020-02-13 18:24:51 +01:00
|
|
|
import { AssignmentPollService } from 'app/site/assignments/services/assignment-poll.service';
|
|
|
|
import { MotionPollService } from 'app/site/motions/services/motion-poll.service';
|
|
|
|
import { PollData } from 'app/site/polls/services/poll.service';
|
2020-02-04 16:25:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses a number and a ViewPoll-object.
|
|
|
|
* Converts the number to the voting percent base using the
|
|
|
|
* given 100%-Base option in the poll object
|
|
|
|
*
|
|
|
|
* returns null if a percent calculation is not possible
|
|
|
|
* or the result is 0
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```html
|
|
|
|
* <span> {{ voteYes | pollPercentBase: poll }} </span>
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@Pipe({
|
|
|
|
name: 'pollPercentBase'
|
|
|
|
})
|
|
|
|
export class PollPercentBasePipe implements PipeTransform {
|
|
|
|
private decimalPlaces = 3;
|
|
|
|
|
2020-02-13 18:24:51 +01:00
|
|
|
public constructor(
|
|
|
|
private assignmentPollService: AssignmentPollService,
|
|
|
|
private motionPollService: MotionPollService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
public transform(value: number, poll: PollData): string | null {
|
|
|
|
let totalByBase: number;
|
|
|
|
if ((<any>poll).assignment) {
|
|
|
|
totalByBase = this.assignmentPollService.getPercentBase(poll);
|
|
|
|
} else {
|
|
|
|
totalByBase = this.motionPollService.getPercentBase(poll);
|
|
|
|
}
|
2020-02-04 16:25:42 +01:00
|
|
|
|
|
|
|
if (totalByBase) {
|
|
|
|
const percentNumber = (value / totalByBase) * 100;
|
|
|
|
if (percentNumber > 0) {
|
|
|
|
const result = percentNumber % 1 === 0 ? percentNumber : percentNumber.toFixed(this.decimalPlaces);
|
2020-02-24 16:55:07 +01:00
|
|
|
return `(${result} %)`;
|
2020-02-04 16:25:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|