|
|
@@ -1,16 +1,28 @@
|
|
|
+import {HttpClient} from '@angular/common/http';
|
|
|
+import {Injectable} from '@angular/core';
|
|
|
+import {Subject} from 'rxjs';
|
|
|
+
|
|
|
+import {HsUtilsService} from 'hslayers-ng/components/utils/utils.service';
|
|
|
+
|
|
|
// import attractivity from '../Attractivity.json';
|
|
|
-import nuts from '../nuts.js';
|
|
|
+import nuts from '../nuts';
|
|
|
//import {factors} from './factors.js';
|
|
|
|
|
|
+@Injectable({providedIn: 'root'})
|
|
|
export class AdjusterService {
|
|
|
- constructor(HsCore, HsUtilsService, $rootScope, $http, $location) {
|
|
|
- 'ngInject';
|
|
|
- //this.HsCore = HsCore;
|
|
|
- this.HsUtilsService = HsUtilsService;
|
|
|
- this.$rootScope = $rootScope;
|
|
|
- this.$http = $http;
|
|
|
+ serviceBaseUrl: string;
|
|
|
+ factors;
|
|
|
+ clusters;
|
|
|
+ clustersLoaded: Subject<any> = new Subject();
|
|
|
+ method: string;
|
|
|
+ _clusteringInProcess: boolean;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private hsUtilsService: HsUtilsService,
|
|
|
+ private httpClient: HttpClient
|
|
|
+ ) {
|
|
|
this.serviceBaseUrl =
|
|
|
- $location.host() === 'localhost'
|
|
|
+ window.location.hostname === 'localhost'
|
|
|
? 'https://jmacura.ml/ws/' // 'http://localhost:3000/'
|
|
|
: 'https://publish.lesprojekt.cz/nodejs/';
|
|
|
this.factors = [];
|
|
|
@@ -24,13 +36,11 @@ export class AdjusterService {
|
|
|
* Sends a request to polirural-attractiveness-service
|
|
|
* and applies the returned values
|
|
|
*/
|
|
|
- apply() {
|
|
|
+ apply(): void {
|
|
|
const f = () => {
|
|
|
this._clusteringInProcess = true;
|
|
|
- this.$http({
|
|
|
- method: 'post',
|
|
|
- url: this.serviceBaseUrl + 'clusters',
|
|
|
- data: {
|
|
|
+ this.httpClient
|
|
|
+ .post(this.serviceBaseUrl + 'clusters', {
|
|
|
factors: this.factors.map((f) => {
|
|
|
return {
|
|
|
factor: f.name,
|
|
|
@@ -40,11 +50,10 @@ export class AdjusterService {
|
|
|
.map((ds) => ds.name),
|
|
|
};
|
|
|
}),
|
|
|
- },
|
|
|
- })
|
|
|
- .then((response) => {
|
|
|
- const clusterData = response.data.response;
|
|
|
- console.log(clusterData);
|
|
|
+ })
|
|
|
+ .toPromise()
|
|
|
+ .then((data: any) => {
|
|
|
+ const clusterData = data.response;
|
|
|
/*let max = 0;
|
|
|
this.clusters.forEach((a) => {
|
|
|
if (a.aggregate > max) {
|
|
|
@@ -82,32 +91,31 @@ export class AdjusterService {
|
|
|
}
|
|
|
this.clusters = clusters;
|
|
|
this._clusteringInProcess = false;
|
|
|
- this.$rootScope.$broadcast('clusters_loaded');
|
|
|
+ this.clustersLoaded.next();
|
|
|
})
|
|
|
.catch((error) => {
|
|
|
- console.error(
|
|
|
- `Error obtaining data from ${this.serviceBaseUrl}: ${error}`
|
|
|
- );
|
|
|
+ console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
|
|
|
+ console.log(error);
|
|
|
this._clusteringInProcess = false;
|
|
|
});
|
|
|
};
|
|
|
- this.HsUtilsService.debounce(f, 300)();
|
|
|
+ this.hsUtilsService.debounce(f, 300, false, this)();
|
|
|
}
|
|
|
|
|
|
- init() {
|
|
|
- this.$http({
|
|
|
- url: this.serviceBaseUrl + 'datasets',
|
|
|
- })
|
|
|
- .then((response) => {
|
|
|
- this.factors = response.data.map((dataset) => {
|
|
|
+ init(): void {
|
|
|
+ this.httpClient
|
|
|
+ .get(this.serviceBaseUrl + 'datasets')
|
|
|
+ .toPromise()
|
|
|
+ .then((data: any) => {
|
|
|
+ this.factors = data.map((dataset) => {
|
|
|
return {name: dataset.Factor, weight: 1, datasets: []};
|
|
|
});
|
|
|
- this.factors = this.HsUtilsService.removeDuplicates(
|
|
|
+ this.factors = this.hsUtilsService.removeDuplicates(
|
|
|
this.factors,
|
|
|
'name'
|
|
|
);
|
|
|
this.factors.forEach((factor) => {
|
|
|
- factor.datasets = response.data
|
|
|
+ factor.datasets = data
|
|
|
.filter((ds) => ds.Factor === factor.name)
|
|
|
.map((ds) => {
|
|
|
return {
|
|
|
@@ -120,7 +128,8 @@ export class AdjusterService {
|
|
|
this.apply();
|
|
|
})
|
|
|
.catch((error) => {
|
|
|
- console.error(`Web service at ${this.serviceBaseUrl} unavailable!`);
|
|
|
+ console.warn(`Web service at ${this.serviceBaseUrl} unavailable!`);
|
|
|
+ console.log(error);
|
|
|
this._clusteringInProcess = false;
|
|
|
});
|
|
|
}
|
|
|
@@ -128,7 +137,7 @@ export class AdjusterService {
|
|
|
/**
|
|
|
* @returns {boolean} true if clustering is in process, false otherwise
|
|
|
*/
|
|
|
- isClusteringInProcess() {
|
|
|
+ isClusteringInProcess(): boolean {
|
|
|
return this._clusteringInProcess;
|
|
|
}
|
|
|
}
|