forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon.service.ts
150 lines (123 loc) · 4.64 KB
/
icon.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { Platform } from '@angular/cdk/platform';
import { DOCUMENT } from '@angular/common';
import { HttpBackend } from '@angular/common/http';
import { Inject, Injectable, InjectionToken, OnDestroy, Optional, RendererFactory2, Self } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { Subject, Subscription } from 'rxjs';
import { IconDefinition, IconService } from '@ant-design/icons-angular';
import { IconConfig, NzConfigService } from 'ng-zorro-antd/core/config';
import { warn } from 'ng-zorro-antd/core/logger';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NZ_ICONS_USED_BY_ZORRO } from './icons';
export interface NzIconfontOption {
scriptUrl: string;
}
export const NZ_ICONS = new InjectionToken('nz_icons');
export const NZ_ICON_DEFAULT_TWOTONE_COLOR = new InjectionToken('nz_icon_default_twotone_color');
export const DEFAULT_TWOTONE_COLOR = '#1890ff';
/**
* It should be a global singleton, otherwise registered icons could not be found.
*/
@Injectable({
providedIn: 'root'
})
export class NzIconService extends IconService implements OnDestroy {
configUpdated$ = new Subject<void>();
protected override get _disableDynamicLoading(): boolean {
return !this.platform.isBrowser;
}
private iconfontCache = new Set<string>();
private subscription: Subscription | null = null;
ngOnDestroy(): void {
if (this.subscription) {
this.subscription.unsubscribe();
this.subscription = null;
}
}
normalizeSvgElement(svg: SVGElement): void {
if (!svg.getAttribute('viewBox')) {
this._renderer.setAttribute(svg, 'viewBox', '0 0 1024 1024');
}
if (!svg.getAttribute('width') || !svg.getAttribute('height')) {
this._renderer.setAttribute(svg, 'width', '1em');
this._renderer.setAttribute(svg, 'height', '1em');
}
if (!svg.getAttribute('fill')) {
this._renderer.setAttribute(svg, 'fill', 'currentColor');
}
}
fetchFromIconfont(opt: NzIconfontOption): void {
const { scriptUrl } = opt;
if (this._document && !this.iconfontCache.has(scriptUrl)) {
const script = this._renderer.createElement('script');
this._renderer.setAttribute(script, 'src', scriptUrl);
this._renderer.setAttribute(script, 'data-namespace', scriptUrl.replace(/^(https?|http):/g, ''));
this._renderer.appendChild(this._document.body, script);
this.iconfontCache.add(scriptUrl);
}
}
createIconfontIcon(type: string): SVGElement {
return this._createSVGElementFromString(`<svg><use xlink:href="${type}"></svg>`);
}
constructor(
rendererFactory: RendererFactory2,
sanitizer: DomSanitizer,
protected nzConfigService: NzConfigService,
private platform: Platform,
@Optional() handler: HttpBackend,
@Optional() @Inject(DOCUMENT) _document: NzSafeAny,
@Optional() @Inject(NZ_ICONS) icons?: IconDefinition[]
) {
super(rendererFactory, handler, _document, sanitizer, [...NZ_ICONS_USED_BY_ZORRO, ...(icons || [])]);
this.onConfigChange();
this.configDefaultTwotoneColor();
this.configDefaultTheme();
}
private onConfigChange(): void {
this.subscription = this.nzConfigService.getConfigChangeEventForComponent('icon').subscribe(() => {
this.configDefaultTwotoneColor();
this.configDefaultTheme();
this.configUpdated$.next();
});
}
private configDefaultTheme(): void {
const iconConfig = this.getConfig();
this.defaultTheme = iconConfig.nzTheme || 'outline';
}
private configDefaultTwotoneColor(): void {
const iconConfig = this.getConfig();
const defaultTwotoneColor = iconConfig.nzTwotoneColor || DEFAULT_TWOTONE_COLOR;
let primaryColor = DEFAULT_TWOTONE_COLOR;
if (defaultTwotoneColor) {
if (defaultTwotoneColor.startsWith('#')) {
primaryColor = defaultTwotoneColor;
} else {
warn('Twotone color must be a hex color!');
}
}
this.twoToneColor = { primaryColor };
}
private getConfig(): IconConfig {
return this.nzConfigService.getConfigForComponent('icon') || {};
}
}
export const NZ_ICONS_PATCH = new InjectionToken('nz_icons_patch');
@Injectable()
export class NzIconPatchService {
patched = false;
constructor(
@Self() @Inject(NZ_ICONS_PATCH) private extraIcons: IconDefinition[],
private rootIconService: NzIconService
) {}
doPatch(): void {
if (this.patched) {
return;
}
this.extraIcons.forEach(iconDefinition => this.rootIconService.addIcon(iconDefinition));
this.patched = true;
}
}