summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/loader/loader.component.ts
blob: 4d90b2853dd1fa11742b993e47efb7ce3c9f67cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Created by rc2122 on 6/6/2017.
 */
import {Component, Input, ElementRef, Renderer, SimpleChanges} from "@angular/core";
@Component({
    selector: 'loader',
    templateUrl: './loader.component.html',
    styleUrls: ['./loader.component.less']
})
export class LoaderComponent {

    @Input() display:boolean;
    @Input() size:string;// small || medium || large
    @Input() elementSelector:string; // required if is relative true
    @Input() relative:boolean;

    interval;

    constructor (private el: ElementRef, private renderer: Renderer){
    }

    ngOnInit() {

        if (this.elementSelector) {
            let elemParent = angular.element(this.elementSelector);
            let positionStyle:string = elemParent.css('position');
            this.setStyle(positionStyle);
        }

        if (this.relative === true) {
            let positionStyle:string = this.el.nativeElement.parentElement.style.position;
            this.setStyle(positionStyle);
        }
        if (!this.size) {
            this.size = 'large';
        }
    }

    ngOnDestroy(){
        clearInterval(this.interval);
    }

    calculateSizesForFixPosition = (positionStyle:string):void => {
        // This is problematic, I do not want to change the parent position.
        // set the loader on all the screen
        let parentLeft = this.el.nativeElement.parentElement.offsetLeft;
        let parentTop = this.el.nativeElement.parentElement.offsetTop;
        let parentWidth = this.el.nativeElement.parentElement.offsetWidth;
        let parentHeight = this.el.nativeElement.parentElement.offsetHeight;
        this.renderer.setElementStyle(this.el.nativeElement, 'position', positionStyle);
        this.renderer.setElementStyle(this.el.nativeElement, 'top', parentTop);
        this.renderer.setElementStyle(this.el.nativeElement, 'left', parentLeft);
        this.renderer.setElementStyle(this.el.nativeElement, 'width', parentWidth);
        this.renderer.setElementStyle(this.el.nativeElement, 'height', parentHeight);
    };

    setStyle = (positionStyle:string):void => {

        switch (positionStyle) {
            case 'absolute':
            case 'fixed':
                // The parent size is not set yet, still loading, so need to use interval to update the size.
                this.interval = window.setInterval(()=> {
                    this.calculateSizesForFixPosition(positionStyle);
                }, 2000);
                break;
            default:
                // Can change the parent position to relative without causing style issues.
                this.renderer.setElementStyle(this.el.nativeElement.parentElement,'position', 'relative');
                break;
        }
    };

    ngOnChanges(changes: SimpleChanges) {
        if(changes.display){
            this.changeLoaderDisplay(false);
            if ( this.display ) {
                window.setTimeout(():void => {
                    this.changeLoaderDisplay(true);
                }, 500);
            } else {
                window.setTimeout(():void => {
                    this.changeLoaderDisplay(false);
                }, 0);
            }
        }
    }

    changeLoaderDisplay = (display:boolean):void => {
        this.renderer.setElementStyle(this.el.nativeElement, 'display', display ? 'block' : 'none');
    }
}