calculator.component.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Component, OnInit, ViewRef} from '@angular/core';
  2. import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
  3. import { BehaviorSubject } from 'rxjs';
  4. import {
  5. HsLanguageService,
  6. HsLayoutService,
  7. // eslint-disable-next-line import/named
  8. HsPanelComponent,
  9. } from 'hslayers-ng';
  10. import {CalculatorService, Index} from './calculator.service';
  11. import {FieldService} from './field.service';
  12. @Component({
  13. selector: 'calculator-panel',
  14. templateUrl: './calculator.component.html',
  15. styleUrls: ['./calculator.component.scss'],
  16. })
  17. export class CalculatorComponent implements HsPanelComponent, OnInit {
  18. data: {
  19. selectedProduct: Index;
  20. selectedFieldsProperties: {[x: string]: any}[];
  21. };
  22. lpisWfsVisible: boolean;
  23. name: 'calculator';
  24. viewRef: ViewRef;
  25. isVisible$ = new BehaviorSubject<boolean>(true);
  26. constructor(
  27. public calcService: CalculatorService,
  28. private fieldService: FieldService,
  29. public hsLanguageService: HsLanguageService,
  30. public hsLayoutService: HsLayoutService
  31. ) {
  32. this.fieldService.fieldSelects.subscribe(({features}) => {
  33. this.data.selectedFieldsProperties = [];
  34. for (const feature of features) {
  35. this.data.selectedFieldsProperties.push(feature.getProperties());
  36. }
  37. });
  38. this.calcService.viewChanges.subscribe((view) => {
  39. if (view.getZoom() > this.calcService.MIN_LPIS_VISIBLE_ZOOM) {
  40. this.lpisWfsVisible = true;
  41. return;
  42. }
  43. this.lpisWfsVisible = false;
  44. });
  45. }
  46. ngOnInit() {
  47. this.data.selectedProduct = null;
  48. }
  49. isVisible(): boolean {
  50. return this.hsLayoutService.panelVisible('calculator');
  51. }
  52. noFieldSelected(): boolean {
  53. return this.fieldService.noFieldSelected();
  54. }
  55. noProductSelected(): boolean {
  56. return this.data.selectedProduct === null;
  57. }
  58. noDates(): boolean {
  59. return this.calcService.noDates();
  60. }
  61. noDateSelected(): boolean {
  62. return this.calcService.selectedDate === undefined;
  63. }
  64. getDates() {
  65. this.calcService.getDates({product: this.data.selectedProduct});
  66. }
  67. getZones() {
  68. this.calcService.getZones({
  69. product: this.data.selectedProduct,
  70. });
  71. }
  72. /**
  73. * Used when product is changed, so the selected date can be reset
  74. */
  75. resetDate() {
  76. this.calcService.selectedDate = undefined;
  77. this.calcService.availableDates = undefined;
  78. }
  79. updateRangeSlider(value: string) {
  80. this.calcService.dateCalendarSelects.next({date: value});
  81. }
  82. updateSelectedDate(value: NgbDateStruct) {
  83. this.calcService.selectedDate = value.year + '-' + value.month + '-' + value.day;
  84. }
  85. isWeekend(date: NgbDateStruct) {
  86. return date.day > 5;
  87. }
  88. hasTask(date: NgbDateStruct): boolean {
  89. if (this.calcService && this.calcService.availableDates) {
  90. let found = this.calcService.availableDates.filter((item, index) => {
  91. return item.indexOf(`${date.year}-${date.month.toString().padStart(2, "0")}-${date.day.toString().padStart(2, "0")}`) == 0;
  92. }); // date.year == 2022 && date.month == 6 && date.day == 16;
  93. return found.length > 0;
  94. }
  95. return false;
  96. }
  97. }