material-badge.umd.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/cdk/a11y'), require('@angular/cdk/coercion'), require('@angular/core'), require('@angular/material/core'), require('@angular/platform-browser/animations')) :
  10. typeof define === 'function' && define.amd ? define('@angular/material/badge', ['exports', '@angular/cdk/a11y', '@angular/cdk/coercion', '@angular/core', '@angular/material/core', '@angular/platform-browser/animations'], factory) :
  11. (factory((global.ng = global.ng || {}, global.ng.material = global.ng.material || {}, global.ng.material.badge = {}),global.ng.cdk.a11y,global.ng.cdk.coercion,global.ng.core,global.ng.material.core,global.ng.platformBrowser.animations));
  12. }(this, (function (exports,a11y,coercion,core,core$1,animations) { 'use strict';
  13. /*! *****************************************************************************
  14. Copyright (c) Microsoft Corporation. All rights reserved.
  15. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  16. this file except in compliance with the License. You may obtain a copy of the
  17. License at http://www.apache.org/licenses/LICENSE-2.0
  18. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  20. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  21. MERCHANTABLITY OR NON-INFRINGEMENT.
  22. See the Apache Version 2.0 License for specific language governing permissions
  23. and limitations under the License.
  24. ***************************************************************************** */
  25. /* global Reflect, Promise */
  26. var extendStatics = function(d, b) {
  27. extendStatics = Object.setPrototypeOf ||
  28. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  29. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  30. return extendStatics(d, b);
  31. };
  32. function __extends(d, b) {
  33. extendStatics(d, b);
  34. function __() { this.constructor = d; }
  35. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  36. }
  37. /**
  38. * @fileoverview added by tsickle
  39. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  40. */
  41. /** @type {?} */
  42. var nextId = 0;
  43. // Boilerplate for applying mixins to MatBadge.
  44. /**
  45. * \@docs-private
  46. */
  47. var
  48. // Boilerplate for applying mixins to MatBadge.
  49. /**
  50. * \@docs-private
  51. */
  52. MatBadgeBase = /** @class */ (function () {
  53. function MatBadgeBase() {
  54. }
  55. return MatBadgeBase;
  56. }());
  57. /** @type {?} */
  58. var _MatBadgeMixinBase = core$1.mixinDisabled(MatBadgeBase);
  59. /**
  60. * Directive to display a text badge.
  61. */
  62. var MatBadge = /** @class */ (function (_super) {
  63. __extends(MatBadge, _super);
  64. function MatBadge(_ngZone, _elementRef, _ariaDescriber, _renderer, _animationMode) {
  65. var _this = _super.call(this) || this;
  66. _this._ngZone = _ngZone;
  67. _this._elementRef = _elementRef;
  68. _this._ariaDescriber = _ariaDescriber;
  69. _this._renderer = _renderer;
  70. _this._animationMode = _animationMode;
  71. /**
  72. * Whether the badge has any content.
  73. */
  74. _this._hasContent = false;
  75. _this._color = 'primary';
  76. _this._overlap = true;
  77. /**
  78. * Position the badge should reside.
  79. * Accepts any combination of 'above'|'below' and 'before'|'after'
  80. */
  81. _this.position = 'above after';
  82. /**
  83. * Size of the badge. Can be 'small', 'medium', or 'large'.
  84. */
  85. _this.size = 'medium';
  86. /**
  87. * Unique id for the badge
  88. */
  89. _this._id = nextId++;
  90. if (core.isDevMode()) {
  91. /** @type {?} */
  92. var nativeElement = _elementRef.nativeElement;
  93. if (nativeElement.nodeType !== nativeElement.ELEMENT_NODE) {
  94. throw Error('matBadge must be attached to an element node.');
  95. }
  96. }
  97. return _this;
  98. }
  99. Object.defineProperty(MatBadge.prototype, "color", {
  100. /** The color of the badge. Can be `primary`, `accent`, or `warn`. */
  101. get: /**
  102. * The color of the badge. Can be `primary`, `accent`, or `warn`.
  103. * @return {?}
  104. */
  105. function () { return this._color; },
  106. set: /**
  107. * @param {?} value
  108. * @return {?}
  109. */
  110. function (value) {
  111. this._setColor(value);
  112. this._color = value;
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. Object.defineProperty(MatBadge.prototype, "overlap", {
  118. /** Whether the badge should overlap its contents or not */
  119. get: /**
  120. * Whether the badge should overlap its contents or not
  121. * @return {?}
  122. */
  123. function () { return this._overlap; },
  124. set: /**
  125. * @param {?} val
  126. * @return {?}
  127. */
  128. function (val) {
  129. this._overlap = coercion.coerceBooleanProperty(val);
  130. },
  131. enumerable: true,
  132. configurable: true
  133. });
  134. Object.defineProperty(MatBadge.prototype, "description", {
  135. /** Message used to describe the decorated element via aria-describedby */
  136. get: /**
  137. * Message used to describe the decorated element via aria-describedby
  138. * @return {?}
  139. */
  140. function () { return this._description; },
  141. set: /**
  142. * @param {?} newDescription
  143. * @return {?}
  144. */
  145. function (newDescription) {
  146. if (newDescription !== this._description) {
  147. /** @type {?} */
  148. var badgeElement = this._badgeElement;
  149. this._updateHostAriaDescription(newDescription, this._description);
  150. this._description = newDescription;
  151. if (badgeElement) {
  152. newDescription ? badgeElement.setAttribute('aria-label', newDescription) :
  153. badgeElement.removeAttribute('aria-label');
  154. }
  155. }
  156. },
  157. enumerable: true,
  158. configurable: true
  159. });
  160. Object.defineProperty(MatBadge.prototype, "hidden", {
  161. /** Whether the badge is hidden. */
  162. get: /**
  163. * Whether the badge is hidden.
  164. * @return {?}
  165. */
  166. function () { return this._hidden; },
  167. set: /**
  168. * @param {?} val
  169. * @return {?}
  170. */
  171. function (val) {
  172. this._hidden = coercion.coerceBooleanProperty(val);
  173. },
  174. enumerable: true,
  175. configurable: true
  176. });
  177. /** Whether the badge is above the host or not */
  178. /**
  179. * Whether the badge is above the host or not
  180. * @return {?}
  181. */
  182. MatBadge.prototype.isAbove = /**
  183. * Whether the badge is above the host or not
  184. * @return {?}
  185. */
  186. function () {
  187. return this.position.indexOf('below') === -1;
  188. };
  189. /** Whether the badge is after the host or not */
  190. /**
  191. * Whether the badge is after the host or not
  192. * @return {?}
  193. */
  194. MatBadge.prototype.isAfter = /**
  195. * Whether the badge is after the host or not
  196. * @return {?}
  197. */
  198. function () {
  199. return this.position.indexOf('before') === -1;
  200. };
  201. /**
  202. * @param {?} changes
  203. * @return {?}
  204. */
  205. MatBadge.prototype.ngOnChanges = /**
  206. * @param {?} changes
  207. * @return {?}
  208. */
  209. function (changes) {
  210. /** @type {?} */
  211. var contentChange = changes['content'];
  212. if (contentChange) {
  213. /** @type {?} */
  214. var value = contentChange.currentValue;
  215. this._hasContent = value != null && ("" + value).trim().length > 0;
  216. this._updateTextContent();
  217. }
  218. };
  219. /**
  220. * @return {?}
  221. */
  222. MatBadge.prototype.ngOnDestroy = /**
  223. * @return {?}
  224. */
  225. function () {
  226. /** @type {?} */
  227. var badgeElement = this._badgeElement;
  228. if (badgeElement) {
  229. if (this.description) {
  230. this._ariaDescriber.removeDescription(badgeElement, this.description);
  231. }
  232. // When creating a badge through the Renderer, Angular will keep it in an index.
  233. // We have to destroy it ourselves, otherwise it'll be retained in memory.
  234. if (this._renderer.destroyNode) {
  235. this._renderer.destroyNode(badgeElement);
  236. }
  237. }
  238. };
  239. /**
  240. * Gets the element into which the badge's content is being rendered.
  241. * Undefined if the element hasn't been created (e.g. if the badge doesn't have content).
  242. */
  243. /**
  244. * Gets the element into which the badge's content is being rendered.
  245. * Undefined if the element hasn't been created (e.g. if the badge doesn't have content).
  246. * @return {?}
  247. */
  248. MatBadge.prototype.getBadgeElement = /**
  249. * Gets the element into which the badge's content is being rendered.
  250. * Undefined if the element hasn't been created (e.g. if the badge doesn't have content).
  251. * @return {?}
  252. */
  253. function () {
  254. return this._badgeElement;
  255. };
  256. /** Injects a span element into the DOM with the content. */
  257. /**
  258. * Injects a span element into the DOM with the content.
  259. * @private
  260. * @return {?}
  261. */
  262. MatBadge.prototype._updateTextContent = /**
  263. * Injects a span element into the DOM with the content.
  264. * @private
  265. * @return {?}
  266. */
  267. function () {
  268. if (!this._badgeElement) {
  269. this._badgeElement = this._createBadgeElement();
  270. }
  271. else {
  272. this._badgeElement.textContent = this.content;
  273. }
  274. return this._badgeElement;
  275. };
  276. /** Creates the badge element */
  277. /**
  278. * Creates the badge element
  279. * @private
  280. * @return {?}
  281. */
  282. MatBadge.prototype._createBadgeElement = /**
  283. * Creates the badge element
  284. * @private
  285. * @return {?}
  286. */
  287. function () {
  288. /** @type {?} */
  289. var badgeElement = this._renderer.createElement('span');
  290. /** @type {?} */
  291. var activeClass = 'mat-badge-active';
  292. /** @type {?} */
  293. var contentClass = 'mat-badge-content';
  294. // Clear any existing badges which may have persisted from a server-side render.
  295. this._clearExistingBadges(contentClass);
  296. badgeElement.setAttribute('id', "mat-badge-content-" + this._id);
  297. badgeElement.classList.add(contentClass);
  298. badgeElement.textContent = this.content;
  299. if (this._animationMode === 'NoopAnimations') {
  300. badgeElement.classList.add('_mat-animation-noopable');
  301. }
  302. if (this.description) {
  303. badgeElement.setAttribute('aria-label', this.description);
  304. }
  305. this._elementRef.nativeElement.appendChild(badgeElement);
  306. // animate in after insertion
  307. if (typeof requestAnimationFrame === 'function' && this._animationMode !== 'NoopAnimations') {
  308. this._ngZone.runOutsideAngular((/**
  309. * @return {?}
  310. */
  311. function () {
  312. requestAnimationFrame((/**
  313. * @return {?}
  314. */
  315. function () {
  316. badgeElement.classList.add(activeClass);
  317. }));
  318. }));
  319. }
  320. else {
  321. badgeElement.classList.add(activeClass);
  322. }
  323. return badgeElement;
  324. };
  325. /** Sets the aria-label property on the element */
  326. /**
  327. * Sets the aria-label property on the element
  328. * @private
  329. * @param {?} newDescription
  330. * @param {?} oldDescription
  331. * @return {?}
  332. */
  333. MatBadge.prototype._updateHostAriaDescription = /**
  334. * Sets the aria-label property on the element
  335. * @private
  336. * @param {?} newDescription
  337. * @param {?} oldDescription
  338. * @return {?}
  339. */
  340. function (newDescription, oldDescription) {
  341. // ensure content available before setting label
  342. /** @type {?} */
  343. var content = this._updateTextContent();
  344. if (oldDescription) {
  345. this._ariaDescriber.removeDescription(content, oldDescription);
  346. }
  347. if (newDescription) {
  348. this._ariaDescriber.describe(content, newDescription);
  349. }
  350. };
  351. /** Adds css theme class given the color to the component host */
  352. /**
  353. * Adds css theme class given the color to the component host
  354. * @private
  355. * @param {?} colorPalette
  356. * @return {?}
  357. */
  358. MatBadge.prototype._setColor = /**
  359. * Adds css theme class given the color to the component host
  360. * @private
  361. * @param {?} colorPalette
  362. * @return {?}
  363. */
  364. function (colorPalette) {
  365. if (colorPalette !== this._color) {
  366. if (this._color) {
  367. this._elementRef.nativeElement.classList.remove("mat-badge-" + this._color);
  368. }
  369. if (colorPalette) {
  370. this._elementRef.nativeElement.classList.add("mat-badge-" + colorPalette);
  371. }
  372. }
  373. };
  374. /** Clears any existing badges that might be left over from server-side rendering. */
  375. /**
  376. * Clears any existing badges that might be left over from server-side rendering.
  377. * @private
  378. * @param {?} cssClass
  379. * @return {?}
  380. */
  381. MatBadge.prototype._clearExistingBadges = /**
  382. * Clears any existing badges that might be left over from server-side rendering.
  383. * @private
  384. * @param {?} cssClass
  385. * @return {?}
  386. */
  387. function (cssClass) {
  388. /** @type {?} */
  389. var element = this._elementRef.nativeElement;
  390. /** @type {?} */
  391. var childCount = element.children.length;
  392. // Use a reverse while, because we'll be removing elements from the list as we're iterating.
  393. while (childCount--) {
  394. /** @type {?} */
  395. var currentChild = element.children[childCount];
  396. if (currentChild.classList.contains(cssClass)) {
  397. element.removeChild(currentChild);
  398. }
  399. }
  400. };
  401. MatBadge.decorators = [
  402. { type: core.Directive, args: [{
  403. selector: '[matBadge]',
  404. inputs: ['disabled: matBadgeDisabled'],
  405. host: {
  406. 'class': 'mat-badge',
  407. '[class.mat-badge-overlap]': 'overlap',
  408. '[class.mat-badge-above]': 'isAbove()',
  409. '[class.mat-badge-below]': '!isAbove()',
  410. '[class.mat-badge-before]': '!isAfter()',
  411. '[class.mat-badge-after]': 'isAfter()',
  412. '[class.mat-badge-small]': 'size === "small"',
  413. '[class.mat-badge-medium]': 'size === "medium"',
  414. '[class.mat-badge-large]': 'size === "large"',
  415. '[class.mat-badge-hidden]': 'hidden || !_hasContent',
  416. '[class.mat-badge-disabled]': 'disabled',
  417. },
  418. },] },
  419. ];
  420. /** @nocollapse */
  421. MatBadge.ctorParameters = function () { return [
  422. { type: core.NgZone },
  423. { type: core.ElementRef },
  424. { type: a11y.AriaDescriber },
  425. { type: core.Renderer2 },
  426. { type: String, decorators: [{ type: core.Optional }, { type: core.Inject, args: [animations.ANIMATION_MODULE_TYPE,] }] }
  427. ]; };
  428. MatBadge.propDecorators = {
  429. color: [{ type: core.Input, args: ['matBadgeColor',] }],
  430. overlap: [{ type: core.Input, args: ['matBadgeOverlap',] }],
  431. position: [{ type: core.Input, args: ['matBadgePosition',] }],
  432. content: [{ type: core.Input, args: ['matBadge',] }],
  433. description: [{ type: core.Input, args: ['matBadgeDescription',] }],
  434. size: [{ type: core.Input, args: ['matBadgeSize',] }],
  435. hidden: [{ type: core.Input, args: ['matBadgeHidden',] }]
  436. };
  437. return MatBadge;
  438. }(_MatBadgeMixinBase));
  439. /**
  440. * @fileoverview added by tsickle
  441. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  442. */
  443. var MatBadgeModule = /** @class */ (function () {
  444. function MatBadgeModule() {
  445. }
  446. MatBadgeModule.decorators = [
  447. { type: core.NgModule, args: [{
  448. imports: [
  449. a11y.A11yModule,
  450. core$1.MatCommonModule
  451. ],
  452. exports: [MatBadge],
  453. declarations: [MatBadge],
  454. },] },
  455. ];
  456. return MatBadgeModule;
  457. }());
  458. exports.MatBadgeModule = MatBadgeModule;
  459. exports.MatBadge = MatBadge;
  460. Object.defineProperty(exports, '__esModule', { value: true });
  461. })));
  462. //# sourceMappingURL=material-badge.umd.js.map