forked from ng-bootstrap/ng-bootstrap
/
nav.ts
286 lines (244 loc) · 7.85 KB
/
nav.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import {
AfterContentChecked,
AfterContentInit,
Attribute,
ChangeDetectorRef,
ContentChildren,
Directive,
ElementRef,
EventEmitter,
forwardRef,
Inject,
Input,
OnInit,
Output,
QueryList,
TemplateRef
} from '@angular/core';
import {isDefined} from '../util/util';
import {NgbNavConfig} from './nav-config';
const isValidNavId = (id: any) => isDefined(id) && id !== '';
let navCounter = 0;
/**
* Context passed to the nav content template.
*
* See [this demo](#/components/nav/examples#keep-content) as the example.
*
* @since 5.2.0
*/
export interface NgbNavContentContext {
/**
* If `true`, current nav content is visible and active
*/
$implicit: boolean;
}
/**
* This directive must be used to wrap content to be displayed in the nav.
*
* @since 5.2.0
*/
@Directive({selector: 'ng-template[ngbNavContent]'})
export class NgbNavContent {
constructor(public templateRef: TemplateRef<any>) {}
}
/**
* The directive used to group nav link and related nav content. As well as set nav identifier and some options.
*
* @since 5.2.0
*/
@Directive({selector: '[ngbNavItem]', exportAs: 'ngbNavItem', host: {'[class.nav-item]': 'true'}})
export class NgbNavItem implements AfterContentChecked, OnInit {
private _nav: NgbNav;
/**
* If `true`, non-active current nav item content will be removed from DOM
* Otherwise it will just be hidden
*/
@Input() destroyOnHide;
/**
* If `true`, the current nav item is disabled and can't be toggled by user.
*
* Nevertheless disabled nav can be selected programmatically via the `.select()` method and the `[activeId]` binding.
*/
@Input() disabled = false;
/**
* The id used for the DOM elements.
* Must be unique inside the document in case you have multiple `ngbNav`s on the page.
*
* Autogenerated as `ngb-nav-XXX` if not provided.
*/
@Input() domId: string;
/**
* The id used as a model for active nav.
* It can be anything, but must be unique inside one `ngbNav`.
*
* The only limitation is that it is not possible to have the `''` (empty string) as id,
* because ` ngbNavItem `, `ngbNavItem=''` and `[ngbNavItem]="''"` are indistinguishable
*/
@Input('ngbNavItem') _id: any;
contentTpl: NgbNavContent | null;
@ContentChildren(NgbNavContent, {descendants: false}) contentTpls: QueryList<NgbNavContent>;
constructor(@Inject(forwardRef(() => NgbNav)) nav, public elementRef: ElementRef<any>) {
// TODO: cf https://github.com/angular/angular/issues/30106
this._nav = nav;
}
ngAfterContentChecked() {
// We are using @ContentChildren instead of @ContentChild as in the Angular version being used
// only @ContentChildren allows us to specify the {descendants: false} option.
// Without {descendants: false} we are hitting bugs described in:
// https://github.com/ng-bootstrap/ng-bootstrap/issues/2240
this.contentTpl = this.contentTpls.first;
}
ngOnInit() {
if (!isDefined(this.domId)) {
this.domId = `ngb-nav-${navCounter++}`;
}
}
get active() { return this._nav.activeId === this.id; }
get id() { return isValidNavId(this._id) ? this._id : this.domId; }
get panelDomId() { return `${this.domId}-panel`; }
isPanelInDom() {
return (isDefined(this.destroyOnHide) ? !this.destroyOnHide : !this._nav.destroyOnHide) || this.active;
}
}
/**
* A nav directive that helps with implementing tabbed navigation components.
*
* @since 5.2.0
*/
@Directive({
selector: '[ngbNav]',
exportAs: 'ngbNav',
host: {
'[class.nav]': 'true',
'[class.flex-column]': `orientation === 'vertical'`,
'[attr.aria-orientation]': `orientation === 'vertical' && roles === 'tablist' ? 'vertical' : undefined`,
'[attr.role]': `role ? role : roles ? 'tablist' : undefined`,
}
})
export class NgbNav implements AfterContentInit {
/**
* The id of the nav that should be active
*
* You could also use the `.select()` method and the `(navChange)` event
*/
@Input() activeId: any;
/**
* The event emitted after the active nav changes
* The payload of the event is the newly active nav id
*
* If you want to prevent nav change, you should use `(navChange)` event
*/
@Output() activeIdChange = new EventEmitter<any>();
/**
* If `true`, non-active nav content will be removed from DOM
* Otherwise it will just be hidden
*/
@Input() destroyOnHide;
/**
* The orientation of navs.
*
* Using `vertical` will also add the `aria-orientation` attribute
*/
@Input() orientation: 'horizontal' | 'vertical';
/**
* Role attribute generating strategy:
* - `false` - no role attributes will be generated
* - `'tablist'` - 'tablist', 'tab' and 'tabpanel' will be generated (default)
*/
@Input() roles: 'tablist' | false;
@ContentChildren(NgbNavItem) items: QueryList<NgbNavItem>;
constructor(@Attribute('role') public role: string, config: NgbNavConfig, private _cd: ChangeDetectorRef) {
this.destroyOnHide = config.destroyOnHide;
this.orientation = config.orientation;
this.roles = config.roles;
}
/**
* The nav change event emitted right before the nav change happens on user click.
*
* This event won't be emitted if nav is changed programmatically via `[activeId]` or `.select()`.
*
* See [`NgbNavChangeEvent`](#/components/nav/api#NgbNavChangeEvent) for payload details.
*/
@Output() navChange = new EventEmitter<NgbNavChangeEvent>();
click(item: NgbNavItem) {
if (!item.disabled) {
this._updateActiveId(item.id);
}
}
/**
* Selects the nav with the given id and shows its associated pane.
* Any other nav that was previously selected becomes unselected and its associated pane is hidden.
*/
select(id: any) { this._updateActiveId(id, false); }
ngAfterContentInit() {
if (!isDefined(this.activeId)) {
const nextId = this.items.first ? this.items.first.id : null;
if (isValidNavId(nextId)) {
this._updateActiveId(nextId);
this._cd.detectChanges();
}
}
}
private _updateActiveId(nextId: any, emitNavChange = true) {
if (this.activeId !== nextId) {
let defaultPrevented = false;
if (emitNavChange) {
this.navChange.emit({activeId: this.activeId, nextId, preventDefault: () => { defaultPrevented = true; }});
}
if (!defaultPrevented) {
this.activeId = nextId;
this.activeIdChange.emit(nextId);
}
}
}
}
/**
* A directive to put on the nav link.
*
* @since 5.2.0
*/
@Directive({
selector: 'a[ngbNavLink]',
host: {
'[id]': 'navItem.domId',
'[class.nav-link]': 'true',
'[class.nav-item]': 'hasNavItemClass()',
'[attr.role]': `role ? role : nav.roles ? 'tab' : undefined`,
'href': '',
'[class.active]': 'navItem.active',
'[class.disabled]': 'navItem.disabled',
'[attr.tabindex]': 'navItem.disabled ? -1 : undefined',
'[attr.aria-controls]': 'navItem.isPanelInDom() ? navItem.panelDomId : null',
'[attr.aria-selected]': 'navItem.active',
'[attr.aria-disabled]': 'navItem.disabled',
'(click)': 'nav.click(navItem); $event.preventDefault()'
}
})
export class NgbNavLink {
constructor(@Attribute('role') public role: string, public navItem: NgbNavItem, public nav: NgbNav) {}
hasNavItemClass() {
// with alternative markup we have to add `.nav-item` class, because `ngbNavItem` is on the ng-container
return this.navItem.elementRef.nativeElement.nodeType === Node.COMMENT_NODE;
}
}
/**
* The payload of the change event emitted right before the nav change happens on user click.
*
* This event won't be emitted if nav is changed programmatically via `[activeId]` or `.select()`.
*
* @since 5.2.0
*/
export interface NgbNavChangeEvent {
/**
* Id of the currently active nav.
*/
activeId: any;
/**
* Id of the newly selected nav.
*/
nextId: any;
/**
* Function that will prevent nav change if called.
*/
preventDefault: () => void;
}