my-lib/icon/icon.service.ts
Properties |
Methods |
constructor(injector: Injector)
|
||||||||
Defined in my-lib/icon/icon.service.ts:13
|
||||||||
Parameters :
|
Private _unique |
_unique()
|
Defined in my-lib/icon/icon.service.ts:95
|
Returns :
void
|
addIcon | ||||||||
addIcon(icon: any)
|
||||||||
Defined in my-lib/icon/icon.service.ts:56
|
||||||||
Parameters :
Returns :
void
|
checkExist | ||||||||
checkExist(name: string)
|
||||||||
Defined in my-lib/icon/icon.service.ts:36
|
||||||||
Parameters :
Returns :
boolean
|
checkLoaded | ||||||||
checkLoaded(naem: string)
|
||||||||
Defined in my-lib/icon/icon.service.ts:46
|
||||||||
Parameters :
Returns :
boolean
|
loadIcon | ||||||||
loadIcon(name: string)
|
||||||||
Defined in my-lib/icon/icon.service.ts:109
|
||||||||
Parameters :
Returns :
Observable<literal type>
|
loadSprite |
loadSprite()
|
Defined in my-lib/icon/icon.service.ts:84
|
Returns :
void
|
loadSvg |
loadSvg()
|
Defined in my-lib/icon/icon.service.ts:114
|
Returns :
void
|
renderSvgSprite |
renderSvgSprite()
|
Defined in my-lib/icon/icon.service.ts:72
|
Returns :
string
|
strToJson | ||||||||
strToJson(str: string)
|
||||||||
Defined in my-lib/icon/icon.service.ts:31
|
||||||||
Parameters :
Returns :
any
|
svgSprite | ||||||||
svgSprite(contents: any)
|
||||||||
Defined in my-lib/icon/icon.service.ts:58
|
||||||||
Parameters :
Returns :
string
|
allIcons |
allIcons:
|
Type : any[]
|
Default value : []
|
Defined in my-lib/icon/icon.service.ts:11
|
http |
http:
|
Type : HttpClient
|
Defined in my-lib/icon/icon.service.ts:10
|
icons |
icons:
|
Type : any
|
Defined in my-lib/icon/icon.service.ts:9
|
Public injector |
injector:
|
Type : Injector
|
Defined in my-lib/icon/icon.service.ts:15
|
key |
key:
|
Type : string
|
Default value : '__meepo.icons'
|
Defined in my-lib/icon/icon.service.ts:13
|
loadedIcons |
loadedIcons:
|
Type : any[]
|
Default value : []
|
Defined in my-lib/icon/icon.service.ts:12
|
root |
root:
|
Type : any
|
Defined in my-lib/icon/icon.service.ts:8
|
import { Injectable, InjectionToken, Inject, Injector, ReflectiveInjector } from '@angular/core';
export const ICONS = new InjectionToken('ICONS');
export const ICON_ROOT = new InjectionToken('ICON_ROOT');
import { HttpClient } from '@angular/common/http';
import { merge, from, of, Observable } from 'meepo-utils';
@Injectable()
export class IconStore {
root: any;
icons: any;
http: HttpClient;
allIcons: any[] = [];
loadedIcons: any[] = [];
key: string = '__meepo.icons'
constructor(
public injector: Injector
) {
// 获取缓存svgg
const icons = localStorage.getItem(this.key);
if (icons) {
this.allIcons = this.strToJson(icons);
}
this.root = this.injector.get(ICON_ROOT);
this.icons = this.injector.get(ICONS);
// 去掉重复
this._unique();
this.http = this.injector.get(HttpClient);
// 加载svg
this.loadSvg();
this.loadSprite();
}
strToJson(str: string) {
const json = (new Function("return " + str))();
return json;
}
checkExist(name: string) {
let has = false;
this.allIcons.map(res => {
if (res.name === name) {
has = true;
}
});
return has;
}
checkLoaded(naem: string) {
let has = false;
this.loadedIcons.map(res => {
if (res.name === name) {
has = true;
}
});
return has;
}
addIcon(icon: any) { }
svgSprite(contents: any) {
return `
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
id="__ANTD_MOBILE_SVG_SPRITE_NODE__"
style="position:absolute;width:0;height:0"
>
<defs>
${contents}
</defs>
</svg>`;
}
renderSvgSprite() {
const symbol = this.allIcons.map((res: any) => {
const { name, content } = res;
if (!this.checkLoaded(name)) {
this.loadedIcons.push(res);
const svgContent = content.split('svg')[1];
return `<symbol id=${name}${svgContent}symbol>`;
}
}).join('');
return this.svgSprite(symbol);
}
loadSprite() {
if (!document) {
return;
}
const existing = document.getElementById('__ANTD_MOBILE_SVG_SPRITE_NODE__');
const mountNode = document.head;
if (!existing) {
mountNode.insertAdjacentHTML('afterbegin', this.renderSvgSprite());
}
}
// 去掉重复房间号
private _unique() {
const res = [];
const json: Object = {};
this.icons.map((icons) => {
icons.map(name => {
if (!json[name]) {
json[name] = true;
res.push(name);
}
});
});
this.icons = res;
}
// http
loadIcon(name: string): Observable<{content: any, name: string}> {
return this.http.get(this.root + name + '.svg', { responseType: 'text' })
.map(res => ({ content: res, name: name }));
}
// http
loadSvg() {
const iconses: any[] = [];
const load$: any = [];
this.icons.map(icon => {
load$.push(this.loadIcon(icon));
});
// 去掉已经加载的
of(...this.icons)
.filter(res => {
return !this.checkExist(res)
})
.subscribe((res: any) => {
iconses.push({
name: res.name,
content: res.content
});
this.allIcons = iconses;
localStorage.setItem(this.key, JSON.stringify(this.allIcons));
});
}
}