all files / component/mouse-dragdrop/ mouse-draggable.directive.ts

88.14% Statements 52/59
52% Branches 13/25
92.86% Functions 13/14
87.72% Lines 50/57
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                                                                                                                                                                             
/*
 *  @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 { Directive, ElementRef, Input, OnDestroy } from '@angular/core';
import { fromEvent as observableFromEvent, merge as observableMerge, Observable, Subject } from 'rxjs';
import { filter, first, takeUntil, takeWhile, tap } from 'rxjs/operators';
import { Position } from '../../common/core/graphics/position';
import { Rect } from '../../common/core/graphics/rect';
import { DejaMouseDragDropService, IDragCursorInfos, IDragDropContext } from './mouse-dragdrop.service';
 
@Directive({
    selector: '[deja-mouse-draggable]',
})
export class DejaMouseDraggableDirective implements OnDestroy {
    private _context: IDejaMouseDraggableContext;
    private isAlive = true;
 
    @Input('deja-mouse-draggable')
    public set context(value: IDejaMouseDraggableContext) {
        this._context = value;
    }
 
    public get context() {
        return this._context;
    }
 
    constructor(elementRef: ElementRef, dragDropService: DejaMouseDragDropService) {
        const element = elementRef.nativeElement as HTMLElement;
 
        const leave$ = observableFromEvent(element, 'mouseleave');
 
        const mouseUp$ = observableFromEvent(element.ownerDocument, 'mouseup');
 
        observableFromEvent(element, 'mouseenter').pipe(
            takeWhile(() => this.isAlive))
            .subscribe(() => {
                observableFromEvent(element, 'mousedown').pipe(
                    takeUntil(leave$),
                    filter((event: MouseEvent) => event.buttons === 1))
                    .subscribe((event: MouseEvent) => {
                        const moveUp$ = new Subject();
                        let target: HTMLElement;
 
                        const match = (el: HTMLElement) => {
                            return el.tagName === this.context.target.toUpperCase() || el.id === this.context.target.substr(1) || el.hasAttribute(this.context.target.substring(1, this.context.target.length - 1));
                        };
 
                        const startDrag = () => {
                            const kill$ = observableMerge(mouseUp$, moveUp$).pipe(
                                first(),
                                tap(() => {
                                    dragDropService.dragCursor$.next(undefined);
                                    dragDropService.dragging$.next(false);
                                }));
 
                            observableFromEvent(element.ownerDocument, 'mousemove').pipe(
                                takeUntil(kill$))
                                .subscribe((ev: MouseEvent) => {
                                    Eif (target && ev.buttons === 1) {
                                        const bounds = new Rect(element.getBoundingClientRect());
                                        const position = new Position(ev.pageX, ev.pageY);
                                        const html = bounds.containsPoint(position) ? target.innerHTML : undefined;
 
                                        // Post cursor infos to service
                                        dragDropService.dragCursor$.next({
                                            position: position,
                                            html: html,
                                            width: target.offsetWidth,
                                            height: target.offsetHeight,
                                            className: this.context.className,
                                            originalEvent: ev,
                                        } as IDragCursorInfos);
 
                                    } else {
                                        moveUp$.next();
                                    }
 
                                    ev.preventDefault();
                                    return false;
                                });
 
                            dragDropService.dragging$.next(true);
                        };
 
                        Eif (this.context) {
                            Iif (this.context.target) {
                                target = event.target as HTMLElement;
                                while (target && !match(target)) {
                                    target = target.parentElement;
                                }
                            } else {
                                target = element;
                            }
 
                            Eif (target && this.context.dragStart) {
                                const dragContext = this.context.dragStart(target);
                                Eif (dragContext) {
                                    Eif (dragContext.subscribe) {
                                        const context = dragContext as Observable<any>;
                                        // Observable
                                        context.pipe(
                                            first()
                                        ).subscribe((ddctx: IDragDropContext) => {
                                            dragDropService.context = ddctx;
                                            Eif (ddctx) {
                                                startDrag();
                                            }
                                        });
                                        return;
                                    } else {
                                        dragDropService.context = dragContext;
                                        startDrag();
                                    }
                                }
                            }
                        }
                    });
            });
    }
 
    public ngOnDestroy() {
        this.isAlive = false;
    }
}
 
export interface IDejaMouseDraggableContext {
    target?: string; // Tagname or #id or [attribute]
    className?: string;
    dragStart?(element: HTMLElement): any; // Return object or observable<object>
}