cedvdb / ng2draggable

draggable directive for angular 2
https://cedvdb.github.io/ng2draggable/
MIT License
18 stars 16 forks source link

The draggable handle not working if it contains nested elements #17

Open alhimik1986 opened 7 years ago

alhimik1986 commented 7 years ago

Hello. I suggest to use the following code:

  @HostListener('mousedown', ['$event'])
  onMouseDown(event:MouseEvent) {
    if(event.button === 2)
      return; // prevents right click drag, remove his if you don't want it

    // search the handle in parent elements
    if (this._handle !== undefined) {
      let node = event.target;
      while(true) {
        if ( ! node || node === this._handle)
          break;
        node = node['parentNode'];
      }
      if ( ! node)
        return;
    }

    this.md = true;
    this.topStart = event.clientY - this.element.nativeElement.style.top.replace('px','');
    this.leftStart = event.clientX - this.element.nativeElement.style.left.replace('px','');
  }

I could make a fork but I could not find the repository in github, I suppose the repository is in npmjs. Please update your repository, if the code is normal.

aprapplicationsupport commented 7 years ago

Thanks for this alhimik1986, that code snippet works like a charm!