hit-dice.component.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Component, EventEmitter, Output } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. @Component({
  4. selector: 'hit-dice',
  5. templateUrl: './hit-dice.component.html',
  6. styleUrls: ['./hit-dice.component.scss'],
  7. })
  8. export class HitDiceComponent {
  9. public hitDice: any;
  10. showEditButtons: boolean = false;
  11. @Output() public setHitDice: EventEmitter<any> = new EventEmitter();
  12. constructor(private dataAccessor: DataService) {
  13. this.hitDice = this.dataAccessor.hitDice;
  14. }
  15. public ngOnInit(): void {
  16. this.correctHitDiceView();
  17. this.emitHitDice();
  18. }
  19. public handleUsedHitDice(index: number, element: any): void {
  20. console.log('HITDICE in hitdice first: ', this.hitDice);
  21. if (element.checked) {
  22. console.log('The checkbox is checked now ');
  23. this.hitDice.diceUsed++;
  24. if (index + 1 !== this.hitDice.diceUsed) {
  25. this.correctHitDiceView();
  26. }
  27. } else {
  28. console.log('The checkbox is unchecked now ');
  29. this.hitDice.diceUsed--;
  30. if (index !== this.hitDice.diceUsed) {
  31. this.correctHitDiceView();
  32. }
  33. }
  34. console.log('HITDICE in hitdice: ', this.hitDice);
  35. this.emitHitDice();
  36. }
  37. private correctHitDiceView(): void {
  38. const totalHitDice = this.hitDice.diceNumber;
  39. const usedHitDice = this.hitDice.diceUsed;
  40. for (let index = 0; index < usedHitDice; index++) {
  41. setTimeout(() => {
  42. (
  43. document.getElementById('checkbox' + index) as HTMLInputElement
  44. ).checked = true;
  45. });
  46. }
  47. for (let index = usedHitDice; index < totalHitDice; index++) {
  48. setTimeout(() => {
  49. (
  50. document.getElementById('checkbox' + index) as HTMLInputElement
  51. ).checked = false;
  52. });
  53. }
  54. }
  55. public getArray(length: number): any[] {
  56. return Array.from({ length: length });
  57. }
  58. private emitHitDice(): void {
  59. if (this.hitDice.diceUsed > this.hitDice.diceNumber) {
  60. this.hitDice.diceUsed = this.hitDice.diceNumber;
  61. }
  62. this.setHitDice.emit(this.hitDice);
  63. }
  64. }