calculator.component.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. this.calcService.addLayersFromComposition(
  49. 'https://www.agrihub.cz/rest/hana_kubickova92/maps/voda',
  50. {path: 'Voda'}
  51. );
  52. }
  53. isVisible(): boolean {
  54. return this.hsLayoutService.panelVisible('calculator');
  55. }
  56. noFieldSelected(): boolean {
  57. return this.fieldService.noFieldSelected();
  58. }
  59. noProductSelected(): boolean {
  60. return this.data.selectedProduct === null;
  61. }
  62. noDates(): boolean {
  63. return this.calcService.noDates();
  64. }
  65. noDateSelected(): boolean {
  66. return this.calcService.selectedDate === undefined;
  67. }
  68. getDates() {
  69. this.calcService.getDates({product: this.data.selectedProduct});
  70. }
  71. getZones() {
  72. this.calcService.getZones({
  73. product: this.data.selectedProduct,
  74. });
  75. }
  76. /**
  77. * Used when product is changed, so the selected date can be reset
  78. */
  79. resetDate() {
  80. this.calcService.selectedDate = undefined;
  81. this.calcService.selectedDateCalendar = undefined;
  82. this.calcService.availableDates = undefined;
  83. }
  84. updateSelectedDate(value: NgbDateStruct) {
  85. this.calcService.selectedDate = `${value.year}-${
  86. value.month < 10 ? '0' : ''
  87. }${value.month}-${value.day < 10 ? '0' : ''}${value.day}`;
  88. this.calcService.updateImageBackground(this.calcService.selectedDate);
  89. }
  90. hasDataAvailable(date: NgbDateStruct): boolean {
  91. if (this.calcService && this.calcService.availableDates) {
  92. const found = this.calcService.availableDates.filter((item, index) => {
  93. return (
  94. item.indexOf(
  95. `${date.year}-${date.month.toString().padStart(2, '0')}-${date.day
  96. .toString()
  97. .padStart(2, '0')}`
  98. ) == 0
  99. );
  100. }); // date.year == 2022 && date.month == 6 && date.day == 16;
  101. return found.length > 0;
  102. }
  103. return false;
  104. }
  105. }