import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { environment } from '../../environments/environment'; @Injectable({ providedIn: 'root' }) export class SpriteStyleService { constructor(private httpClient: HttpClient) { } //Media query for device private deviceMediaQuery: string = 'screen and (max-device-width: 599px)'; //Media query for desktop private desktopMediaQuery: string = 'screen and (min-device-width: 600px)'; //Remove elment by id from dom private removeElementByID(id:string): void { var element = document.getElementById(id); if (element) element.parentNode.removeChild(element); } //Include CSS file private addStyleSheet(filename:string, id:string, mediaQuery:string): void { this.httpClient.get(`${environment.apiUrl}/api/v1/sprite?stylesheetFilePath=${filename}`).subscribe( result => { // tslint:disable-next-line: no-string-literal const isFileFound: boolean = result['data'] as boolean; if (isFileFound === true) { const head = document.getElementsByTagName('head')[0]; const style = document.createElement('link'); style.href = environment.apiUrl + filename; style.type = 'text/css'; style.media = mediaQuery; style.id = id; style.rel = 'stylesheet'; head.appendChild(style); } }); } //Add stylesheet for home page. addHomePageStyleSheet() { window['Modernizr'].on('webp', (isWebPSupported) => { let cssFileName = isWebPSupported ? "sprite_webp.css" : "sprite.css"; this.addStyleSheet('/images/desktop/home/' + cssFileName, 'desktophome_stylesheet', this.desktopMediaQuery); this.addStyleSheet('/images/device/home/' + cssFileName, 'devicehome_stylesheet', this.deviceMediaQuery); // this.addStyleSheet('/images/app/home/' + cssFileName, 'apphome_stylesheet', this.deviceMediaQuery); }); } //Remove home page style sheet. removeHomePageStyleSheet() { this.removeElementByID('desktophome_stylesheet'); this.removeElementByID('devicehome_stylesheet'); this.removeElementByID('apphome_stylesheet'); } //Add stylesheet for specific item category folder. addItemDetailsStyleSheet(itemId:Number) { window['Modernizr'].on('webp', (isWebPSupported) => { let cssFileName = isWebPSupported ? "sprite_webp.css" : "sprite.css"; this.addStyleSheet('/images/desktop/itemdetails/' + itemId + '/' + cssFileName, 'desktopitemdetails_stylesheet', this.desktopMediaQuery); this.addStyleSheet('/images/device/itemdetails/' + itemId + '/' + cssFileName, 'deviceitemdetails_stylesheet', this.deviceMediaQuery); // this.addStyleSheet('/images/app/itemdetails/' + itemId + '/' + cssFileName, 'appitemdetails_stylesheet', this.deviceMediaQuery); }); } //Remove item details style sheet removeItemsDetailsStyleSheet() { this.removeElementByID('desktopitemdetails_stylesheet'); this.removeElementByID('deviceitemdetails_stylesheet'); this.removeElementByID('appitemdetails_stylesheet'); } }