Created
December 12, 2017 02:55
-
-
Save gildniy/c604b0e1380d8f91c26bab7a109100cc to your computer and use it in GitHub Desktop.
Spinner the Angular 4 way!
This file contains 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 { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; | |
import { SpinnerService } from './spinner.service'; | |
@Component({ | |
selector: 'spinner', | |
template: ` | |
<span *ngIf="show"> | |
<img *ngIf="loadingImage" [src]="loadingImage"/> | |
<ng-content></ng-content> | |
</span> | |
` | |
}) | |
// <spinner [loadingImage]="path/to/loading.gif" [show]="true"></spinner> | |
// <spinner name="mySpinner" [(show)]="spinnerShowing"></spinner> | |
export class SpinnerComponent implements OnInit { | |
@Input() name: string; | |
@Input() group: string; | |
@Input() loadingImage: string; | |
private isShowing = false; | |
@Input() | |
get show(): boolean { | |
return this.isShowing; | |
} | |
@Output() showChange = new EventEmitter<boolean>(); | |
set show(val: boolean) { | |
this.isShowing = val; | |
this.showChange.emit(this.isShowing); | |
} | |
constructor(private spinnerService: SpinnerService) { | |
} | |
ngOnInit(): void { | |
if (!this.name) throw new Error('Spinner must have a \'name\' attribute.'); | |
this.spinnerService._register(this); | |
} | |
ngOnDestroy(): void { | |
this.spinnerService._unregister(this); | |
} | |
} |
This file contains 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 { Injectable } from '@angular/core'; | |
import { SpinnerComponent } from './spinner.component'; | |
@Injectable() | |
export class SpinnerService { | |
private spinnerCache = new Set<SpinnerComponent>(); | |
_register(spinner: SpinnerComponent): void { | |
this.spinnerCache.add(spinner); | |
} | |
show(spinnerName: string): void { | |
this.spinnerCache.forEach(spinner => { | |
if (spinner.name === spinnerName) { | |
spinner.show = true; | |
} | |
}); | |
} | |
hide(spinnerName: string): void { | |
this.spinnerCache.forEach(spinner => { | |
if (spinner.name === spinnerName) { | |
spinner.show = false; | |
} | |
}); | |
} | |
showGroup(spinnerGroup: string): void { | |
this.spinnerCache.forEach(spinner => { | |
if (spinner.group === spinnerGroup) { | |
spinner.show = true; | |
} | |
}); | |
} | |
hideGroup(spinnerGroup: string): void { | |
this.spinnerCache.forEach(spinner => { | |
if (spinner.group === spinnerGroup) { | |
spinner.show = false; | |
} | |
}); | |
} | |
showAll(): void { | |
this.spinnerCache.forEach(spinner => spinner.show = true); | |
} | |
hideAll(): void { | |
this.spinnerCache.forEach(spinner => spinner.show = false); | |
} | |
isShowing(spinnerName: string): boolean | undefined { | |
let showing = undefined; | |
this.spinnerCache.forEach(spinner => { | |
if (spinner.name === spinnerName) { | |
showing = spinner.show; | |
} | |
}); | |
return showing; | |
} | |
_unregister(spinnerToRemove: SpinnerComponent): void { | |
this.spinnerCache.forEach(spinner => { | |
if (spinner === spinnerToRemove) { | |
this.spinnerCache.delete(spinner); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment