1 | import { Directive, NgZone, ElementRef, Renderer2, Optional, Inject, Input, NgModule } from '@angular/core';
|
---|
2 | import { mixinDisabled, MatCommonModule } from '@angular/material/core';
|
---|
3 | import { AriaDescriber, A11yModule } from '@angular/cdk/a11y';
|
---|
4 | import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
---|
5 | import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations';
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * @license
|
---|
9 | * Copyright Google LLC All Rights Reserved.
|
---|
10 | *
|
---|
11 | * Use of this source code is governed by an MIT-style license that can be
|
---|
12 | * found in the LICENSE file at https://angular.io/license
|
---|
13 | */
|
---|
14 | let nextId = 0;
|
---|
15 | // Boilerplate for applying mixins to MatBadge.
|
---|
16 | /** @docs-private */
|
---|
17 | const _MatBadgeBase = mixinDisabled(class {
|
---|
18 | });
|
---|
19 | const BADGE_CONTENT_CLASS = 'mat-badge-content';
|
---|
20 | /** Directive to display a text badge. */
|
---|
21 | class MatBadge extends _MatBadgeBase {
|
---|
22 | constructor(_ngZone, _elementRef, _ariaDescriber, _renderer, _animationMode) {
|
---|
23 | super();
|
---|
24 | this._ngZone = _ngZone;
|
---|
25 | this._elementRef = _elementRef;
|
---|
26 | this._ariaDescriber = _ariaDescriber;
|
---|
27 | this._renderer = _renderer;
|
---|
28 | this._animationMode = _animationMode;
|
---|
29 | this._color = 'primary';
|
---|
30 | this._overlap = true;
|
---|
31 | /**
|
---|
32 | * Position the badge should reside.
|
---|
33 | * Accepts any combination of 'above'|'below' and 'before'|'after'
|
---|
34 | */
|
---|
35 | this.position = 'above after';
|
---|
36 | /** Size of the badge. Can be 'small', 'medium', or 'large'. */
|
---|
37 | this.size = 'medium';
|
---|
38 | /** Unique id for the badge */
|
---|
39 | this._id = nextId++;
|
---|
40 | /** Whether the OnInit lifecycle hook has run yet */
|
---|
41 | this._isInitialized = false;
|
---|
42 | if (typeof ngDevMode === 'undefined' || ngDevMode) {
|
---|
43 | const nativeElement = _elementRef.nativeElement;
|
---|
44 | if (nativeElement.nodeType !== nativeElement.ELEMENT_NODE) {
|
---|
45 | throw Error('matBadge must be attached to an element node.');
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | /** The color of the badge. Can be `primary`, `accent`, or `warn`. */
|
---|
50 | get color() { return this._color; }
|
---|
51 | set color(value) {
|
---|
52 | this._setColor(value);
|
---|
53 | this._color = value;
|
---|
54 | }
|
---|
55 | /** Whether the badge should overlap its contents or not */
|
---|
56 | get overlap() { return this._overlap; }
|
---|
57 | set overlap(val) {
|
---|
58 | this._overlap = coerceBooleanProperty(val);
|
---|
59 | }
|
---|
60 | /** The content for the badge */
|
---|
61 | get content() {
|
---|
62 | return this._content;
|
---|
63 | }
|
---|
64 | set content(newContent) {
|
---|
65 | this._updateRenderedContent(newContent);
|
---|
66 | }
|
---|
67 | /** Message used to describe the decorated element via aria-describedby */
|
---|
68 | get description() { return this._description; }
|
---|
69 | set description(newDescription) {
|
---|
70 | this._updateHostAriaDescription(newDescription);
|
---|
71 | }
|
---|
72 | /** Whether the badge is hidden. */
|
---|
73 | get hidden() { return this._hidden; }
|
---|
74 | set hidden(val) {
|
---|
75 | this._hidden = coerceBooleanProperty(val);
|
---|
76 | }
|
---|
77 | /** Whether the badge is above the host or not */
|
---|
78 | isAbove() {
|
---|
79 | return this.position.indexOf('below') === -1;
|
---|
80 | }
|
---|
81 | /** Whether the badge is after the host or not */
|
---|
82 | isAfter() {
|
---|
83 | return this.position.indexOf('before') === -1;
|
---|
84 | }
|
---|
85 | /**
|
---|
86 | * Gets the element into which the badge's content is being rendered. Undefined if the element
|
---|
87 | * hasn't been created (e.g. if the badge doesn't have content).
|
---|
88 | */
|
---|
89 | getBadgeElement() {
|
---|
90 | return this._badgeElement;
|
---|
91 | }
|
---|
92 | ngOnInit() {
|
---|
93 | // We may have server-side rendered badge that we need to clear.
|
---|
94 | // We need to do this in ngOnInit because the full content of the component
|
---|
95 | // on which the badge is attached won't necessarily be in the DOM until this point.
|
---|
96 | this._clearExistingBadges();
|
---|
97 | if (this.content && !this._badgeElement) {
|
---|
98 | this._badgeElement = this._createBadgeElement();
|
---|
99 | this._updateRenderedContent(this.content);
|
---|
100 | }
|
---|
101 | this._isInitialized = true;
|
---|
102 | }
|
---|
103 | ngOnDestroy() {
|
---|
104 | // ViewEngine only: when creating a badge through the Renderer, Angular remembers its index.
|
---|
105 | // We have to destroy it ourselves, otherwise it'll be retained in memory.
|
---|
106 | if (this._renderer.destroyNode) {
|
---|
107 | this._renderer.destroyNode(this._badgeElement);
|
---|
108 | }
|
---|
109 | this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this.description);
|
---|
110 | }
|
---|
111 | /** Creates the badge element */
|
---|
112 | _createBadgeElement() {
|
---|
113 | const badgeElement = this._renderer.createElement('span');
|
---|
114 | const activeClass = 'mat-badge-active';
|
---|
115 | badgeElement.setAttribute('id', `mat-badge-content-${this._id}`);
|
---|
116 | // The badge is aria-hidden because we don't want it to appear in the page's navigation
|
---|
117 | // flow. Instead, we use the badge to describe the decorated element with aria-describedby.
|
---|
118 | badgeElement.setAttribute('aria-hidden', 'true');
|
---|
119 | badgeElement.classList.add(BADGE_CONTENT_CLASS);
|
---|
120 | if (this._animationMode === 'NoopAnimations') {
|
---|
121 | badgeElement.classList.add('_mat-animation-noopable');
|
---|
122 | }
|
---|
123 | this._elementRef.nativeElement.appendChild(badgeElement);
|
---|
124 | // animate in after insertion
|
---|
125 | if (typeof requestAnimationFrame === 'function' && this._animationMode !== 'NoopAnimations') {
|
---|
126 | this._ngZone.runOutsideAngular(() => {
|
---|
127 | requestAnimationFrame(() => {
|
---|
128 | badgeElement.classList.add(activeClass);
|
---|
129 | });
|
---|
130 | });
|
---|
131 | }
|
---|
132 | else {
|
---|
133 | badgeElement.classList.add(activeClass);
|
---|
134 | }
|
---|
135 | return badgeElement;
|
---|
136 | }
|
---|
137 | /** Update the text content of the badge element in the DOM, creating the element if necessary. */
|
---|
138 | _updateRenderedContent(newContent) {
|
---|
139 | const newContentNormalized = `${newContent !== null && newContent !== void 0 ? newContent : ''}`.trim();
|
---|
140 | // Don't create the badge element if the directive isn't initialized because we want to
|
---|
141 | // append the badge element to the *end* of the host element's content for backwards
|
---|
142 | // compatibility.
|
---|
143 | if (this._isInitialized && newContentNormalized && !this._badgeElement) {
|
---|
144 | this._badgeElement = this._createBadgeElement();
|
---|
145 | }
|
---|
146 | if (this._badgeElement) {
|
---|
147 | this._badgeElement.textContent = newContentNormalized;
|
---|
148 | }
|
---|
149 | this._content = newContentNormalized;
|
---|
150 | }
|
---|
151 | /** Updates the host element's aria description via AriaDescriber. */
|
---|
152 | _updateHostAriaDescription(newDescription) {
|
---|
153 | this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this.description);
|
---|
154 | if (newDescription) {
|
---|
155 | this._ariaDescriber.describe(this._elementRef.nativeElement, newDescription);
|
---|
156 | }
|
---|
157 | this._description = newDescription;
|
---|
158 | }
|
---|
159 | /** Adds css theme class given the color to the component host */
|
---|
160 | _setColor(colorPalette) {
|
---|
161 | const classList = this._elementRef.nativeElement.classList;
|
---|
162 | classList.remove(`mat-badge-${this._color}`);
|
---|
163 | if (colorPalette) {
|
---|
164 | classList.add(`mat-badge-${colorPalette}`);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | /** Clears any existing badges that might be left over from server-side rendering. */
|
---|
168 | _clearExistingBadges() {
|
---|
169 | // Only check direct children of this host element in order to avoid deleting
|
---|
170 | // any badges that might exist in descendant elements.
|
---|
171 | const badges = this._elementRef.nativeElement.querySelectorAll(`:scope > .${BADGE_CONTENT_CLASS}`);
|
---|
172 | for (const badgeElement of Array.from(badges)) {
|
---|
173 | if (badgeElement !== this._badgeElement) {
|
---|
174 | badgeElement.remove();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | MatBadge.decorators = [
|
---|
180 | { type: Directive, args: [{
|
---|
181 | selector: '[matBadge]',
|
---|
182 | inputs: ['disabled: matBadgeDisabled'],
|
---|
183 | host: {
|
---|
184 | 'class': 'mat-badge',
|
---|
185 | '[class.mat-badge-overlap]': 'overlap',
|
---|
186 | '[class.mat-badge-above]': 'isAbove()',
|
---|
187 | '[class.mat-badge-below]': '!isAbove()',
|
---|
188 | '[class.mat-badge-before]': '!isAfter()',
|
---|
189 | '[class.mat-badge-after]': 'isAfter()',
|
---|
190 | '[class.mat-badge-small]': 'size === "small"',
|
---|
191 | '[class.mat-badge-medium]': 'size === "medium"',
|
---|
192 | '[class.mat-badge-large]': 'size === "large"',
|
---|
193 | '[class.mat-badge-hidden]': 'hidden || !content',
|
---|
194 | '[class.mat-badge-disabled]': 'disabled',
|
---|
195 | },
|
---|
196 | },] }
|
---|
197 | ];
|
---|
198 | MatBadge.ctorParameters = () => [
|
---|
199 | { type: NgZone },
|
---|
200 | { type: ElementRef },
|
---|
201 | { type: AriaDescriber },
|
---|
202 | { type: Renderer2 },
|
---|
203 | { type: String, decorators: [{ type: Optional }, { type: Inject, args: [ANIMATION_MODULE_TYPE,] }] }
|
---|
204 | ];
|
---|
205 | MatBadge.propDecorators = {
|
---|
206 | color: [{ type: Input, args: ['matBadgeColor',] }],
|
---|
207 | overlap: [{ type: Input, args: ['matBadgeOverlap',] }],
|
---|
208 | position: [{ type: Input, args: ['matBadgePosition',] }],
|
---|
209 | content: [{ type: Input, args: ['matBadge',] }],
|
---|
210 | description: [{ type: Input, args: ['matBadgeDescription',] }],
|
---|
211 | size: [{ type: Input, args: ['matBadgeSize',] }],
|
---|
212 | hidden: [{ type: Input, args: ['matBadgeHidden',] }]
|
---|
213 | };
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * @license
|
---|
217 | * Copyright Google LLC All Rights Reserved.
|
---|
218 | *
|
---|
219 | * Use of this source code is governed by an MIT-style license that can be
|
---|
220 | * found in the LICENSE file at https://angular.io/license
|
---|
221 | */
|
---|
222 | class MatBadgeModule {
|
---|
223 | }
|
---|
224 | MatBadgeModule.decorators = [
|
---|
225 | { type: NgModule, args: [{
|
---|
226 | imports: [
|
---|
227 | A11yModule,
|
---|
228 | MatCommonModule
|
---|
229 | ],
|
---|
230 | exports: [MatBadge, MatCommonModule],
|
---|
231 | declarations: [MatBadge],
|
---|
232 | },] }
|
---|
233 | ];
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * @license
|
---|
237 | * Copyright Google LLC All Rights Reserved.
|
---|
238 | *
|
---|
239 | * Use of this source code is governed by an MIT-style license that can be
|
---|
240 | * found in the LICENSE file at https://angular.io/license
|
---|
241 | */
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Generated bundle index. Do not edit.
|
---|
245 | */
|
---|
246 |
|
---|
247 | export { MatBadge, MatBadgeModule };
|
---|
248 | //# sourceMappingURL=badge.js.map
|
---|