mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
126 lines
5.0 KiB
TypeScript
126 lines
5.0 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { EventEmitter, Injectable, Output } from '@angular/core';
|
|
import { AlfrescoApiService, TranslationService } from '@alfresco/adf-core';
|
|
import { Observable, of, Subject } from 'rxjs';
|
|
import { AttachFileWidgetDialogComponentData } from './attach-file-widget-dialog-component.interface';
|
|
import { AlfrescoEndpointRepresentation, Node, ContentApi } from '@alfresco/js-api';
|
|
import { AttachFileWidgetDialogComponent } from './attach-file-widget-dialog.component';
|
|
import { switchMap } from 'rxjs/operators';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
// eslint-disable-next-line @angular-eslint/directive-class-suffix
|
|
export class AttachFileWidgetDialogService {
|
|
/** Emitted when an error occurs. */
|
|
@Output()
|
|
error = new EventEmitter<any>();
|
|
|
|
private externalApis: { [key: string]: AlfrescoApiService } = {};
|
|
|
|
constructor(private dialog: MatDialog,
|
|
private translation: TranslationService) {
|
|
}
|
|
|
|
/**
|
|
* Opens a dialog to choose a file to upload.
|
|
*
|
|
* @param repository Alfresco endpoint that represents the content service
|
|
* @param currentFolderId Upload file from specific folder
|
|
* @param accountIdentifier account identifier
|
|
* @returns Information about the chosen file(s)
|
|
*/
|
|
openLogin(repository: AlfrescoEndpointRepresentation, currentFolderId = '-my-', accountIdentifier?: string): Observable<Node[]> {
|
|
const { title, ecmHost, selected, registerExternalHost } = this.constructPayload(repository);
|
|
const data: AttachFileWidgetDialogComponentData = {
|
|
title,
|
|
selected,
|
|
ecmHost,
|
|
currentFolderId,
|
|
isSelectionValid: (entry: Node) => entry.isFile,
|
|
showFilesInResult: true,
|
|
registerExternalHost,
|
|
accountIdentifier
|
|
};
|
|
|
|
this.openLoginDialog(data, 'adf-attach-file-widget-dialog', '630px');
|
|
return selected;
|
|
}
|
|
|
|
downloadURL(repository: AlfrescoEndpointRepresentation, sourceId: string): Observable<string> {
|
|
const { accountIdentifier } = this.constructPayload(repository);
|
|
|
|
if (this.externalApis[accountIdentifier]?.getInstance()) {
|
|
const contentApi = new ContentApi(this.externalApis[accountIdentifier].getInstance());
|
|
|
|
if (this.externalApis[accountIdentifier].getInstance().isLoggedIn()) {
|
|
return of(contentApi.getContentUrl(sourceId));
|
|
}
|
|
}
|
|
|
|
return this.showExternalHostLoginDialog(repository).pipe(
|
|
switchMap(() => {
|
|
const contentApi = new ContentApi(this.externalApis[accountIdentifier].getInstance());
|
|
return of(contentApi.getContentUrl(sourceId));
|
|
})
|
|
);
|
|
}
|
|
|
|
addService(accountIdentifier: string, apiService: AlfrescoApiService) {
|
|
if (!this.externalApis[accountIdentifier]) {
|
|
this.externalApis[accountIdentifier] = apiService;
|
|
}
|
|
}
|
|
|
|
/** Closes the currently open dialog. */
|
|
close() {
|
|
this.dialog.closeAll();
|
|
}
|
|
|
|
private openLoginDialog(data: AttachFileWidgetDialogComponentData, currentPanelClass: string, chosenWidth: string) {
|
|
this.dialog.open(AttachFileWidgetDialogComponent, { data, panelClass: currentPanelClass, width: chosenWidth });
|
|
}
|
|
|
|
private showExternalHostLoginDialog(repository: AlfrescoEndpointRepresentation): Observable<AlfrescoApiService> {
|
|
const data = {
|
|
...this.constructPayload(repository),
|
|
loginOnly: true
|
|
};
|
|
return this.dialog.open(AttachFileWidgetDialogComponent, { data, panelClass: 'adf-attach-file-widget-dialog', width: '630px' })
|
|
.afterClosed();
|
|
}
|
|
|
|
private constructPayload(repository: AlfrescoEndpointRepresentation) {
|
|
const accountIdentifier = 'alfresco-' + repository.id + '-' + repository.name;
|
|
const ecmHost = repository.repositoryUrl.replace('/alfresco', '');
|
|
const selected = new Subject<Node[]>();
|
|
selected.subscribe({
|
|
complete: this.close.bind(this)
|
|
});
|
|
const title = this.getLoginTitleTranslation(ecmHost);
|
|
const registerExternalHost = this.addService.bind(this);
|
|
return { ecmHost, accountIdentifier, selected, title, registerExternalHost };
|
|
}
|
|
|
|
private getLoginTitleTranslation(ecmHost: string): string {
|
|
return this.translation.instant(`ATTACH-FILE.DIALOG.LOGIN`, { ecmHost });
|
|
}
|
|
}
|