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

89.47% Statements 102/114
70% Branches 21/30
80% Functions 28/35
89.38% Lines 101/113
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                      235× 235× 235× 235× 235× 235× 235× 235× 235× 235× 235× 235× 235×     235× 235× 235× 235× 235× 235× 235× 235×           235× 235× 235× 235× 235× 235×           235×         1904×                 406× 274× 274×                       677×     18× 18× 18×         1697×                   18× 18× 18×             602× 261× 261×         1814×             212×             292×     12× 12× 12×         677×     6147×     1773×     316×       23858×     6263×                 952× 952×                     677×                                  
/*
 *  @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 { BehaviorSubject, ReplaySubject, Subject } from 'rxjs';
import { Rect } from '../../common/core/graphics/rect';
import { IDejaTile } from './tile.interface';
 
export class DejaTile implements IDejaTile {
    private static currentId = 0;
 
    public cutted$ = new BehaviorSubject<boolean>(false);
    public dragging$ = new BehaviorSubject<boolean>(false);
    public dropping$ = new BehaviorSubject<boolean>(false);
    public pressed$ = new BehaviorSubject<boolean>(false);
    public selected$ = new BehaviorSubject<boolean>(false);
    public expanded$ = new BehaviorSubject<boolean>(false);
    public hidden$ = new ReplaySubject<boolean>(1);
    public pending$ = new BehaviorSubject<boolean>(false);
    public deleted$ = new Subject();
    public pixelBounds$ = new BehaviorSubject<Rect>(null);
    public isTemporary = false;
    public fading = false;
    public refresh$ = new Subject();
 
    private _id: string;
    private _isCutted = false;
    private _isDragging = false;
    private _isDropping = false;
    private _isPressed = false;
    private _isSelected = false;
    private _isExpanded = false;
    private _isHidden = false;
    private _isPending = false;
    private _pixelBounds: Rect;
    private _percentBounds: Rect;
    private _model: IDejaTile;
    private _color: string;
 
    constructor(private tile: IDejaTile) {
        this._model = tile;
        this._id = tile.id;
        this._percentBounds = tile.bounds;
        this._color = tile.color;
        Iif (tile.effects) {
            this.isCutted = tile.effects.cutted;
            this.isSelected = tile.effects.selected;
            this._isPending = tile.effects.pending;
            this.fading = tile.effects.fading;
        }
        if (!this._id) {
            this._id = `#${DejaTile.currentId++}`;
        }
        // console.log(`Creating tile ${this._id}`);
    }
 
    public get model() {
        return this._model;
    }
 
    public set color(value: string) {
        this._color = value;
    }
 
    public get color() {
        return this._color;
    }
 
    public set pixelBounds(value: Rect) {
        if (!Rect.equals(this._pixelBounds, value)) {
            this._pixelBounds = value;
            this.pixelBounds$.next(value);
        }
    }
 
    public get pixelBounds() {
        return this._pixelBounds;
    }
 
    public set isCutted(value: boolean) {
        Eif (this._isCutted !== value) {
            this._isCutted = value;
            this.cutted$.next(value);
        }
    }
 
    public get isCutted() {
        return this._isCutted;
    }
 
    public set isDragging(value: boolean) {
        Eif (this._isDragging !== value) {
            this._isDragging = value;
            this.dragging$.next(value);
        }
    }
 
    public get isDragging() {
        return this._isDragging;
    }
 
    public set isDropping(value: boolean) {
        Eif (this._isDropping !== value) {
            this._isDropping = value;
            this.dropping$.next(value);
        }
    }
 
    public get isDropping() {
        return this._isDropping;
    }
 
    public set isPressed(value: boolean) {
        Eif (this._isPressed !== value) {
            this._isPressed = value;
            this.pressed$.next(value);
        }
    }
 
    public get isPressed() {
        return this._isPressed;
    }
 
    public set isSelected(value: boolean) {
        if (this._isSelected !== value) {
            this._isSelected = value;
            this.selected$.next(value);
        }
    }
 
    public get isSelected() {
        return this._isSelected;
    }
 
    public set isExpanded(value: boolean) {
        Eif (this._isExpanded !== value) {
            this._isExpanded = value;
            this.expanded$.next(value);
        }
    }
 
    public get isExpanded() {
        return this._isExpanded;
    }
 
    public set isHidden(value: boolean) {
        Eif (this._isHidden !== value) {
            this._isHidden = value;
            this.hidden$.next(value);
        }
    }
 
    public get isHidden() {
        return this._isHidden;
    }
 
    public set isPending(value: boolean) {
        Eif (this._isPending !== value) {
            this._isPending = value;
            this.pending$.next(value);
        }
    }
 
    public get isPending() {
        return this._isPending;
    }
 
    public get id() {
        return this._id;
    }
 
    public get type() {
        return this.tile.type;
    }
 
    public set percentBounds(bounds: Rect) {
        this._percentBounds = bounds;
    }
 
    public get percentBounds() {
        return this._percentBounds;
    }
 
    public get templateModel() {
        return this.tile.templateModel;
    }
 
    public makeId() {
        this._id = `#${DejaTile.currentId++}`;
    }
 
    public refreshBounds() {
        this.percentBounds = this.model.bounds;
    }
 
    public equalsTo(tile: IDejaTile) {
        Eif (this.model.id) {
            return this.model.id === tile.id;
        } else {
            return this.model === tile;
        }
    }
 
    public clone() {
        return new DejaTile(this.toTileModel());
    }
 
    public delete() {
        this.deleted$.next();
    }
 
    public toTileModel() {
        return {
            id: this.id,
            type: this.type,
            bounds: this.percentBounds,
            color: this._color,
            templateModel: this.templateModel,
            effects: {
                pending: this.isPending || undefined,
                cutted: this.isCutted || undefined,
                selected: this.isSelected || undefined,
            }
        } as IDejaTile;
    }
 
    public refresh() {
        this.refresh$.next();
    }
}