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
|
|
|
|
*/
|
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
import {Inject, Injectable} from '@angular/core';
|
|
|
|
|
2015-10-02 09:30:36 -07:00
|
|
|
import {DOCUMENT} from './dom_tokens';
|
2015-07-24 15:28:44 -07:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SharedStylesHost {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-25 12:58:00 -07:00
|
|
|
_styles: string[] = [];
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-29 11:11:06 -07:00
|
|
|
_stylesSet = new Set<string>();
|
2015-07-24 15:28:44 -07:00
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
addStyles(styles: string[]) {
|
2016-11-12 14:08:58 +01:00
|
|
|
const additions: any[] /** TODO #9100 */ = [];
|
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);
|
|
|
|
this._styles.push(style);
|
|
|
|
additions.push(style);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.onStylesAdded(additions);
|
|
|
|
}
|
|
|
|
|
2015-09-25 12:58:00 -07:00
|
|
|
onStylesAdded(additions: string[]) {}
|
2015-07-24 15:28:44 -07:00
|
|
|
|
|
|
|
getAllStyles(): string[] { return this._styles; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DomSharedStylesHost extends SharedStylesHost {
|
2015-09-29 11:11:06 -07:00
|
|
|
private _hostNodes = new Set<Node>();
|
2015-08-10 21:42:47 -07:00
|
|
|
constructor(@Inject(DOCUMENT) doc: any) {
|
2015-07-24 15:28:44 -07:00
|
|
|
super();
|
|
|
|
this._hostNodes.add(doc.head);
|
|
|
|
}
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-07-24 15:28:44 -07:00
|
|
|
_addStylesToHost(styles: string[], host: Node) {
|
2016-11-12 14:08:58 +01:00
|
|
|
for (let i = 0; i < styles.length; i++) {
|
2016-11-02 16:59:24 -07:00
|
|
|
const styleEl = document.createElement('style');
|
|
|
|
styleEl.textContent = styles[i];
|
|
|
|
host.appendChild(styleEl);
|
2015-07-24 15:28:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addHost(hostNode: Node) {
|
|
|
|
this._addStylesToHost(this._styles, hostNode);
|
|
|
|
this._hostNodes.add(hostNode);
|
|
|
|
}
|
2016-08-11 23:26:57 -07:00
|
|
|
removeHost(hostNode: Node) { this._hostNodes.delete(hostNode); }
|
2015-07-24 15:28:44 -07:00
|
|
|
|
|
|
|
onStylesAdded(additions: string[]) {
|
|
|
|
this._hostNodes.forEach((hostNode) => { this._addStylesToHost(additions, hostNode); });
|
|
|
|
}
|
|
|
|
}
|