mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
added functionality to view a previous version (#5913)
This commit is contained in:
@@ -20,7 +20,7 @@ import {
|
||||
Input, OnChanges, Output, TemplateRef,
|
||||
ViewEncapsulation, OnInit, OnDestroy
|
||||
} from '@angular/core';
|
||||
import { RenditionPaging, SharedLinkEntry, Node, RenditionEntry, NodeEntry } from '@alfresco/js-api';
|
||||
import { SharedLinkEntry, Node, Version, RenditionEntry, NodeEntry, VersionEntry } from '@alfresco/js-api';
|
||||
import { BaseEvent } from '../../events';
|
||||
import { AlfrescoApiService } from '../../services/alfresco-api.service';
|
||||
import { LogService } from '../../services/log.service';
|
||||
@@ -74,6 +74,10 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
@Input()
|
||||
nodeId: string = null;
|
||||
|
||||
/** Version Id of the file to load. */
|
||||
@Input()
|
||||
versionId: string = null;
|
||||
|
||||
/** Shared link id (to display shared file). */
|
||||
@Input()
|
||||
sharedLinkId: string = null;
|
||||
@@ -206,6 +210,7 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
viewerType = 'unknown';
|
||||
isLoading = false;
|
||||
nodeEntry: NodeEntry;
|
||||
versionEntry: VersionEntry;
|
||||
|
||||
extensionTemplates: { template: TemplateRef<any>, isVisible: boolean }[] = [];
|
||||
externalExtensions: string[] = [];
|
||||
@@ -255,6 +260,17 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
).subscribe((node) => this.onNodeUpdated(node))
|
||||
);
|
||||
|
||||
this.subscriptions.push(
|
||||
this.viewUtils.viewerTypeChange.subscribe((type: string) => {
|
||||
this.viewerType = type;
|
||||
})
|
||||
);
|
||||
this.subscriptions.push(
|
||||
this.viewUtils.urlFileContentChange.subscribe((content: string) => {
|
||||
this.urlFileContent = content;
|
||||
})
|
||||
);
|
||||
|
||||
this.loadExtensions();
|
||||
}
|
||||
|
||||
@@ -298,9 +314,20 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
this.apiService.nodesApi.getNode(this.nodeId, { include: ['allowableOperations'] }).then(
|
||||
(node: NodeEntry) => {
|
||||
this.nodeEntry = node;
|
||||
this.setUpNodeFile(node.entry).then(() => {
|
||||
this.isLoading = false;
|
||||
});
|
||||
if (this.versionId) {
|
||||
this.apiService.versionsApi.getVersion(this.nodeId, this.versionId).then(
|
||||
(version: VersionEntry) => {
|
||||
this.versionEntry = version;
|
||||
this.setUpNodeFile(node.entry, version.entry).then(() => {
|
||||
this.isLoading = false;
|
||||
});
|
||||
}
|
||||
);
|
||||
} else {
|
||||
this.setUpNodeFile(node.entry).then(() => {
|
||||
this.isLoading = false;
|
||||
});
|
||||
}
|
||||
},
|
||||
() => {
|
||||
this.isLoading = false;
|
||||
@@ -353,21 +380,23 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
this.scrollTop();
|
||||
}
|
||||
|
||||
private async setUpNodeFile(data: Node) {
|
||||
private async setUpNodeFile(nodeData: Node, versionData?: Version) {
|
||||
let setupNode;
|
||||
|
||||
if (data.content) {
|
||||
this.mimeType = data.content.mimeType;
|
||||
if (versionData && versionData.content) {
|
||||
this.mimeType = versionData.content.mimeType;
|
||||
} else if (nodeData.content) {
|
||||
this.mimeType = nodeData.content.mimeType;
|
||||
}
|
||||
|
||||
this.fileTitle = this.getDisplayName(data.name);
|
||||
this.fileTitle = this.getDisplayName(nodeData.name);
|
||||
|
||||
this.urlFileContent = this.apiService.contentApi.getContentUrl(data.id);
|
||||
this.urlFileContent = versionData ? this.apiService.contentApi.getVersionContentUrl(this.nodeId, versionData.id) :
|
||||
this.urlFileContent = this.cacheBusterNumber ? this.urlFileContent + '&' + this.cacheBusterNumber : this.urlFileContent;
|
||||
|
||||
this.extension = this.getFileExtension(data.name);
|
||||
this.extension = this.getFileExtension(versionData ? versionData.name : nodeData.name);
|
||||
|
||||
this.fileName = data.name;
|
||||
this.fileName = versionData ? versionData.name : nodeData.name;
|
||||
|
||||
this.viewerType = this.getViewerTypeByExtension(this.extension);
|
||||
if (this.viewerType === 'unknown') {
|
||||
@@ -375,12 +404,12 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
if (this.viewerType === 'unknown') {
|
||||
setupNode = this.displayNodeRendition(data.id);
|
||||
setupNode = this.viewUtils.displayNodeRendition(nodeData.id, versionData ? versionData.id : undefined);
|
||||
}
|
||||
|
||||
this.extensionChange.emit(this.extension);
|
||||
this.sidebarRightTemplateContext.node = data;
|
||||
this.sidebarLeftTemplateContext.node = data;
|
||||
this.sidebarRightTemplateContext.node = nodeData;
|
||||
this.sidebarLeftTemplateContext.node = nodeData;
|
||||
this.scrollTop();
|
||||
|
||||
return setupNode;
|
||||
@@ -603,25 +632,6 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
private async displayNodeRendition(nodeId: string) {
|
||||
try {
|
||||
const rendition = await this.resolveRendition(nodeId, 'pdf');
|
||||
if (rendition) {
|
||||
const renditionId = rendition.entry.id;
|
||||
|
||||
if (renditionId === 'pdf') {
|
||||
this.viewerType = 'pdf';
|
||||
} else if (renditionId === 'imgpreview') {
|
||||
this.viewerType = 'image';
|
||||
}
|
||||
|
||||
this.urlFileContent = this.apiService.contentApi.getRenditionUrl(nodeId, renditionId);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logService.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
private async displaySharedLinkRendition(sharedId: string) {
|
||||
try {
|
||||
const rendition: RenditionEntry = await this.apiService.renditionsApi.getSharedLinkRendition(sharedId, 'pdf');
|
||||
@@ -643,69 +653,6 @@ export class ViewerComponent implements OnChanges, OnInit, OnDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveRendition(nodeId: string, renditionId: string): Promise<RenditionEntry> {
|
||||
renditionId = renditionId.toLowerCase();
|
||||
|
||||
const supportedRendition: RenditionPaging = await this.apiService.renditionsApi.getRenditions(nodeId);
|
||||
|
||||
let rendition: RenditionEntry = supportedRendition.list.entries.find((renditionEntry: RenditionEntry) => renditionEntry.entry.id.toLowerCase() === renditionId);
|
||||
if (!rendition) {
|
||||
renditionId = 'imgpreview';
|
||||
rendition = supportedRendition.list.entries.find((renditionEntry: RenditionEntry) => renditionEntry.entry.id.toLowerCase() === renditionId);
|
||||
}
|
||||
|
||||
if (rendition) {
|
||||
const status: string = rendition.entry.status.toString();
|
||||
|
||||
if (status === 'NOT_CREATED') {
|
||||
try {
|
||||
await this.apiService.renditionsApi.createRendition(nodeId, { id: renditionId }).then(() => {
|
||||
this.viewerType = 'in_creation';
|
||||
});
|
||||
rendition = await this.waitRendition(nodeId, renditionId);
|
||||
} catch (err) {
|
||||
this.logService.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rendition;
|
||||
}
|
||||
|
||||
private async waitRendition(nodeId: string, renditionId: string): Promise<RenditionEntry> {
|
||||
let currentRetry: number = 0;
|
||||
return new Promise<RenditionEntry>((resolve, reject) => {
|
||||
const intervalId = setInterval(() => {
|
||||
currentRetry++;
|
||||
if (this.maxRetries >= currentRetry) {
|
||||
this.apiService.renditionsApi.getRendition(nodeId, renditionId).then((rendition: RenditionEntry) => {
|
||||
const status: string = rendition.entry.status.toString();
|
||||
if (status === 'CREATED') {
|
||||
|
||||
if (renditionId === 'pdf') {
|
||||
this.viewerType = 'pdf';
|
||||
} else if (renditionId === 'imgpreview') {
|
||||
this.viewerType = 'image';
|
||||
}
|
||||
|
||||
this.urlFileContent = this.apiService.contentApi.getRenditionUrl(nodeId, renditionId);
|
||||
|
||||
clearInterval(intervalId);
|
||||
return resolve(rendition);
|
||||
}
|
||||
}, () => {
|
||||
this.viewerType = 'error_in_creation';
|
||||
return reject();
|
||||
});
|
||||
} else {
|
||||
this.isLoading = false;
|
||||
this.viewerType = 'error_in_creation';
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
}, this.TRY_TIMEOUT);
|
||||
});
|
||||
}
|
||||
|
||||
checkExtensions(extensionAllowed) {
|
||||
if (typeof extensionAllowed === 'string') {
|
||||
return this.extension.toLowerCase() === extensionAllowed.toLowerCase();
|
||||
|
Reference in New Issue
Block a user