all files / component/iframe/ iframe.component.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 4/4
100% Lines 11/11
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                                                           
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { Component, ElementRef, Input, ViewChild } from '@angular/core';
 
@Component({
    selector: 'deja-iframe',
    styles: [
        require('./iframe.component.scss'),
    ],
    template: `<iframe id="djframe" #iframe>
        <ng-content></ng-content>
    </iframe>`
})
export class DejaIFrameComponent {
    @ViewChild('iframe')
    public iframe: ElementRef;
 
    @Input()
    public set sourceUrl(url: string) {
        const iframeElement = this.iframe.nativeElement as HTMLElement;
 
        if (url) {
            iframeElement.setAttribute('src', url);
        } else {
            iframeElement.removeAttribute('src');
        }
    }
 
    public get sourceUrl() {
        const iframeElement = this.iframe.nativeElement as HTMLElement;
        return iframeElement.hasAttribute('src') ? iframeElement.getAttribute('src') : undefined;
    }
}