snack-bar.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 { Subject } from 'rxjs';
  9. import { InjectionToken, Component, ViewEncapsulation, Inject, ChangeDetectionStrategy, ChangeDetectorRef, ElementRef, NgZone, ViewChild, NgModule, Injectable, Injector, Optional, SkipSelf, TemplateRef, ɵɵdefineInjectable, ɵɵinject, INJECTOR } from '@angular/core';
  10. import { animate, state, style, transition, trigger } from '@angular/animations';
  11. import { BasePortalOutlet, CdkPortalOutlet, PortalModule, ComponentPortal, PortalInjector, TemplatePortal } from '@angular/cdk/portal';
  12. import { take, takeUntil } from 'rxjs/operators';
  13. import { OverlayModule, Overlay, OverlayConfig } from '@angular/cdk/overlay';
  14. import { CommonModule } from '@angular/common';
  15. import { MatCommonModule } from '@angular/material/core';
  16. import { MatButtonModule } from '@angular/material/button';
  17. import { LiveAnnouncer } from '@angular/cdk/a11y';
  18. import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
  19. /**
  20. * @fileoverview added by tsickle
  21. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  22. */
  23. /**
  24. * Reference to a snack bar dispatched from the snack bar service.
  25. * @template T
  26. */
  27. class MatSnackBarRef {
  28. /**
  29. * @param {?} containerInstance
  30. * @param {?} _overlayRef
  31. */
  32. constructor(containerInstance, _overlayRef) {
  33. this._overlayRef = _overlayRef;
  34. /**
  35. * Subject for notifying the user that the snack bar has been dismissed.
  36. */
  37. this._afterDismissed = new Subject();
  38. /**
  39. * Subject for notifying the user that the snack bar has opened and appeared.
  40. */
  41. this._afterOpened = new Subject();
  42. /**
  43. * Subject for notifying the user that the snack bar action was called.
  44. */
  45. this._onAction = new Subject();
  46. /**
  47. * Whether the snack bar was dismissed using the action button.
  48. */
  49. this._dismissedByAction = false;
  50. this.containerInstance = containerInstance;
  51. // Dismiss snackbar on action.
  52. this.onAction().subscribe((/**
  53. * @return {?}
  54. */
  55. () => this.dismiss()));
  56. containerInstance._onExit.subscribe((/**
  57. * @return {?}
  58. */
  59. () => this._finishDismiss()));
  60. }
  61. /**
  62. * Dismisses the snack bar.
  63. * @return {?}
  64. */
  65. dismiss() {
  66. if (!this._afterDismissed.closed) {
  67. this.containerInstance.exit();
  68. }
  69. clearTimeout(this._durationTimeoutId);
  70. }
  71. /**
  72. * Marks the snackbar action clicked.
  73. * @return {?}
  74. */
  75. dismissWithAction() {
  76. if (!this._onAction.closed) {
  77. this._dismissedByAction = true;
  78. this._onAction.next();
  79. this._onAction.complete();
  80. }
  81. }
  82. /**
  83. * Marks the snackbar action clicked.
  84. * @deprecated Use `dismissWithAction` instead.
  85. * \@breaking-change 8.0.0
  86. * @return {?}
  87. */
  88. closeWithAction() {
  89. this.dismissWithAction();
  90. }
  91. /**
  92. * Dismisses the snack bar after some duration
  93. * @param {?} duration
  94. * @return {?}
  95. */
  96. _dismissAfter(duration) {
  97. this._durationTimeoutId = setTimeout((/**
  98. * @return {?}
  99. */
  100. () => this.dismiss()), duration);
  101. }
  102. /**
  103. * Marks the snackbar as opened
  104. * @return {?}
  105. */
  106. _open() {
  107. if (!this._afterOpened.closed) {
  108. this._afterOpened.next();
  109. this._afterOpened.complete();
  110. }
  111. }
  112. /**
  113. * Cleans up the DOM after closing.
  114. * @private
  115. * @return {?}
  116. */
  117. _finishDismiss() {
  118. this._overlayRef.dispose();
  119. if (!this._onAction.closed) {
  120. this._onAction.complete();
  121. }
  122. this._afterDismissed.next({ dismissedByAction: this._dismissedByAction });
  123. this._afterDismissed.complete();
  124. this._dismissedByAction = false;
  125. }
  126. /**
  127. * Gets an observable that is notified when the snack bar is finished closing.
  128. * @return {?}
  129. */
  130. afterDismissed() {
  131. return this._afterDismissed.asObservable();
  132. }
  133. /**
  134. * Gets an observable that is notified when the snack bar has opened and appeared.
  135. * @return {?}
  136. */
  137. afterOpened() {
  138. return this.containerInstance._onEnter;
  139. }
  140. /**
  141. * Gets an observable that is notified when the snack bar action is called.
  142. * @return {?}
  143. */
  144. onAction() {
  145. return this._onAction.asObservable();
  146. }
  147. }
  148. /**
  149. * @fileoverview added by tsickle
  150. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  151. */
  152. /**
  153. * Injection token that can be used to access the data that was passed in to a snack bar.
  154. * @type {?}
  155. */
  156. const MAT_SNACK_BAR_DATA = new InjectionToken('MatSnackBarData');
  157. /**
  158. * Configuration used when opening a snack-bar.
  159. * @template D
  160. */
  161. class MatSnackBarConfig {
  162. constructor() {
  163. /**
  164. * The politeness level for the MatAriaLiveAnnouncer announcement.
  165. */
  166. this.politeness = 'assertive';
  167. /**
  168. * Message to be announced by the LiveAnnouncer. When opening a snackbar without a custom
  169. * component or template, the announcement message will default to the specified message.
  170. */
  171. this.announcementMessage = '';
  172. /**
  173. * The length of time in milliseconds to wait before automatically dismissing the snack bar.
  174. */
  175. this.duration = 0;
  176. /**
  177. * Data being injected into the child component.
  178. */
  179. this.data = null;
  180. /**
  181. * The horizontal position to place the snack bar.
  182. */
  183. this.horizontalPosition = 'center';
  184. /**
  185. * The vertical position to place the snack bar.
  186. */
  187. this.verticalPosition = 'bottom';
  188. }
  189. }
  190. /**
  191. * @fileoverview added by tsickle
  192. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  193. */
  194. /**
  195. * A component used to open as the default snack bar, matching material spec.
  196. * This should only be used internally by the snack bar service.
  197. */
  198. class SimpleSnackBar {
  199. /**
  200. * @param {?} snackBarRef
  201. * @param {?} data
  202. */
  203. constructor(snackBarRef, data) {
  204. this.snackBarRef = snackBarRef;
  205. this.data = data;
  206. }
  207. /**
  208. * Performs the action on the snack bar.
  209. * @return {?}
  210. */
  211. action() {
  212. this.snackBarRef.dismissWithAction();
  213. }
  214. /**
  215. * If the action button should be shown.
  216. * @return {?}
  217. */
  218. get hasAction() {
  219. return !!this.data.action;
  220. }
  221. }
  222. SimpleSnackBar.decorators = [
  223. { type: Component, args: [{selector: 'simple-snack-bar',
  224. template: "<span>{{data.message}}</span><div class=\"mat-simple-snackbar-action\" *ngIf=\"hasAction\"><button mat-button (click)=\"action()\">{{data.action}}</button></div>",
  225. styles: [".mat-simple-snackbar{display:flex;justify-content:space-between;align-items:center;height:100%;line-height:20px;opacity:1}.mat-simple-snackbar-action{flex-shrink:0;margin:-8px -8px -8px 8px}.mat-simple-snackbar-action button{max-height:36px;min-width:0}[dir=rtl] .mat-simple-snackbar-action{margin-left:-8px;margin-right:8px}"],
  226. encapsulation: ViewEncapsulation.None,
  227. changeDetection: ChangeDetectionStrategy.OnPush,
  228. host: {
  229. 'class': 'mat-simple-snackbar',
  230. }
  231. },] },
  232. ];
  233. /** @nocollapse */
  234. SimpleSnackBar.ctorParameters = () => [
  235. { type: MatSnackBarRef },
  236. { type: undefined, decorators: [{ type: Inject, args: [MAT_SNACK_BAR_DATA,] }] }
  237. ];
  238. /**
  239. * @fileoverview added by tsickle
  240. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  241. */
  242. /**
  243. * Animations used by the Material snack bar.
  244. * \@docs-private
  245. * @type {?}
  246. */
  247. const matSnackBarAnimations = {
  248. /**
  249. * Animation that shows and hides a snack bar.
  250. */
  251. snackBarState: trigger('state', [
  252. state('void, hidden', style({
  253. transform: 'scale(0.8)',
  254. opacity: 0,
  255. })),
  256. state('visible', style({
  257. transform: 'scale(1)',
  258. opacity: 1,
  259. })),
  260. transition('* => visible', animate('150ms cubic-bezier(0, 0, 0.2, 1)')),
  261. transition('* => void, * => hidden', animate('75ms cubic-bezier(0.4, 0.0, 1, 1)', style({
  262. opacity: 0
  263. }))),
  264. ])
  265. };
  266. /**
  267. * @fileoverview added by tsickle
  268. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  269. */
  270. /**
  271. * Internal component that wraps user-provided snack bar content.
  272. * \@docs-private
  273. */
  274. class MatSnackBarContainer extends BasePortalOutlet {
  275. /**
  276. * @param {?} _ngZone
  277. * @param {?} _elementRef
  278. * @param {?} _changeDetectorRef
  279. * @param {?} snackBarConfig
  280. */
  281. constructor(_ngZone, _elementRef, _changeDetectorRef, snackBarConfig) {
  282. super();
  283. this._ngZone = _ngZone;
  284. this._elementRef = _elementRef;
  285. this._changeDetectorRef = _changeDetectorRef;
  286. this.snackBarConfig = snackBarConfig;
  287. /**
  288. * Whether the component has been destroyed.
  289. */
  290. this._destroyed = false;
  291. /**
  292. * Subject for notifying that the snack bar has exited from view.
  293. */
  294. this._onExit = new Subject();
  295. /**
  296. * Subject for notifying that the snack bar has finished entering the view.
  297. */
  298. this._onEnter = new Subject();
  299. /**
  300. * The state of the snack bar animations.
  301. */
  302. this._animationState = 'void';
  303. // Based on the ARIA spec, `alert` and `status` roles have an
  304. // implicit `assertive` and `polite` politeness respectively.
  305. if (snackBarConfig.politeness === 'assertive' && !snackBarConfig.announcementMessage) {
  306. this._role = 'alert';
  307. }
  308. else if (snackBarConfig.politeness === 'off') {
  309. this._role = null;
  310. }
  311. else {
  312. this._role = 'status';
  313. }
  314. }
  315. /**
  316. * Attach a component portal as content to this snack bar container.
  317. * @template T
  318. * @param {?} portal
  319. * @return {?}
  320. */
  321. attachComponentPortal(portal) {
  322. this._assertNotAttached();
  323. this._applySnackBarClasses();
  324. return this._portalOutlet.attachComponentPortal(portal);
  325. }
  326. /**
  327. * Attach a template portal as content to this snack bar container.
  328. * @template C
  329. * @param {?} portal
  330. * @return {?}
  331. */
  332. attachTemplatePortal(portal) {
  333. this._assertNotAttached();
  334. this._applySnackBarClasses();
  335. return this._portalOutlet.attachTemplatePortal(portal);
  336. }
  337. /**
  338. * Handle end of animations, updating the state of the snackbar.
  339. * @param {?} event
  340. * @return {?}
  341. */
  342. onAnimationEnd(event) {
  343. const { fromState, toState } = event;
  344. if ((toState === 'void' && fromState !== 'void') || toState === 'hidden') {
  345. this._completeExit();
  346. }
  347. if (toState === 'visible') {
  348. // Note: we shouldn't use `this` inside the zone callback,
  349. // because it can cause a memory leak.
  350. /** @type {?} */
  351. const onEnter = this._onEnter;
  352. this._ngZone.run((/**
  353. * @return {?}
  354. */
  355. () => {
  356. onEnter.next();
  357. onEnter.complete();
  358. }));
  359. }
  360. }
  361. /**
  362. * Begin animation of snack bar entrance into view.
  363. * @return {?}
  364. */
  365. enter() {
  366. if (!this._destroyed) {
  367. this._animationState = 'visible';
  368. this._changeDetectorRef.detectChanges();
  369. }
  370. }
  371. /**
  372. * Begin animation of the snack bar exiting from view.
  373. * @return {?}
  374. */
  375. exit() {
  376. // Note: this one transitions to `hidden`, rather than `void`, in order to handle the case
  377. // where multiple snack bars are opened in quick succession (e.g. two consecutive calls to
  378. // `MatSnackBar.open`).
  379. this._animationState = 'hidden';
  380. return this._onExit;
  381. }
  382. /**
  383. * Makes sure the exit callbacks have been invoked when the element is destroyed.
  384. * @return {?}
  385. */
  386. ngOnDestroy() {
  387. this._destroyed = true;
  388. this._completeExit();
  389. }
  390. /**
  391. * Waits for the zone to settle before removing the element. Helps prevent
  392. * errors where we end up removing an element which is in the middle of an animation.
  393. * @private
  394. * @return {?}
  395. */
  396. _completeExit() {
  397. this._ngZone.onMicrotaskEmpty.asObservable().pipe(take(1)).subscribe((/**
  398. * @return {?}
  399. */
  400. () => {
  401. this._onExit.next();
  402. this._onExit.complete();
  403. }));
  404. }
  405. /**
  406. * Applies the various positioning and user-configured CSS classes to the snack bar.
  407. * @private
  408. * @return {?}
  409. */
  410. _applySnackBarClasses() {
  411. /** @type {?} */
  412. const element = this._elementRef.nativeElement;
  413. /** @type {?} */
  414. const panelClasses = this.snackBarConfig.panelClass;
  415. if (panelClasses) {
  416. if (Array.isArray(panelClasses)) {
  417. // Note that we can't use a spread here, because IE doesn't support multiple arguments.
  418. panelClasses.forEach((/**
  419. * @param {?} cssClass
  420. * @return {?}
  421. */
  422. cssClass => element.classList.add(cssClass)));
  423. }
  424. else {
  425. element.classList.add(panelClasses);
  426. }
  427. }
  428. if (this.snackBarConfig.horizontalPosition === 'center') {
  429. element.classList.add('mat-snack-bar-center');
  430. }
  431. if (this.snackBarConfig.verticalPosition === 'top') {
  432. element.classList.add('mat-snack-bar-top');
  433. }
  434. }
  435. /**
  436. * Asserts that no content is already attached to the container.
  437. * @private
  438. * @return {?}
  439. */
  440. _assertNotAttached() {
  441. if (this._portalOutlet.hasAttached()) {
  442. throw Error('Attempting to attach snack bar content after content is already attached');
  443. }
  444. }
  445. }
  446. MatSnackBarContainer.decorators = [
  447. { type: Component, args: [{selector: 'snack-bar-container',
  448. template: "<ng-template cdkPortalOutlet></ng-template>",
  449. styles: [".mat-snack-bar-container{border-radius:4px;box-sizing:border-box;display:block;margin:24px;max-width:33vw;min-width:344px;padding:14px 16px;min-height:48px;transform-origin:center}@media (-ms-high-contrast:active){.mat-snack-bar-container{border:solid 1px}}.mat-snack-bar-handset{width:100%}.mat-snack-bar-handset .mat-snack-bar-container{margin:8px;max-width:100%;min-width:0;width:100%}"],
  450. // In Ivy embedded views will be change detected from their declaration place, rather than
  451. // where they were stamped out. This means that we can't have the snack bar container be OnPush,
  452. // because it might cause snack bars that were opened from a template not to be out of date.
  453. // tslint:disable-next-line:validate-decorators
  454. changeDetection: ChangeDetectionStrategy.Default,
  455. encapsulation: ViewEncapsulation.None,
  456. animations: [matSnackBarAnimations.snackBarState],
  457. host: {
  458. '[attr.role]': '_role',
  459. 'class': 'mat-snack-bar-container',
  460. '[@state]': '_animationState',
  461. '(@state.done)': 'onAnimationEnd($event)'
  462. },
  463. },] },
  464. ];
  465. /** @nocollapse */
  466. MatSnackBarContainer.ctorParameters = () => [
  467. { type: NgZone },
  468. { type: ElementRef },
  469. { type: ChangeDetectorRef },
  470. { type: MatSnackBarConfig }
  471. ];
  472. MatSnackBarContainer.propDecorators = {
  473. _portalOutlet: [{ type: ViewChild, args: [CdkPortalOutlet, { static: true },] }]
  474. };
  475. /**
  476. * @fileoverview added by tsickle
  477. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  478. */
  479. class MatSnackBarModule {
  480. }
  481. MatSnackBarModule.decorators = [
  482. { type: NgModule, args: [{
  483. imports: [
  484. OverlayModule,
  485. PortalModule,
  486. CommonModule,
  487. MatButtonModule,
  488. MatCommonModule,
  489. ],
  490. exports: [MatSnackBarContainer, MatCommonModule],
  491. declarations: [MatSnackBarContainer, SimpleSnackBar],
  492. entryComponents: [MatSnackBarContainer, SimpleSnackBar],
  493. },] },
  494. ];
  495. /**
  496. * @fileoverview added by tsickle
  497. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  498. */
  499. /**
  500. * Injection token that can be used to specify default snack bar.
  501. * @type {?}
  502. */
  503. const MAT_SNACK_BAR_DEFAULT_OPTIONS = new InjectionToken('mat-snack-bar-default-options', {
  504. providedIn: 'root',
  505. factory: MAT_SNACK_BAR_DEFAULT_OPTIONS_FACTORY,
  506. });
  507. /**
  508. * \@docs-private
  509. * @return {?}
  510. */
  511. function MAT_SNACK_BAR_DEFAULT_OPTIONS_FACTORY() {
  512. return new MatSnackBarConfig();
  513. }
  514. /**
  515. * Service to dispatch Material Design snack bar messages.
  516. */
  517. class MatSnackBar {
  518. /**
  519. * @param {?} _overlay
  520. * @param {?} _live
  521. * @param {?} _injector
  522. * @param {?} _breakpointObserver
  523. * @param {?} _parentSnackBar
  524. * @param {?} _defaultConfig
  525. */
  526. constructor(_overlay, _live, _injector, _breakpointObserver, _parentSnackBar, _defaultConfig) {
  527. this._overlay = _overlay;
  528. this._live = _live;
  529. this._injector = _injector;
  530. this._breakpointObserver = _breakpointObserver;
  531. this._parentSnackBar = _parentSnackBar;
  532. this._defaultConfig = _defaultConfig;
  533. /**
  534. * Reference to the current snack bar in the view *at this level* (in the Angular injector tree).
  535. * If there is a parent snack-bar service, all operations should delegate to that parent
  536. * via `_openedSnackBarRef`.
  537. */
  538. this._snackBarRefAtThisLevel = null;
  539. }
  540. /**
  541. * Reference to the currently opened snackbar at *any* level.
  542. * @return {?}
  543. */
  544. get _openedSnackBarRef() {
  545. /** @type {?} */
  546. const parent = this._parentSnackBar;
  547. return parent ? parent._openedSnackBarRef : this._snackBarRefAtThisLevel;
  548. }
  549. /**
  550. * @param {?} value
  551. * @return {?}
  552. */
  553. set _openedSnackBarRef(value) {
  554. if (this._parentSnackBar) {
  555. this._parentSnackBar._openedSnackBarRef = value;
  556. }
  557. else {
  558. this._snackBarRefAtThisLevel = value;
  559. }
  560. }
  561. /**
  562. * Creates and dispatches a snack bar with a custom component for the content, removing any
  563. * currently opened snack bars.
  564. *
  565. * @template T
  566. * @param {?} component Component to be instantiated.
  567. * @param {?=} config Extra configuration for the snack bar.
  568. * @return {?}
  569. */
  570. openFromComponent(component, config) {
  571. return (/** @type {?} */ (this._attach(component, config)));
  572. }
  573. /**
  574. * Creates and dispatches a snack bar with a custom template for the content, removing any
  575. * currently opened snack bars.
  576. *
  577. * @param {?} template Template to be instantiated.
  578. * @param {?=} config Extra configuration for the snack bar.
  579. * @return {?}
  580. */
  581. openFromTemplate(template, config) {
  582. return this._attach(template, config);
  583. }
  584. /**
  585. * Opens a snackbar with a message and an optional action.
  586. * @param {?} message The message to show in the snackbar.
  587. * @param {?=} action The label for the snackbar action.
  588. * @param {?=} config Additional configuration options for the snackbar.
  589. * @return {?}
  590. */
  591. open(message, action = '', config) {
  592. /** @type {?} */
  593. const _config = Object.assign({}, this._defaultConfig, config);
  594. // Since the user doesn't have access to the component, we can
  595. // override the data to pass in our own message and action.
  596. _config.data = { message, action };
  597. if (!_config.announcementMessage) {
  598. _config.announcementMessage = message;
  599. }
  600. return this.openFromComponent(SimpleSnackBar, _config);
  601. }
  602. /**
  603. * Dismisses the currently-visible snack bar.
  604. * @return {?}
  605. */
  606. dismiss() {
  607. if (this._openedSnackBarRef) {
  608. this._openedSnackBarRef.dismiss();
  609. }
  610. }
  611. /**
  612. * @return {?}
  613. */
  614. ngOnDestroy() {
  615. // Only dismiss the snack bar at the current level on destroy.
  616. if (this._snackBarRefAtThisLevel) {
  617. this._snackBarRefAtThisLevel.dismiss();
  618. }
  619. }
  620. /**
  621. * Attaches the snack bar container component to the overlay.
  622. * @private
  623. * @param {?} overlayRef
  624. * @param {?} config
  625. * @return {?}
  626. */
  627. _attachSnackBarContainer(overlayRef, config) {
  628. /** @type {?} */
  629. const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
  630. /** @type {?} */
  631. const injector = new PortalInjector(userInjector || this._injector, new WeakMap([
  632. [MatSnackBarConfig, config]
  633. ]));
  634. /** @type {?} */
  635. const containerPortal = new ComponentPortal(MatSnackBarContainer, config.viewContainerRef, injector);
  636. /** @type {?} */
  637. const containerRef = overlayRef.attach(containerPortal);
  638. containerRef.instance.snackBarConfig = config;
  639. return containerRef.instance;
  640. }
  641. /**
  642. * Places a new component or a template as the content of the snack bar container.
  643. * @private
  644. * @template T
  645. * @param {?} content
  646. * @param {?=} userConfig
  647. * @return {?}
  648. */
  649. _attach(content, userConfig) {
  650. /** @type {?} */
  651. const config = Object.assign({}, new MatSnackBarConfig(), this._defaultConfig, userConfig);
  652. /** @type {?} */
  653. const overlayRef = this._createOverlay(config);
  654. /** @type {?} */
  655. const container = this._attachSnackBarContainer(overlayRef, config);
  656. /** @type {?} */
  657. const snackBarRef = new MatSnackBarRef(container, overlayRef);
  658. if (content instanceof TemplateRef) {
  659. /** @type {?} */
  660. const portal = new TemplatePortal(content, (/** @type {?} */ (null)), (/** @type {?} */ ({
  661. $implicit: config.data,
  662. snackBarRef
  663. })));
  664. snackBarRef.instance = container.attachTemplatePortal(portal);
  665. }
  666. else {
  667. /** @type {?} */
  668. const injector = this._createInjector(config, snackBarRef);
  669. /** @type {?} */
  670. const portal = new ComponentPortal(content, undefined, injector);
  671. /** @type {?} */
  672. const contentRef = container.attachComponentPortal(portal);
  673. // We can't pass this via the injector, because the injector is created earlier.
  674. snackBarRef.instance = contentRef.instance;
  675. }
  676. // Subscribe to the breakpoint observer and attach the mat-snack-bar-handset class as
  677. // appropriate. This class is applied to the overlay element because the overlay must expand to
  678. // fill the width of the screen for full width snackbars.
  679. this._breakpointObserver.observe(Breakpoints.Handset).pipe(takeUntil(overlayRef.detachments().pipe(take(1)))).subscribe((/**
  680. * @param {?} state
  681. * @return {?}
  682. */
  683. state$$1 => {
  684. if (state$$1.matches) {
  685. overlayRef.overlayElement.classList.add('mat-snack-bar-handset');
  686. }
  687. else {
  688. overlayRef.overlayElement.classList.remove('mat-snack-bar-handset');
  689. }
  690. }));
  691. this._animateSnackBar(snackBarRef, config);
  692. this._openedSnackBarRef = snackBarRef;
  693. return this._openedSnackBarRef;
  694. }
  695. /**
  696. * Animates the old snack bar out and the new one in.
  697. * @private
  698. * @param {?} snackBarRef
  699. * @param {?} config
  700. * @return {?}
  701. */
  702. _animateSnackBar(snackBarRef, config) {
  703. // When the snackbar is dismissed, clear the reference to it.
  704. snackBarRef.afterDismissed().subscribe((/**
  705. * @return {?}
  706. */
  707. () => {
  708. // Clear the snackbar ref if it hasn't already been replaced by a newer snackbar.
  709. if (this._openedSnackBarRef == snackBarRef) {
  710. this._openedSnackBarRef = null;
  711. }
  712. if (config.announcementMessage) {
  713. this._live.clear();
  714. }
  715. }));
  716. if (this._openedSnackBarRef) {
  717. // If a snack bar is already in view, dismiss it and enter the
  718. // new snack bar after exit animation is complete.
  719. this._openedSnackBarRef.afterDismissed().subscribe((/**
  720. * @return {?}
  721. */
  722. () => {
  723. snackBarRef.containerInstance.enter();
  724. }));
  725. this._openedSnackBarRef.dismiss();
  726. }
  727. else {
  728. // If no snack bar is in view, enter the new snack bar.
  729. snackBarRef.containerInstance.enter();
  730. }
  731. // If a dismiss timeout is provided, set up dismiss based on after the snackbar is opened.
  732. if (config.duration && config.duration > 0) {
  733. snackBarRef.afterOpened().subscribe((/**
  734. * @return {?}
  735. */
  736. () => snackBarRef._dismissAfter((/** @type {?} */ (config.duration)))));
  737. }
  738. if (config.announcementMessage) {
  739. this._live.announce(config.announcementMessage, config.politeness);
  740. }
  741. }
  742. /**
  743. * Creates a new overlay and places it in the correct location.
  744. * @private
  745. * @param {?} config The user-specified snack bar config.
  746. * @return {?}
  747. */
  748. _createOverlay(config) {
  749. /** @type {?} */
  750. const overlayConfig = new OverlayConfig();
  751. overlayConfig.direction = config.direction;
  752. /** @type {?} */
  753. let positionStrategy = this._overlay.position().global();
  754. // Set horizontal position.
  755. /** @type {?} */
  756. const isRtl = config.direction === 'rtl';
  757. /** @type {?} */
  758. const isLeft = (config.horizontalPosition === 'left' ||
  759. (config.horizontalPosition === 'start' && !isRtl) ||
  760. (config.horizontalPosition === 'end' && isRtl));
  761. /** @type {?} */
  762. const isRight = !isLeft && config.horizontalPosition !== 'center';
  763. if (isLeft) {
  764. positionStrategy.left('0');
  765. }
  766. else if (isRight) {
  767. positionStrategy.right('0');
  768. }
  769. else {
  770. positionStrategy.centerHorizontally();
  771. }
  772. // Set horizontal position.
  773. if (config.verticalPosition === 'top') {
  774. positionStrategy.top('0');
  775. }
  776. else {
  777. positionStrategy.bottom('0');
  778. }
  779. overlayConfig.positionStrategy = positionStrategy;
  780. return this._overlay.create(overlayConfig);
  781. }
  782. /**
  783. * Creates an injector to be used inside of a snack bar component.
  784. * @private
  785. * @template T
  786. * @param {?} config Config that was used to create the snack bar.
  787. * @param {?} snackBarRef Reference to the snack bar.
  788. * @return {?}
  789. */
  790. _createInjector(config, snackBarRef) {
  791. /** @type {?} */
  792. const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
  793. return new PortalInjector(userInjector || this._injector, new WeakMap([
  794. [MatSnackBarRef, snackBarRef],
  795. [MAT_SNACK_BAR_DATA, config.data]
  796. ]));
  797. }
  798. }
  799. MatSnackBar.decorators = [
  800. { type: Injectable, args: [{ providedIn: MatSnackBarModule },] },
  801. ];
  802. /** @nocollapse */
  803. MatSnackBar.ctorParameters = () => [
  804. { type: Overlay },
  805. { type: LiveAnnouncer },
  806. { type: Injector },
  807. { type: BreakpointObserver },
  808. { type: MatSnackBar, decorators: [{ type: Optional }, { type: SkipSelf }] },
  809. { type: MatSnackBarConfig, decorators: [{ type: Inject, args: [MAT_SNACK_BAR_DEFAULT_OPTIONS,] }] }
  810. ];
  811. /** @nocollapse */ MatSnackBar.ngInjectableDef = ɵɵdefineInjectable({ factory: function MatSnackBar_Factory() { return new MatSnackBar(ɵɵinject(Overlay), ɵɵinject(LiveAnnouncer), ɵɵinject(INJECTOR), ɵɵinject(BreakpointObserver), ɵɵinject(MatSnackBar, 12), ɵɵinject(MAT_SNACK_BAR_DEFAULT_OPTIONS)); }, token: MatSnackBar, providedIn: MatSnackBarModule });
  812. /**
  813. * @fileoverview added by tsickle
  814. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  815. */
  816. /**
  817. * @fileoverview added by tsickle
  818. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  819. */
  820. export { MatSnackBarModule, MAT_SNACK_BAR_DEFAULT_OPTIONS_FACTORY, MAT_SNACK_BAR_DEFAULT_OPTIONS, MatSnackBar, MatSnackBarContainer, MAT_SNACK_BAR_DATA, MatSnackBarConfig, MatSnackBarRef, SimpleSnackBar, matSnackBarAnimations };
  821. //# sourceMappingURL=snack-bar.js.map