import * as i0 from '@angular/core'; import { InjectionToken, EventEmitter, booleanAttribute, Directive, Optional, Inject, Input, Output, Injectable, SkipSelf, inject, ChangeDetectorRef, ElementRef, signal, Component, ViewEncapsulation, ChangeDetectionStrategy, NgModule } from '@angular/core'; import { FocusMonitor, AriaDescriber } from '@angular/cdk/a11y'; import { SPACE, ENTER } from '@angular/cdk/keycodes'; import { ReplaySubject, Subject, merge } from 'rxjs'; import { _CdkPrivateStyleLoader } from '@angular/cdk/private'; import { _ as _animationsDisabled } from './animation-DfMFjxHu.mjs'; import { _ as _StructuralStylesLoader } from './structural-styles-CObeNzjn.mjs'; import { M as MatCommonModule } from './common-module-cKSwHniA.mjs'; import '@angular/cdk/layout'; import '@angular/cdk/bidi'; /** @docs-private */ function getSortDuplicateSortableIdError(id) { return Error(`Cannot have two MatSortables with the same id (${id}).`); } /** @docs-private */ function getSortHeaderNotContainedWithinSortError() { return Error(`MatSortHeader must be placed within a parent element with the MatSort directive.`); } /** @docs-private */ function getSortHeaderMissingIdError() { return Error(`MatSortHeader must be provided with a unique id.`); } /** @docs-private */ function getSortInvalidDirectionError(direction) { return Error(`${direction} is not a valid sort direction ('asc' or 'desc').`); } /** Injection token to be used to override the default options for `mat-sort`. */ const MAT_SORT_DEFAULT_OPTIONS = new InjectionToken('MAT_SORT_DEFAULT_OPTIONS'); /** Container for MatSortables to manage the sort state and provide default sort parameters. */ class MatSort { _defaultOptions; _initializedStream = new ReplaySubject(1); /** Collection of all registered sortables that this directive manages. */ sortables = new Map(); /** Used to notify any child components listening to state changes. */ _stateChanges = new Subject(); /** The id of the most recently sorted MatSortable. */ active; /** * The direction to set when an MatSortable is initially sorted. * May be overridden by the MatSortable's sort start. */ start = 'asc'; /** The sort direction of the currently active MatSortable. */ get direction() { return this._direction; } set direction(direction) { if (direction && direction !== 'asc' && direction !== 'desc' && (typeof ngDevMode === 'undefined' || ngDevMode)) { throw getSortInvalidDirectionError(direction); } this._direction = direction; } _direction = ''; /** * Whether to disable the user from clearing the sort by finishing the sort direction cycle. * May be overridden by the MatSortable's disable clear input. */ disableClear; /** Whether the sortable is disabled. */ disabled = false; /** Event emitted when the user changes either the active sort or sort direction. */ sortChange = new EventEmitter(); /** Emits when the paginator is initialized. */ initialized = this._initializedStream; constructor(_defaultOptions) { this._defaultOptions = _defaultOptions; } /** * Register function to be used by the contained MatSortables. Adds the MatSortable to the * collection of MatSortables. */ register(sortable) { if (typeof ngDevMode === 'undefined' || ngDevMode) { if (!sortable.id) { throw getSortHeaderMissingIdError(); } if (this.sortables.has(sortable.id)) { throw getSortDuplicateSortableIdError(sortable.id); } } this.sortables.set(sortable.id, sortable); } /** * Unregister function to be used by the contained MatSortables. Removes the MatSortable from the * collection of contained MatSortables. */ deregister(sortable) { this.sortables.delete(sortable.id); } /** Sets the active sort id and determines the new sort direction. */ sort(sortable) { if (this.active != sortable.id) { this.active = sortable.id; this.direction = sortable.start ? sortable.start : this.start; } else { this.direction = this.getNextSortDirection(sortable); } this.sortChange.emit({ active: this.active, direction: this.direction }); } /** Returns the next sort direction of the active sortable, checking for potential overrides. */ getNextSortDirection(sortable) { if (!sortable) { return ''; } // Get the sort direction cycle with the potential sortable overrides. const disableClear = sortable?.disableClear ?? this.disableClear ?? !!this._defaultOptions?.disableClear; let sortDirectionCycle = getSortDirectionCycle(sortable.start || this.start, disableClear); // Get and return the next direction in the cycle let nextDirectionIndex = sortDirectionCycle.indexOf(this.direction) + 1; if (nextDirectionIndex >= sortDirectionCycle.length) { nextDirectionIndex = 0; } return sortDirectionCycle[nextDirectionIndex]; } ngOnInit() { this._initializedStream.next(); } ngOnChanges() { this._stateChanges.next(); } ngOnDestroy() { this._stateChanges.complete(); this._initializedStream.complete(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: MatSort, deps: [{ token: MAT_SORT_DEFAULT_OPTIONS, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "16.1.0", version: "20.0.0", type: MatSort, isStandalone: true, selector: "[matSort]", inputs: { active: ["matSortActive", "active"], start: ["matSortStart", "start"], direction: ["matSortDirection", "direction"], disableClear: ["matSortDisableClear", "disableClear", booleanAttribute], disabled: ["matSortDisabled", "disabled", booleanAttribute] }, outputs: { sortChange: "matSortChange" }, host: { classAttribute: "mat-sort" }, exportAs: ["matSort"], usesOnChanges: true, ngImport: i0 }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: MatSort, decorators: [{ type: Directive, args: [{ selector: '[matSort]', exportAs: 'matSort', host: { 'class': 'mat-sort', }, }] }], ctorParameters: () => [{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_SORT_DEFAULT_OPTIONS] }] }], propDecorators: { active: [{ type: Input, args: ['matSortActive'] }], start: [{ type: Input, args: ['matSortStart'] }], direction: [{ type: Input, args: ['matSortDirection'] }], disableClear: [{ type: Input, args: [{ alias: 'matSortDisableClear', transform: booleanAttribute }] }], disabled: [{ type: Input, args: [{ alias: 'matSortDisabled', transform: booleanAttribute }] }], sortChange: [{ type: Output, args: ['matSortChange'] }] } }); /** Returns the sort direction cycle to use given the provided parameters of order and clear. */ function getSortDirectionCycle(start, disableClear) { let sortOrder = ['asc', 'desc']; if (start == 'desc') { sortOrder.reverse(); } if (!disableClear) { sortOrder.push(''); } return sortOrder; } /** * To modify the labels and text displayed, create a new instance of MatSortHeaderIntl and * include it in a custom provider. */ class MatSortHeaderIntl { /** * Stream that emits whenever the labels here are changed. Use this to notify * components if the labels have changed after initialization. */ changes = new Subject(); static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: MatSortHeaderIntl, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: MatSortHeaderIntl, providedIn: 'root' }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: MatSortHeaderIntl, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); /** * @docs-private * @deprecated No longer used, will be removed. * @breaking-change 21.0.0 */ function MAT_SORT_HEADER_INTL_PROVIDER_FACTORY(parentIntl) { return parentIntl || new MatSortHeaderIntl(); } /** * @docs-private * @deprecated No longer used, will be removed. * @breaking-change 21.0.0 */ const MAT_SORT_HEADER_INTL_PROVIDER = { // If there is already an MatSortHeaderIntl available, use that. Otherwise, provide a new one. provide: MatSortHeaderIntl, deps: [[new Optional(), new SkipSelf(), MatSortHeaderIntl]], useFactory: MAT_SORT_HEADER_INTL_PROVIDER_FACTORY, }; /** * Applies sorting behavior (click to change sort) and styles to an element, including an * arrow to display the current sort direction. * * Must be provided with an id and contained within a parent MatSort directive. * * If used on header cells in a CdkTable, it will automatically default its id from its containing * column definition. */ class MatSortHeader { _intl = inject(MatSortHeaderIntl); _sort = inject(MatSort, { optional: true }); _columnDef = inject('MAT_SORT_HEADER_COLUMN_DEF', { optional: true, }); _changeDetectorRef = inject(ChangeDetectorRef); _focusMonitor = inject(FocusMonitor); _elementRef = inject(ElementRef); _ariaDescriber = inject(AriaDescriber, { optional: true }); _renderChanges; _animationsDisabled = _animationsDisabled(); /** * Indicates which state was just cleared from the sort header. * Will be reset on the next interaction. Used for coordinating animations. */ _recentlyCleared = signal(null); /** * The element with role="button" inside this component's view. We need this * in order to apply a description with AriaDescriber. */ _sortButton; /** * ID of this sort header. If used within the context of a CdkColumnDef, this will default to * the column's name. */ id; /** Sets the position of the arrow that displays when sorted. */ arrowPosition = 'after'; /** Overrides the sort start value of the containing MatSort for this MatSortable. */ start; /** whether the sort header is disabled. */ disabled = false; /** * Description applied to MatSortHeader's button element with aria-describedby. This text should * describe the action that will occur when the user clicks the sort header. */ get sortActionDescription() { return this._sortActionDescription; } set sortActionDescription(value) { this._updateSortActionDescription(value); } // Default the action description to "Sort" because it's better than nothing. // Without a description, the button's label comes from the sort header text content, // which doesn't give any indication that it performs a sorting operation. _sortActionDescription = 'Sort'; /** Overrides the disable clear value of the containing MatSort for this MatSortable. */ disableClear; constructor() { inject(_CdkPrivateStyleLoader).load(_StructuralStylesLoader); const defaultOptions = inject(MAT_SORT_DEFAULT_OPTIONS, { optional: true, }); // Note that we use a string token for the `_columnDef`, because the value is provided both by // `material/table` and `cdk/table` and we can't have the CDK depending on Material, // and we want to avoid having the sort header depending on the CDK table because // of this single reference. if (!this._sort && (typeof ngDevMode === 'undefined' || ngDevMode)) { throw getSortHeaderNotContainedWithinSortError(); } if (defaultOptions?.arrowPosition) { this.arrowPosition = defaultOptions?.arrowPosition; } } ngOnInit() { if (!this.id && this._columnDef) { this.id = this._columnDef.name; } this._sort.register(this); this._renderChanges = merge(this._sort._stateChanges, this._sort.sortChange).subscribe(() => this._changeDetectorRef.markForCheck()); this._sortButton = this._elementRef.nativeElement.querySelector('.mat-sort-header-container'); this._updateSortActionDescription(this._sortActionDescription); } ngAfterViewInit() { // We use the focus monitor because we also want to style // things differently based on the focus origin. this._focusMonitor .monitor(this._elementRef, true) .subscribe(() => this._recentlyCleared.set(null)); } ngOnDestroy() { this._focusMonitor.stopMonitoring(this._elementRef); this._sort.deregister(this); this._renderChanges?.unsubscribe(); if (this._sortButton) { this._ariaDescriber?.removeDescription(this._sortButton, this._sortActionDescription); } } /** Triggers the sort on this sort header and removes the indicator hint. */ _toggleOnInteraction() { if (!this._isDisabled()) { const wasSorted = this._isSorted(); const prevDirection = this._sort.direction; this._sort.sort(this); this._recentlyCleared.set(wasSorted && !this._isSorted() ? prevDirection : null); } } _handleKeydown(event) { if (event.keyCode === SPACE || event.keyCode === ENTER) { event.preventDefault(); this._toggleOnInteraction(); } } /** Whether this MatSortHeader is currently sorted in either ascending or descending order. */ _isSorted() { return (this._sort.active == this.id && (this._sort.direction === 'asc' || this._sort.direction === 'desc')); } _isDisabled() { return this._sort.disabled || this.disabled; } /** * Gets the aria-sort attribute that should be applied to this sort header. If this header * is not sorted, returns null so that the attribute is removed from the host element. Aria spec * says that the aria-sort property should only be present on one header at a time, so removing * ensures this is true. */ _getAriaSortAttribute() { if (!this._isSorted()) { return 'none'; } return this._sort.direction == 'asc' ? 'ascending' : 'descending'; } /** Whether the arrow inside the sort header should be rendered. */ _renderArrow() { return !this._isDisabled() || this._isSorted(); } _updateSortActionDescription(newDescription) { // We use AriaDescriber for the sort button instead of setting an `aria-label` because some // screen readers (notably VoiceOver) will read both the column header *and* the button's label // for every *cell* in the table, creating a lot of unnecessary noise. // If _sortButton is undefined, the component hasn't been initialized yet so there's // nothing to update in the DOM. if (this._sortButton) { // removeDescription will no-op if there is no existing message. // TODO(jelbourn): remove optional chaining when AriaDescriber is required. this._ariaDescriber?.removeDescription(this._sortButton, this._sortActionDescription); this._ariaDescriber?.describe(this._sortButton, newDescription); } this._sortActionDescription = newDescription; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: MatSortHeader, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.0.0", type: MatSortHeader, isStandalone: true, selector: "[mat-sort-header]", inputs: { id: ["mat-sort-header", "id"], arrowPosition: "arrowPosition", start: "start", disabled: ["disabled", "disabled", booleanAttribute], sortActionDescription: "sortActionDescription", disableClear: ["disableClear", "disableClear", booleanAttribute] }, host: { listeners: { "click": "_toggleOnInteraction()", "keydown": "_handleKeydown($event)", "mouseleave": "_recentlyCleared.set(null)" }, properties: { "attr.aria-sort": "_getAriaSortAttribute()", "class.mat-sort-header-disabled": "_isDisabled()" }, classAttribute: "mat-sort-header" }, exportAs: ["matSortHeader"], ngImport: i0, template: "\n