[AAE-4428] Add selected file counter to attach file widget (#6881)

* [AAE-4428] Add selected file counter to attach file widget

* [AAE-4428] Add getSelectedCount method

* [AAE-4428] Add unit tests

* [AAE-4428] Changed so TranslationService injection is not necessary

* [AAE-4428] Added unit tests to check the element has been injected

* spacing

* [AAE-4428] Remove unnecessary check in unit test

* [AAE-4428] Add node counter directive

* [ci skip] Fix linting issues

* Remove changing the selection to the nabigated node

* Revert removing selection on folder loaded, add unit test

* Added documentation

Co-authored-by: adomi <ardit.domi@alfresco.com>
This commit is contained in:
Thomas Hunter
2021-04-15 13:57:26 +01:00
committed by GitHub
parent 7b1d32b06b
commit 96c94988c1
13 changed files with 208 additions and 6 deletions

View File

@@ -0,0 +1,28 @@
---
Title: Node Counter directive
Added: v4.4.0
Status: Active
Last reviewed: 2021-04-15
---
# [Node Counter directive](../../../lib/content-services/src/lib/directives/node-counter.directive.ts "Defined in node-counter.directive.ts")
Appends a counter to an element.
## Basic Usage
```html
<adf-toolbar>
<adf-toolbar-title [adf-node-counter]="getSelectedCount()">
...
</adf-toolbar-title>
</adf-toolbar>
```
## Class members
### Properties
| Name | Type | Default value | Description |
| ---- | ---- | ------------- | ----------- |
| counter | `number` | | Number of nodes selected. |

View File

@@ -43,7 +43,7 @@
</adf-search-panel> </adf-search-panel>
<div class="adf-content-node-selector-document-list-container"> <div class="adf-content-node-selector-document-list-container">
<adf-toolbar> <adf-toolbar>
<adf-toolbar-title> <adf-toolbar-title [adf-node-counter]="getSelectedCount()">
<ng-container *ngIf="!showBreadcrumbs()"> <ng-container *ngIf="!showBreadcrumbs()">
<span role="heading" aria-level="3" class="adf-search-results-label">{{ 'NODE_SELECTOR.SEARCH_RESULTS' | translate }}</span> <span role="heading" aria-level="3" class="adf-search-results-label">{{ 'NODE_SELECTOR.SEARCH_RESULTS' | translate }}</span>
</ng-container> </ng-container>

View File

@@ -372,6 +372,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
beforeEach(() => { beforeEach(() => {
const documentListService = TestBed.inject(DocumentListService); const documentListService = TestBed.inject(DocumentListService);
const expectedDefaultFolderNode = <NodeEntry> { entry: { path: { elements: [] } } }; const expectedDefaultFolderNode = <NodeEntry> { entry: { path: { elements: [] } } };
component.isSelectionValid = (node: Node) => node.isFile;
spyOn(documentListService, 'getFolderNode').and.returnValue(of(expectedDefaultFolderNode)); spyOn(documentListService, 'getFolderNode').and.returnValue(of(expectedDefaultFolderNode));
spyOn(documentListService, 'getFolder').and.returnValue(of({ spyOn(documentListService, 'getFolder').and.returnValue(of({
@@ -1026,6 +1027,14 @@ describe('ContentNodeSelectorPanelComponent', () => {
spyOn(sitesService, 'getSites').and.returnValue(of({ list: { entries: [] } })); spyOn(sitesService, 'getSites').and.returnValue(of({ list: { entries: [] } }));
}); });
it('should the selection become the currently navigated folder when the folder loads (Acts as destination for cases like copy action)', () => {
const fakeFolderNode = <Node> { id: 'fakeNodeId', isFolder: true };
component.documentList.folderNode = fakeFolderNode;
component.onFolderLoaded();
expect(component.chosenNode).toEqual([fakeFolderNode]);
});
describe('in the case when isSelectionValid is a custom function for checking permissions,', () => { describe('in the case when isSelectionValid is a custom function for checking permissions,', () => {
beforeEach(() => { beforeEach(() => {
@@ -1360,5 +1369,24 @@ describe('ContentNodeSelectorPanelComponent', () => {
expect(component.sorting).toEqual(['createdAt', 'desc']); expect(component.sorting).toEqual(['createdAt', 'desc']);
}); });
}); });
describe('Selected nodes counter', () => {
it('should getSelectedCount return 0 by default', () => {
expect(component.getSelectedCount()).toBe(0);
});
it('should getSelectedCount return 1 when node is selected', () => {
component.onCurrentSelection([{ entry: new Node({ id: 'fake', isFile: true }) }]);
expect(component.getSelectedCount()).toBe(1);
});
it('should getSelectedCount return 0 when the chosen nodes are reset', () => {
component.onCurrentSelection([{ entry: new Node({ id: 'fake', isFile: true }) }]);
component.resetChosenNode();
expect(component.getSelectedCount()).toBe(0);
});
});
}); });
}); });

View File

@@ -279,6 +279,10 @@ export class ContentNodeSelectorPanelComponent implements OnInit, OnDestroy {
return this._chosenNode; return this._chosenNode;
} }
getSelectedCount(): number {
return this.chosenNode?.length || 0;
}
ngOnInit() { ngOnInit() {
this.searchInput.valueChanges this.searchInput.valueChanges
.pipe( .pipe(

View File

@@ -35,7 +35,7 @@
<mat-tab *ngIf="canPerformLocalUpload()" <mat-tab *ngIf="canPerformLocalUpload()"
[disabled]="isNotAllowedToUpload()"> [disabled]="isNotAllowedToUpload()">
<adf-toolbar> <adf-toolbar>
<adf-toolbar-title> <adf-toolbar-title [adf-node-counter]="getSelectedCount()">
<adf-dropdown-breadcrumb <adf-dropdown-breadcrumb
class="adf-content-node-selector-content-breadcrumb" class="adf-content-node-selector-content-breadcrumb"
[folderNode]="breadcrumbFolderNode" [folderNode]="breadcrumbFolderNode"

View File

@@ -26,6 +26,10 @@
.adf-upload-dialog-container { .adf-upload-dialog-container {
height: 456px; height: 456px;
} }
.adf-toolbar-title {
place-items: center;
}
} }
.adf-content-node-selector-dialog { .adf-content-node-selector-dialog {

View File

@@ -435,4 +435,16 @@ describe('ContentNodeSelectorComponent', () => {
expect(emptyListTemplate).toBeNull(); expect(emptyListTemplate).toBeNull();
}); });
}); });
describe('Selected nodes counter', () => {
it('should getSelectedCount return 0 by default', () => {
expect(component.getSelectedCount()).toBe(0);
});
it('should getSelectedCount return 1 when a node is selected', () => {
component.onSelect([new Node({ id: 'fake' })]);
expect(component.getSelectedCount()).toBe(1);
});
});
}); });

