Ignore:
Timestamp:
10/16/21 18:10:51 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
eed0bf8
Parents:
6a3a178
Message:

adding new components

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trip-planner-front/node_modules/@angular/material/fesm2015/badge.js

    r6a3a178 rfa375fe  
    1717const _MatBadgeBase = mixinDisabled(class {
    1818});
    19 const BADGE_CONTENT_CLASS = 'mat-badge-content';
    2019/** Directive to display a text badge. */
    2120class MatBadge extends _MatBadgeBase {
     
    2726        this._renderer = _renderer;
    2827        this._animationMode = _animationMode;
     28        /** Whether the badge has any content. */
     29        this._hasContent = false;
    2930        this._color = 'primary';
    3031        this._overlap = true;
     
    3839        /** Unique id for the badge */
    3940        this._id = nextId++;
    40         /** Whether the OnInit lifecycle hook has run yet */
    41         this._isInitialized = false;
    4241        if (typeof ngDevMode === 'undefined' || ngDevMode) {
    4342            const nativeElement = _elementRef.nativeElement;
     
    5857        this._overlap = coerceBooleanProperty(val);
    5958    }
    60     /** The content for the badge */
    61     get content() {
    62         return this._content;
    63     }
    64     set content(newContent) {
    65         this._updateRenderedContent(newContent);
    66     }
    6759    /** Message used to describe the decorated element via aria-describedby */
    6860    get description() { return this._description; }
    6961    set description(newDescription) {
    70         this._updateHostAriaDescription(newDescription);
     62        if (newDescription !== this._description) {
     63            const badgeElement = this._badgeElement;
     64            this._updateHostAriaDescription(newDescription, this._description);
     65            this._description = newDescription;
     66            if (badgeElement) {
     67                newDescription ? badgeElement.setAttribute('aria-label', newDescription) :
     68                    badgeElement.removeAttribute('aria-label');
     69            }
     70        }
    7171    }
    7272    /** Whether the badge is hidden. */
     
    8383        return this.position.indexOf('before') === -1;
    8484    }
     85    ngOnChanges(changes) {
     86        const contentChange = changes['content'];
     87        if (contentChange) {
     88            const value = contentChange.currentValue;
     89            this._hasContent = value != null && `${value}`.trim().length > 0;
     90            this._updateTextContent();
     91        }
     92    }
     93    ngOnDestroy() {
     94        const badgeElement = this._badgeElement;
     95        if (badgeElement) {
     96            if (this.description) {
     97                this._ariaDescriber.removeDescription(badgeElement, this.description);
     98            }
     99            // When creating a badge through the Renderer, Angular will keep it in an index.
     100            // We have to destroy it ourselves, otherwise it'll be retained in memory.
     101            if (this._renderer.destroyNode) {
     102                this._renderer.destroyNode(badgeElement);
     103            }
     104        }
     105    }
    85106    /**
    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).
     107     * Gets the element into which the badge's content is being rendered.
     108     * Undefined if the element hasn't been created (e.g. if the badge doesn't have content).
    88109     */
    89110    getBadgeElement() {
    90111        return this._badgeElement;
    91112    }
    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) {
     113    /** Injects a span element into the DOM with the content. */
     114    _updateTextContent() {
     115        if (!this._badgeElement) {
    98116            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);
     117        }
     118        else {
     119            this._badgeElement.textContent = this._stringifyContent();
     120        }
     121        return this._badgeElement;
    110122    }
    111123    /** Creates the badge element */
     
    113125        const badgeElement = this._renderer.createElement('span');
    114126        const activeClass = 'mat-badge-active';
     127        const contentClass = 'mat-badge-content';
     128        // Clear any existing badges which may have persisted from a server-side render.
     129        this._clearExistingBadges(contentClass);
    115130        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);
     131        badgeElement.classList.add(contentClass);
     132        badgeElement.textContent = this._stringifyContent();
    120133        if (this._animationMode === 'NoopAnimations') {
    121134            badgeElement.classList.add('_mat-animation-noopable');
     135        }
     136        if (this.description) {
     137            badgeElement.setAttribute('aria-label', this.description);
    122138        }
    123139        this._elementRef.nativeElement.appendChild(badgeElement);
     
    135151        return badgeElement;
    136152    }
    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);
     153    /** Sets the aria-label property on the element */
     154    _updateHostAriaDescription(newDescription, oldDescription) {
     155        // ensure content available before setting label
     156        const content = this._updateTextContent();
     157        if (oldDescription) {
     158            this._ariaDescriber.removeDescription(content, oldDescription);
     159        }
    154160        if (newDescription) {
    155             this._ariaDescriber.describe(this._elementRef.nativeElement, newDescription);
    156         }
    157         this._description = newDescription;
     161            this._ariaDescriber.describe(content, newDescription);
     162        }
    158163    }
    159164    /** Adds css theme class given the color to the component host */
    160165    _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}`);
     166        if (colorPalette !== this._color) {
     167            const classList = this._elementRef.nativeElement.classList;
     168            if (this._color) {
     169                classList.remove(`mat-badge-${this._color}`);
     170            }
     171            if (colorPalette) {
     172                classList.add(`mat-badge-${colorPalette}`);
     173            }
    165174        }
    166175    }
    167176    /** 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    _clearExistingBadges(cssClass) {
     178        const element = this._elementRef.nativeElement;
     179        let childCount = element.children.length;
     180        // Use a reverse while, because we'll be removing elements from the list as we're iterating.
     181        while (childCount--) {
     182            const currentChild = element.children[childCount];
     183            if (currentChild.classList.contains(cssClass)) {
     184                element.removeChild(currentChild);
     185            }
     186        }
     187    }
     188    /** Gets the string representation of the badge content. */
     189    _stringifyContent() {
     190        // Convert null and undefined to an empty string which is consistent
     191        // with how Angular handles them in inside template interpolations.
     192        const content = this.content;
     193        return content == null ? '' : `${content}`;
    177194    }
    178195}
     
    191208                    '[class.mat-badge-medium]': 'size === "medium"',
    192209                    '[class.mat-badge-large]': 'size === "large"',
    193                     '[class.mat-badge-hidden]': 'hidden || !content',
     210                    '[class.mat-badge-hidden]': 'hidden || !_hasContent',
    194211                    '[class.mat-badge-disabled]': 'disabled',
    195212                },
Note: See TracChangeset for help on using the changeset viewer.