From ff58248eef638f459757e000c0df8b4e107bfe2f Mon Sep 17 00:00:00 2001 From: jojohoch Date: Mon, 16 Nov 2026 12:29:00 +0100 Subject: [PATCH] Let the header checkbox act on the shown or on all units (#281) With a search term set, the header checkbox selected hidden units as well and nothing showed them. A toggle sets its scope as in Studio's unit lists, and the count line names and deselects hidden selected units. The grey summary below the table repeated that count and is gone. --- .../codebook-export.component.html | 38 ++++++++--- .../codebook-export.component.scss | 44 ++++++++++-- .../codebook-export.component.ts | 59 +++++++++++++++- .../codebook-export.component.spec.ts | 68 ++++++++++++++++++- .../translations/de.json | 7 +- 5 files changed, 194 insertions(+), 22 deletions(-) diff --git a/projects/ngx-coding-components/codebook-export/codebook-export.component.html b/projects/ngx-coding-components/codebook-export/codebook-export.component.html index 4cf822e6..40f6b846 100644 --- a/projects/ngx-coding-components/codebook-export/codebook-export.component.html +++ b/projects/ngx-coding-components/codebook-export/codebook-export.component.html @@ -4,9 +4,25 @@

{{ 'codebook.workspace.export-coding-book' | translate }}
- @if (selection.length === 0) {

{{ 'codebook.workspace.select-unit' | translate }}

} - @if (selection.length === 1) {

{{ 'codebook.workspace.unit-selected' | translate }}

} - @if (selection.length > 1) {

{{ selection.length }} {{ 'codebook.workspace.units-selected' | translate }}

} + +

+ + @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 }} } + + {{ + 'codebook.coding.hidden-selected' | translate: { count: hiddenSelectedCount } + }} + · + +

{{ 'codebook.search-filter.filter-units' | translate }} @@ -17,13 +33,18 @@

{{ 'codebook.workspace.export-coding-book' | translate }} + + {{ 'codebook.coding.shown-only' | translate }} + - + {{ 'codebook.workspace.export-coding-book' | translate }} -
- {{ 'codebook.coding.selected-units' | translate: { count: selection.length } }} -

diff --git a/projects/ngx-coding-components/codebook-export/codebook-export.component.scss b/projects/ngx-coding-components/codebook-export/codebook-export.component.scss index 1150fea9..830c61e3 100644 --- a/projects/ngx-coding-components/codebook-export/codebook-export.component.scss +++ b/projects/ngx-coding-components/codebook-export/codebook-export.component.scss @@ -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; } diff --git a/projects/ngx-coding-components/codebook-export/codebook-export.component.ts b/projects/ngx-coding-components/codebook-export/codebook-export.component.ts index ec1394e6..5a9bb24a 100644 --- a/projects/ngx-coding-components/codebook-export/codebook-export.component.ts +++ b/projects/ngx-coding-components/codebook-export/codebook-export.component.ts @@ -10,6 +10,7 @@ import { MatButtonModule } from '@angular/material/button'; 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'; @@ -36,7 +37,7 @@ export const DEFAULT_CODEBOOK_OPTIONS: CodeBookContentSetting = { 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'] @@ -60,6 +61,9 @@ export class CodebookExportComponent implements OnChanges, AfterViewInit { 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; @@ -111,6 +115,42 @@ export class CodebookExportComponent implements OnChanges, AfterViewInit { 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); @@ -126,9 +166,24 @@ export class CodebookExportComponent implements OnChanges, AfterViewInit { 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]); } diff --git a/projects/ngx-coding-components/src/lib/codebook-export/codebook-export.component.spec.ts b/projects/ngx-coding-components/src/lib/codebook-export/codebook-export.component.spec.ts index 2a497569..4af2fcef 100644 --- a/projects/ngx-coding-components/src/lib/codebook-export/codebook-export.component.spec.ts +++ b/projects/ngx-coding-components/src/lib/codebook-export/codebook-export.component.spec.ts @@ -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(); @@ -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(); }); @@ -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(); + }); }); diff --git a/projects/ngx-coding-components/translations/de.json b/projects/ngx-coding-components/translations/de.json index 34e52a43..1214d1af 100644 --- a/projects/ngx-coding-components/translations/de.json +++ b/projects/ngx-coding-components/translations/de.json @@ -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.", @@ -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.",