Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fallback icon in case of broken link #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions lib/svg-icon-registry.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Optional, SkipSelf } from '@angular/core';
import { Inject, Injectable, Optional, SkipSelf } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
Expand All @@ -8,57 +8,63 @@ import 'rxjs/add/operator/do';
import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/share';
import 'rxjs/observable/throw';
import 'rxjs/add/operator/catch';


@Injectable()
export class SvgIconRegistryService {

private iconsByUrl = new Map<string, SVGElement>();
private iconsLoadingByUrl = new Map<string, Observable<SVGElement>>();

constructor(private http:HttpClient) {
}
private iconsByUrl = new Map<string, SVGElement>();
private iconsLoadingByUrl = new Map<string, Observable<SVGElement>>();

loadSvg(url:string): Observable<SVGElement> {
constructor(private http: HttpClient, private fallbackIconUrl: string) {
}

if (this.iconsByUrl.has(url)) {
return Observable.of(this.iconsByUrl.get(url));
} else if (this.iconsLoadingByUrl.has(url)) {
return this.iconsLoadingByUrl.get(url);
} else {
const o = <Observable<SVGElement>> this.http.get(url, { responseType: 'text' })
.map(svg => {
const div = document.createElement('DIV');
div.innerHTML = svg;
return <SVGElement>div.querySelector('svg');
})
.do(svg => {
this.iconsByUrl.set(url, svg);
})
.finally(() => {
this.iconsLoadingByUrl.delete(url);
})
.share();
loadSvg(url: string): Observable<SVGElement> {
if (this.iconsByUrl.has(url)) {
return Observable.of(this.iconsByUrl.get(url));
} else if (this.iconsLoadingByUrl.has(url)) {
return this.iconsLoadingByUrl.get(url);
} else {
const o = this.http.get(url, {responseType: 'text'})
.map(svg => {
const div = document.createElement('DIV');
div.innerHTML = svg;
return <SVGElement>div.querySelector('svg');
})
.catch((error) => {
if (this.fallbackIconUrl) {
return this.loadSvg(this.fallbackIconUrl);
} else {
return Observable.throw(error);
}
})
.do(svg => {
this.iconsByUrl.set(url, svg);
})
.finally(() => {
this.iconsLoadingByUrl.delete(url);
})
.share();
this.iconsLoadingByUrl.set(url, o);
return o;
}

this.iconsLoadingByUrl.set(url, o);
return o;
}
}
}

unloadSvg(url:string) {
if (this.iconsByUrl.has(url)) {
this.iconsByUrl.delete(url);
}
}
unloadSvg(url: string) {
if (this.iconsByUrl.has(url)) {
this.iconsByUrl.delete(url);
}
}

}

export function SVG_ICON_REGISTRY_PROVIDER_FACTORY(parentRegistry:SvgIconRegistryService, http:HttpClient) {
return parentRegistry || new SvgIconRegistryService(http);
export function SVG_ICON_REGISTRY_PROVIDER_FACTORY(parentRegistry: SvgIconRegistryService, http: HttpClient, fallBackIconUrl: string) {
return parentRegistry || new SvgIconRegistryService(http, fallBackIconUrl);
}

export const SVG_ICON_REGISTRY_PROVIDER = {
provide: SvgIconRegistryService,
deps: [ [new Optional(), new SkipSelf(), SvgIconRegistryService], HttpClient ],
useFactory: SVG_ICON_REGISTRY_PROVIDER_FACTORY
provide: SvgIconRegistryService,
deps: [[new Optional(), new SkipSelf(), SvgIconRegistryService], HttpClient, [new Inject('FALLBACK_ICON')]],
useFactory: SVG_ICON_REGISTRY_PROVIDER_FACTORY,
};