Skip to content

Commit c2d9e28

Browse files
author
Xie, Ziyu
authored
Merge pull request #133 from xieziyu/patch-2.2.0
Release v2.2.0
2 parents 3e4c1a4 + 5b0e539 commit c2d9e28

File tree

13 files changed

+2094
-1357
lines changed

13 files changed

+2094
-1357
lines changed

README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ 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**:
32+
+ Performance update. Fix [issue #112](https://github.com/xieziyu/angular2-draggable/issues/112): Control change detection with HostListener events.
33+
+ Fix [issue #128](https://github.com/xieziyu/angular2-draggable/issues/128): Multiple phone draggables at the same time.
34+
+ New `[lockAxis]` input.
35+
+ **ngResizable**:
36+
+ Fix [issue #132](https://github.com/xieziyu/angular2-draggable/issues/132): Aspect ratio feature exits Y-Axis boundary on resize.
37+
3038
+ 2018.11.29: 2.1.9
3139
+ **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))
3240

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

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)
5856

5957
# Installation
6058
```
@@ -140,7 +138,8 @@ Well you can use both directives concurrently if you wish:
140138
| gridSize | number | 1 | Use it for snapping to grid. Refer to [demo](https://xieziyu.github.io/angular2-draggable/#/advance/snap-grid) |
141139
| preventDefaultEvent | boolean | `false` | Whether to prevent default mouse event |
142140
| scale | number | 1 | Set it when parent element has CSS transform scale |
143-
141+
| lockAxis | `'x' | 'y'` | null | Restrict dragging to a specific axis by locking another one |
142+
144143
+ `ngResizable` directive support following input porperties:
145144

146145
| Input | Type | Default | Description |

package.json

+23-23
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

+1-1
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

+53-15
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
@@ -77,6 +80,9 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
7780
/** Set initial position by offsets */
7881
@Input() position: IPosition = { x: 0, y: 0 };
7982

83+
/** Lock axis: 'x' or 'y' */
84+
@Input() lockAxis: string = null;
85+
8086
/** Emit position offsets when moving */
8187
@Output() movingOffset = new EventEmitter<IPosition>();
8288

@@ -88,7 +94,7 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
8894
if (setting !== undefined && setting !== null && setting !== '') {
8995
this.allowDrag = !!setting;
9096

91-
let element = this.handle ? this.handle : this.el.nativeElement;
97+
let element = this.getDragEl();
9298

9399
if (this.allowDrag) {
94100
this.renderer.addClass(element, 'ng-draggable');
@@ -104,7 +110,7 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
104110

105111
ngOnInit() {
106112
if (this.allowDrag) {
107-
let element = this.handle ? this.handle : this.el.nativeElement;
113+
let element = this.getDragEl();
108114
this.renderer.addClass(element, 'ng-draggable');
109115
}
110116
this.resetPosition();
@@ -119,6 +125,10 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
119125
this.currTrans = null;
120126
this._helperBlock.dispose();
121127
this._helperBlock = null;
128+
129+
if (this.draggingSub) {
130+
this.draggingSub.unsubscribe();
131+
}
122132
}
123133

124134
ngOnChanges(changes: SimpleChanges) {
@@ -152,6 +162,10 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
152162
}
153163
}
154164

165+
private getDragEl() {
166+
return this.handle ? this.handle : this.el.nativeElement;
167+
}
168+
155169
resetPosition() {
156170
if (Position.isIPosition(this.position)) {
157171
this.oldTrans.set(this.position);
@@ -177,10 +191,17 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
177191
}
178192

179193
private transform() {
180-
181194
let translateX = this.tempTrans.x + this.oldTrans.x;
182195
let translateY = this.tempTrans.y + this.oldTrans.y;
183196

197+
if (this.lockAxis === 'x') {
198+
translateX = this.oldTrans.x;
199+
this.tempTrans.x = 0;
200+
} else if (this.lockAxis === 'y') {
201+
translateY = this.oldTrans.y;
202+
this.tempTrans.y = 0;
203+
}
204+
184205
// Snap to grid: by grid size
185206
if (this.gridSize > 1) {
186207
translateX = Math.round(translateX / this.gridSize) * this.gridSize;
@@ -230,9 +251,30 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
230251
if (!this.moving) {
231252
this.started.emit(this.el.nativeElement);
232253
this.moving = true;
254+
255+
/**
256+
* Fix performance issue:
257+
* https://github.com/xieziyu/angular2-draggable/issues/112
258+
*/
259+
this.subscribeEvents();
233260
}
234261
}
235262

263+
private subscribeEvents() {
264+
this.draggingSub = fromEvent(document, 'mousemove', { passive: false }).subscribe(event => this.onMouseMove(event as MouseEvent));
265+
this.draggingSub.add(fromEvent(document, 'touchmove', { passive: false }).subscribe(event => this.onMouseMove(event as TouchEvent)));
266+
this.draggingSub.add(fromEvent(document, 'mouseup', { passive: false }).subscribe(() => this.putBack()));
267+
this.draggingSub.add(fromEvent(document, 'mouseleave', { passive: false }).subscribe(() => this.putBack()));
268+
this.draggingSub.add(fromEvent(document, 'touchend', { passive: false }).subscribe(() => this.putBack()));
269+
this.draggingSub.add(fromEvent(document, 'touchcancel', { passive: false }).subscribe(() => this.putBack()));
270+
271+
}
272+
273+
private unsubscribeEvents() {
274+
this.draggingSub.unsubscribe();
275+
this.draggingSub = null;
276+
}
277+
236278
boundsCheck() {
237279
if (this.bounds) {
238280
let boundary = this.bounds.getBoundingClientRect();
@@ -317,6 +359,12 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
317359
if (!this.trackPosition) {
318360
this.transform();
319361
}
362+
363+
/**
364+
* Fix performance issue:
365+
* https://github.com/xieziyu/angular2-draggable/issues/112
366+
*/
367+
this.unsubscribeEvents();
320368
}
321369
}
322370

@@ -366,20 +414,10 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
366414
event.preventDefault();
367415
}
368416

369-
this.orignal = Position.fromEvent(event);
417+
this.orignal = Position.fromEvent(event, this.getDragEl());
370418
this.pickUp();
371419
}
372420

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'])
383421
onMouseMove(event: MouseEvent | TouchEvent) {
384422
if (this.moving && this.allowDrag) {
385423
if (this.preventDefaultEvent) {
@@ -389,7 +427,7 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
389427

390428
// Add a transparent helper div:
391429
this._helperBlock.add();
392-
this.moveTo(Position.fromEvent(event));
430+
this.moveTo(Position.fromEvent(event, this.getDragEl()));
393431
}
394432
}
395433
}

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

+46-9
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));
@@ -434,8 +447,10 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
434447
if (this._containment) {
435448
const maxWidth = this._bounding.width - this._bounding.pr - this.el.nativeElement.offsetLeft - this._bounding.translateX;
436449
const maxHeight = this._bounding.height - this._bounding.pb - this.el.nativeElement.offsetTop - this._bounding.translateY;
450+
const minHeight = !this.rzMinHeight ? 1 : this.rzMinHeight;
451+
const minWidth = !this.rzMinWidth ? 1 : this.rzMinWidth;
437452

438-
if (this._direction.n && (this._currPos.y + this._bounding.translateY) < 0) {
453+
if (this._direction.n && (this._currPos.y + this._bounding.translateY < 0)) {
439454
this._currPos.y = -this._bounding.translateY;
440455
this._currSize.height = this._origSize.height + this._origPos.y + this._bounding.translateY;
441456
}
@@ -452,6 +467,28 @@ export class AngularResizableDirective implements OnInit, OnChanges, OnDestroy,
452467
if (this._currSize.height > maxHeight) {
453468
this._currSize.height = maxHeight;
454469
}
470+
471+
/**
472+
* Fix Issue: Additional check for aspect ratio
473+
* https://github.com/xieziyu/angular2-draggable/issues/132
474+
*/
475+
if (this._aspectRatio) {
476+
if ((this._currSize.width / this._aspectRatio) > maxHeight) {
477+
this._currSize.width = maxHeight * this._aspectRatio;
478+
479+
if (this._direction.w) {
480+
this._currPos.x = this._origPos.x;
481+
}
482+
}
483+
484+
if ((this._currSize.height * this._aspectRatio) > maxWidth) {
485+
this._currSize.height = maxWidth / this._aspectRatio;
486+
487+
if (this._direction.n) {
488+
this._currPos.y = this._origPos.y;
489+
}
490+
}
491+
}
455492
}
456493
}
457494

0 commit comments

Comments
 (0)