Skip to content

Commit 2c7b2da

Browse files
author
Xie, Ziyu
committed
Subscribe to document events only when dragging
1 parent 3e4c1a4 commit 2c7b2da

File tree

6 files changed

+1985
-1336
lines changed

6 files changed

+1985
-1336
lines changed

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ angular2-draggable has angular directives that make the DOM element draggable an
2727
+ provided since v2.0, requires Angular >= 6
2828

2929
# Latest Update
30+
+ 2018.12.22: 2.2.0
31+
+ **ngDraggable**: Performance update. Fix [issue #112](https://github.com/xieziyu/angular2-draggable/issues/112): Control change detection with HostListener events.
32+
33+
3034
+ 2018.11.29: 2.1.9
3135
+ **ngDraggable**: fix [issue #31](https://github.com/xieziyu/angular2-draggable/issues/31): Problems when scale transform applied to parent. ([by rathodsanjay](https://github.com/rathodsanjay) - [PR #123](https://github.com/xieziyu/angular2-draggable/pull/123))
3236

@@ -45,16 +49,6 @@ angular2-draggable has angular directives that make the DOM element draggable an
4549
+ 2018.08.03: 2.0.0
4650
+ Fix [issue #84](https://github.com/xieziyu/angular2-draggable/issues/84): iFrames, and context unrelated elements block all events, and are unusable
4751

48-
+ 2018.07.02: 2.0.0-beta.2
49-
+ **ngResizable**: Provide `[rzAspectRatio]`, whether the element should be constrained to a specific aspect ratio. [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/aspect-ratio)
50-
+ **ngResizable**: Provide `[rzContainment]`, constrains resizing to within the bounds of the specified element or region. [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/containment)
51-
52-
+ 2018.06.26: 2.0.0-beta.1
53-
+ **ngResizable**: Provide `(rzStart)`, `(rzResizing)`, `(rzStop)` event emitters
54-
+ **ngResizable**: Provide `resetSize()`, `getStatus()` methods
55-
56-
+ 2018.06.25: 2.0.0-beta.0
57-
+ New: `ngResizable` directive which you can use to make the element resizable! More resizable options are planning. Refer to [demo](https://xieziyu.github.io/angular2-draggable/#/resizable/default)
5852

5953
# Installation
6054
```

package.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,36 @@
2121
},
2222
"private": true,
2323
"dependencies": {
24-
"@angular/animations": "^6.0.3",
25-
"@angular/common": "^6.0.3",
26-
"@angular/compiler": "^6.0.3",
27-
"@angular/core": "^6.0.3",
28-
"@angular/forms": "^6.0.3",
29-
"@angular/http": "^6.0.3",
30-
"@angular/platform-browser": "^6.0.3",
31-
"@angular/platform-browser-dynamic": "^6.0.3",
32-
"@angular/router": "^6.0.3",
24+
"@angular/animations": "^7.1.4",
25+
"@angular/common": "^7.1.4",
26+
"@angular/compiler": "^7.1.4",
27+
"@angular/core": "^7.1.4",
28+
"@angular/forms": "^7.1.4",
29+
"@angular/http": "^7.1.4",
30+
"@angular/platform-browser": "^7.1.4",
31+
"@angular/platform-browser-dynamic": "^7.1.4",
32+
"@angular/router": "^7.1.4",
3333
"classlist.js": "^1.1.20150312",
3434
"core-js": "^2.5.4",
3535
"font-awesome": "^4.7.0",
3636
"ngx-bootstrap": "3.0.1",
37-
"ngx-markdown": "^1.6.0",
38-
"rxjs": "^6.0.0",
37+
"ngx-markdown": "^7.0.3",
38+
"rxjs": "^6.3.3",
39+
"tslib": "^1.9.0",
3940
"zone.js": "^0.8.26"
4041
},
4142
"devDependencies": {
42-
"@angular-devkit/build-angular": "~0.6.8",
43-
"@angular-devkit/build-ng-packagr": "~0.6.8",
44-
"@angular/cli": "~6.0.8",
45-
"@angular/compiler-cli": "^6.0.3",
46-
"@angular/language-service": "^6.0.3",
43+
"@angular-devkit/build-angular": "~0.11.0",
44+
"@angular-devkit/build-ng-packagr": "~0.11.0",
45+
"@angular/cli": "~7.1.4",
46+
"@angular/compiler-cli": "^7.1.4",
47+
"@angular/language-service": "^7.1.4",
4748
"@compodoc/compodoc": "^1.1.2",
4849
"@types/jasmine": "~2.8.6",
4950
"@types/jasminewd2": "~2.0.3",
5051
"@types/node": "~8.9.4",
5152
"bootstrap": "^4.1.1",
52-
"codelyzer": "~4.2.1",
53+
"codelyzer": "~4.5.0",
5354
"cpr": "^3.0.1",
5455
"html-loader": "^0.5.5",
5556
"jasmine-core": "~2.99.1",
@@ -59,14 +60,13 @@
5960
"karma-coverage-istanbul-reporter": "~2.0.0",
6061
"karma-jasmine": "~1.1.1",
6162
"karma-jasmine-html-reporter": "^0.2.2",
62-
"ng-packagr": "^3.0.0-rc.2",
63+
"ng-packagr": "^4.2.0",
6364
"npm-run-all": "^4.1.3",
6465
"protractor": "~5.3.0",
6566
"raw-loader": "^0.5.1",
66-
"ts-node": "~5.0.1",
67-
"tsickle": ">=0.25.5",
68-
"tslib": "^1.7.1",
69-
"tslint": "~5.9.1",
70-
"typescript": "~2.7.2"
67+
"ts-node": "~7.0.0",
68+
"tsickle": ">=0.29.0",
69+
"tslint": "~5.11.0",
70+
"typescript": "~3.1.6"
7171
}
7272
}

projects/angular2-draggable/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-draggable",
3-
"version": "2.1.9",
3+
"version": "2.2.0",
44
"author": "Xie, Ziyu",
55
"license": "MIT",
66
"keywords": [

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

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

7+
import { Subscription, fromEvent } from 'rxjs';
78
import { IPosition, Position } from './models/position';
89
import { HelperBlock } from './widgets/helper-block';
910

@@ -22,6 +23,8 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
2223
private _zIndex = '';
2324
private needTransform = false;
2425

26+
private draggingSub: Subscription = null;
27+
2528
/**
2629
* Bugfix: iFrames, and context unrelated elements block all events, and are unusable
2730
* https://github.com/xieziyu/angular2-draggable/issues/84
@@ -119,6 +122,10 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
119122
this.currTrans = null;
120123
this._helperBlock.dispose();
121124
this._helperBlock = null;
125+
126+
if (this.draggingSub) {
127+
this.draggingSub.unsubscribe();
128+
}
122129
}
123130

124131
ngOnChanges(changes: SimpleChanges) {
@@ -230,9 +237,28 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
230237
if (!this.moving) {
231238
this.started.emit(this.el.nativeElement);
232239
this.moving = true;
240+
241+
/**
242+
* Fix performance issue:
243+
* https://github.com/xieziyu/angular2-draggable/issues/112
244+
*/
245+
this.subscribeEvents();
233246
}
234247
}
235248

249+
private subscribeEvents() {
250+
this.draggingSub = fromEvent(document, 'mousemove').subscribe(event => this.onMouseMove(event as MouseEvent));
251+
this.draggingSub.add(fromEvent(document, 'touchmove').subscribe(event => this.onMouseMove(event as TouchEvent)));
252+
this.draggingSub.add(fromEvent(document, 'mouseup').subscribe(() => this.putBack()));
253+
this.draggingSub.add(fromEvent(document, 'mouseleave').subscribe(() => this.putBack()));
254+
this.draggingSub.add(fromEvent(document, 'touchend').subscribe(() => this.putBack()));
255+
}
256+
257+
private unsubscribeEvents() {
258+
this.draggingSub.unsubscribe();
259+
this.draggingSub = null;
260+
}
261+
236262
boundsCheck() {
237263
if (this.bounds) {
238264
let boundary = this.bounds.getBoundingClientRect();
@@ -317,6 +343,12 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
317343
if (!this.trackPosition) {
318344
this.transform();
319345
}
346+
347+
/**
348+
* Fix performance issue:
349+
* https://github.com/xieziyu/angular2-draggable/issues/112
350+
*/
351+
this.unsubscribeEvents();
320352
}
321353
}
322354

@@ -370,16 +402,6 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
370402
this.pickUp();
371403
}
372404

373-
@HostListener('document:mouseup')
374-
@HostListener('document:mouseleave')
375-
@HostListener('document:touchend')
376-
@HostListener('document:touchcancel')
377-
onMouseLeave() {
378-
this.putBack();
379-
}
380-
381-
@HostListener('document:mousemove', ['$event'])
382-
@HostListener('document:touchmove', ['$event'])
383405
onMouseMove(event: MouseEvent | TouchEvent) {
384406
if (this.moving && this.allowDrag) {
385407
if (this.preventDefaultEvent) {

src/assets/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.2.0 (2018-12-22)
2+
3+
#### Bugfix
4+
+ **ngDraggable**: Performance update. Fix [issue #112](https://github.com/xieziyu/angular2-draggable/issues/112): Control change detection with HostListener events.
5+
6+
---
7+
18
## 2.1.9 (2018-11-29)
29

310
#### Bugfix

0 commit comments

Comments
 (0)