|
1 | 1 | import { |
2 | 2 | Directive, ElementRef, Renderer2, |
3 | | - Input, Output, OnInit, HostListener, |
4 | | - EventEmitter, OnChanges, SimpleChanges, |
| 3 | + Input, Output, OnInit, EventEmitter, OnChanges, SimpleChanges, |
5 | 4 | OnDestroy, AfterViewInit |
6 | 5 | } from '@angular/core'; |
7 | 6 |
|
| 7 | +import { Subscription, fromEvent } from 'rxjs'; |
8 | 8 | import { HelperBlock } from './widgets/helper-block'; |
9 | 9 | import { ResizeHandle } from './widgets/resize-handle'; |
10 | 10 | import { ResizeHandleType } from './models/resize-handle-type'; |
@@ -49,6 +49,8 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy, |
49 | 49 | */ |
50 | 50 | private _helperBlock: HelperBlock = null; |
51 | 51 |
|
| 52 | + private draggingSub: Subscription = null; |
| 53 | + |
52 | 54 | /** Disables the resizable if set to false. */ |
53 | 55 | @Input() set ngResizable(v: any) { |
54 | 56 | if (v !== undefined && v !== null && v !== '') { |
@@ -296,22 +298,33 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy, |
296 | 298 | if (!this._handleResizing) { |
297 | 299 | this._origMousePos = Position.fromEvent(event); |
298 | 300 | this.startResize(handle); |
| 301 | + |
| 302 | + this.subscribeEvents(); |
299 | 303 | } |
300 | 304 | } |
301 | 305 |
|
302 | | - @HostListener('document:mouseup') |
303 | | - @HostListener('document:mouseleave') |
304 | | - @HostListener('document:touchend') |
305 | | - @HostListener('document:touchcancel') |
| 306 | + private subscribeEvents() { |
| 307 | + this.draggingSub = fromEvent(document, 'mousemove', { passive: false }).subscribe(event => this.onMouseMove(event as MouseEvent)); |
| 308 | + this.draggingSub.add(fromEvent(document, 'touchmove', { passive: false }).subscribe(event => this.onMouseMove(event as TouchEvent))); |
| 309 | + this.draggingSub.add(fromEvent(document, 'mouseup', { passive: false }).subscribe(() => this.onMouseLeave())); |
| 310 | + this.draggingSub.add(fromEvent(document, 'mouseleave', { passive: false }).subscribe(() => this.onMouseLeave())); |
| 311 | + this.draggingSub.add(fromEvent(document, 'touchend', { passive: false }).subscribe(() => this.onMouseLeave())); |
| 312 | + this.draggingSub.add(fromEvent(document, 'touchcancel', { passive: false }).subscribe(() => this.onMouseLeave())); |
| 313 | + } |
| 314 | + |
| 315 | + private unsubscribeEvents() { |
| 316 | + this.draggingSub.unsubscribe(); |
| 317 | + this.draggingSub = null; |
| 318 | + } |
| 319 | + |
306 | 320 | onMouseLeave() { |
307 | 321 | if (this._handleResizing) { |
308 | 322 | this.stopResize(); |
309 | 323 | this._origMousePos = null; |
| 324 | + this.unsubscribeEvents(); |
310 | 325 | } |
311 | 326 | } |
312 | 327 |
|
313 | | - @HostListener('document:mousemove', ['$event']) |
314 | | - @HostListener('document:touchmove', ['$event']) |
315 | 328 | onMouseMove(event: MouseEvent | TouchEvent) { |
316 | 329 | if (this._handleResizing && this._resizable && this._origMousePos && this._origPos && this._origSize) { |
317 | 330 | this.resizeTo(Position.fromEvent(event)); |
|
0 commit comments