Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@ <h2 mat-dialog-title>{{ 'codebook.workspace.export-coding-book' | translate }}</
<div class="fx-row-space-between-stretch">
<div class="fx-column-start-stretch">
<div class="selected-units-hint">
@if (selection.length === 0) { <p>{{ 'codebook.workspace.select-unit' | translate }}</p> }
@if (selection.length === 1) { <p>{{ 'codebook.workspace.unit-selected' | translate }}</p> }
@if (selection.length > 1) { <p>{{ selection.length }} {{ 'codebook.workspace.units-selected' | translate }}</p> }
<!-- A flex row, so that the comma of the hidden count follows the count without a space. The
hidden part is always there and invisible while nothing is hidden, so the line keeps its height. -->
<p class="selection-count">
<span>
@if (selection.length === 0) { {{ 'codebook.workspace.select-unit' | translate }} }
@else if (selection.length === 1) { {{ 'codebook.workspace.unit-selected' | translate }} }
@else { {{ selection.length }} {{ 'codebook.workspace.units-selected' | translate }} }
</span>
<span class="hidden-selection" [class.hidden-part-placeholder]="!hiddenSelectedCount"
data-cy="selection-scope-hidden">{{
'codebook.coding.hidden-selected' | translate: { count: hiddenSelectedCount }
}}</span>
<span class="separator" [class.hidden-part-placeholder]="!hiddenSelectedCount">&middot;</span>
<button type="button" class="deselect-hidden-button" data-cy="selection-scope-deselect-hidden"
[class.hidden-part-placeholder]="!hiddenSelectedCount"
[disabled]="loading || busy || !hiddenSelectedCount" (click)="deselectHidden()">
{{ 'codebook.coding.deselect-hidden' | translate }}
</button>
</p>
</div>
<mat-form-field>
<mat-label>{{ 'codebook.search-filter.filter-units' | translate }}</mat-label>
Expand All @@ -17,13 +33,18 @@ <h2 mat-dialog-title>{{ 'codebook.workspace.export-coding-book' | translate }}</
[matTooltip]="'codebook.search-filter.delete-filter' | translate"
(click)="filter = ''; applyFilter()"><span aria-hidden="true">×</span></button>
</mat-form-field>
<mat-slide-toggle class="selection-scope" data-cy="selection-scope-shown-only" [(ngModel)]="shownOnly">
{{ 'codebook.coding.shown-only' | translate }}
</mat-slide-toggle>
<mat-table [dataSource]="dataSource" matSort matSortActive="key" matSortDirection="asc" matSortDisableClear>
<ng-container matColumnDef="select">
<mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="toggleAll()" [disabled]="loading || busy"
[checked]="allSelected" [indeterminate]="selection.length > 0 && !allSelected"
[aria-label]="'codebook.coding.select-all-units' | translate"
[matTooltip]="filter ? ('codebook.coding.select-all-including-hidden' | translate) : ''"></mat-checkbox>
<mat-checkbox (change)="toggleAll()" [disabled]="loading || busy || headerEmpty"
data-cy="selection-scope-toggle-all"
[checked]="headerChecked" [indeterminate]="headerIndeterminate"
[aria-label]="(shownOnly ? 'codebook.coding.toggle-shown' : 'codebook.coding.toggle-all') | translate"
[matTooltip]="(shownOnly ? 'codebook.coding.toggle-shown' : 'codebook.coding.toggle-all') | translate"
matTooltipPosition="above"></mat-checkbox>
</mat-header-cell>
<mat-cell *matCellDef="let unit">
<mat-checkbox [checked]="selection.includes(unit.unitId)" [disabled]="unit.disabled || loading || busy"
Expand All @@ -45,9 +66,6 @@ <h2 mat-dialog-title>{{ 'codebook.workspace.export-coding-book' | translate }}</
<mat-header-row *matHeaderRowDef="columns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let unit; columns: columns"></mat-row>
</mat-table>
<div class="selection-summary">
{{ 'codebook.coding.selected-units' | translate: { count: selection.length } }}
</div>
</div>
<div class="settings">
<section class="example-section fx-column-start-start">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,44 @@ mat-select {
}
}

.selection-count {
display: flex;
flex-wrap: wrap;
align-items: baseline;
}

.hidden-selection {
color: #b71c1c;
}

.separator {
margin: 0 6px;
}

