2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 * Created by rc2122 on 6/6/2017.
24 import { Component, Input, ViewContainerRef, SimpleChanges} from "@angular/core";
27 templateUrl: './loader.component.html',
28 styleUrls: ['./loader.component.less']
30 export class LoaderComponent {
32 @Input() display: boolean;
33 @Input() size: string;// small || medium || large
34 @Input() relative: boolean; // If is relative is set to true, loader will appear over parent element. Otherwise, will be fixed over the entire page.
35 @Input() loaderDelay: number; //optional - number of ms to delay loader display.
37 private isVisible: boolean = false;
45 constructor(private viewContainerRef: ViewContainerRef) {
52 if (this.display === true) {
53 this.changeLoaderDisplay(true);
57 ngOnChanges(changes: SimpleChanges) {
59 this.changeLoaderDisplay(this.display);
63 private changeLoaderDisplay = (display: boolean): void => {
65 window.setTimeout((): void => {
66 this.display && this.showLoader(); //only show loader if this.display has not changed in the meanwhile.
67 }, this.loaderDelay || 0);
69 this.isVisible = false;
73 private showLoader = () => {
74 if (this.relative === true) {
75 let parentElement = this.viewContainerRef.element.nativeElement.parentElement;
77 left: (parentElement.offsetLeft !== undefined) ? parentElement.offsetLeft + "px" : undefined,
78 top: (parentElement.offsetTop !== undefined) ? parentElement.offsetTop + "px" : undefined,
79 width: (parentElement.offsetWidth !== undefined) ? parentElement.offsetWidth + "px" : undefined,
80 height: (parentElement.offsetHeight !== undefined) ? parentElement.offsetHeight + "px" : undefined
83 this.isVisible = true;