all files / component/tiles/ tile.component.ts

88.66% Statements 86/97
82.35% Branches 28/34
85.11% Functions 40/47
88.17% Lines 82/93
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                  235× 235×     235×     235×   235× 235× 235×     235× 235×   235× 235× 1720× 60×   1660×       235×         235× 467×   232× 232×                     235× 509× 509×   274× 274×   274× 274× 274× 274× 274× 274×     235× 253× 253× 253×   235× 492× 492× 492×   235× 253× 253× 253×   235× 237× 236× 236×   235× 237× 237× 237×   235× 237× 237× 237×   235×   235× 12×     235×       235×           235×               2731×     235×            
/*
 *  @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 { ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, EventEmitter, Input, OnDestroy, Output } from '@angular/core';
import { from as observableFrom } from 'rxjs';
import { debounceTime, delay, filter, first, takeWhile, tap } from 'rxjs/operators';
import { DejaTile } from './tile.class';
 
@Component({
    changeDetection: ChangeDetectionStrategy.OnPush,
    selector: 'deja-tile',
    styles: [
        require('./tile.component.scss'),
    ],
    template: require('./tile.component.html'),
})
export class DejaTileComponent implements OnDestroy {
    @Input() public template: any;
    @Input() public designMode: boolean;
    @Output() public modelChanged = new EventEmitter();
    @Output() public close = new EventEmitter<Event>();
 
    public element: HTMLElement;
    public progressDiameter = 100;
 
    private _tile: DejaTile;
    private isAlive = true;
 
    constructor(el: ElementRef, private changeDetectorRef: ChangeDetectorRef) {
        this.element = el.nativeElement as HTMLElement;
        this.element.setAttribute('hidden', '0');
    }
 
    @Input()
    public set tile(tile: DejaTile) {
        this._tile = tile;
 
        Eif (tile) {
            const toogleAttribute = (attribute: string, value: string | boolean) => {
                if (value) {
                    this.element.setAttribute(attribute, value.toString());
                } else {
                    this.element.removeAttribute(attribute);
                }
            };
 
            Iif (tile.fading) {
                this.element.setAttribute('fading', '0');
                this.changeDetectorRef.markForCheck();
            }
 
            observableFrom(tile.pixelBounds$).pipe(
                filter((bounds) => !!bounds),
                first(),
                takeWhile(() => this.isAlive && !!this._tile),
                filter(() => tile.fading),
                tap(() => {
                    this.element.setAttribute('fading', '1');
                    this.changeDetectorRef.markForCheck();
                }),
                delay(200))
                .subscribe(() => {
                    this.element.removeAttribute('fading');
                    this.changeDetectorRef.markForCheck();
                });
 
            observableFrom(tile.pixelBounds$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                filter((bounds) => !!bounds))
                .subscribe((bounds) => {
                    Eif (!tile.isHidden) {
                        this.element.removeAttribute('hidden');
                    }
                    this.element.style.left = `${bounds.left}px`;
                    this.element.style.top = `${bounds.top}px`;
                    this.element.style.width = `${bounds.width}px`;
                    this.element.style.height = `${bounds.height}px`;
                    this.progressDiameter = Math.min(100, Math.round(Math.max(bounds.width * 0.4, bounds.height * 0.4)));
                    this.changeDetectorRef.markForCheck();
                });
 
            observableFrom(tile.pressed$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap((value) => toogleAttribute('pressed', value)))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            observableFrom(tile.selected$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap((value) => toogleAttribute('selected', value)))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            observableFrom(tile.dragging$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap((value) => toogleAttribute('drag', value)))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            observableFrom(tile.dropping$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap((value) => toogleAttribute('drop', value)))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            observableFrom(tile.cutted$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap((value) => toogleAttribute('cutted', value)))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            observableFrom(tile.expanded$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap((value) => toogleAttribute('expanded', value)))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            observableFrom(tile.deleted$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                tap(() => this.element.remove()))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            const tooogleHide$ = observableFrom(tile.hidden$).pipe(
                tap((value) => toogleAttribute('hidden', value ? '1' : '2')));
 
            // Hide
            tooogleHide$.pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                debounceTime(1000),
                filter((value) => value),
                tap(() => this.element.setAttribute('hidden', '0')))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            // Show
            tooogleHide$.pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                debounceTime(1),
                filter((value) => !value),
                tap(() => this.element.removeAttribute('hidden')))
                .subscribe(() => this.changeDetectorRef.markForCheck());
 
            // Refresh
            observableFrom(tile.refresh$).pipe(
                takeWhile(() => this.isAlive && !!this._tile),
                debounceTime(1))
                .subscribe(() => this.changeDetectorRef.markForCheck());
        }
    }
 
    public get tile() {
        return this._tile;
    }
 
    public ngOnDestroy() {
        this.isAlive = false;
    }
 
    protected onModelChanged() {
        this.modelChanged.emit();
    }
}