All files / src/app/components/nav-menu nav-menu.component.ts

100% Statements 41/41
100% Branches 8/8
100% Functions 8/8
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1011x 1x 1x 1x 1x 1x   1x                 1x     38x   38x   38x                   38x 38x 38x 38x             38x   48x   38x 163x   12x 10x 20x 20x 10x   10x           38x   7x 7x 7x 7x           7x 7x 1x 1x           1x     7x   14x 6x               7x        
import { isPlatformBrowser } from '@angular/common';
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { Subject } from 'rxjs';
import { filter } from 'rxjs/internal/operators';
import { languageNames } from '../../app-config';
import { ConfigModel, LanguageModel, PageBaseModel } from '../../models';
import { ConfigService, PageService } from '../../services';
 
/**
 * Nav Menu Component
 */
@Component({
    selector: 'app-nav-menu',
    templateUrl: './nav-menu.component.html'
})
export class NavMenuComponent implements OnInit {
    // tslint:disable:member-ordering
    /** array of languages */
    private readonly languages = new Subject<Array<LanguageModel>>();
    /** observable languages */
    languages$ = this.languages.asObservable();
    /** main menu items */
    mainMenuItems = [];
 
    /**
     * constructor of NavMenuComponent
     * @param platformId: PLATFORM_ID
     * @param router: Router
     * @param pageService: PageService
     * @param configService: ConfigService
     */
    constructor(
        @Inject(PLATFORM_ID) private readonly platformId: string,
        public router: Router,
        public pageService: PageService,
        public configService: ConfigService) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.configService.getConfig()
            .subscribe((config: ConfigModel) => {
                this.mainMenuItems = config.mainMenuItems;
            });
        this.router.events
            .pipe(filter(event => event instanceof NavigationEnd))
            .subscribe((event: NavigationEnd) => {
                if (isPlatformBrowser(this.platformId)) {
                    const scrollToTop = window.setInterval(() => {
                        const pos = window.pageYOffset;
                        if (pos > 0) {
                            window.scrollTo(0, pos - 60); // how far to scroll on each step
                        } else {
                            window.clearInterval(scrollToTop);
                        }
                    }, 16);
                }
            });
 
        this.pageService.getPage()
            .subscribe((page: PageBaseModel) => {
                const existLanguages = [];
                const languageList: Array<LanguageModel> = [];
                const languageCode = this.pageService.locale.substring(0, 2);
                languageList.push({
                    languageCode,
                    languageName: languageNames[languageCode],
                    url: `/${languageCode}${this.router.url}`,
                    isExist: true
                });
                existLanguages.push(languageCode);
                if (page.locales) {
                    for (const locale of page.locales) {
                        languageList.push({
                            languageCode: locale.cultureCode.substring(0, 2),
                            languageName: languageNames[locale.cultureCode.substring(0, 2)],
                            url: `/${locale.slug}`,
                            isExist: true
                        });
                        existLanguages.push(locale.cultureCode.substring(0, 2));
                    }
                }
                Object.keys(languageNames)
                    .forEach(langCode => {
                        if (existLanguages.indexOf(langCode) === -1) {
                            languageList.push({
                                languageCode: langCode,
                                languageName: languageNames[langCode],
                                url: `/${langCode}/home`,
                                isExist: false
                            });
                        }
                });
                this.languages.next(languageList);
            });
    }
}