ipsos-components
Version:
Material Design components for Angular
172 lines (151 loc) • 5.85 kB
text/typescript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Input,
Optional,
ViewEncapsulation
} from '@angular/core';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {
trigger,
state,
style,
animate,
transition,
keyframes,
} from '@angular/animations';
import {CdkColumnDef} from '@angular/cdk/table';
import {Subscription} from 'rxjs/Subscription';
import {merge} from 'rxjs/observable/merge';
import {MatSort, MatSortable} from './sort';
import {MatSortHeaderIntl} from './sort-header-intl';
import {getSortHeaderNotContainedWithinSortError} from './sort-errors';
import {AnimationCurves, AnimationDurations} from '@angular/material/core';
import {CanDisable, mixinDisabled} from '@angular/material/core';
const SORT_ANIMATION_TRANSITION =
AnimationDurations.ENTERING + ' ' + AnimationCurves.STANDARD_CURVE;
// Boilerplate for applying mixins to the sort header.
/** @docs-private */
export class MatSortHeaderBase {}
export const _MatSortHeaderMixinBase = mixinDisabled(MatSortHeaderBase);
/**
* 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.
*/
export class MatSortHeader extends _MatSortHeaderMixinBase implements MatSortable, CanDisable {
private _rerenderSubscription: Subscription;
/**
* ID of this sort header. If used within the context of a CdkColumnDef, this will default to
* the column's name.
*/
id: string;
/** Sets the position of the arrow that displays when sorted. */
arrowPosition: 'before' | 'after' = 'after';
/** Overrides the sort start value of the containing MatSort for this MatSortable. */
start: 'asc' | 'desc';
/** Overrides the disable clear value of the containing MatSort for this MatSortable. */
get disableClear(): boolean { return this._disableClear; }
set disableClear(v) { this._disableClear = coerceBooleanProperty(v); }
private _disableClear: boolean;
constructor(public _intl: MatSortHeaderIntl,
changeDetectorRef: ChangeDetectorRef,
public _sort: MatSort,
public _cdkColumnDef: CdkColumnDef) {
super();
if (!_sort) {
throw getSortHeaderNotContainedWithinSortError();
}
this._rerenderSubscription = merge(_sort.sortChange, _sort._stateChanges, _intl.changes)
.subscribe(() => changeDetectorRef.markForCheck());
}
ngOnInit() {
if (!this.id && this._cdkColumnDef) {
this.id = this._cdkColumnDef.name;
}
this._sort.register(this);
}
ngOnDestroy() {
this._sort.deregister(this);
this._rerenderSubscription.unsubscribe();
}
/** Handles click events on the header. */
_handleClick() {
if (!this._isDisabled()) {
this._sort.sort(this);
}
}
/** 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;
}
}