Last active
July 19, 2019 06:11
-
-
Save codebymark/19edab47bac1fa3bda4a2cbc65b584b9 to your computer and use it in GitHub Desktop.
grunticon angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Directive, ElementRef, Renderer2, AfterViewInit} from '@angular/core'; | |
declare let window: CustomWindow; | |
@Directive({ | |
selector: '[appGrunticonEmbeded]' | |
}) | |
export class GrunticonDirective implements AfterViewInit { | |
constructor(private element: ElementRef, private renderer: Renderer2) {} | |
ngAfterViewInit() { | |
if( !window.grunticon ) { | |
console.warn('No grunticon on window'); | |
return; | |
} | |
this.renderer.setProperty(this.element.nativeElement, 'background-image', 'none'); | |
this.renderer.setProperty(this.element.nativeElement, 'innerHTML', this.getSVG()); | |
} | |
private getSVG(){ | |
return this.getDecodedBgImage().replace('data:image/svg+xml;charset=US-ASCII,', ''); | |
} | |
private getDecodedBgImage(){ | |
return window.decodeURIComponent(this.getBackgroundImage()); | |
} | |
private getBackgroundImage(){ | |
return window.getComputedStyle(this.element.nativeElement) | |
.getPropertyValue('background-image') | |
.slice(4, -1) | |
.replace(/"/g, ''); | |
} | |
} | |
export interface CustomWindow extends Window { | |
grunticon: any; | |
decodeURIComponent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment