|
| 1 | +import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { OutsidePlacement } from '../../../../projects/toppy/src/lib/models'; |
| 3 | +import { RelativePosition, Toppy, ToppyRef } from '../../../../projects/toppy/src/public_api'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + selector: 'app-drag-example', |
| 7 | + templateUrl: './drag-example.component.html' |
| 8 | +}) |
| 9 | +export class DragExampleComponent implements OnInit { |
| 10 | + pos1 = 0; |
| 11 | + pos2 = 0; |
| 12 | + pos3 = 0; |
| 13 | + pos4 = 0; |
| 14 | + @ViewChild('el') el: ElementRef; |
| 15 | + private _toppyRef: ToppyRef; |
| 16 | + constructor(private toppy: Toppy) {} |
| 17 | + |
| 18 | + ngOnInit() { |
| 19 | + this._toppyRef = this.toppy |
| 20 | + .overlay( |
| 21 | + new RelativePosition({ |
| 22 | + placement: OutsidePlacement.TOP, |
| 23 | + src: this.el.nativeElement, |
| 24 | + hostWidth: 'auto', |
| 25 | + autoUpdate: true |
| 26 | + }), |
| 27 | + { |
| 28 | + backdrop: false, |
| 29 | + dismissOnDocumentClick: false |
| 30 | + } |
| 31 | + ) |
| 32 | + .host('<div class="tooltip">tooltip</div>', { hasHTML: true }) |
| 33 | + .create(); |
| 34 | + } |
| 35 | + |
| 36 | + ngAfterViewInit() { |
| 37 | + this.dragElement(); |
| 38 | + } |
| 39 | + reset() { |
| 40 | + this.el.nativeElement.style.left = '0px'; |
| 41 | + this.el.nativeElement.style.top = '0px'; |
| 42 | + } |
| 43 | + onMouseOver() { |
| 44 | + this._toppyRef.open(); |
| 45 | + } |
| 46 | + onMouseLeave() { |
| 47 | + this._toppyRef.close(); |
| 48 | + } |
| 49 | + dragMouseDown(e) { |
| 50 | + e = e || window.event; |
| 51 | + e.preventDefault(); |
| 52 | + // get the mouse cursor position at startup: |
| 53 | + this.pos3 = e.clientX; |
| 54 | + this.pos4 = e.clientY; |
| 55 | + document.onmouseup = this.closeDragElement.bind(this); |
| 56 | + // call a function whenever the cursor moves: |
| 57 | + document.onmousemove = this.elementDrag.bind(this); |
| 58 | + } |
| 59 | + |
| 60 | + elementDrag(e) { |
| 61 | + e = e || window.event; |
| 62 | + e.preventDefault(); |
| 63 | + // calculate the new cursor position: |
| 64 | + this.pos1 = this.pos3 - e.clientX; |
| 65 | + this.pos2 = this.pos4 - e.clientY; |
| 66 | + this.pos3 = e.clientX; |
| 67 | + this.pos4 = e.clientY; |
| 68 | + this.el.nativeElement.style.top = this.el.nativeElement.offsetTop - this.pos2 + 'px'; |
| 69 | + this.el.nativeElement.style.left = this.el.nativeElement.offsetLeft - this.pos1 + 'px'; |
| 70 | + } |
| 71 | + |
| 72 | + closeDragElement() { |
| 73 | + document.onmouseup = null; |
| 74 | + document.onmousemove = null; |
| 75 | + } |
| 76 | + |
| 77 | + dragElement() { |
| 78 | + console.log('fofo'); |
| 79 | + this.el.nativeElement.onmousedown = this.dragMouseDown.bind(this); |
| 80 | + } |
| 81 | +} |
0 commit comments