| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0 |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| |
| import {Inject, Injectable} from "@angular/core"; |
| import {Actions, createEffect, ofType} from "@ngrx/effects"; |
| import {filter, mergeMap, switchMap} from "rxjs/operators"; |
| import {AuthService} from "../../../core/auth"; |
| import {URL_TOKEN, WINDOW} from "../../../core/dom"; |
| import {CONTACT_DATA_BASE_ROUTE, SPA_BACKEND_ROUTE} from "../../../core/external-routes"; |
| import {urlJoin} from "../../../util/http"; |
| import {openAttachmentAction, openContactDataBaseAction, openFileAction} from "../actions"; |
| |
| @Injectable({providedIn: "root"}) |
| export class OpenNewTabEffect { |
| |
| public openContactDataBase$ = createEffect(() => this.actions.pipe( |
| ofType(openContactDataBaseAction), |
| filter(() => this.authenticationService.token != null), |
| switchMap(() => this.open(this.contactDataBaseRoute, true)) |
| ), {dispatch: false}); |
| |
| public openAttachment$ = createEffect(() => this.actions.pipe( |
| ofType(openAttachmentAction), |
| filter(() => this.authenticationService.token != null), |
| filter((action) => action.statementId != null && action.attachmentId != null), |
| switchMap((action) => this.openAttachment(action.statementId, action.attachmentId)) |
| ), {dispatch: false}); |
| |
| public openFile$ = createEffect(() => this.actions.pipe( |
| ofType(openFileAction), |
| filter((action) => action.file instanceof File), |
| mergeMap((action) => this.openFile(action.file)) |
| ), {dispatch: false}); |
| |
| public constructor( |
| public actions: Actions, |
| private readonly authenticationService: AuthService, |
| @Inject(SPA_BACKEND_ROUTE) private readonly spaBackendRoute: string, |
| @Inject(CONTACT_DATA_BASE_ROUTE) private readonly contactDataBaseRoute: string, |
| @Inject(WINDOW) private readonly window: Window, |
| @Inject(URL_TOKEN) private readonly url: typeof URL |
| ) { |
| |
| } |
| |
| public async openAttachment(statementId: number, attachmentId: number) { |
| const endPoint = `/statements/${statementId}/attachments/${attachmentId}/file`; |
| return this.open(urlJoin(this.spaBackendRoute, endPoint), true); |
| } |
| |
| public async openFile(file: File) { |
| const objectUrl = this.url.createObjectURL(file); |
| const tab = await this.open(objectUrl, false, file.name); |
| tab.onbeforeunload = () => this.url.revokeObjectURL(objectUrl); |
| return tab; |
| } |
| |
| public async open(url: string, withToken?: boolean, title?: string) { |
| url += withToken ? "?accessToken=" + this.authenticationService.token : ""; |
| const tab = this.window.open(url, "_blank"); |
| if (title != null) { |
| tab.onload = () => tab.document.title = title; |
| } |
| return tab; |
| } |
| |
| |
| } |