/**
 * Created by jd on 28/04/2016.
 */
import {Directive, Input, OnInit, Output, EventEmitter, OnDestroy, HostListener} from '@angular/core';

@Directive({
               selector: 'core-document-mouseup'
           })

export class CoreDocumentMouseUp implements OnInit, OnDestroy
{

    @Input() canHandler:boolean;
    @Output() handler = new EventEmitter<MouseEvent>();

    //not propagate click event on host component
    @HostListener('mouseup', [ '$event' ])
    private mouseup = ( $event ) =>
    {
        $event.stopPropagation();
    };

    constructor()
    {
    }


    private iMouseUp = ( event:MouseEvent ):void =>
    {
        if (this.canHandler == null || this.canHandler == true)
            this.handler.emit(event);
    };

    ngOnInit()
    {
        setTimeout(() =>
                   {
                       document.addEventListener('mouseup', this.iMouseUp);
                   }, 0);
    }

    ngOnDestroy()
    {
        document.removeEventListener('mouseup', this.iMouseUp);
    }


}