2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-03-11 19:20:40 -05:00
|
|
|
import {DOCUMENT} from '@angular/common';
|
2017-01-02 15:42:05 +03:00
|
|
|
import {Inject, Injectable, OnDestroy} from '@angular/core';
|
2017-01-02 13:20:32 +03:00
|
|
|
import {getDOM} from './dom_adapter';
|
2015-07-24 15:28:44 -07:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SharedStylesHost {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2017-01-02 13:20:32 +03:00
|
|
|
protected _stylesSet = new Set<string>();
|
2015-07-24 15:28:44 -07:00
|
|
|
|
2017-01-02 13:20:32 +03:00
|
|
|
addStyles(styles: string[]): void {
|
|
|
|
const additions = new Set<string>();
|
2015-07-24 15:28:44 -07:00
|
|
|
styles.forEach(style => {
|
2016-08-11 23:26:57 -07:00
|
|
|
if (!this._stylesSet.has(style)) {
|
2015-07-24 15:28:44 -07:00
|
|
|
this._stylesSet.add(style);
|
2017-01-02 13:20:32 +03:00
|
|
|
additions.add(style);
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.onStylesAdded(additions);
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:20:32 +03:00
|
|
|
onStylesAdded(additions: Set<string>): void {}
|
2015-07-24 15:28:44 -07:00
|
|
|
|
2017-01-02 13:20:32 +03:00
|
|
|
getAllStyles(): string[] { return Array.from(this._stylesSet); }
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
2017-01-02 15:42:05 +03:00
|
|
|
export class DomSharedStylesHost extends SharedStylesHost implements OnDestroy {
|
2015-09-29 11:11:06 -07:00
|
|
|
private _hostNodes = new Set<Node>();
|
2017-01-02 15:42:05 +03:00
|
|
|
private _styleNodes = new Set<Node>();
|
2017-01-02 13:20:32 +03:00
|
|
|
constructor(@Inject(DOCUMENT) private _doc: any) {
|
2015-07-24 15:28:44 -07:00
|
|
|
super();
|
2017-01-02 13:20:32 +03:00
|
|
|
this._hostNodes.add(_doc.head);
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|
2017-01-02 13:20:32 +03:00
|
|
|
|
|
|
|
private _addStylesToHost(styles: Set<string>, host: Node): void {
|
|
|
|
styles.forEach((style: string) => {
|
|
|
|
const styleEl = this._doc.createElement('style');
|
|
|
|
styleEl.textContent = style;
|
2017-01-02 15:42:05 +03:00
|
|
|
this._styleNodes.add(host.appendChild(styleEl));
|
2017-01-02 13:20:32 +03:00
|
|
|
});
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|
2017-01-02 13:20:32 +03:00
|
|
|
|
|
|
|
addHost(hostNode: Node): void {
|
|
|
|
this._addStylesToHost(this._stylesSet, hostNode);
|
2015-07-24 15:28:44 -07:00
|
|
|
this._hostNodes.add(hostNode);
|
|
|
|
}
|
|
|
|
|
2017-01-02 13:20:32 +03:00
|
|
|
removeHost(hostNode: Node): void { this._hostNodes.delete(hostNode); }
|
|
|
|
|
|
|
|
onStylesAdded(additions: Set<string>): void {
|
|
|
|
this._hostNodes.forEach(hostNode => this._addStylesToHost(additions, hostNode));
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|
2017-01-02 15:42:05 +03:00
|
|
|
|
|
|
|
ngOnDestroy(): void { this._styleNodes.forEach(styleNode => getDOM().remove(styleNode)); }
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|