ViewIncome.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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();
  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. render(){
  23. new Elem("h1")
  24. .text(`${user.account.name} income`)
  25. .appendTo(this.container);
  26. const incomeContainer = new Elem("div")
  27. .addClass("categoryContainer")
  28. .appendTo(this.container);
  29. const income = user.account.income;
  30. for(let i = 0; i < income.length; i++){
  31. const spent = user.account.categorySpent(income[i]);
  32. const spentAsCurrency = Format.currency(user.account.categorySpent(income[i]));
  33. const amount = income[i].amount;
  34. const amountAsCurrency = Format.currency(income[i].amount);
  35. new Elem("div")
  36. .addClass("viewCategoryItem")
  37. .append(new Elem("p")
  38. .text(income[i].name)
  39. )
  40. .append(new Elem("p")
  41. .addClass("categoryItemSpent")
  42. .append(new Elem("span")
  43. .text(spentAsCurrency)
  44. .addStyle("color", this.generateColor(spent, amount))
  45. )
  46. .append(new Elem("span")
  47. .text(" / ")
  48. )
  49. .append(new Elem("span")
  50. .text(amountAsCurrency)
  51. .addStyle("color", "hsl(120, 50%, 28%)")
  52. )
  53. )
  54. .appendTo(incomeContainer);
  55. }
  56. }
  57. }