-
Notifications
You must be signed in to change notification settings - Fork 263
/
content-node-dialog.service.ts
281 lines (248 loc) · 10.8 KB
/
content-node-dialog.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*!
* @license
* Copyright © 2005-2024 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, MatDialogRef } from '@angular/material/dialog';
import { EventEmitter, Injectable, Output } from '@angular/core';
import { ThumbnailService, TranslationService } from '@alfresco/adf-core';
import { Subject, Observable, throwError } from 'rxjs';
import { ShareDataRow } from '../document-list/data/share-data-row.model';
import { AllowableOperationsEnum } from '../common/models/allowable-operations.enum';
import { ContentService } from '../common/services/content.service';
import { Node, NodeEntry, SitePaging } from '@alfresco/js-api';
import { DocumentListService } from '../document-list/services/document-list.service';
import { ContentNodeSelectorComponent } from './content-node-selector.component';
import { ContentNodeSelectorComponentData } from './content-node-selector.component-data.interface';
import { NodeAction } from '../document-list/models/node-action.enum';
import { NodeLockDialogComponent } from '../dialogs/node-lock/node-lock.dialog';
import { switchMap } from 'rxjs/operators';
import { SitesService } from '../common/services/sites.service';
@Injectable({
providedIn: 'root'
})
// eslint-disable-next-line @angular-eslint/directive-class-suffix
export class ContentNodeDialogService {
static nonDocumentSiteContent = ['blog', 'calendar', 'dataLists', 'discussions', 'links', 'wiki'];
/** Emitted when an error occurs. */
@Output()
error: EventEmitter<any> = new EventEmitter<any>();
constructor(
private dialog: MatDialog,
private contentService: ContentService,
private documentListService: DocumentListService,
private siteService: SitesService,
private translation: TranslationService,
private thumbnailService: ThumbnailService
) {}
/**
* Opens a file browser at a chosen folder location.
* shows files and folders in the dialog search result.
*
* @param folderNodeId ID of the folder to use
* @returns Information about the selected file(s)
*/
openFileBrowseDialogByFolderId(folderNodeId: string): Observable<Node[]> {
return this.documentListService
.getFolderNode(folderNodeId)
.pipe(switchMap((nodeEntry: NodeEntry) => this.openUploadFileDialog(NodeAction.CHOOSE, nodeEntry.entry, true)));
}
/**
* Opens a lock node dialog.
*
* @param contentEntry Node to lock
* @returns Error/status message (if any)
*/
public openLockNodeDialog(contentEntry: Node): Subject<string> {
const observable: Subject<string> = new Subject<string>();
if (this.contentService.hasAllowableOperations(contentEntry, AllowableOperationsEnum.LOCK)) {
this.dialog.open(NodeLockDialogComponent, {
data: {
node: contentEntry,
onError: (error) => {
this.error.emit(error);
observable.error(error);
}
},
width: '400px'
});
} else {
observable.error('OPERATION.FAIL.NODE.NO_PERMISSION');
}
return observable;
}
/**
* Opens a file browser at a chosen site location.
* shows files and folders in the dialog search result.
*
* @returns Information about the selected file(s)
*/
openFileBrowseDialogBySite(): Observable<Node[]> {
return this.siteService
.getSites()
.pipe(switchMap((response: SitePaging) => this.openFileBrowseDialogByFolderId(response.list.entries[0].entry.guid)));
}
/**
* Opens a file browser at a default myFile location.
* shows files and folders in the dialog search result.
*
* @returns Information about the selected file(s)
*/
openFileBrowseDialogByDefaultLocation(): Observable<Node[]> {
return this.openFileBrowseDialogByFolderId('-my-');
}
/**
* Opens a folder browser at a chosen site location.
*
* @returns Information about the selected folder(s)
*/
openFolderBrowseDialogBySite(): Observable<Node[]> {
return this.openFolderBrowseDialogByFolderId('-my-');
}
/**
* Opens a folder browser at a chosen folder location.
*
* @param folderNodeId ID of the folder to use
* @returns Information about the selected folder(s)
*/
openFolderBrowseDialogByFolderId(folderNodeId: string): Observable<Node[]> {
return this.documentListService
.getFolderNode(folderNodeId)
.pipe(switchMap((node: NodeEntry) => this.openUploadFolderDialog(NodeAction.CHOOSE, node.entry)));
}
/**
* Opens a dialog to copy or move an item to a new location.
*
* @param action Name of the action (eg, "Copy" or "Move") to show in the title
* @param contentEntry Item to be copied or moved
* @param permission Permission for the operation
* @param excludeSiteContent The site content that should be filtered out
* @returns Information about files that were copied/moved
*/
openCopyMoveDialog(action: NodeAction, contentEntry: Node, permission?: string, excludeSiteContent?: string[]): Observable<Node[]> {
if (this.contentService.hasAllowableOperations(contentEntry, permission)) {
const select = new Subject<Node[]>();
const data: ContentNodeSelectorComponentData = {
title: this.getTitleTranslation(action, contentEntry.name),
actionName: action,
selectionMode: 'single',
currentFolderId: contentEntry.parentId,
imageResolver: this.imageResolver.bind(this),
where: '(isFolder=true)',
isSelectionValid: this.isCopyMoveSelectionValid.bind(this),
excludeSiteContent: excludeSiteContent || ContentNodeDialogService.nonDocumentSiteContent,
select
};
const dialogRef = this.openContentNodeDialog(data, 'adf-content-node-selector-dialog', '630px');
dialogRef.afterClosed().subscribe({ next: () => select.complete() });
return select;
} else {
const errors = new Error(JSON.stringify({ error: { statusCode: 403 } }));
return throwError(errors);
}
}
/**
* Gets the translation of the dialog title.
*
* @param action Name of the action to display in the dialog title
* @param name Name of the item on which the action is being performed
* @returns Translated version of the title
*/
getTitleTranslation(action: string, name: string): string {
return this.translation.instant(`NODE_SELECTOR.${action.toUpperCase()}_ITEM`, { name });
}
/**
* Opens a dialog to choose folders to upload.
*
* @param action Name of the action to show in the title
* @param contentEntry Item to upload
* @returns Information about the chosen folder(s)
*/
openUploadFolderDialog(action: NodeAction, contentEntry: Node): Observable<Node[]> {
const select = new Subject<Node[]>();
const data: ContentNodeSelectorComponentData = {
title: this.getTitleTranslation(action, this.translation.instant('DROPDOWN.MY_FILES_OPTION')),
actionName: action,
selectionMode: 'single',
currentFolderId: contentEntry.id,
imageResolver: this.imageResolver.bind(this),
isSelectionValid: this.hasAllowableOperationsOnNodeFolder.bind(this),
where: '(isFolder=true)',
select
};
const dialogRef = this.openContentNodeDialog(data, 'adf-content-node-selector-dialog', '630px');
dialogRef.afterClosed().subscribe({ next: () => select.complete() });
return select;
}
/**
* Opens a dialog to choose a file to upload.
*
* @param action Name of the action to show in the title
* @param contentEntry Item to upload
* @param showFilesInResult Show files in dialog search result
* @returns Information about the chosen file(s)
*/
openUploadFileDialog(action: NodeAction, contentEntry: Node, showFilesInResult = false): Observable<Node[]> {
const select = new Subject<Node[]>();
const data: ContentNodeSelectorComponentData = {
title: this.getTitleTranslation(action, this.translation.instant('DROPDOWN.MY_FILES_OPTION')),
actionName: action,
selectionMode: 'single',
currentFolderId: contentEntry.id,
imageResolver: this.imageResolver.bind(this),
isSelectionValid: (entry: Node) => entry.isFile,
select,
showFilesInResult
};
const dialogRef = this.openContentNodeDialog(data, 'adf-content-node-selector-dialog', '630px');
dialogRef.afterClosed().subscribe({ next: () => select.complete() });
return select;
}
private openContentNodeDialog(
data: ContentNodeSelectorComponentData,
panelClass: string,
width: string
): MatDialogRef<ContentNodeSelectorComponent> {
return this.dialog.open(ContentNodeSelectorComponent, {
data,
panelClass,
width,
disableClose: true
});
}
private imageResolver(row: ShareDataRow): string | null {
const entry: Node = row.node.entry;
if (!this.contentService.hasAllowableOperations(entry, 'create')) {
if (this.isNodeFolder(entry)) {
return this.thumbnailService.getMimeTypeIcon('disable/folder');
}
}
return null;
}
private hasAllowableOperationsOnNodeFolder(entry: Node): boolean {
return this.isNodeFolder(entry) && this.contentService.hasAllowableOperations(entry, 'create');
}
private isNodeFolder(entry: Node): boolean {
return entry.isFolder;
}
private isCopyMoveSelectionValid(entry: Node): boolean {
return this.hasEntityCreatePermission(entry) && !this.isSite(entry);
}
private hasEntityCreatePermission(entry: Node): boolean {
return this.contentService.hasAllowableOperations(entry, 'create');
}
private isSite(entry) {
return !!entry.guid || entry.nodeType === 'st:site' || entry.nodeType === 'st:sites';
}
}