calculator-panel.component.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. HsSidebarService,
  10. } from 'hslayers-ng';
  11. import {AppService} from '../app.service';
  12. import {FcCalculatorService, Index} from './calculator.service';
  13. import {FcFieldService} from './field.service';
  14. @Component({
  15. selector: 'calculator-panel',
  16. templateUrl: './calculator-panel.component.html',
  17. styleUrls: ['./calculator-panel.component.scss'],
  18. })
  19. export class FcCalculatorComponent implements HsPanelComponent, OnInit {
  20. data: {
  21. selectedProduct: Index;
  22. selectedFieldsProperties: {[x: string]: any}[];
  23. };
  24. lpisWfsVisible: boolean;
  25. name: 'fieldcalc';
  26. viewRef: ViewRef;
  27. isVisible$ = new BehaviorSubject<boolean>(true);
  28. constructor(
  29. public calcService: FcCalculatorService,
  30. private fieldService: FcFieldService,
  31. public hsLanguageService: HsLanguageService,
  32. public hsLayoutService: HsLayoutService,
  33. private hsSidebarService: HsSidebarService,
  34. private appService: AppService
  35. ) {
  36. this.fieldService.fieldSelects.subscribe(({features}) => {
  37. this.data.selectedFieldsProperties = [];
  38. for (const feature of features) {
  39. this.data.selectedFieldsProperties.push(feature.getProperties());
  40. }
  41. });
  42. this.calcService.viewChanges.subscribe((view) => {
  43. if (view.getZoom() > this.calcService.MIN_LPIS_VISIBLE_ZOOM) {
  44. this.lpisWfsVisible = true;
  45. return;
  46. }
  47. this.lpisWfsVisible = false;
  48. });
  49. }
  50. ngOnInit() {
  51. this.data.selectedProduct = null;
  52. this.hsSidebarService.addButton(
  53. {
  54. panel: 'calculator',
  55. module: 'calculator',
  56. order: 0,
  57. fits: true,
  58. visible: true,
  59. title: 'Field Calculator', //() =>
  60. //this.hsLanguageService.getTranslation('ADJUSTER.adjustFactors'),
  61. description: 'Adjust factors for computation',
  62. icon: 'icon-analytics-piechart',
  63. },
  64. 'default'
  65. );
  66. this.appService.init();
  67. this.calcService.addLayersFromComposition(
  68. 'https://www.agrihub.cz/rest/hana_kubickova92/maps/voda',
  69. {path: 'Voda'}
  70. );
  71. }
  72. isVisible(): boolean {
  73. return this.hsLayoutService.panelVisible('calculator');
  74. }
  75. noFieldSelected(): boolean {
  76. return this.fieldService.noFieldSelected();
  77. }
  78. noProductSelected(): boolean {
  79. return this.data.selectedProduct === null;
  80. }
  81. noDates(): boolean {
  82. return this.calcService.noDates();
  83. }
  84. noDateSelected(): boolean {
  85. return this.calcService.selectedDate === undefined;
  86. }
  87. getDates() {
  88. this.calcService.getDates({product: this.data.selectedProduct});
  89. }
  90. getZones() {
  91. this.calcService.getZones({
  92. product: this.data.selectedProduct,
  93. });
  94. }
  95. /**
  96. * Used when product is changed, so the selected date can be reset
  97. */
  98. resetDate() {
  99. this.calcService.selectedDate = undefined;
  100. this.calcService.selectedDateCalendar = undefined;
  101. this.calcService.availableDates = undefined;
  102. }
  103. updateSelectedDate(value: NgbDateStruct) {
  104. this.calcService.selectedDate = `${value.year}-${
  105. value.month < 10 ? '0' : ''
  106. }${value.month}-${value.day < 10 ? '0' : ''}${value.day}`;
  107. this.calcService.updateImageBackground(this.calcService.selectedDate);
  108. }
  109. hasDataAvailable(date: NgbDateStruct): boolean {
  110. if (this.calcService && this.calcService.availableDates) {
  111. const found = this.calcService.availableDates.filter((item, index) => {
  112. return (
  113. item.indexOf(
  114. `${date.year}-${date.month.toString().padStart(2, '0')}-${date.day
  115. .toString()
  116. .padStart(2, '0')}`
  117. ) == 0
  118. );
  119. }); // date.year == 2022 && date.month == 6 && date.day == 16;
  120. return found.length > 0;
  121. }
  122. return false;
  123. }
  124. }