Skip to content

Instantly share code, notes, and snippets.

@codebymark
Last active July 19, 2019 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebymark/19edab47bac1fa3bda4a2cbc65b584b9 to your computer and use it in GitHub Desktop.
Save codebymark/19edab47bac1fa3bda4a2cbc65b584b9 to your computer and use it in GitHub Desktop.
grunticon angular
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