View File

@@ -107,6 +107,10 @@ export class ContentNodeSelectorComponent implements OnInit {
return this.translation.instant(`NODE_SELECTOR.${action}_ITEM`, { name: this.translation.instant(name) }); return this.translation.instant(`NODE_SELECTOR.${action}_ITEM`, { name: this.translation.instant(name) });
} }
getSelectedCount(): number {
return this.chosenNode?.length || 0;
}
isMultipleSelection(): boolean { isMultipleSelection(): boolean {
return this.data.selectionMode === 'multiple'; return this.data.selectionMode === 'multiple';
} }

View File

@@ -30,6 +30,7 @@ import { DocumentListModule } from '../document-list/document-list.module';
import { NameLocationCellComponent } from './name-location-cell/name-location-cell.component'; import { NameLocationCellComponent } from './name-location-cell/name-location-cell.component';
import { UploadModule } from '../upload/upload.module'; import { UploadModule } from '../upload/upload.module';
import { SearchQueryBuilderService } from '../search/search-query-builder.service'; import { SearchQueryBuilderService } from '../search/search-query-builder.service';
import { ContentDirectiveModule } from '../directives/content-directive.module';
@NgModule({ @NgModule({
imports: [ imports: [
@@ -42,7 +43,8 @@ import { SearchQueryBuilderService } from '../search/search-query-builder.servic
BreadcrumbModule, BreadcrumbModule,
SearchModule, SearchModule,
DocumentListModule, DocumentListModule,
UploadModule UploadModule,
ContentDirectiveModule
], ],
exports: [ exports: [
ContentNodeSelectorPanelComponent, ContentNodeSelectorPanelComponent,

View File

@@ -18,19 +18,25 @@
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { MaterialModule } from '../material.module'; import { MaterialModule } from '../material.module';
import { TranslateModule } from '@ngx-translate/core';
import { NodeLockDirective } from './node-lock.directive'; import { NodeLockDirective } from './node-lock.directive';
import { NodeCounterComponent, NodeCounterDirective } from './node-counter.directive';
@NgModule({ @NgModule({
imports: [ imports: [
CommonModule, CommonModule,
MaterialModule MaterialModule,
TranslateModule
], ],
declarations: [ declarations: [
NodeLockDirective NodeLockDirective,
NodeCounterDirective,
NodeCounterComponent
], ],
exports: [ exports: [
NodeLockDirective NodeLockDirective,
NodeCounterDirective
] ]
}) })
export class ContentDirectiveModule { export class ContentDirectiveModule {

View File

@@ -0,0 +1,56 @@
/*!
* @license
* Copyright 2019 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 } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NodeCounterDirective, NodeCounterComponent } from './node-counter.directive';
import { By } from '@angular/platform-browser';
import { TranslateModule } from '@ngx-translate/core';
@Component({
template: `<div [adf-node-counter]="count"></div>`
})
class TestComponent {
count: number = 0;
}
describe('NodeCounterDirective', () => {
let fixture: ComponentFixture<TestComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot()
],
declarations: [
NodeCounterDirective,
NodeCounterComponent,
TestComponent
]
});
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
});
it('should display the counter component', () => {
fixture.whenStable().then(() => {
const counterElement = fixture.debugElement.query(By.css('adf-node-counter'));
expect(counterElement).not.toBeNull();
expect(counterElement.nativeElement.innerText).toBe('NODE_COUNTER.SELECTED_COUNT');
});
});
});

View File

@@ -0,0 +1,55 @@
/*!
* @license
* Copyright 2019 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 { Directive, Input, Component, OnInit, OnChanges, ComponentFactoryResolver, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[adf-node-counter]'
})
export class NodeCounterDirective implements OnInit, OnChanges {
@Input('adf-node-counter')
counter: number;
componentRef: NodeCounterComponent;
constructor(
private resolver: ComponentFactoryResolver,
public viewContainerRef: ViewContainerRef
) {}
ngOnInit() {
const componentFactory = this.resolver.resolveComponentFactory(NodeCounterComponent);
this.componentRef = this.viewContainerRef.createComponent(componentFactory).instance;
this.componentRef.counter = this.counter;
}
ngOnChanges() {
if (this.componentRef) {
this.componentRef.counter = this.counter;
}
}
}
@Component({
selector: 'adf-node-counter',
template: `
<div>{{ 'NODE_COUNTER.SELECTED_COUNT' | translate: { count: counter } }}</div>
`
})
export class NodeCounterComponent {
counter: number;
}

View File

@@ -474,5 +474,8 @@
"OVER-TABLE-MESSAGE" : "Select property aspects", "OVER-TABLE-MESSAGE" : "Select property aspects",
"SELECTED": "Selected" "SELECTED": "Selected"
} }
},
"NODE_COUNTER": {
"SELECTED_COUNT": "{{ count }} selected"
} }
} }