Skip to content
Snippets Groups Projects
home.component.ts 4.83 KiB
Newer Older
Florian Azizian's avatar
Florian Azizian committed
import { ChangeDetectorRef, Component, OnInit, ViewChild, QueryList, ViewChildren } from '@angular/core';
import { MediaMatcher } from '@angular/cdk/layout';
import { HttpClient } from '@angular/common/http';
import { LANG } from '../translate.component';
import { MatDialog, MatSidenav, MatExpansionPanel, MatTableDataSource } from '@angular/material';
import { NotificationService } from '../notification.service';
import { HeaderService }        from '../../service/header.service';
Florian Azizian's avatar
Florian Azizian committed

import { AutoCompletePlugin } from '../../plugins/autocomplete.plugin';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
Florian Azizian's avatar
Florian Azizian committed

declare function $j(selector: any): any;

declare var angularGlobals: any;

@Component({
    templateUrl: "home.component.html",
    styleUrls: ['home.component.scss'],
Florian Azizian's avatar
Florian Azizian committed
    providers: [NotificationService]
})
export class HomeComponent extends AutoCompletePlugin implements OnInit {

    private _mobileQueryListener    : () => void;
    mobileQuery                     : MediaQueryList;
    mobileMode                      : boolean   = false;
Florian Azizian's avatar
Florian Azizian committed

    coreUrl             : string;
    lang                : any       = LANG;
    loading             : boolean   = false;

    thumbnailUrl        : string;
    docUrl              : string    = '';
    homeData            : any;
    homeMessage         : string;
    dataSource          : any;
    currentDate         : string    = "";


    public innerHtml    : SafeHtml;
    displayedColumns    : string[] = ['res_id', 'subject', 'creation_date'];
Florian Azizian's avatar
Florian Azizian committed

    @ViewChild('snav') snav: MatSidenav;
    @ViewChild('snav2') sidenavRight: MatSidenav;
Florian Azizian's avatar
Florian Azizian committed
    @ViewChildren(MatExpansionPanel) viewPanels: QueryList<MatExpansionPanel>;
Damien's avatar
Damien committed
    constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher, public http: HttpClient, public dialog: MatDialog, private sanitizer: DomSanitizer, private notify: NotificationService, private headerService: HeaderService) {
Florian Azizian's avatar
Florian Azizian committed
        super(http, ['users']);
        this.mobileMode = angularGlobals.mobileMode;
        $j("link[href='merged_css.php']").remove();
        this.mobileQuery = media.matchMedia('(max-width: 768px)');
        this._mobileQueryListener = () => changeDetectorRef.detectChanges();
        this.mobileQuery.addListener(this._mobileQueryListener);
        (<any>window).pdfWorkerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.min.js';
Florian Azizian's avatar
Florian Azizian committed
    }

    ngOnInit(): void {
        this.loading = true;
        if (this.mobileMode) {
            this.displayedColumns = ['res_id', 'subject'];
        }
Alex ORLUC's avatar
Alex ORLUC committed
        this.headerService.setHeader(this.lang.home);
        window['MainHeaderComponent'].setSnav(this.snav);
        window['MainHeaderComponent'].setSnavRight(null);
Florian Azizian's avatar
Florian Azizian committed
        this.coreUrl = angularGlobals.coreUrl;
        let event = new Date();
        let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

        this.currentDate = event.toLocaleDateString('fr-FR', options);
Florian Azizian's avatar
Florian Azizian committed

        this.http.get(this.coreUrl + "rest/home")
            .subscribe((data: any) => {
                this.homeData = data;
        });
    }

    ngAfterViewInit(): void {
        this.http.get(this.coreUrl + "rest/home/lastRessources")
        .subscribe((data: any) => {
            setTimeout(() => {
                this.dataSource = new MatTableDataSource(data.lastResources);
                this.loading = false;
Florian Azizian's avatar
Florian Azizian committed
        });
    }

        if (this.docUrl == this.coreUrl+'rest/res/'+row.res_id+'/content' && this.sidenavRight.opened) {
            this.sidenavRight.close();
        } else {
            this.docUrl = this.coreUrl+'rest/res/'+row.res_id+'/content';
            this.innerHtml = this.sanitizer.bypassSecurityTrustHtml(
                "<iframe style='height:100%;width:100%;' src='" + this.docUrl + "' class='embed-responsive-item'>" +
                "</iframe>");  
            this.sidenavRight.open();
    viewThumbnail(row:any) {
        this.thumbnailUrl = this.coreUrl+'rest/res/' + row.res_id + '/thumbnail';
        $j('#viewThumbnail').show();
        $j('#listContent').css({"overflow":"hidden"});
    }

    closeThumbnail() {
        $j('#viewThumbnail').hide();
        $j('#listContent').css({"overflow":"auto"});
    goToDetail(row:any) {
        this.http.get(this.coreUrl + "rest/resources/" + row.res_id + "/isAllowed")
            .subscribe((data: any) => {
                if (data['isAllowed']) {
                    location.href = "index.php?page=details&dir=indexing_searching&id=" + row.res_id;
                } else {
                    this.notify.error(this.lang.documentOutOfPerimeter);
                }
            }, () => {
                this.notify.error(this.lang.errorOccured);
            });
Damien's avatar
Damien committed
}