mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
@@ -0,0 +1,55 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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 { CommonModule } from '@angular/common';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { MaterialModule } from '../material.module';
|
||||
|
||||
import { DownloadZipDialogComponent } from './download-zip.dialog';
|
||||
import { FolderDialogComponent } from './folder.dialog';
|
||||
|
||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { NodesApiService, NotificationService, TranslationService } from '@alfresco/core';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
MaterialModule,
|
||||
TranslateModule,
|
||||
FormsModule,
|
||||
ReactiveFormsModule
|
||||
],
|
||||
declarations: [
|
||||
DownloadZipDialogComponent,
|
||||
FolderDialogComponent
|
||||
],
|
||||
providers: [
|
||||
NodesApiService,
|
||||
NotificationService,
|
||||
TranslationService
|
||||
],
|
||||
exports: [
|
||||
DownloadZipDialogComponent,
|
||||
FolderDialogComponent
|
||||
],
|
||||
entryComponents: [
|
||||
DownloadZipDialogComponent,
|
||||
FolderDialogComponent
|
||||
]
|
||||
})
|
||||
export class DialogModule {}
|
||||
@@ -0,0 +1,10 @@
|
||||
<h1 matDialogTitle>{{ 'CORE.DIALOG.DOWNLOAD_ZIP.TITLE' | translate }}</h1>
|
||||
<div mat-dialog-content>
|
||||
<mat-progress-bar color="primary" mode="indeterminate"></mat-progress-bar>
|
||||
</div>
|
||||
<div mat-dialog-actions>
|
||||
<span class="spacer"></span>
|
||||
<button mat-button color="primary" (click)="cancelDownload()">
|
||||
{{ 'CORE.DIALOG.DOWNLOAD_ZIP.ACTIONS.CANCEL' | translate }}
|
||||
</button>
|
||||
</div>
|
||||
@@ -0,0 +1,5 @@
|
||||
.spacer { flex: 1 1 auto; }
|
||||
|
||||
.adf-download-zip-dialog .mat-dialog-actions .mat-button-wrapper {
|
||||
text-transform: uppercase;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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 { Component, Inject, OnInit, ViewEncapsulation } from '@angular/core';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
|
||||
import { DownloadEntry, MinimalNodeEntity } from 'alfresco-js-api';
|
||||
import { LogService, AlfrescoApiService } from '@alfresco/core';
|
||||
|
||||
@Component({
|
||||
selector: 'adf-download-zip-dialog',
|
||||
templateUrl: './download-zip.dialog.html',
|
||||
styleUrls: ['./download-zip.dialog.scss'],
|
||||
host: { 'class': 'adf-download-zip-dialog' },
|
||||
encapsulation: ViewEncapsulation.None
|
||||
})
|
||||
export class DownloadZipDialogComponent implements OnInit {
|
||||
|
||||
// flag for async threads
|
||||
private cancelled = false;
|
||||
|
||||
constructor(private apiService: AlfrescoApiService,
|
||||
private dialogRef: MatDialogRef<DownloadZipDialogComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) private data: { nodeIds?: string[] },
|
||||
private logService: LogService) {
|
||||
}
|
||||
|
||||
private get downloadsApi() {
|
||||
return this.apiService.getInstance().core.downloadsApi;
|
||||
}
|
||||
|
||||
private get nodesApi() {
|
||||
return this.apiService.getInstance().core.nodesApi;
|
||||
}
|
||||
|
||||
private get contentApi() {
|
||||
return this.apiService.getInstance().content;
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
if (this.data && this.data.nodeIds && this.data.nodeIds.length > 0) {
|
||||
// change timeout to have a delay for demo purposes
|
||||
setTimeout(() => {
|
||||
if (!this.cancelled) {
|
||||
this.downloadZip(this.data.nodeIds);
|
||||
} else {
|
||||
this.logService.log('Cancelled');
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
|
||||
cancelDownload() {
|
||||
this.cancelled = true;
|
||||
this.dialogRef.close(false);
|
||||
}
|
||||
|
||||
downloadZip(nodeIds: string[]) {
|
||||
if (nodeIds && nodeIds.length > 0) {
|
||||
|
||||
const promise: any = this.downloadsApi.createDownload({ nodeIds });
|
||||
|
||||
promise.on('progress', progress => this.logService.log('Progress', progress));
|
||||
promise.on('error', error => this.logService.error('Error', error));
|
||||
promise.on('abort', data => this.logService.log('Abort', data));
|
||||
|
||||
promise.on('success', (data: DownloadEntry) => {
|
||||
if (data && data.entry && data.entry.id) {
|
||||
const url = this.contentApi.getContentUrl(data.entry.id, true);
|
||||
// the call is needed only to get the name of the package
|
||||
this.nodesApi.getNode(data.entry.id).then((downloadNode: MinimalNodeEntity) => {
|
||||
this.logService.log(downloadNode);
|
||||
const fileName = downloadNode.entry.name;
|
||||
this.waitAndDownload(data.entry.id, url, fileName);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
waitAndDownload(downloadId: string, url: string, fileName: string) {
|
||||
if (this.cancelled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.downloadsApi.getDownload(downloadId).then((d: DownloadEntry) => {
|
||||
if (d.entry) {
|
||||
if (d.entry.status === 'DONE') {
|
||||
this.download(url, fileName);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
this.waitAndDownload(downloadId, url, fileName);
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
download(url: string, fileName: string) {
|
||||
if (url && fileName) {
|
||||
const link = document.createElement('a');
|
||||
|
||||
link.style.display = 'none';
|
||||
link.download = fileName;
|
||||
link.href = url;
|
||||
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
}
|
||||
this.dialogRef.close(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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 { FormControl } from '@angular/forms';
|
||||
|
||||
const I18N_ERRORS_PATH = 'CORE.FOLDER_DIALOG.FOLDER_NAME.ERRORS';
|
||||
|
||||
export function forbidSpecialCharacters({ value }: FormControl) {
|
||||
const specialCharacters: RegExp = /([\*\"\<\>\\\/\?\:\|])/;
|
||||
const isValid: boolean = !specialCharacters.test(value);
|
||||
|
||||
return (isValid) ? null : {
|
||||
message: `${I18N_ERRORS_PATH}.SPECIAL_CHARACTERS`
|
||||
};
|
||||
}
|
||||
|
||||
export function forbidEndingDot({ value }: FormControl) {
|
||||
const isValid: boolean = ((value || '').trim().split('').pop() !== '.');
|
||||
|
||||
return isValid ? null : {
|
||||
message: `${I18N_ERRORS_PATH}.ENDING_DOT`
|
||||
};
|
||||
}
|
||||
|
||||
export function forbidOnlySpaces({ value }: FormControl) {
|
||||
const isValid: boolean = !!((value || '')).trim();
|
||||
|
||||
return isValid ? null : {
|
||||
message: `${I18N_ERRORS_PATH}.ONLY_SPACES`
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<h2 mat-dialog-title>
|
||||
{{
|
||||
(editing
|
||||
? 'CORE.FOLDER_DIALOG.EDIT_FOLDER_TITLE'
|
||||
: 'CORE.FOLDER_DIALOG.CREATE_FOLDER_TITLE'
|
||||
) | translate
|
||||
}}
|
||||
</h2>
|
||||
|
||||
<mat-dialog-content>
|
||||
<form [formGroup]="form" (submit)="submit()">
|
||||
<mat-input-container class="adf-full-width">
|
||||
<input
|
||||
placeholder="{{ 'CORE.FOLDER_DIALOG.FOLDER_NAME.LABEL' | translate }}"
|
||||
matInput
|
||||
required
|
||||
[formControl]="form.controls['name']"
|
||||
/>
|
||||
|
||||
<mat-hint *ngIf="form.controls['name'].dirty">
|
||||
<span *ngIf="form.controls['name'].errors?.required">
|
||||
{{ 'CORE.FOLDER_DIALOG.FOLDER_NAME.ERRORS.REQUIRED' | translate }}
|
||||
</span>
|
||||
|
||||
<span *ngIf="!form.controls['name'].errors?.required && form.controls['name'].errors?.message">
|
||||
{{ form.controls['name'].errors?.message | translate }}
|
||||
</span>
|
||||
</mat-hint>
|
||||
</mat-input-container>
|
||||
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<mat-input-container class="adf-full-width">
|
||||
<textarea
|
||||
matInput
|
||||
placeholder="{{ 'CORE.FOLDER_DIALOG.FOLDER_DESCRIPTION.LABEL' | translate }}"
|
||||
rows="4"
|
||||
[formControl]="form.controls['description']"></textarea>
|
||||
</mat-input-container>
|
||||
</form>
|
||||
</mat-dialog-content>
|
||||
|
||||
<mat-dialog-actions class="adf-dialog-buttons">
|
||||
<span class="adf-fill-remaining-space"></span>
|
||||
|
||||
<button
|
||||
mat-button
|
||||
mat-dialog-close>
|
||||
{{ 'CORE.FOLDER_DIALOG.CANCEL_BUTTON.LABEL' | translate }}
|
||||
</button>
|
||||
|
||||
<button class="adf-dialog-action-button"
|
||||
mat-button
|
||||
(click)="submit()"
|
||||
[disabled]="!form.valid">
|
||||
{{
|
||||
(editing
|
||||
? 'CORE.FOLDER_DIALOG.UPDATE_BUTTON.LABEL'
|
||||
: 'CORE.FOLDER_DIALOG.CREATE_BUTTON.LABEL'
|
||||
) | translate
|
||||
}}
|
||||
</button>
|
||||
</mat-dialog-actions>
|
||||
@@ -0,0 +1,20 @@
|
||||
.adf-fill-remaining-space {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.adf-full-width {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@mixin adf-dialog-theme($theme) {
|
||||
|
||||
$primary: map-get($theme, primary);
|
||||
|
||||
.adf-dialog-buttons button {
|
||||
text-transform: uppercase;
|
||||
}
|
||||
.adf-dialog-action-button:enabled {
|
||||
color: mat-color($primary);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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 { async, TestBed } from '@angular/core/testing';
|
||||
import { ComponentFixture } from '@angular/core/testing';
|
||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||
import { MatDialogRef } from '@angular/material';
|
||||
import { MaterialModule } from '../material.module';
|
||||
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
|
||||
import { Observable } from 'rxjs/Rx';
|
||||
|
||||
import { NodesApiService, NotificationService, TranslationService } from '@alfresco/core';
|
||||
import { FolderDialogComponent } from './folder.dialog';
|
||||
|
||||
describe('FolderDialogComponent', () => {
|
||||
|
||||
let fixture: ComponentFixture<FolderDialogComponent>;
|
||||
let component: FolderDialogComponent;
|
||||
let translationService: TranslationService;
|
||||
let nodesApi: NodesApiService;
|
||||
let notificationService: NotificationService;
|
||||
let dialogRef;
|
||||
|
||||
beforeEach(async(() => {
|
||||
dialogRef = {
|
||||
close: jasmine.createSpy('close')
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
MaterialModule,
|
||||
FormsModule,
|
||||
ReactiveFormsModule,
|
||||
BrowserDynamicTestingModule
|
||||
],
|
||||
declarations: [
|
||||
FolderDialogComponent
|
||||
],
|
||||
providers: [
|
||||
{ provide: MatDialogRef, useValue: dialogRef }
|
||||
]
|
||||
});
|
||||
|
||||
// entryComponents are not supported yet on TestBed, that is why this ugly workaround:
|
||||
// https://github.com/angular/angular/issues/10760
|
||||
TestBed.overrideModule(BrowserDynamicTestingModule, {
|
||||
set: {entryComponents: [ FolderDialogComponent ]}
|
||||
});
|
||||
|
||||
TestBed.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(FolderDialogComponent);
|
||||
component = fixture.componentInstance;
|
||||
|
||||
nodesApi = TestBed.get(NodesApiService);
|
||||
notificationService = TestBed.get(NotificationService);
|
||||
|
||||
translationService = TestBed.get(TranslationService);
|
||||
spyOn(translationService, 'get').and.returnValue(Observable.of('message'));
|
||||
});
|
||||
|
||||
describe('Edit', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
component.data = {
|
||||
folder: {
|
||||
id: 'node-id',
|
||||
name: 'folder-name',
|
||||
properties: {
|
||||
['cm:description']: 'folder-description'
|
||||
}
|
||||
}
|
||||
};
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should init form with folder name and description', () => {
|
||||
expect(component.name).toBe('folder-name');
|
||||
expect(component.description).toBe('folder-description');
|
||||
});
|
||||
|
||||
it('should update form input', () => {
|
||||
component.form.controls['name'].setValue('folder-name-update');
|
||||
component.form.controls['description'].setValue('folder-description-update');
|
||||
|
||||
expect(component.name).toBe('folder-name-update');
|
||||
expect(component.description).toBe('folder-description-update');
|
||||
});
|
||||
|
||||
it('should submit updated values if form is valid', () => {
|
||||
spyOn(nodesApi, 'updateNode').and.returnValue(Observable.of({}));
|
||||
|
||||
component.form.controls['name'].setValue('folder-name-update');
|
||||
component.form.controls['description'].setValue('folder-description-update');
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(nodesApi.updateNode).toHaveBeenCalledWith(
|
||||
'node-id',
|
||||
{
|
||||
name: 'folder-name-update',
|
||||
properties: {
|
||||
'cm:title': 'folder-name-update',
|
||||
'cm:description': 'folder-description-update'
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('should call dialog to close with form data when submit is succesfluly', () => {
|
||||
const folder = {
|
||||
data: 'folder-data'
|
||||
};
|
||||
|
||||
spyOn(nodesApi, 'updateNode').and.returnValue(Observable.of(folder));
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(dialogRef.close).toHaveBeenCalledWith(folder);
|
||||
});
|
||||
|
||||
it('should not submit if form is invalid', () => {
|
||||
spyOn(nodesApi, 'updateNode');
|
||||
|
||||
component.form.controls['name'].setValue('');
|
||||
component.form.controls['description'].setValue('');
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(component.form.valid).toBe(false);
|
||||
expect(nodesApi.updateNode).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call dialog to close if submit fails', () => {
|
||||
spyOn(nodesApi, 'updateNode').and.returnValue(Observable.throw('error'));
|
||||
spyOn(component, 'handleError').and.callFake(val => val);
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(component.handleError).toHaveBeenCalled();
|
||||
expect(dialogRef.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Create', () => {
|
||||
beforeEach(() => {
|
||||
component.data = {
|
||||
parentNodeId: 'parentNodeId',
|
||||
folder: null
|
||||
};
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should init form with empty inputs', () => {
|
||||
expect(component.name).toBe('');
|
||||
expect(component.description).toBe('');
|
||||
});
|
||||
|
||||
it('should update form input', () => {
|
||||
component.form.controls['name'].setValue('folder-name-update');
|
||||
component.form.controls['description'].setValue('folder-description-update');
|
||||
|
||||
expect(component.name).toBe('folder-name-update');
|
||||
expect(component.description).toBe('folder-description-update');
|
||||
});
|
||||
|
||||
it('should submit updated values if form is valid', () => {
|
||||
spyOn(nodesApi, 'createFolder').and.returnValue(Observable.of({}));
|
||||
|
||||
component.form.controls['name'].setValue('folder-name-update');
|
||||
component.form.controls['description'].setValue('folder-description-update');
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(nodesApi.createFolder).toHaveBeenCalledWith(
|
||||
'parentNodeId',
|
||||
{
|
||||
name: 'folder-name-update',
|
||||
properties: {
|
||||
'cm:title': 'folder-name-update',
|
||||
'cm:description': 'folder-description-update'
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('should call dialog to close with form data when submit is succesfluly', () => {
|
||||
const folder = {
|
||||
data: 'folder-data'
|
||||
};
|
||||
|
||||
component.form.controls['name'].setValue('name');
|
||||
component.form.controls['description'].setValue('description');
|
||||
|
||||
spyOn(nodesApi, 'createFolder').and.returnValue(Observable.of(folder));
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(dialogRef.close).toHaveBeenCalledWith(folder);
|
||||
});
|
||||
|
||||
it('should not submit if form is invalid', () => {
|
||||
spyOn(nodesApi, 'createFolder');
|
||||
|
||||
component.form.controls['name'].setValue('');
|
||||
component.form.controls['description'].setValue('');
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(component.form.valid).toBe(false);
|
||||
expect(nodesApi.createFolder).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call dialog to close if submit fails', () => {
|
||||
spyOn(nodesApi, 'createFolder').and.returnValue(Observable.throw('error'));
|
||||
spyOn(component, 'handleError').and.callFake(val => val);
|
||||
|
||||
component.form.controls['name'].setValue('name');
|
||||
component.form.controls['description'].setValue('description');
|
||||
|
||||
component.submit();
|
||||
|
||||
expect(component.handleError).toHaveBeenCalled();
|
||||
expect(dialogRef.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleError()', () => {
|
||||
it('should raise error for 409', () => {
|
||||
spyOn(notificationService, 'openSnackMessage').and.stub();
|
||||
|
||||
const error = {
|
||||
message: '{ "error": { "statusCode" : 409 } }'
|
||||
};
|
||||
|
||||
component.handleError(error);
|
||||
|
||||
expect(notificationService.openSnackMessage).toHaveBeenCalled();
|
||||
expect(translationService.get).toHaveBeenCalledWith('CORE.MESSAGES.ERRORS.EXISTENT_FOLDER');
|
||||
});
|
||||
|
||||
it('should raise generic error', () => {
|
||||
spyOn(notificationService, 'openSnackMessage').and.stub();
|
||||
|
||||
const error = {
|
||||
message: '{ "error": { "statusCode" : 123 } }'
|
||||
};
|
||||
|
||||
component.handleError(error);
|
||||
|
||||
expect(notificationService.openSnackMessage).toHaveBeenCalled();
|
||||
expect(translationService.get).toHaveBeenCalledWith('CORE.MESSAGES.ERRORS.GENERIC');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,140 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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 { Observable } from 'rxjs/Rx';
|
||||
|
||||
import { Component, Inject, OnInit, Optional } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
|
||||
|
||||
import { MinimalNodeEntryEntity } from 'alfresco-js-api';
|
||||
import { NodesApiService, NotificationService, TranslationService } from '@alfresco/core';
|
||||
|
||||
import { forbidEndingDot, forbidOnlySpaces, forbidSpecialCharacters } from './folder-name.validators';
|
||||
|
||||
@Component({
|
||||
selector: 'adf-folder-dialog',
|
||||
styleUrls: ['./folder.dialog.scss'],
|
||||
templateUrl: './folder.dialog.html'
|
||||
})
|
||||
export class FolderDialogComponent implements OnInit {
|
||||
form: FormGroup;
|
||||
folder: MinimalNodeEntryEntity = null;
|
||||
|
||||
constructor(
|
||||
private formBuilder: FormBuilder,
|
||||
private dialog: MatDialogRef<FolderDialogComponent>,
|
||||
private nodesApi: NodesApiService,
|
||||
private translation: TranslationService,
|
||||
private notification: NotificationService,
|
||||
@Optional()
|
||||
@Inject(MAT_DIALOG_DATA)
|
||||
public data: any
|
||||
) {}
|
||||
|
||||
get editing(): boolean {
|
||||
return !!this.data.folder;
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
const { folder } = this.data;
|
||||
let name = '';
|
||||
let description = '';
|
||||
|
||||
if (folder) {
|
||||
const { properties } = folder;
|
||||
|
||||
name = folder.name || '';
|
||||
description = properties ? properties['cm:description'] : '';
|
||||
}
|
||||
|
||||
const validators = {
|
||||
name: [
|
||||
Validators.required,
|
||||
forbidSpecialCharacters,
|
||||
forbidEndingDot,
|
||||
forbidOnlySpaces
|
||||
]
|
||||
};
|
||||
|
||||
this.form = this.formBuilder.group({
|
||||
name: [ name, validators.name ],
|
||||
description: [ description ]
|
||||
});
|
||||
}
|
||||
|
||||
get name(): string {
|
||||
let { name } = this.form.value;
|
||||
|
||||
return (name || '').trim();
|
||||
}
|
||||
|
||||
get description(): string {
|
||||
let { description } = this.form.value;
|
||||
|
||||
return (description || '').trim();
|
||||
}
|
||||
|
||||
private get properties(): any {
|
||||
const { name: title, description } = this;
|
||||
|
||||
return {
|
||||
'cm:title': title,
|
||||
'cm:description': description
|
||||
};
|
||||
}
|
||||
|
||||
private create(): Observable<MinimalNodeEntryEntity> {
|
||||
const { name, properties, nodesApi, data: { parentNodeId} } = this;
|
||||
return nodesApi.createFolder(parentNodeId, { name, properties });
|
||||
}
|
||||
|
||||
private edit(): Observable<MinimalNodeEntryEntity> {
|
||||
const { name, properties, nodesApi, data: { folder: { id: nodeId }} } = this;
|
||||
return nodesApi.updateNode(nodeId, { name, properties });
|
||||
}
|
||||
|
||||
submit() {
|
||||
const { form, dialog, editing } = this;
|
||||
|
||||
if (!form.valid) { return; }
|
||||
|
||||
(editing ? this.edit() : this.create())
|
||||
.subscribe(
|
||||
(folder: MinimalNodeEntryEntity) => dialog.close(folder),
|
||||
(error) => this.handleError(error)
|
||||
);
|
||||
}
|
||||
|
||||
handleError(error: any): any {
|
||||
let i18nMessageString = 'CORE.MESSAGES.ERRORS.GENERIC';
|
||||
|
||||
try {
|
||||
const { error: { statusCode } } = JSON.parse(error.message);
|
||||
|
||||
if (statusCode === 409) {
|
||||
i18nMessageString = 'CORE.MESSAGES.ERRORS.EXISTENT_FOLDER';
|
||||
}
|
||||
} catch (err) { /* Do nothing, keep the original message */ }
|
||||
|
||||
this.translation.get(i18nMessageString).subscribe(message => {
|
||||
this.notification.openSnackMessage(message, 3000);
|
||||
});
|
||||
|
||||
return error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export * from './public-api';
|
||||
@@ -0,0 +1,21 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export * from './download-zip.dialog';
|
||||
export * from './folder.dialog';
|
||||
|
||||
export * from './dialog.module';
|
||||
Reference in New Issue
Block a user