Skip to content

Commit

Permalink
fix: svg
Browse files Browse the repository at this point in the history
  • Loading branch information
alyleui committed Apr 22, 2018
1 parent c6c40ef commit 1147323
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
8 changes: 2 additions & 6 deletions src/lib/svg/ly-svg.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class LySvgComponent implements OnInit, OnChanges {

@Input('src')
set src(value: string) {
if (!this._strEndsWith(value, '.svg')) {
if (!value.endsWith('.svg')) {
this._src = `${value}.svg`;
} else {
this._src = value;
Expand Down Expand Up @@ -57,15 +57,11 @@ export class LySvgComponent implements OnInit, OnChanges {
this.styleWidth = this.size;
this.styleHeight = this.size;
}
private _strEndsWith(str: any, suffix: string) {
return str.match(suffix + '$') == suffix;
}


constructor(
private svgService: LySvgService,
private _elementRef: ElementRef
) { }
) {}

ngOnInit() {
}
Expand Down
14 changes: 3 additions & 11 deletions src/lib/svg/ly-svg.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LySvgComponent } from './ly-svg.component';
import { LySvgService } from './ly-svg.service';

@NgModule({
imports: [
CommonModule
],
exports: [LySvgComponent],
declarations: [LySvgComponent],
providers: [LySvgService]
declarations: [LySvgComponent]
})
export class LySvgModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: LySvgModule,
};
}
}
export class LySvgModule { }
19 changes: 18 additions & 1 deletion src/lib/svg/ly-svg.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { LyTheme as Theme, StyleData, Platform } from 'alyle-ui/core';
@Injectable({
providedIn: 'root'
})
export class LySvgService {
private static readonly _svg: Map<string, SVGElement> = new Map<string, SVGElement>();
constructor(private _http: HttpClient) {
stylesData: {
[key: string]: StyleData
} = {};
constructor(
private _http: HttpClient,
private theme: Theme
) {
this.stylesData.style_host = this.theme.createStyle('svg', () => {
return `width: inherit;` +
`height: inherit;` +
`fill: currentColor;` +
`display: block;`;
});
}
getSVG(url: string): Promise<SVGElement> {
if (!Platform.isBrowser) {
return Promise.resolve(this._svgElementFromString(`<svg height="48" viewBox="0 0 20 20" width="48" class="ly_7"><circle cx="10" cy="10" r="10"></circle></svg>`));
}
if (LySvgService._svg.has(url)) {
return Promise.resolve(this._cloneSVG(LySvgService._svg.get(url)));
}
Expand All @@ -33,6 +49,7 @@ export class LySvgService {
if (!svg) {
throw new Error('No SVG found in loaded contents');
}
svg.classList.add(this.stylesData.style_host.id);
return svg;
}
private _cloneSVG(svg: SVGElement): SVGElement {
Expand Down

0 comments on commit 1147323

Please sign in to comment.