Skip to content

Commit 48801bc

Browse files
author
Xie, Ziyu
committed
ngResizable: performance update (remove HostListener)
1 parent 6f71ed6 commit 48801bc

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

projects/angular2-draggable/src/lib/angular-draggable.directive.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
EventEmitter, OnChanges, SimpleChanges, OnDestroy, AfterViewInit
55
} from '@angular/core';
66

7-
import { Subscription, fromEvent, fromEventPattern } from 'rxjs';
7+
import { Subscription, fromEvent } from 'rxjs';
88
import { IPosition, Position } from './models/position';
99
import { HelperBlock } from './widgets/helper-block';
1010

@@ -256,6 +256,8 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
256256
this.draggingSub.add(fromEvent(document, 'mouseup', { passive: false }).subscribe(() => this.putBack()));
257257
this.draggingSub.add(fromEvent(document, 'mouseleave', { passive: false }).subscribe(() => this.putBack()));
258258
this.draggingSub.add(fromEvent(document, 'touchend', { passive: false }).subscribe(() => this.putBack()));
259+
this.draggingSub.add(fromEvent(document, 'touchcancel', { passive: false }).subscribe(() => this.putBack()));
260+
259261
}
260262

261263
private unsubscribeEvents() {

projects/angular2-draggable/src/lib/angular-resizable.directive.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
Directive, ElementRef, Renderer2,
3-
Input, Output, OnInit, HostListener,
4-
EventEmitter, OnChanges, SimpleChanges,
3+
Input, Output, OnInit, EventEmitter, OnChanges, SimpleChanges,
54
OnDestroy, AfterViewInit
65
} from '@angular/core';
76

7+
import { Subscription, fromEvent } from 'rxjs';
88
import { HelperBlock } from './widgets/helper-block';
99
import { ResizeHandle } from './widgets/resize-handle';
1010
import { ResizeHandleType } from './models/resize-handle-type';
@@ -49,6 +49,8 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
4949
*/
5050
private _helperBlock: HelperBlock = null;
5151

52+
private draggingSub: Subscription = null;
53+
5254
/** Disables the resizable if set to false. */
5355
@Input() set ngResizable(v: any) {
5456
if (v !== undefined && v !== null && v !== '') {
@@ -296,22 +298,33 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
296298
if (!this._handleResizing) {
297299
this._origMousePos = Position.fromEvent(event);
298300
this.startResize(handle);
301+
302+
this.subscribeEvents();
299303
}
300304
}
301305

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+
306320
onMouseLeave() {
307321
if (this._handleResizing) {
308322
this.stopResize();
309323
this._origMousePos = null;
324+
this.unsubscribeEvents();
310325
}
311326
}
312327

313-
@HostListener('document:mousemove', ['$event'])
314-
@HostListener('document:touchmove', ['$event'])
315328
onMouseMove(event: MouseEvent | TouchEvent) {
316329
if (this._handleResizing && this._resizable && this._origMousePos && this._origPos && this._origSize) {
317330
this.resizeTo(Position.fromEvent(event));

projects/angular2-draggable/src/lib/widgets/resize-handle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class ResizeHandle {
2828

2929
// create and register event listener
3030
this._onResize = (event) => { onMouseDown(event, this); };
31-
handle.addEventListener('mousedown', this._onResize);
32-
handle.addEventListener('touchstart', this._onResize);
31+
handle.addEventListener('mousedown', this._onResize, { passive: false });
32+
handle.addEventListener('touchstart', this._onResize, { passive: false });
3333

3434
// done
3535
this._handle = handle;

0 commit comments

Comments
 (0)