ViewAllowances.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Page from "./Page.js";
  2. import Elem from "../Elem.js";
  3. import Format from "../Format.js";
  4. export default class ViewAllowances extends Page{
  5. constructor(){
  6. super("ViewAllowances", ["home", "back-viewMenu", "logout"]);
  7. this.render(false);
  8. }
  9. generateColor(spent, amount){
  10. if(amount <= 0) return "hsl(0, 100%, 50%)";
  11. const percent = Math.min(spent / amount, 1);
  12. let hue;
  13. if(percent < 0.5){
  14. hue = percent / 0.5 * 45;
  15. }else{
  16. hue = 45 + (percent - 0.5) / 0.5 * (120 -45);
  17. }
  18. const saturation = 65 - percent * 15;
  19. const lightness = 28 + (1 - Math.abs(percent - 0.5) * 2) * 10;
  20. return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
  21. }
  22. toggleActive(){
  23. const showArchived = this.container.querySelector(".archiveCheckbox").checked;
  24. this.renderItems(showArchived);
  25. }
  26. renderItems(showArchived){
  27. const container = this.container.querySelector(".categoryContainer");
  28. while(container.children.length > 0){
  29. container.removeChild(container.lastChild);
  30. }
  31. const allowances = user.account.allowances;
  32. const income = user.account.getIncomeSum(true);
  33. for(let i = 0; i < allowances.length; i++){
  34. if(!showArchived && !allowances[i].active) continue;
  35. const spent = user.account.categorySpent(allowances[i]);
  36. const spentAsCurrency = Format.currency(spent);
  37. const amount = allowances[i].amountInDollars(income);
  38. const amountAsCurrency = Format.currency(amount);
  39. new Elem("button")
  40. .addClass("viewCategoryItem")
  41. .onclick(()=>{changePage("editAllowance")})
  42. .append(new Elem("p")
  43. .text(allowances[i].name)
  44. .addClass(allowances[i].active ? "none" : "strike")
  45. )
  46. .append(new Elem("p")
  47. .addClass("categoryItemSpent")
  48. .append(new Elem("span")
  49. .text(spentAsCurrency)
  50. .addStyle("color", this.generateColor(spent, amount))
  51. )
  52. .append(new Elem("span")
  53. .text(" / ")
  54. )
  55. .append(new Elem("span")
  56. .text(amountAsCurrency)
  57. .addStyle("color", "hsl(120, 50%, 28%)")
  58. )
  59. )
  60. .appendTo(container);
  61. }
  62. }
  63. render(showArchived){
  64. new Elem("h1")
  65. .text(`${user.account.name} Allowances`)
  66. .appendTo(this.container);
  67. new Elem("label")
  68. .addClass("switch")
  69. .append(new Elem("p")
  70. .text("Show Archived")
  71. )
  72. .append(new Elem("input")
  73. .type("checkbox")
  74. .addClass("archiveCheckbox")
  75. .checked(false)
  76. .onchange(this.toggleActive.bind(this))
  77. )
  78. .append(new Elem("span")
  79. .addClass("slider")
  80. )
  81. .appendTo(this.container);
  82. new Elem("div")
  83. .addClass("categoryContainer")
  84. .appendTo(this.container);
  85. this.renderItems(showArchived);
  86. }
  87. }