fbb37c7ec886cdd73a5863ba4fb79ff84a84850b
[sdc/sdc-workflow-designer.git] /
1 /**
2  * @license
3  * Copyright Google Inc. 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 /* tslint:disable:array-type member-access variable-name typedef
9  only-arrow-functions directive-class-suffix component-class-suffix
10  component-selector*/
11 import {Injectable, Optional, SkipSelf} from '@angular/core';
12
13
14 /**
15  * The OverlayContainer is the container in which all overlays will load.
16  * It should be provided in the root component to ensure it is properly shared.
17  */
18 @Injectable()
19 export class OverlayContainer {
20   protected _containerElement: HTMLElement;
21
22   private _themeClass: string;
23
24   /**
25    * Base theme to be applied to all overlay-based components.
26    */
27   get themeClass(): string {
28         return this._themeClass;
29   }
30   set themeClass(value: string) {
31         if (this._containerElement) {
32                 this._containerElement.classList.remove(this._themeClass);
33
34                 if (value) {
35                 this._containerElement.classList.add(value);
36                 }
37         }
38
39         this._themeClass = value;
40   }
41
42   /**
43    * This method returns the overlay container element.  It will lazily
44    * create the element the first time  it is called to facilitate using
45    * the container in non-browser environments.
46    * @returns the container element
47    */
48   getContainerElement(): HTMLElement {
49         if (!this._containerElement) {
50                 this._createContainer();
51         }
52         return this._containerElement;
53   }
54
55   /**
56    * Create the overlay container element, which is simply a div
57    * with the 'cdk-overlay-container' class on the document body.
58    */
59   protected _createContainer(): void {
60         const container = document.createElement('div');
61         container.classList.add('nz-overlay-container');
62
63         if (this._themeClass) {
64                 container.classList.add(this._themeClass);
65         }
66
67         document.body.appendChild(container);
68         this._containerElement = container;
69   }
70 }
71
72 export function OVERLAY_CONTAINER_PROVIDER_FACTORY(
73         parentContainer: OverlayContainer) {
74   return parentContainer || new OverlayContainer();
75 }
76
77 export const OVERLAY_CONTAINER_PROVIDER = {
78   // If there is already an OverlayContainer available, use that. Otherwise,
79   // provide a new one.
80   provide: OverlayContainer,
81   deps: [[new Optional(), new SkipSelf(), OverlayContainer]],
82   useFactory: OVERLAY_CONTAINER_PROVIDER_FACTORY
83 };