progress-bar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. import { Component, ChangeDetectionStrategy, ElementRef, Inject, Input, Output, EventEmitter, Optional, NgZone, ViewEncapsulation, ViewChild, InjectionToken, inject, NgModule } from '@angular/core';
  9. import { fromEvent, Subscription } from 'rxjs';
  10. import { filter } from 'rxjs/operators';
  11. import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations';
  12. import { mixinColor, MatCommonModule } from '@angular/material/core';
  13. import { DOCUMENT, CommonModule } from '@angular/common';
  14. /**
  15. * @fileoverview added by tsickle
  16. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  17. */
  18. // Boilerplate for applying mixins to MatProgressBar.
  19. /**
  20. * \@docs-private
  21. */
  22. class MatProgressBarBase {
  23. /**
  24. * @param {?} _elementRef
  25. */
  26. constructor(_elementRef) {
  27. this._elementRef = _elementRef;
  28. }
  29. }
  30. /** @type {?} */
  31. const _MatProgressBarMixinBase = mixinColor(MatProgressBarBase, 'primary');
  32. /**
  33. * Injection token used to provide the current location to `MatProgressBar`.
  34. * Used to handle server-side rendering and to stub out during unit tests.
  35. * \@docs-private
  36. * @type {?}
  37. */
  38. const MAT_PROGRESS_BAR_LOCATION = new InjectionToken('mat-progress-bar-location', { providedIn: 'root', factory: MAT_PROGRESS_BAR_LOCATION_FACTORY });
  39. /**
  40. * \@docs-private
  41. * @return {?}
  42. */
  43. function MAT_PROGRESS_BAR_LOCATION_FACTORY() {
  44. /** @type {?} */
  45. const _document = inject(DOCUMENT);
  46. /** @type {?} */
  47. const _location = _document ? _document.location : null;
  48. return {
  49. // Note that this needs to be a function, rather than a property, because Angular
  50. // will only resolve it once, but we want the current path on each call.
  51. getPathname: (/**
  52. * @return {?}
  53. */
  54. () => _location ? (_location.pathname + _location.search) : '')
  55. };
  56. }
  57. /**
  58. * Counter used to generate unique IDs for progress bars.
  59. * @type {?}
  60. */
  61. let progressbarId = 0;
  62. /**
  63. * `<mat-progress-bar>` component.
  64. */
  65. class MatProgressBar extends _MatProgressBarMixinBase {
  66. /**
  67. * @param {?} _elementRef
  68. * @param {?} _ngZone
  69. * @param {?=} _animationMode
  70. * @param {?=} location
  71. */
  72. constructor(_elementRef, _ngZone, _animationMode,
  73. /**
  74. * @deprecated `location` parameter to be made required.
  75. * @breaking-change 8.0.0
  76. */
  77. location) {
  78. super(_elementRef);
  79. this._elementRef = _elementRef;
  80. this._ngZone = _ngZone;
  81. this._animationMode = _animationMode;
  82. /**
  83. * Flag that indicates whether NoopAnimations mode is set to true.
  84. */
  85. this._isNoopAnimation = false;
  86. this._value = 0;
  87. this._bufferValue = 0;
  88. /**
  89. * Event emitted when animation of the primary progress bar completes. This event will not
  90. * be emitted when animations are disabled, nor will it be emitted for modes with continuous
  91. * animations (indeterminate and query).
  92. */
  93. this.animationEnd = new EventEmitter();
  94. /**
  95. * Reference to animation end subscription to be unsubscribed on destroy.
  96. */
  97. this._animationEndSubscription = Subscription.EMPTY;
  98. /**
  99. * Mode of the progress bar.
  100. *
  101. * Input must be one of these values: determinate, indeterminate, buffer, query, defaults to
  102. * 'determinate'.
  103. * Mirrored to mode attribute.
  104. */
  105. this.mode = 'determinate';
  106. /**
  107. * ID of the progress bar.
  108. */
  109. this.progressbarId = `mat-progress-bar-${progressbarId++}`;
  110. // We need to prefix the SVG reference with the current path, otherwise they won't work
  111. // in Safari if the page has a `<base>` tag. Note that we need quotes inside the `url()`,
  112. // because named route URLs can contain parentheses (see #12338). Also we don't use since
  113. // we can't tell the difference between whether
  114. // the consumer is using the hash location strategy or not, because `Location` normalizes
  115. // both `/#/foo/bar` and `/foo/bar` to the same thing.
  116. /** @type {?} */
  117. const path = location ? location.getPathname().split('#')[0] : '';
  118. this._rectangleFillValue = `url('${path}#${this.progressbarId}')`;
  119. this._isNoopAnimation = _animationMode === 'NoopAnimations';
  120. }
  121. /**
  122. * Value of the progress bar. Defaults to zero. Mirrored to aria-valuenow.
  123. * @return {?}
  124. */
  125. get value() { return this._value; }
  126. /**
  127. * @param {?} v
  128. * @return {?}
  129. */
  130. set value(v) {
  131. this._value = clamp(v || 0);
  132. // When noop animation is set to true, trigger animationEnd directly.
  133. if (this._isNoopAnimation) {
  134. this._emitAnimationEnd();
  135. }
  136. }
  137. /**
  138. * Buffer value of the progress bar. Defaults to zero.
  139. * @return {?}
  140. */
  141. get bufferValue() { return this._bufferValue; }
  142. /**
  143. * @param {?} v
  144. * @return {?}
  145. */
  146. set bufferValue(v) { this._bufferValue = clamp(v || 0); }
  147. /**
  148. * Gets the current transform value for the progress bar's primary indicator.
  149. * @return {?}
  150. */
  151. _primaryTransform() {
  152. /** @type {?} */
  153. const scale = this.value / 100;
  154. return { transform: `scaleX(${scale})` };
  155. }
  156. /**
  157. * Gets the current transform value for the progress bar's buffer indicator. Only used if the
  158. * progress mode is set to buffer, otherwise returns an undefined, causing no transformation.
  159. * @return {?}
  160. */
  161. _bufferTransform() {
  162. if (this.mode === 'buffer') {
  163. /** @type {?} */
  164. const scale = this.bufferValue / 100;
  165. return { transform: `scaleX(${scale})` };
  166. }
  167. return undefined;
  168. }
  169. /**
  170. * @return {?}
  171. */
  172. ngAfterViewInit() {
  173. if (!this._isNoopAnimation) {
  174. // Run outside angular so change detection didn't get triggered on every transition end
  175. // instead only on the animation that we care about (primary value bar's transitionend)
  176. this._ngZone.runOutsideAngular(((/**
  177. * @return {?}
  178. */
  179. () => {
  180. /** @type {?} */
  181. const element = this._primaryValueBar.nativeElement;
  182. this._animationEndSubscription =
  183. ((/** @type {?} */ (fromEvent(element, 'transitionend'))))
  184. .pipe(filter(((/**
  185. * @param {?} e
  186. * @return {?}
  187. */
  188. (e) => e.target === element))))
  189. .subscribe((/**
  190. * @return {?}
  191. */
  192. () => this._ngZone.run((/**
  193. * @return {?}
  194. */
  195. () => this._emitAnimationEnd()))));
  196. })));
  197. }
  198. }
  199. /**
  200. * @return {?}
  201. */
  202. ngOnDestroy() {
  203. this._animationEndSubscription.unsubscribe();
  204. }
  205. /**
  206. * Emit an animationEnd event if in determinate or buffer mode.
  207. * @private
  208. * @return {?}
  209. */
  210. _emitAnimationEnd() {
  211. if (this.mode === 'determinate' || this.mode === 'buffer') {
  212. this.animationEnd.next({ value: this.value });
  213. }
  214. }
  215. }
  216. MatProgressBar.decorators = [
  217. { type: Component, args: [{selector: 'mat-progress-bar',
  218. exportAs: 'matProgressBar',
  219. host: {
  220. 'role': 'progressbar',
  221. 'aria-valuemin': '0',
  222. 'aria-valuemax': '100',
  223. '[attr.aria-valuenow]': '(mode === "indeterminate" || mode === "query") ? null : value',
  224. '[attr.mode]': 'mode',
  225. 'class': 'mat-progress-bar',
  226. '[class._mat-animation-noopable]': '_isNoopAnimation',
  227. },
  228. inputs: ['color'],
  229. template: "<svg width=\"100%\" height=\"4\" focusable=\"false\" class=\"mat-progress-bar-background mat-progress-bar-element\"><defs><pattern [id]=\"progressbarId\" x=\"4\" y=\"0\" width=\"8\" height=\"4\" patternUnits=\"userSpaceOnUse\"><circle cx=\"2\" cy=\"2\" r=\"2\"/></pattern></defs><rect [attr.fill]=\"_rectangleFillValue\" width=\"100%\" height=\"100%\"/></svg><div class=\"mat-progress-bar-buffer mat-progress-bar-element\" [ngStyle]=\"_bufferTransform()\"></div><div class=\"mat-progress-bar-primary mat-progress-bar-fill mat-progress-bar-element\" [ngStyle]=\"_primaryTransform()\" #primaryValueBar></div><div class=\"mat-progress-bar-secondary mat-progress-bar-fill mat-progress-bar-element\"></div>",
  230. styles: [".mat-progress-bar{display:block;height:4px;overflow:hidden;position:relative;transition:opacity 250ms linear;width:100%}._mat-animation-noopable.mat-progress-bar{transition:none;animation:none}.mat-progress-bar .mat-progress-bar-element,.mat-progress-bar .mat-progress-bar-fill::after{height:100%;position:absolute;width:100%}.mat-progress-bar .mat-progress-bar-background{width:calc(100% + 10px)}@media (-ms-high-contrast:active){.mat-progress-bar .mat-progress-bar-background{display:none}}.mat-progress-bar .mat-progress-bar-buffer{transform-origin:top left;transition:transform 250ms ease}@media (-ms-high-contrast:active){.mat-progress-bar .mat-progress-bar-buffer{border-top:solid 5px;opacity:.5}}.mat-progress-bar .mat-progress-bar-secondary{display:none}.mat-progress-bar .mat-progress-bar-fill{animation:none;transform-origin:top left;transition:transform 250ms ease}@media (-ms-high-contrast:active){.mat-progress-bar .mat-progress-bar-fill{border-top:solid 4px}}.mat-progress-bar .mat-progress-bar-fill::after{animation:none;content:'';display:inline-block;left:0}.mat-progress-bar[dir=rtl],[dir=rtl] .mat-progress-bar{transform:rotateY(180deg)}.mat-progress-bar[mode=query]{transform:rotateZ(180deg)}.mat-progress-bar[mode=query][dir=rtl],[dir=rtl] .mat-progress-bar[mode=query]{transform:rotateZ(180deg) rotateY(180deg)}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-fill,.mat-progress-bar[mode=query] .mat-progress-bar-fill{transition:none}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary,.mat-progress-bar[mode=query] .mat-progress-bar-primary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-translate 2s infinite linear;left:-145.166611%}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-primary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary,.mat-progress-bar[mode=query] .mat-progress-bar-secondary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-translate 2s infinite linear;left:-54.888891%;display:block}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-secondary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=buffer] .mat-progress-bar-background{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-background-scroll 250ms infinite linear;display:block}.mat-progress-bar._mat-animation-noopable .mat-progress-bar-background,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-buffer,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary.mat-progress-bar-fill::after{animation:none;transition:none}@keyframes mat-progress-bar-primary-indeterminate-translate{0%{transform:translateX(0)}20%{animation-timing-function:cubic-bezier(.5,0,.70173,.49582);transform:translateX(0)}59.15%{animation-timing-function:cubic-bezier(.30244,.38135,.55,.95635);transform:translateX(83.67142%)}100%{transform:translateX(200.61106%)}}@keyframes mat-progress-bar-primary-indeterminate-scale{0%{transform:scaleX(.08)}36.65%{animation-timing-function:cubic-bezier(.33473,.12482,.78584,1);transform:scaleX(.08)}69.15%{animation-timing-function:cubic-bezier(.06,.11,.6,1);transform:scaleX(.66148)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-secondary-indeterminate-translate{0%{animation-timing-function:cubic-bezier(.15,0,.51506,.40969);transform:translateX(0)}25%{animation-timing-function:cubic-bezier(.31033,.28406,.8,.73371);transform:translateX(37.65191%)}48.35%{animation-timing-function:cubic-bezier(.4,.62704,.6,.90203);transform:translateX(84.38617%)}100%{transform:translateX(160.27778%)}}@keyframes mat-progress-bar-secondary-indeterminate-scale{0%{animation-timing-function:cubic-bezier(.15,0,.51506,.40969);transform:scaleX(.08)}19.15%{animation-timing-function:cubic-bezier(.31033,.28406,.8,.73371);transform:scaleX(.4571)}44.15%{animation-timing-function:cubic-bezier(.4,.62704,.6,.90203);transform:scaleX(.72796)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-background-scroll{to{transform:translateX(-8px)}}"],
  231. changeDetection: ChangeDetectionStrategy.OnPush,
  232. encapsulation: ViewEncapsulation.None,
  233. },] },
  234. ];
  235. /** @nocollapse */
  236. MatProgressBar.ctorParameters = () => [
  237. { type: ElementRef },
  238. { type: NgZone },
  239. { type: String, decorators: [{ type: Optional }, { type: Inject, args: [ANIMATION_MODULE_TYPE,] }] },
  240. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_PROGRESS_BAR_LOCATION,] }] }
  241. ];
  242. MatProgressBar.propDecorators = {
  243. value: [{ type: Input }],
  244. bufferValue: [{ type: Input }],
  245. _primaryValueBar: [{ type: ViewChild, args: ['primaryValueBar', { static: false },] }],
  246. animationEnd: [{ type: Output }],
  247. mode: [{ type: Input }]
  248. };
  249. /**
  250. * Clamps a value to be between two numbers, by default 0 and 100.
  251. * @param {?} v
  252. * @param {?=} min
  253. * @param {?=} max
  254. * @return {?}
  255. */
  256. function clamp(v, min = 0, max = 100) {
  257. return Math.max(min, Math.min(max, v));
  258. }
  259. /**
  260. * @fileoverview added by tsickle
  261. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  262. */
  263. class MatProgressBarModule {
  264. }
  265. MatProgressBarModule.decorators = [
  266. { type: NgModule, args: [{
  267. imports: [CommonModule, MatCommonModule],
  268. exports: [MatProgressBar, MatCommonModule],
  269. declarations: [MatProgressBar],
  270. },] },
  271. ];
  272. /**
  273. * @fileoverview added by tsickle
  274. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  275. */
  276. /**
  277. * @fileoverview added by tsickle
  278. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  279. */
  280. export { MatProgressBarModule, MAT_PROGRESS_BAR_LOCATION_FACTORY, MAT_PROGRESS_BAR_LOCATION, MatProgressBar };
  281. //# sourceMappingURL=progress-bar.js.map