2 * Created by rc2122 on 6/6/2017.
4 import {Component, Input, ElementRef, Renderer, SimpleChanges} from "@angular/core";
7 templateUrl: './loader.component.html',
8 styleUrls: ['./loader.component.less']
10 export class LoaderComponent {
12 @Input() display:boolean;
13 @Input() size:string;// small || medium || large
14 @Input() relative: boolean;
15 @Input() elementSelector: string; // optional. If is relative is set to true, option to pass in element that loader should be relative to. Otherwise, will be relative to parent element.
18 constructor (private el: ElementRef, private renderer: Renderer){
25 if (this.display === true) {
26 this.changeLoaderDisplay(true);
30 ngOnChanges(changes: SimpleChanges) {
33 this.changeLoaderDisplay(false); //display is set to true, so loader will appear unless we explicitly tell it not to.
34 window.setTimeout((): void => {
35 this.display && this.changeLoaderDisplay(true); //only show loader if we still need to display it.
38 window.setTimeout(():void => {
39 this.changeLoaderDisplay(false);
45 changeLoaderDisplay = (display: boolean): void => {
47 this.calculateLoaderPosition();
48 this.renderer.setElementStyle(this.el.nativeElement, 'display', 'block');
50 this.renderer.setElementStyle(this.el.nativeElement, 'display', 'none');
54 calculateLoaderPosition = () => {
55 if (this.relative === true) { // Can change the parent position to relative without causing style issues.
56 let parent = (this.elementSelector) ? angular.element(this.elementSelector).get(0) : this.el.nativeElement.parentElement;
57 this.renderer.setElementStyle(parent, 'position', 'relative');
58 this.setLoaderPosition(0, 0); //will be relative to parent and appear over specified element
59 //TODO: DONT force parent to have position relative; set inner div's style instead of outer element
60 // let parentPos: ClientRect = this.el.nativeElement.parentElement.getBoundingClientRect();
61 // this.setLoaderPosition(parentPos.top, parentPos.left, parentPos.width, parentPos.height);
63 this.setLoaderPosition(0, 0); //will appear over whole page
67 setLoaderPosition = (top:number, left:number, width?:number, height?:number): void => {
68 this.renderer.setElementStyle(this.el.nativeElement, 'position', 'absolute');
69 this.renderer.setElementStyle(this.el.nativeElement, 'top', top? top.toString() : "0");
70 this.renderer.setElementStyle(this.el.nativeElement, 'left', left? left.toString() : "0");
71 this.renderer.setElementStyle(this.el.nativeElement, 'width', width? width.toString() : "100%");
72 this.renderer.setElementStyle(this.el.nativeElement, 'height', height? height.toString() : "100%");