imporove sorting management and reduce repetitive code (#381)

* rework sorting management

* remove fdescribe

* test fixes

* unified desctructor

* unified page reload

* test fixes

* code fixes

* test fixes

* test fixes
This commit is contained in:
Denys Vuika
2018-06-02 16:35:55 +01:00
committed by Cilibiu Bogdan
parent 0ac33f820b
commit 7bb0905045
23 changed files with 195 additions and 486 deletions

View File

@@ -91,9 +91,9 @@
currentFolderId="-recent-"
selectionMode="multiple"
[navigate]="false"
[sorting]="sorting"
[sorting]="[ 'modifiedAt', 'desc' ]"
[acaSortingPreferenceKey]="sortingPreferenceKey"
[imageResolver]="imageResolver"
(sorting-changed)="onSortingChanged($event)"
(node-dblclick)="onNodeDoubleClick($event.detail?.node?.entry)"
(node-select)="onNodeSelect($event, documentList)">

View File

@@ -23,9 +23,9 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { TestBed, async } from '@angular/core/testing';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import {
@@ -47,12 +47,11 @@ import { NodePermissionService } from '../../common/services/node-permission.ser
import { RecentFilesComponent } from './recent-files.component';
describe('RecentFiles Routed Component', () => {
let fixture;
let component;
let fixture: ComponentFixture<RecentFilesComponent>;
let component: RecentFilesComponent;
let router: Router;
let alfrescoApi: AlfrescoApiService;
let contentService: ContentManagementService;
let preferenceService: UserPreferencesService;
let page;
beforeEach(() => {
@@ -85,9 +84,6 @@ describe('RecentFiles Routed Component', () => {
AppConfigPipe
],
providers: [
{ provide: ActivatedRoute, useValue: {
snapshot: { data: { preferencePrefix: 'prefix' } }
} } ,
{ provide: TranslationService, useClass: TranslationMock },
AuthenticationService,
UserPreferencesService,
@@ -111,7 +107,6 @@ describe('RecentFiles Routed Component', () => {
router = TestBed.get(Router);
contentService = TestBed.get(ContentManagementService);
preferenceService = TestBed.get(UserPreferencesService);
alfrescoApi = TestBed.get(AlfrescoApiService);
alfrescoApi.reset();
});
@@ -127,7 +122,7 @@ describe('RecentFiles Routed Component', () => {
describe('OnInit()', () => {
beforeEach(() => {
spyOn(component, 'refresh').and.stub();
spyOn(component, 'reload').and.stub();
});
it('should reload nodes on onDeleteNode event', () => {
@@ -135,7 +130,7 @@ describe('RecentFiles Routed Component', () => {
contentService.nodeDeleted.next();
expect(component.refresh).toHaveBeenCalled();
expect(component.reload).toHaveBeenCalled();
});
it('should reload on onRestoreNode event', () => {
@@ -143,7 +138,7 @@ describe('RecentFiles Routed Component', () => {
contentService.nodeRestored.next();
expect(component.refresh).toHaveBeenCalled();
expect(component.reload).toHaveBeenCalled();
});
it('should reload on move node event', () => {
@@ -151,7 +146,7 @@ describe('RecentFiles Routed Component', () => {
contentService.nodeMoved.next();
expect(component.refresh).toHaveBeenCalled();
expect(component.reload).toHaveBeenCalled();
});
});
@@ -182,40 +177,9 @@ describe('RecentFiles Routed Component', () => {
spyOn(component.documentList, 'reload');
fixture.detectChanges();
component.refresh();
component.reload();
expect(component.documentList.reload).toHaveBeenCalled();
});
});
describe('onSortingChanged', () => {
it('should save sorting input', () => {
spyOn(preferenceService, 'set');
const event = <any>{
detail: {
key: 'some-name',
direction: 'some-direction'
}
};
component.onSortingChanged(event);
expect(preferenceService.set).toHaveBeenCalledWith('prefix.sorting.key', 'some-name');
expect(preferenceService.set).toHaveBeenCalledWith('prefix.sorting.direction', 'some-direction');
});
it('should save default sorting when no input', () => {
spyOn(preferenceService, 'set');
const event = <any>{
detail: {}
};
component.onSortingChanged(event);
expect(preferenceService.set).toHaveBeenCalledWith('prefix.sorting.key', 'modifiedAt');
expect(preferenceService.set).toHaveBeenCalledWith('prefix.sorting.direction', 'desc');
});
});
});

View File

@@ -23,12 +23,10 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
import { Subscription } from 'rxjs/Rx';
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { MinimalNodeEntryEntity } from 'alfresco-js-api';
import { UserPreferencesService } from '@alfresco/adf-core';
import { DocumentListComponent } from '@alfresco/adf-content-services';
import { ContentManagementService } from '../../common/services/content-management.service';
import { PageComponent } from '../page.component';
@@ -37,41 +35,25 @@ import { NodePermissionService } from '../../common/services/node-permission.ser
@Component({
templateUrl: './recent-files.component.html'
})
export class RecentFilesComponent extends PageComponent implements OnInit, OnDestroy {
@ViewChild(DocumentListComponent)
documentList: DocumentListComponent;
private subscriptions: Subscription[] = [];
sorting = [ 'modifiedAt', 'desc' ];
export class RecentFilesComponent extends PageComponent implements OnInit {
constructor(
private router: Router,
private route: ActivatedRoute,
route: ActivatedRoute,
private content: ContentManagementService,
public permission: NodePermissionService,
preferences: UserPreferencesService) {
super(preferences);
const sortingKey = preferences.get(`${this.prefix}.sorting.key`) || 'modifiedAt';
const sortingDirection = preferences.get(`${this.prefix}.sorting.direction`) || 'desc';
this.sorting = [sortingKey, sortingDirection];
super(preferences, route);
}
ngOnInit() {
this.subscriptions = this.subscriptions.concat([
this.content.nodeDeleted.subscribe(() => this.refresh()),
this.content.nodeMoved.subscribe(() => this.refresh()),
this.content.nodeRestored.subscribe(() => this.refresh())
this.content.nodeDeleted.subscribe(() => this.reload()),
this.content.nodeMoved.subscribe(() => this.reload()),
this.content.nodeRestored.subscribe(() => this.reload())
]);
}
ngOnDestroy() {
this.subscriptions.forEach(s => s.unsubscribe());
}
onNodeDoubleClick(node: MinimalNodeEntryEntity) {
if (node && PageComponent.isLockedNode(node)) {
event.preventDefault();
@@ -80,19 +62,4 @@ export class RecentFilesComponent extends PageComponent implements OnInit, OnDes
this.router.navigate(['./preview', node.id], { relativeTo: this.route });
}
}
refresh(): void {
if (this.documentList) {
this.documentList.reload();
}
}
onSortingChanged(event: CustomEvent) {
this.preferences.set(`${this.prefix}.sorting.key`, event.detail.key || 'modifiedAt');
this.preferences.set(`${this.prefix}.sorting.direction`, event.detail.direction || 'desc');
}
private get prefix() {
return this.route.snapshot.data.preferencePrefix;
}
}