calculator.component.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {BehaviorSubject} from 'rxjs';
  2. import {Component, OnInit, ViewRef} from '@angular/core';
  3. import {NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';
  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.selectedDateCalendar = undefined;
  78. this.calcService.availableDates = undefined;
  79. }
  80. updateSelectedDate(value: NgbDateStruct) {
  81. this.calcService.selectedDate = `${value.year}-${
  82. value.month < 10 ? '0' : ''
  83. }${value.month}-${value.day < 10 ? '0' : ''}${value.day}`;
  84. this.calcService.updateImageBackground(this.calcService.selectedDate);
  85. }
  86. hasDataAvailable(date: NgbDateStruct): boolean {
  87. if (this.calcService && this.calcService.availableDates) {
  88. const found = this.calcService.availableDates.filter((item, index) => {
  89. return (
  90. item.indexOf(
  91. `${date.year}-${date.month.toString().padStart(2, '0')}-${date.day
  92. .toString()
  93. .padStart(2, '0')}`
  94. ) == 0
  95. );
  96. }); // date.year == 2022 && date.month == 6 && date.day == 16;
  97. return found.length > 0;
  98. }
  99. return false;
  100. }
  101. }