-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathsort-header.ts
101 lines (88 loc) · 3.09 KB
/
sort-header.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @license
* Copyright Google Inc. 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 {CdkColumnDef} from '@angular/cdk/table';
import {Subscription} from 'rxjs/Subscription';
import {merge} from 'rxjs/observable/merge';
import {MdSort, MdSortable} from './sort';
import {MdSortHeaderIntl} from './sort-header-intl';
import {getMdSortHeaderNotContainedWithinMdSortError} from './sort-errors';
/**
* 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 MdSort directive.
*
* If used on header cells in a CdkTable, it will automatically default its id from its containing
* column definition.
*/
@Component({
moduleId: module.id,
selector: '[md-sort-header], [mat-sort-header]',
templateUrl: 'sort-header.html',
styleUrls: ['sort-header.css'],
host: {
'(click)': '_sort.sort(this)',
'[class.mat-sort-header-sorted]': '_isSorted()',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdSortHeader implements MdSortable {
private _rerenderSubscription: Subscription;
/**
* ID of this sort header. If used within the context of a CdkColumnDef, this will default to
* the column's name.
*/
@Input('md-sort-header') id: string;
/** Sets the position of the arrow that displays when sorted. */
@Input() arrowPosition: 'before' | 'after' = 'after';
/** Overrides the sort start value of the containing MdSort for this MdSortable. */
@Input('start') start: 'asc' | 'desc';
/** Overrides the disable clear value of the containing MdSort for this MdSortable. */
@Input()
get disableClear() { return this._disableClear; }
set disableClear(v) { this._disableClear = coerceBooleanProperty(v); }
private _disableClear: boolean;
@Input('mat-sort-header')
get _id() { return this.id; }
set _id(v: string) { this.id = v; }
constructor(public _intl: MdSortHeaderIntl,
changeDetectorRef: ChangeDetectorRef,
@Optional() public _sort: MdSort,
@Optional() public _cdkColumnDef: CdkColumnDef) {
if (!_sort) {
throw getMdSortHeaderNotContainedWithinMdSortError();
}
this._rerenderSubscription = merge(_sort.mdSortChange, _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();
}
/** Whether this MdSortHeader is currently sorted in either ascending or descending order. */
_isSorted() {
return this._sort.active == this.id && this._sort.direction;
}
}