Skip to content

Commit

Permalink
[ACA-4464] Add user/groups - should not be able to select the same us…
Browse files Browse the repository at this point in the history
…er multiple times (#7066)
  • Loading branch information
dhrn authored Jun 3, 2021
1 parent 95d680b commit 91d813e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@
class="adf-permission-result-list"
[class.adf-permission-result-list-search]="searchedWord.length === 0">
<ng-template let-data>
<mat-selection-list class="adf-permission-result-list-elements" (keydown.control.a)="selectAll( data?.list?.entries)">
<mat-selection-list class="adf-permission-result-list-elements" (keydown.control.a)="onSelectionChange()"
(selectionChange)="onSelectionChange()">
<mat-list-option id="adf-add-permission-group-everyone"
class="adf-list-option-item"
#eveyone
(click)="elementClicked(EVERYONE)">
disableRipple
[value]="EVERYONE">
<adf-user-icon-column [node]="EVERYONE" id="add-group-icon" [selected]="eveyone.selected"></adf-user-icon-column>
<p class="adf-result-name">
{{'PERMISSION_MANAGER.ADD-PERMISSION.EVERYONE' | translate}}
</p>
</mat-list-option>

<mat-list-option *ngFor="let item of data?.list?.entries; let idx = index"
(click)="elementClicked(item)"
disableRipple
[value]="item"
class="adf-list-option-item"
id="result_option_{{idx}}"
#option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,41 @@ describe('AddPermissionPanelComponent', () => {
expect(element.querySelector('#result_option_0 .mat-list-text').innerHTML).not.toEqual(element.querySelector('#result_option_1 .mat-list-text').innerHTML);
});
}));

it('should emit unique element in between multiple search', async(() => {
searchApiService = fixture.componentRef.injector.get(SearchService);
spyOn(searchApiService, 'search').and.returnValue(of(fakeAuthorityListResult));
let searchAttempt = 0;

component.select.subscribe((items) => {
searchAttempt++;
expect(items.length).toBe(1);
expect(items[0].entry.id).toBeDefined();
expect(items[0].entry.id).not.toBeNull();
expect(items[0].entry.id).toBe(fakeAuthorityListResult.list.entries[0].entry.id);
});

typeWordIntoSearchInput('a');
fixture.whenStable().then(() => {
fixture.detectChanges();
let listElement: DebugElement = fixture.debugElement.query(By.css('#result_option_0'));
expect(listElement).not.toBeNull();
listElement.triggerEventHandler('click', {});

const clearButton = fixture.debugElement.query(By.css('#adf-permission-clear-input'));
expect(clearButton).not.toBeNull();
clearButton.triggerEventHandler('click', {});
fixture.detectChanges();

typeWordIntoSearchInput('abc');
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
listElement = fixture.debugElement.query(By.css('#result_option_0'));
expect(listElement).not.toBeNull();
listElement.triggerEventHandler('click', {});
expect(searchAttempt).toBe(2);
});
});
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* limitations under the License.
*/

import { SearchService, SearchConfigurationService } from '@alfresco/adf-core';
import { SearchConfigurationService, SearchService } from '@alfresco/adf-core';
import { NodeEntry } from '@alfresco/js-api';
import { Component, ViewEncapsulation, EventEmitter, Output, ViewChild } from '@angular/core';
import { Component, EventEmitter, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { FormControl } from '@angular/forms';
import { debounceTime } from 'rxjs/operators';
import { SearchPermissionConfigurationService } from './search-config-permission.service';
import { SearchComponent } from '../../../search/components/search.component';
import { MatSelectionList } from '@angular/material/list';

@Component({
selector: 'adf-add-permission-panel',
Expand All @@ -37,9 +38,12 @@ export class AddPermissionPanelComponent {
@ViewChild('search', { static: true })
search: SearchComponent;

@ViewChild(MatSelectionList, { static: false })
matSelectionList: MatSelectionList;

/** Emitted when a permission list item is selected. */
@Output()
select: EventEmitter<any> = new EventEmitter();
select: EventEmitter<NodeEntry[]> = new EventEmitter();

searchInput: FormControl = new FormControl();
searchedWord = '';
Expand All @@ -55,31 +59,27 @@ export class AddPermissionPanelComponent {
debounceTime(this.debounceSearch)
)
.subscribe((searchValue) => {
const selectionOptions = this.matSelectionList.selectedOptions.selected.map(option => option.value);
this.selectedItems.push(...selectionOptions);
this.matSelectionList.deselectAll();
this.searchedWord = searchValue;
if (!searchValue) {
this.search.resetResults();
}
});
}

elementClicked(item: NodeEntry) {
if (this.isAlreadySelected(item)) {
this.selectedItems.splice(this.selectedItems.indexOf(item), 1);
} else {
this.selectedItems.push(item);
}
this.select.emit(this.selectedItems);
}

selectAll(items: NodeEntry[]) {
if (items?.length > 0) {
this.selectedItems = items;
this.select.emit(this.selectedItems);
}
}

private isAlreadySelected(item: NodeEntry): boolean {
return this.selectedItems.indexOf(item) >= 0;
onSelectionChange() {
const currentSelection = this.matSelectionList.selectedOptions.selected.map(option => option.value);
const uniqueSelection = [ ...currentSelection, ...this.selectedItems ]
.reduce((uniquesElements, currentElement) => {
const isExist = uniquesElements.find(uniqueElement => uniqueElement.entry.id === currentElement.entry.id);
if (!isExist) {
uniquesElements.push(currentElement);
}
return uniquesElements;
}, []);
this.select.emit(uniqueSelection);
}

clearSearch() {
Expand Down

0 comments on commit 91d813e

Please sign in to comment.