diff --git a/src/app/common/common.module.ts b/src/app/common/common.module.ts index 85ba72e63..a31010d56 100644 --- a/src/app/common/common.module.ts +++ b/src/app/common/common.module.ts @@ -23,7 +23,6 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AdfModule } from '../adf.module'; import { MaterialModule } from './material.module'; -import { FolderDialogComponent } from './dialogs/folder-dialog.component'; import { NodeCopyDirective } from './directives/node-copy.directive'; import { NodeDeleteDirective } from './directives/node-delete.directive'; import { NodeMoveDirective } from './directives/node-move.directive'; @@ -48,7 +47,6 @@ export function modules() { export function declarations() { return [ - FolderDialogComponent, NodeCopyDirective, NodeDeleteDirective, NodeMoveDirective, @@ -71,9 +69,7 @@ export function providers() { @NgModule({ imports: modules(), declarations: declarations(), - entryComponents: [ - FolderDialogComponent - ], + entryComponents: [], providers: providers(), exports: [ ...modules(), diff --git a/src/app/common/dialogs/folder-dialog.component.html b/src/app/common/dialogs/folder-dialog.component.html deleted file mode 100644 index b392bea9d..000000000 --- a/src/app/common/dialogs/folder-dialog.component.html +++ /dev/null @@ -1,62 +0,0 @@ -

- {{ - (editing - ? 'APP.FOLDER_DIALOG.EDIT_FOLDER_TITLE' - : 'APP.FOLDER_DIALOG.CREATE_FOLDER_TITLE' - ) | translate - }} -

- - -
- - - - - - {{ 'APP.FOLDER_DIALOG.FOLDER_NAME.ERRORS.REQUIRED' | translate }} - - - - {{ form.controls['name'].errors?.message | translate }} - - - - -
-
- - - - -
-
- - - - - - diff --git a/src/app/common/dialogs/folder-dialog.component.spec.ts b/src/app/common/dialogs/folder-dialog.component.spec.ts deleted file mode 100644 index ef118baa6..000000000 --- a/src/app/common/dialogs/folder-dialog.component.spec.ts +++ /dev/null @@ -1,260 +0,0 @@ -/*! - * @license - * Copyright 2017 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 { TestBed, async } from '@angular/core/testing'; -import { Observable } from 'rxjs/Rx'; -import { MatDialogModule, MatDialogRef } from '@angular/material'; -import { CoreModule, NodesApiService, TranslationService, NotificationService } from 'ng2-alfresco-core'; - -import {BrowserDynamicTestingModule} from '@angular/platform-browser-dynamic/testing'; -import { FolderDialogComponent } from './folder-dialog.component'; -import { ComponentFixture } from '@angular/core/testing'; - -describe('FolderDialogComponent', () => { - - let fixture: ComponentFixture; - let component: FolderDialogComponent; - let translationService: TranslationService; - let nodesApi: NodesApiService; - let notificationService: NotificationService; - let dialogRef; - - beforeEach(async(() => { - dialogRef = { - close: jasmine.createSpy('close') - }; - - TestBed.configureTestingModule({ - imports: [ - CoreModule, - MatDialogModule - ], - declarations: [ - FolderDialogComponent - ], - providers: [ - { provide: MatDialogRef, useValue: dialogRef } - ] - }) - .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' - } - } - }; - component.ngOnInit(); - }); - - 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 - }; - component.ngOnInit(); - }); - - 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('APP.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('APP.MESSAGES.ERRORS.GENERIC'); - }); - }); -}); diff --git a/src/app/common/dialogs/folder-dialog.component.ts b/src/app/common/dialogs/folder-dialog.component.ts deleted file mode 100644 index e8a395511..000000000 --- a/src/app/common/dialogs/folder-dialog.component.ts +++ /dev/null @@ -1,138 +0,0 @@ -/*! - * @license - * Copyright 2017 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, Optional, OnInit } from '@angular/core'; -import { FormGroup, FormBuilder, Validators } from '@angular/forms'; -import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; - -import { TranslationService, NodesApiService, NotificationService } from 'ng2-alfresco-core'; -import { MinimalNodeEntryEntity } from 'alfresco-js-api'; - -import { forbidSpecialCharacters, forbidEndingDot, forbidOnlySpaces } from './folder-name.validators'; - -@Component({ - selector: 'app-folder-dialog', - templateUrl: './folder-dialog.component.html' -}) -export class FolderDialogComponent implements OnInit { - form: FormGroup; - folder: MinimalNodeEntryEntity = null; - - constructor( - private formBuilder: FormBuilder, - private dialog: MatDialogRef, - 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 = '', 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 { - const { name } = this.form.value; - - return (name || '').trim(); - } - - get description(): string { - const { 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 { - const { name, properties, nodesApi, data: { parentNodeId} } = this; - return nodesApi.createFolder(parentNodeId, { name, properties }); - } - - private edit(): Observable { - 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 = 'APP.MESSAGES.ERRORS.GENERIC'; - - try { - const { error: { statusCode } } = JSON.parse(error.message); - - if (statusCode === 409) { - i18nMessageString = 'APP.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; - } -} diff --git a/src/app/common/dialogs/folder-name.validators.ts b/src/app/common/dialogs/folder-name.validators.ts deleted file mode 100644 index 714ac127f..000000000 --- a/src/app/common/dialogs/folder-name.validators.ts +++ /dev/null @@ -1,45 +0,0 @@ -/*! - * @license - * Copyright 2017 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 = 'APP.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 || '').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` - }; -}