Competence.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Skill } from "./SolverInput";
  2. export interface ICompetence {
  3. id?: number;
  4. name: string;
  5. description: string;
  6. isPreference?: boolean;
  7. isTeachable?: boolean;
  8. }
  9. export default class Competence {
  10. static maxId = 0;
  11. id: number;
  12. name: string;
  13. description: string;
  14. private _isPreference!: boolean;
  15. renderPreference!: string;
  16. renderTeachable!: string;
  17. public get isPreference(): boolean {
  18. return this._isPreference;
  19. }
  20. public set isPreference(value: boolean) {
  21. this._isPreference = value;
  22. this.renderPreference = Competence.getBox(value);
  23. }
  24. private _isTeachable!: boolean;
  25. public get isTeachable(): boolean {
  26. return this._isTeachable;
  27. }
  28. public set isTeachable(value: boolean) {
  29. this._isTeachable = value;
  30. this.renderTeachable = Competence.getBox(value);
  31. }
  32. constructor(
  33. id: number,
  34. name: string,
  35. description: string,
  36. isPreference = true,
  37. isTeachable = false
  38. ) {
  39. // Change the current max id
  40. if (!isNaN(id)) Competence.maxId = id > Competence.maxId ? id : Competence.maxId;
  41. this.id = id;
  42. this.name = name;
  43. this.description = description;
  44. this.isPreference = isPreference;
  45. this.isTeachable = isTeachable;
  46. }
  47. static getBox(v: boolean): string {
  48. return `<i class="material-icons">${v ? "check_box" : "check_box_outline_blank"}</i>`;
  49. }
  50. get overflowDescription(): string {
  51. // TODO implement tooltiped description
  52. return this.description;
  53. }
  54. get fullname(): string {
  55. return this.name + (this.isPreference ? " (P)" : " (C)");
  56. }
  57. static fromObject(obj: ICompetence): Competence {
  58. let id: number;
  59. if (obj.id && !isNaN(obj.id)) {
  60. id = obj.id;
  61. this.maxId = obj.id > this.maxId ? obj.id : this.maxId;
  62. } else {
  63. this.maxId += 1;
  64. id = this.maxId;
  65. }
  66. return new Competence(
  67. id,
  68. obj.name,
  69. obj.description ? obj.description : "",
  70. obj.isPreference !== undefined ? obj.isPreference : true,
  71. obj.isTeachable !== undefined ? obj.isTeachable : false
  72. );
  73. }
  74. toPlainObject(): ICompetence {
  75. return {
  76. id: this.id,
  77. name: this.name,
  78. description: this.description,
  79. isPreference: this.isPreference,
  80. isTeachable: this.isTeachable,
  81. };
  82. }
  83. toSkill(): Skill {
  84. return {
  85. id: this.id,
  86. name: this.name,
  87. isPreference: this.isPreference,
  88. isTeachable: this.isTeachable,
  89. };
  90. }
  91. toJSON(): ICompetence {
  92. return this.toPlainObject();
  93. }
  94. static fromJSON(input: string | ICompetence): Competence {
  95. let obj: ICompetence;
  96. if (typeof input == "string") {
  97. obj = JSON.parse(input);
  98. } else {
  99. obj = input as ICompetence;
  100. }
  101. return Competence.fromObject(obj);
  102. }
  103. }