.deselect-hidden-button {
padding: 0;
border: none;
background: none;
font: inherit;
color: #006064;
text-decoration: underline;
cursor: pointer;
}

.deselect-hidden-button:disabled {
color: gray;
text-decoration: none;
cursor: default;
}

.selection-scope {
margin: 4px 0;
}

.selection-summary {
margin-top: 12px;
padding: 8px;
background-color: #f5f5f5;
border-radius: 4px;
font-size: 0.9em;
color: #555;
// The hidden part is always laid out and only shown while selected units are hidden: the count line
// sits above the search field, and its wrapping must not move the field while typing.
.hidden-part-placeholder {
visibility: hidden;
}

:host { display: block; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatSelectModule } from '@angular/material/select';
import { MatRadioModule } from '@angular/material/radio';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { TranslateModule } from '@ngx-translate/core';
Expand All @@ -36,7 +37,7 @@
selector: 'ngx-codebook-export',
standalone: true,
imports: [FormsModule, MatTableModule, MatSortModule, MatCheckboxModule, MatDialogModule,
MatButtonModule, MatTooltipModule, MatSelectModule, MatRadioModule, MatFormFieldModule,
MatButtonModule, MatTooltipModule, MatSelectModule, MatRadioModule, MatSlideToggleModule, MatFormFieldModule,
MatInputModule, TranslateModule],
templateUrl: './codebook-export.component.html',
styleUrls: ['./codebook-export.component.scss']
Expand All @@ -60,6 +61,9 @@
columns = ['select', 'key', 'name', 'group'];
selection: number[] = [];
filter = '';
// Whether the header checkbox acts on the units the search shows or on all of them. Filtering
// itself never changes the selection; units it hides stay selected and are counted as hidden.
shownOnly = false;
contentOptions = { ...DEFAULT_CODEBOOK_OPTIONS };
private unitsLoaded = false;
private profilesLoaded = false;
Expand All @@ -77,9 +81,9 @@
if (changes['availableUnits']) {
this.unitsLoaded ||= this.availableUnits.length > 0 || !changes['availableUnits'].firstChange;
this.dataSource.data = this.availableUnits;
this.dataSource.filterPredicate = (unit, filter) => [unit.key, unit.unitName, this.showGroupColumn ? unit.groupName || '' : '']

Check warning on line 84 in projects/ngx-coding-components/codebook-export/codebook-export.component.ts

View workflow job for this annotation

GitHub Actions / lint-and-test

This line has a length of 133. Maximum allowed is 120

Check warning on line 84 in projects/ngx-coding-components/codebook-export/codebook-export.component.ts

View workflow job for this annotation

GitHub Actions / lint-and-test

This line has a length of 133. Maximum allowed is 120
.some(value => value.toLowerCase().includes(filter));
this.dataSource.sortingDataAccessor = (unit, column) => ({ key: unit.key, name: unit.unitName, group: unit.groupName || '' }[column] || '');

Check warning on line 86 in projects/ngx-coding-components/codebook-export/codebook-export.component.ts

View workflow job for this annotation

GitHub Actions / lint-and-test

This line has a length of 146. Maximum allowed is 120

Check warning on line 86 in projects/ngx-coding-components/codebook-export/codebook-export.component.ts

View workflow job for this annotation

GitHub Actions / lint-and-test

This line has a length of 146. Maximum allowed is 120
}
if (changes['missingsProfiles']) {
this.profilesLoaded ||= this.missingsProfiles.length > 0 || !changes['missingsProfiles'].firstChange;
Expand Down Expand Up @@ -111,6 +115,42 @@
return selectable.length > 0 && selectable.every(unit => this.selection.includes(unit.unitId));
}

/** The units the header checkbox acts on, disabled ones left out. */
private get unitsInScope(): UnitSelectionItem[] {
return (this.shownOnly ? this.dataSource.filteredData : this.availableUnits).filter(unit => !unit.disabled);
}

// Read by the template on every change detection, so a set instead of includes() per unit
private get selectedInScope(): { inScope: number; selected: number } {
const selection = new Set(this.selection);
const units = this.unitsInScope;
return { inScope: units.length, selected: units.filter(unit => selection.has(unit.unitId)).length };
}

get headerChecked(): boolean {
const { inScope, selected } = this.selectedInScope;
return inScope > 0 && selected === inScope;
}

get headerIndeterminate(): boolean {
const { inScope, selected } = this.selectedInScope;
return selected > 0 && selected < inScope;
}

// Nothing in scope: a click on the header checkbox would tick it without selecting anything.
get headerEmpty(): boolean {
return !this.unitsInScope.length;
}

private get hiddenSelection(): number[] {
const shown = new Set(this.dataSource.filteredData.map(unit => unit.unitId));
return this.selection.filter(id => !shown.has(id));
}

get hiddenSelectedCount(): number {
return this.hiddenSelection.length;
}

get exportDisabled(): boolean {
const profilePending = this.selectedMissingsProfileId !== 0 &&
!this.missingsProfiles.some(profile => profile.id === this.selectedMissingsProfileId);
Expand All @@ -122,13 +162,28 @@

toggle(unit: UnitSelectionItem): void {
if (unit.disabled || this.loading || this.busy) return;
this.selection = this.selection.includes(unit.unitId) ? this.selection.filter(id => id !== unit.unitId) : [...this.selection, unit.unitId];

Check warning on line 165 in projects/ngx-coding-components/codebook-export/codebook-export.component.ts

View workflow job for this annotation

GitHub Actions / lint-and-test

This line has a length of 143. Maximum allowed is 120

Check warning on line 165 in projects/ngx-coding-components/codebook-export/codebook-export.component.ts

View workflow job for this annotation

GitHub Actions / lint-and-test

This line has a length of 143. Maximum allowed is 120
this.selectionChanged.emit([...this.selection]);
}

/**
* Deselects the units in scope when all of them are selected and selects all of them otherwise,
* a partly ticked box as well. Units out of scope stay as they are.
*/
toggleAll(): void {
if (this.loading || this.busy) return;
this.selection = this.allSelected ? [] : this.availableUnits.filter(unit => !unit.disabled).map(unit => unit.unitId);
const ids = new Set(this.unitsInScope.map(unit => unit.unitId));
const selected = new Set(this.selection);
this.selection = this.headerChecked ?
this.selection.filter(id => !ids.has(id)) :
[...this.selection, ...[...ids].filter(id => !selected.has(id))];
this.selectionChanged.emit([...this.selection]);
}

deselectHidden(): void {
if (this.loading || this.busy) return;
const hidden = new Set(this.hiddenSelection);
this.selection = this.selection.filter(id => !hidden.has(id));
this.selectionChanged.emit([...this.selection]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,40 @@ describe('Studio codebook selection contract', () => {
component.toggleAll(); expect(component.selection).toEqual([]);
});

// The header checkbox acts on the units shown only when switched so, as in Studio's lists (#281)
it('selects and deselects only the shown units with shownOnly, keeping hidden selected ones', () => {
component.toggle(component.availableUnits[1]);
component.shownOnly = true;
component.filter = 'Alpha'; component.applyFilter();
component.toggleAll();
expect(component.selection).toEqual([2, 1]);
expect(component.headerChecked).toBeTrue();
component.toggleAll();
expect(component.selection).toEqual([2]);
});

it('leaves disabled units out of the shown ones as well', () => {
component.shownOnly = true; component.toggleAll();
expect(component.selection).toEqual([1, 2]);
});

it('counts the selected units the search hides and deselects them', () => {
component.toggle(component.availableUnits[1]); component.toggle(component.availableUnits[0]);
component.filter = 'Alpha'; component.applyFilter();
expect(component.hiddenSelectedCount).toBe(1);
const emit = spyOn(component.selectionChanged, 'emit');
component.deselectHidden();
expect(component.selection).toEqual([1]);
expect(emit).toHaveBeenCalledOnceWith([1]);
});

it('has nothing in scope when the search shows no unit', () => {
component.shownOnly = true; component.filter = 'nothing'; component.applyFilter();
expect(component.headerEmpty).toBeTrue();
component.toggleAll();
expect(component.selection).toEqual([]);
});

it('searches names, keys and groups without changing selection', () => {
component.toggle(component.availableUnits[1]);
component.filter = 'gruppe'; component.applyFilter();
Expand Down Expand Up @@ -242,7 +276,7 @@ describe('codebook checkbox accessibility', () => {
fixture.componentRef.setInput('availableUnits', [{ unitId: 1, key: 'TASK', unitName: 'Aufgabe' }]);
fixture.detectChanges();
const inputs = fixture.nativeElement.querySelectorAll('input[type="checkbox"]');
expect(inputs[0].getAttribute('aria-label')).toBe('codebook.coding.select-all-units');
expect(inputs[0].getAttribute('aria-label')).toBe('codebook.coding.toggle-all');
expect(inputs[1].getAttribute('aria-label')).toBe('TASK');
fixture.destroy();
});
Expand Down Expand Up @@ -293,4 +327,36 @@ describe('codebook translations', () => {
expect(text).not.toMatch(/codebook\.[\w.-]+/);
fixture.destroy();
});

it('names the hidden selected units and the scope of the header checkbox', async () => {
await TestBed.configureTestingModule({
imports: [CodebookExportComponent, TranslateModule.forRoot({
loader: { provide: TranslateLoader, useClass: NgxCodingComponentsTranslateLoader }
})]
}).compileComponents();
await firstValueFrom(TestBed.inject(TranslateService).use('de'));
const fixture = TestBed.createComponent(CodebookExportComponent);
fixture.componentRef.setInput('availableUnits', [
{ unitId: 1, key: 'A', unitName: 'Aufgabe A' },
{ unitId: 2, key: 'B', unitName: 'Aufgabe B' }
]);
fixture.componentRef.setInput('selectedUnitIds', [2]);
await fixture.whenStable();
fixture.componentInstance.filter = 'Aufgabe A'; fixture.componentInstance.applyFilter();
fixture.changeDetectorRef.markForCheck();
await fixture.whenStable();
const element = fixture.nativeElement as HTMLElement;
const header = () => element.querySelector('[data-cy="selection-scope-toggle-all"] input') as HTMLInputElement;
expect(element.querySelector('.selected-units-hint')?.textContent).toContain('Eine Aufgabe ausgewählt');
const text = (selector: string) => element.querySelector(selector)?.textContent;
expect(text('[data-cy="selection-scope-hidden"]')).toContain(', davon 1 ausgeblendet');
expect(text('[data-cy="selection-scope-deselect-hidden"]')).toContain('ausgeblendete abwählen');
expect(element.querySelector('[data-cy="selection-scope-shown-only"]')?.textContent)
.toContain('Häkchen in der Kopfzeile wirkt nur auf angezeigte Einträge');
expect(header().getAttribute('aria-label')).toBe('Alle Einträge aus- oder abwählen, auch ausgeblendete');
(element.querySelector('[data-cy="selection-scope-shown-only"] button') as HTMLButtonElement).click();
await fixture.whenStable();
expect(header().getAttribute('aria-label')).toBe('Alle angezeigten Einträge aus- oder abwählen');
fixture.destroy();
});
});
7 changes: 5 additions & 2 deletions projects/ngx-coding-components/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,11 @@
"training-requirement-required": "Nur mit erhöhtem Schulungsbedarf",
"training-requirement-not-required": "Nur ohne erhöhten Schulungsbedarf",
"has-only-manual-coding": "Manuell kodierte Variablen",
"select-all-including-hidden": "Alle verfügbaren Aufgaben auswählen, auch ausgeblendete Suchtreffer.",
"shown-only": "Häkchen in der Kopfzeile wirkt nur auf angezeigte Einträge",
"toggle-shown": "Alle angezeigten Einträge aus- oder abwählen",
"toggle-all": "Alle Einträge aus- oder abwählen, auch ausgeblendete",
"hidden-selected": ", davon {{count}} ausgeblendet",
"deselect-hidden": "ausgeblendete abwählen",
"has-closed-vars": "Geschlossen kodierte Variablen",
"has-derived-vars": "Abgeleitete Variablen",
"has-closed-vars-tooltip": "Variablen mit automatischer oder residualer Kodierung einschließen.",
Expand All @@ -529,7 +533,6 @@
"show-score": "Bewertung anzeigen",
"codebook-export-error": "Das Codebook konnte nicht geladen oder exportiert werden. Bitte erneut versuchen.",
"code-label-to-upper": "Code-Label in Großbuchstaben",
"select-all-units": "Alle Aufgaben auswählen",
"codebook-content": "Variablen-Auswahl",
"has-only-vars-with-codes-tooltip": "Nur Variablen, die mindestens einen Code besitzen, werden in das Codebook aufgenommen.",
"hide-item-var-relation-tooltip": "Die Zuordnung zwischen Items und Variablen im Dokument ausblenden.",
Expand Down
Loading