counter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {
  2. formatInput
  3. } from '../utils/format-input.js';
  4. import Result from './result.js';
  5. const UserActivity = {
  6. MIN: 1.2,
  7. LOW: 1.375,
  8. MEDIUM: 1.55,
  9. HIGH: 1.725,
  10. MAX: 1.9,
  11. };
  12. const CaloriesCoefficient = {
  13. AGE: 5,
  14. WEIGHT: 10,
  15. HEIGHT: 6.25,
  16. };
  17. const GenderCoefficient = {
  18. MALE: 5,
  19. FEMALE: -160
  20. };
  21. const GoalCoefficient = {
  22. MIN: 0.85,
  23. MAX: 1.15
  24. };
  25. export default class Calculator {
  26. constructor(element) {
  27. this.root = element;
  28. this.form = this.root.querySelector(`.counter__form`);
  29. this.elements = this.form.elements;
  30. this.parameters = this.elements.parameters.elements;
  31. this.submit = this.elements.submit;
  32. this.reset = this.elements.reset;
  33. this.gender = this.elements.gender;
  34. this.age = this.elements.age;
  35. this.weight = this.elements.weight;
  36. this.height = this.elements.height;
  37. this.activity = this.elements.activity;
  38. this.result = new Result(this.root);
  39. this.parametersItems = Array.from(this.parameters);
  40. this._onFormFill = this._onFormFill.bind(this);
  41. this._onFormReset = this._onFormReset.bind(this);
  42. this._onFormSubmit = this._onFormSubmit.bind(this);
  43. }
  44. _onFormFill() {
  45. this.submit.disabled = !this.form.checkValidity();
  46. this.reset.disabled = !this.parametersItems.some((el) => el.value);
  47. this.age.value = formatInput(this.age);
  48. this.height.value = formatInput(this.height);
  49. this.weight.value = formatInput(this.weight);
  50. }
  51. _onFormReset() {
  52. this.reset.disabled = true;
  53. this.submit.disabled = true;
  54. this.result.hide();
  55. }
  56. _onFormSubmit(evt) {
  57. evt.preventDefault();
  58. const caloriesData = {
  59. norm: this.getCaloriesNorm(),
  60. min: this.getCaloriesMin(),
  61. max: this.getCaloriesMax()
  62. };
  63. this.result.show(caloriesData);
  64. }
  65. init() {
  66. this.form.addEventListener(`input`, this._onFormFill, true);
  67. this.form.addEventListener(`reset`, this._onFormReset);
  68. this.form.addEventListener(`submit`, this._onFormSubmit);
  69. }
  70. deinit() {
  71. this.form.removeEventListener(`input`, this._onFormFill, true);
  72. this.form.removeEventListener(`reset`, this._onFormReset);
  73. this.form.removeEventListener(`submit`, this._onFormSubmit);
  74. }
  75. getCaloriesNorm() {
  76. const age = CaloriesCoefficient.AGE * this.age.value;
  77. const weight = CaloriesCoefficient.WEIGHT * this.weight.value;
  78. const height = CaloriesCoefficient.HEIGHT * this.height.value;
  79. const gender = GenderCoefficient[this.gender.value.toUpperCase()];
  80. const activity = UserActivity[this.activity.value.toUpperCase()];
  81. return Math.round((weight + height - age + gender) * activity);
  82. }
  83. getCaloriesMin() {
  84. return Math.round(this.getCaloriesNorm() * GoalCoefficient.MIN);
  85. }
  86. getCaloriesMax() {
  87. return Math.round(this.getCaloriesNorm() * GoalCoefficient.MAX);
  88. }
  89. }