Refactor itemtype loading
All checks were successful
FarmMaps.Develop/FarmMapsLib/develop This commit looks good

This commit is contained in:
Willem Dantuma
2020-02-11 18:55:43 +01:00
parent 52170590e3
commit b83aca7969
5 changed files with 83 additions and 47 deletions

View File

@@ -6,12 +6,13 @@ import { IItem } from '../models/item';
import { IItemTask } from '../models/itemTask';
import { HttpClient, HttpParams } from "@angular/common/http";
import { AppConfig } from "../shared/app.config";
import {ItemTypeService} from './itemtype.service';
@Injectable({
providedIn: 'root',
})
export class ItemService {
constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
constructor(public httpClient: HttpClient, public appConfig: AppConfig,private itemTypeService:ItemTypeService) {
}
ApiEndpoint() {
@@ -37,7 +38,13 @@ export class ItemService {
if (searchTags) params = params.append("t", searchTags);
if (startDate) params = params.append("sd", startDate.toISOString());
if (endDate) params = params.append("ed", endDate.toISOString());
if (itemType) params = params.append("it", itemType);
if (itemType) {
params = params.append("it", itemType);
let extraAttributes = this.itemTypeService.getExtraAttributes(itemType);
if(extraAttributes) {
params = params.append("da", extraAttributes);
}
}
if (parentCode) params = params.append("pc", parentCode);
if (dataFilter) params = params.append("df", dataFilter);
if (level) params = params.append("lvl", dataFilter);

View File

@@ -1,20 +1,24 @@
import { Injectable } from '@angular/core';
import {IItemTypes} from '../models/item.types'
import {IItem} from '../models/item'
import {ItemService} from '../services/item.service';
import {AppConfig} from '../shared/app.config';
import {HttpClient, HttpXhrBackend} from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class ItemTypeService {
public itemTypes: IItemTypes;
private httpClient: HttpClient;
constructor(itemService:ItemService) {
itemService.getItemTypes().subscribe((itemTypes) => {
this.itemTypes = itemTypes;
});
constructor(xhrBackend: HttpXhrBackend) {
this.httpClient = new HttpClient(xhrBackend);
}
// itemService.getItemTypes().subscribe((itemTypes) => {
// this.itemTypes = itemTypes;
// });
getIcon(itemType: string) {
var icon = "fa fa-file-o";
if (this.itemTypes[itemType]) icon = this.itemTypes[itemType].icon;
@@ -27,6 +31,12 @@ export class ItemTypeService {
return color;
}
getExtraAttributes(itemType: string) {
var extraAttributes = null;
if (this.itemTypes[itemType]) extraAttributes = this.itemTypes[itemType].extraAttributes;
return extraAttributes;
}
hasViewer(item: IItem) {
let itemType: string = item.itemType;
if (this.itemTypes[itemType]) return this.itemTypes[itemType].viewer !== undefined;
@@ -43,4 +53,16 @@ export class ItemTypeService {
let itemType: string = item.itemType;
return itemType == "vnd.farmmaps.itemtype.geotiff.processed" || itemType == "vnd.farmmaps.itemtype.layer" || itemType == "vnd.farmmaps.itemtype.shape.processed";
}
public load(config:AppConfig): Promise<any> {
var url = `${ config.getConfig("apiEndPoint")}/api/v1/itemtypes/`
return this.httpClient.get(url)
.toPromise()
.then((itemTypes:IItemTypes) => {
this.itemTypes = itemTypes;
//return data;
})
.catch(error => this.itemTypes = null);
};
}