ViewIncome.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import Page from "./Page.js";
  2. import Elem from "../Elem.js";
  3. import Format from "../Format.js";
  4. export default class ViewIncome extends Page{
  5. constructor(){
  6. super("ViewIncome", ["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 income = user.account.income;
  32. for(let i = 0; i < income.length; i++){
  33. if(!showArchived && !income[i].active) continue;
  34. const spent = user.account.categorySpent(income[i]);
  35. const spentAsCurrency = Format.currency(user.account.categorySpent(income[i]));
  36. const amount = income[i].amount;
  37. const amountAsCurrency = Format.currency(income[i].amount);
  38. new Elem("button")
  39. .addClass("viewCategoryItem")
  40. .onclick(()=>{changePage("editIncome", income[i])})
  41. .append(new Elem("p")
  42. .text(income[i].name)
  43. .addClass(income[i].active ? "none" : "strike")
  44. )
  45. .append(new Elem("p")
  46. .addClass("categoryItemSpent")
  47. .append(new Elem("span")
  48. .text(spentAsCurrency)
  49. .addStyle("color", this.generateColor(spent, amount))
  50. )
  51. .append(new Elem("span")
  52. .text(" / ")
  53. )
  54. .append(new Elem("span")
  55. .text(amountAsCurrency)
  56. .addStyle("color", "hsl(120, 50%, 28%)")
  57. )
  58. )
  59. .appendTo(container);
  60. }
  61. }
  62. render(showArchived){
  63. new Elem("h1")
  64. .text(`${user.account.name} income`)
  65. .appendTo(this.container);
  66. new Elem("label")
  67. .addClass("switch")
  68. .append(new Elem("p")
  69. .text("Show Archived")
  70. )
  71. .append(new Elem("input")
  72. .type("checkbox")
  73. .addClass("archiveCheckbox")
  74. .checked(false)
  75. .onchange(this.toggleActive.bind(this))
  76. )
  77. .append(new Elem("span")
  78. .addClass("slider")
  79. )
  80. .appendTo(this.container);
  81. new Elem("div")
  82. .addClass("categoryContainer")
  83. .appendTo(this.container);
  84. this.renderItems(showArchived);
  85. }
  86. }