all files / src/app/pages/playground/ playground.component.ts

100% Statements 35/35
100% Branches 8/8
100% Functions 10/10
100% Lines 33/33
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                            21×   21×   21×   21×   21×                                                                                 21× 21× 21× 21× 21×             18×   18×   18×         18× 18×   16× 16× 16×                                 10×                        
/** window object of browser */
declare let window: any;
 
import { isPlatformBrowser } from '@angular/common';
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { AngularFireStorage } from '@angular/fire/storage';
import { Observable } from 'rxjs';
import { AlertService, PageService, PaginationService } from '../../services';
 
/**
 * Playground Component
 */
@Component({
    selector: 'app-playground',
    templateUrl: './playground.component.html',
    styleUrls: ['./playground.component.scss']
})
export class PlaygroundComponent implements OnInit {
    /** what is rendering this page? Browser or Server */
    rendererText = '';
    /** current page`s title */
    title = 'Play Ground';
    /** Date object of today */
    today: number = Date.now();
    /** current minutes */
    minutes = 0;
    /** current gender */
    gender = 'female';
    /** async image URL */
    imgURL$: Observable<any>;
    /** image URL */
    imgURL: string;
 
    /**
     * increase current minutes by i
     *
     * result can't be more than 5 and less than 0
     * @param i: how many you want to increase
     */
    inc(i: number): void {
        this.minutes = Math.min(5, Math.max(0, this.minutes + i));
    }
 
    /**
     * set male as current gender
     */
    male(): void {
        this.gender = 'male';
    }
 
    /**
     * set female as current gender
     */
    female(): void {
        this.gender = 'female';
    }
 
    /**
     * set other as current gender
     */
    other(): void {
        this.gender = 'other';
    }
 
    /**
     * constructor of PlaygroundComponent
     * @param platformId: PLATFORM_ID
     * @param pageService: PageService
     * @param alert: AlertService
     * @param pagination: PaginationService
     * @param storage: AngularFireStorage
     */
    constructor(@Inject(PLATFORM_ID) private readonly platformId: string,
                public pageService: PageService,
                public alert: AlertService,
                public pagination: PaginationService,
                private readonly storage: AngularFireStorage) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.pagination.init(`blogs_${this.pageService.locale}`, 'created', {reverse: true, prepend: false});
 
        this.rendererText = isPlatformBrowser(this.platformId) ? 'Browser' : 'Server';
 
        this.pageService.initPage({
            title: this.title,
            description: this.title
        });
 
        const ref = this.storage.ref('publicFiles/bad, very bad angel.gif');
        if (isPlatformBrowser(this.platformId)) {
            // following lines are not working on server side because of undefined XMLHttpRequest
            this.imgURL$ = ref.getDownloadURL();
            this.imgURL$.subscribe(result => {
                this.imgURL = result;
            });
        }
    }
 
    /**
     * show alert panel and dialog
     */
    openAlert(): void {
        this.alert.success('This is alert test');
        if (isPlatformBrowser(this.platformId)) {
            window.alert('Yes it is!');
        }
    }
 
    /**
     * scroll handler for pagination
     * @param e: event
     */
    scrollHandler(e): void {
        if (e === 'bottom') {
            this.pagination.more();
        }
    }
 
    /**
     * track blog object array by blog
     * @param index: blog index no
     * @param item: blog object
     */
    trackByBlog(index, item): number {
        return index;
    }
}