diff --git a/src/app/context/context-list/context-list.component.ts b/src/app/context/context-list/context-list.component.ts index 806e51b8a..55bcf316a 100644 --- a/src/app/context/context-list/context-list.component.ts +++ b/src/app/context/context-list/context-list.component.ts @@ -47,7 +47,7 @@ export class ContextListComponent implements ToolComponent, OnInit { this.store .select(s => s.activeContext) - .subscribe((context: Context) => this.selectedContext = context); + .subscribe((context: Context) => this.handleActiveContext(context)); this.contexts = this.contextService.getContexts(); } @@ -55,9 +55,6 @@ export class ContextListComponent implements ToolComponent, OnInit { selectContext(context: Context) { if (context.uri !== this.selectedContext.uri) { this.contextService.loadContext(context.uri); - if (this.mapEditor !== undefined) { - this.store.dispatch({type: 'SELECT_TOOL', payload: this.mapEditor}); - } } } @@ -67,4 +64,14 @@ export class ContextListComponent implements ToolComponent, OnInit { this.store.dispatch({type: 'SELECT_TOOL', payload: this.contextEditor}); } } + + private handleActiveContext(context: Context) { + if (this.mapEditor !== undefined) { + if (this.selectedContext && this.selectedContext !== context) { + this.store.dispatch({type: 'SELECT_TOOL', payload: this.mapEditor}); + } + } + + this.selectedContext = context; + } } diff --git a/src/app/context/shared/context.service.ts b/src/app/context/shared/context.service.ts index 7d5052114..3f0d6b00e 100644 --- a/src/app/context/shared/context.service.ts +++ b/src/app/context/shared/context.service.ts @@ -39,7 +39,7 @@ export class ContextService { } return this.requestService.register( - this.http.get(`contexts/${fileName}`) + this.http.get(`contexts/${fileName}`), 'Context' ).map(res => res.json()); } diff --git a/src/app/core/request.service.ts b/src/app/core/request.service.ts index e035fe610..90bfa84c3 100644 --- a/src/app/core/request.service.ts +++ b/src/app/core/request.service.ts @@ -36,7 +36,13 @@ export class RequestService { const contentType = res.headers.get('content-type'); if (res.status === 200 && contentType.indexOf('application/json') === 0) { - const body = res.json() || {}; + let body; + try { + body = res.json(); + } catch (e) { + throw [{text: 'Invalid JSON received'}]; + } + if (body.status < 200 || body.status >= 300) { throw res; } @@ -51,14 +57,16 @@ export class RequestService { messages = this.extractMessages(res as Response); } - this.handleMessages(messages, title); + this.displayMessages(messages, title); + return Observable.throw(res); } private extractMessages(res: Response): Message[] { - if (!res || !res.headers) { return; } + if (!res || !res.headers) { return []; } let messages = []; + const contentType = res.headers.get('content-type'); if (contentType && contentType.indexOf('application/json') === 0) { const body = res.json() || {}; @@ -70,10 +78,9 @@ export class RequestService { return messages; } - private handleMessages(messages: Message[], title?: string) { - messages.forEach((message) => { - this.messageService.message(Object.assign({title: title}, message)); - }); + private displayMessages(messages, title?: string) { + messages.forEach((message: Message) => + this.messageService.message(Object.assign({title: title}, message))); } } diff --git a/src/app/map/map/map.component.ts b/src/app/map/map/map.component.ts index e7e475df3..20374a229 100644 --- a/src/app/map/map/map.component.ts +++ b/src/app/map/map/map.component.ts @@ -33,21 +33,25 @@ export class MapComponent implements OnInit, AfterViewInit { this.store .select(s => s.map) .filter(s => s !== null) - .subscribe((mapOptions: MapOptions) => this.map.setView(mapOptions.view)); + .subscribe((mapOptions: MapOptions) => + this.map.setView(mapOptions.view)); this.store .select(s => s.layers) - .subscribe((layerOptions: LayerOptions[]) => this.handleLayersChanged(layerOptions)); + .subscribe((layerOptions: LayerOptions[]) => + this.handleLayersChanged(layerOptions)); this.store .select(s => s.focusedResult) .filter(r => r !== null) - .subscribe((result: SearchResult) => this.handleFocusedResult(result)); + .subscribe((result: SearchResult) => + this.handleFocusedResult(result)); this.store .select(s => s.selectedResult) .filter(r => r !== null) - .subscribe((result: SearchResult) => this.handleSelectedResult(result)); + .subscribe((result: SearchResult) => + this.handleSelectedResult(result)); } ngAfterViewInit(): any {