From b83aca79694d2f66709ada9cdc766360d49ab69d Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Tue, 11 Feb 2020 18:55:43 +0100 Subject: [PATCH] Refactor itemtype loading --- .../common/src/fm/common-service.module.ts | 3 +- projects/common/src/fm/models/item.type.ts | 15 ++-- .../common/src/fm/services/item.service.ts | 11 ++- .../src/fm/services/itemtype.service.ts | 32 +++++++-- .../src/fm/shared/app.config.factory.ts | 69 ++++++++++--------- 5 files changed, 83 insertions(+), 47 deletions(-) diff --git a/projects/common/src/fm/common-service.module.ts b/projects/common/src/fm/common-service.module.ts index b4aa0bc..b716159 100644 --- a/projects/common/src/fm/common-service.module.ts +++ b/projects/common/src/fm/common-service.module.ts @@ -65,10 +65,11 @@ export class AppCommonServiceModule { ngModule: AppCommonServiceModule, providers: [ AppConfig, + ItemTypeService, { provide: APP_INITIALIZER, useFactory: appConfigFactory, - deps: [Injector, AppConfig, OAuthService, AuthConfigFactory, OAuthStorage], + deps: [Injector, AppConfig, OAuthService, AuthConfigFactory, OAuthStorage,ItemTypeService], multi: true }, { diff --git a/projects/common/src/fm/models/item.type.ts b/projects/common/src/fm/models/item.type.ts index 151ec05..4c19e57 100644 --- a/projects/common/src/fm/models/item.type.ts +++ b/projects/common/src/fm/models/item.type.ts @@ -1,7 +1,8 @@ -export interface IItemType { - icon?: string; - viewer?: string; - editor?: string; - isFolder?: boolean; - iconColor?: string; -} +export interface IItemType { + icon?: string; + viewer?: string; + editor?: string; + isFolder?: boolean; + iconColor?: string; + extraAttributes?: string; +} diff --git a/projects/common/src/fm/services/item.service.ts b/projects/common/src/fm/services/item.service.ts index d62b343..62a5c50 100644 --- a/projects/common/src/fm/services/item.service.ts +++ b/projects/common/src/fm/services/item.service.ts @@ -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); diff --git a/projects/common/src/fm/services/itemtype.service.ts b/projects/common/src/fm/services/itemtype.service.ts index e010a23..426ea45 100644 --- a/projects/common/src/fm/services/itemtype.service.ts +++ b/projects/common/src/fm/services/itemtype.service.ts @@ -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 { + + 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); + }; } \ No newline at end of file diff --git a/projects/common/src/fm/shared/app.config.factory.ts b/projects/common/src/fm/shared/app.config.factory.ts index a824c10..c630dbd 100644 --- a/projects/common/src/fm/shared/app.config.factory.ts +++ b/projects/common/src/fm/shared/app.config.factory.ts @@ -3,44 +3,49 @@ import { Location} from '@angular/common'; import { Router,UrlSerializer } from '@angular/router'; import { AuthConfig, OAuthService, JwksValidationHandler, OAuthErrorEvent, OAuthStorage } from 'angular-oauth2-oidc'; import { AppConfig } from "./app.config"; +import {ItemTypeService} from '../services/itemtype.service'; import { IAuthconfigFactory } from './authconfigFactory'; -export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthService: OAuthService, authconfigFactory:IAuthconfigFactory,authStorage:OAuthStorage): () => Promise { +export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthService: OAuthService, authconfigFactory:IAuthconfigFactory,authStorage:OAuthStorage,itemtypeService:ItemTypeService): () => Promise { return (): Promise => { - return appConfig.load().then(() => { - oauthService.events.subscribe((event) => { - console.debug(event.type); - if (event.type == 'token_error' || event.type == 'silent_refresh_timeout') { - let e = event as OAuthErrorEvent; - let p = e.params as any; - if (event.type == 'silent_refresh_timeout' || (p.error && p.error == 'login_required')) { - let router = injector.get(Router); - console.debug("Session expired"); - router.navigate(['loggedout'], { queryParams: { redirectTo: router.url } }); - } - } - }); - oauthService.configure(authconfigFactory.getAuthConfig(appConfig)); - oauthService.setStorage(authStorage); - oauthService.tokenValidationHandler = new JwksValidationHandler(); - oauthService.tokenValidationHandler.validateAtHash = function () { - return new Promise((res) => { res(true); }) - }; - oauthService.setupAutomaticSilentRefresh(); - let location = injector.get(Location); - let router = injector.get(Router); - let urlPath = location.path(); - oauthService.loadDiscoveryDocument().then(() => { - oauthService.tryLogin({ - onTokenReceived: (info) => { - urlPath = info.state; + return new Promise((resolve,reject) => { + appConfig.load().then(() => { + oauthService.events.subscribe((event) => { + console.debug(event.type); + if (event.type == 'token_error' || event.type == 'silent_refresh_timeout') { + let e = event as OAuthErrorEvent; + let p = e.params as any; + if (event.type == 'silent_refresh_timeout' || (p.error && p.error == 'login_required')) { + let router = injector.get(Router); + console.debug("Session expired"); + router.navigate(['loggedout'], { queryParams: { redirectTo: router.url } }); + } } - }).then(() => { - router.navigateByUrl(urlPath); - }); - }) + }); + oauthService.configure(authconfigFactory.getAuthConfig(appConfig)); + oauthService.setStorage(authStorage); + oauthService.tokenValidationHandler = new JwksValidationHandler(); + oauthService.tokenValidationHandler.validateAtHash = function () { + return new Promise((res) => { res(true); }) + }; + oauthService.setupAutomaticSilentRefresh(); + let location = injector.get(Location); + let router = injector.get(Router); + let urlPath = location.path(); + oauthService.loadDiscoveryDocument().then(() => { + oauthService.tryLogin({ + onTokenReceived: (info) => { + urlPath = info.state; + } + }).then(() => { + router.navigateByUrl(urlPath); + }); + }) + }).then(() => { + itemtypeService.load(appConfig).then(() => resolve()).catch(() => reject()); + }); }); } }