forked from carbon-design-system/carbon-components-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-toolbar.component.ts
123 lines (116 loc) · 3.86 KB
/
table-toolbar.component.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { TableModel } from "../table-model.class";
import {
Component,
EventEmitter,
Input,
Output
} from "@angular/core";
import { I18n, Overridable } from "carbon-components-angular/i18n";
/**
* The table toolbar is reserved for global table actions such as table settings, complex filter, export, or editing table data.
*
* ## Basic usage
*
* ```html
* <ibm-table-toolbar [model]="model">
* <ibm-table-toolbar-actions>
* <button ibmButton="primary">
* Delete
* <ibm-icon-delete size="16" class="bx--btn__icon"></ibm-icon-delete>
* </button>
* <button ibmButton="primary">
* Save
* <ibm-icon-save size="16" class="bx--btn__icon"></ibm-icon-save>
* </button>
* <button ibmButton="primary">
* Download
* <ibm-icon-download size="16" class="bx--btn__icon"></ibm-icon-download>
* </button>
* </ibm-table-toolbar-actions>
* <ibm-table-toolbar-content>
* <ibm-table-toolbar-search [expandable]="true"></ibm-table-toolbar-search>
* <button ibmButton="toolbar-action">
* <ibm-icon-settings size="16" class="bx--toolbar-action__icon"></ibm-icon-settings>
* </button>
* <button ibmButton="primary" size="sm">
* Primary Button
* <ibm-icon-add size="20" class="bx--btn__icon"></ibm-icon-add>
* </button>
* </ibm-table-toolbar-content>
* </ibm-table-toolbar>
* ```
*
*/
@Component({
selector: "ibm-table-toolbar",
template: `
<section class="bx--table-toolbar">
<div
*ngIf="model"
class="bx--batch-actions"
[ngClass]="{
'bx--batch-actions--active': selected
}"
[attr.aria-label]="actionBarLabel.subject | async">
<div class="bx--action-list">
<ng-content select="ibm-table-toolbar-actions"></ng-content>
<button
ibmButton="primary"
class="bx--batch-summary__cancel"
[tabindex]="selected ? 0 : -1"
(click)="onCancel()">
{{_cancelText.subject | async}}
</button>
</div>
<div class="bx--batch-summary">
<p class="bx--batch-summary__para" *ngIf="count as n">
<ng-container *ngIf="_batchTextLegacy.subject | async as legacyText; else batchTextBlock">
<span>{{n}}</span> {{legacyText}}
</ng-container>
<ng-template #batchTextBlock>
<span *ngIf="n === 1">{{_batchTextSingle.subject | async}}</span>
<span *ngIf="n !== 1">{{_batchTextMultiple.subject | i18nReplace: {count: n} | async}}</span>
</ng-template>
</p>
</div>
</div>
<ng-content></ng-content>
</section>
`
})
export class TableToolbar {
@Input() model: TableModel;
@Input() set batchText (value: string | { SINGLE: string, MULTIPLE: string }) {
if (typeof value === "object") {
this._batchTextSingle.override(value.SINGLE);
this._batchTextMultiple.override(value.MULTIPLE);
} else {
// For compatibility with old code
this._batchTextLegacy.override(value);
}
}
@Input() set ariaLabel (value: { ACTION_BAR: string }) {
this.actionBarLabel.override(value.ACTION_BAR);
}
@Input() set cancelText(value: { CANCEL: string }) {
this._cancelText.override(value.CANCEL);
}
get cancelText(): { CANCEL: string } {
return { CANCEL: this._cancelText.value as string };
}
actionBarLabel: Overridable = this.i18n.getOverridable("TABLE_TOOLBAR.ACTION_BAR");
_cancelText: Overridable = this.i18n.getOverridable("TABLE_TOOLBAR.CANCEL");
_batchTextLegacy: Overridable = this.i18n.getOverridable("TABLE_TOOLBAR.BATCH_TEXT");
_batchTextSingle: Overridable = this.i18n.getOverridable("TABLE_TOOLBAR.BATCH_TEXT_SINGLE");
_batchTextMultiple: Overridable = this.i18n.getOverridable("TABLE_TOOLBAR.BATCH_TEXT_MULTIPLE");
constructor(protected i18n: I18n) {}
get count() {
return this.model.totalDataLength > 0 ? this.model.rowsSelected.reduce((previous, current) => previous + (current ? 1 : 0), 0) : 0;
}
get selected() {
return this.model.totalDataLength > 0 ? this.model.rowsSelected.some(item => item) : false;
}
onCancel() {
this.model.selectAll(false);
}
}