trait-modal.component.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Component, Input, OnInit, OnDestroy } from '@angular/core';
  2. import { Editor } from 'ngx-editor';
  3. import { Trait } from 'src/interfaces/traits';
  4. import { ModalService } from 'src/services/modal/modal.service';
  5. @Component({
  6. selector: 'trait-modal',
  7. templateUrl: './trait-modal.component.html',
  8. styleUrls: ['./trait-modal.component.scss'],
  9. })
  10. export class TraitModalComponent {
  11. @Input() public isUpdate: boolean = false;
  12. @Input() public trait: Trait | undefined;
  13. @Input() public isAddedFromCharacter: boolean = false;
  14. shortEditor: Editor = new Editor();
  15. longEditor: Editor = new Editor();
  16. html = '';
  17. toolbar: any = [
  18. // default value
  19. ['bold', 'italic'],
  20. ['bullet_list'],
  21. [{ heading: ['h3', 'h4', 'h5', 'h6'] }],
  22. ];
  23. public name: string = '';
  24. public shortDescription: string = '';
  25. public longDescription: string = '';
  26. public origin: string = '';
  27. public constructor(public modalAccessor: ModalService) {}
  28. ngOnInit(): void {
  29. if (this.isUpdate) {
  30. this.loadItem();
  31. }
  32. }
  33. ngOnDestroy(): void {
  34. this.shortEditor.destroy();
  35. this.longEditor.destroy();
  36. }
  37. // FUNCTIONS
  38. public loadItem(): void {
  39. this.name = this.trait!.name;
  40. this.shortDescription = this.trait!.shortDescription;
  41. this.longDescription = this.trait!.longDescription;
  42. this.origin = this.trait!.origin;
  43. }
  44. public createTrait(): Trait {
  45. return {
  46. name: this.name,
  47. shortDescription: this.shortDescription,
  48. longDescription: this.longDescription,
  49. origin: this.origin,
  50. };
  51. }
  52. // RESPONSES
  53. public cancel(): void {
  54. this.modalAccessor.handleModalClosing('cancel', undefined);
  55. }
  56. public add(): void {
  57. this.modalAccessor.handleModalClosing('add', this.createTrait());
  58. }
  59. public update(): void {
  60. this.modalAccessor.handleModalClosing('update', this.createTrait());
  61. }
  62. }