Skip to content

Commit 5b0e539

Browse files
author
Xie, Ziyu
committed
Add lockAxis to ngDraggable
1 parent 625f627 commit 5b0e539

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ angular2-draggable has angular directives that make the DOM element draggable an
3131
+ **ngDraggable**:
3232
+ Performance update. Fix [issue #112](https://github.com/xieziyu/angular2-draggable/issues/112): Control change detection with HostListener events.
3333
+ Fix [issue #128](https://github.com/xieziyu/angular2-draggable/issues/128): Multiple phone draggables at the same time.
34+
+ New `[lockAxis]` input.
3435
+ **ngResizable**:
3536
+ Fix [issue #132](https://github.com/xieziyu/angular2-draggable/issues/132): Aspect ratio feature exits Y-Axis boundary on resize.
3637

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

143145
| Input | Type | Default | Description |

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
8080
/** Set initial position by offsets */
8181
@Input() position: IPosition = { x: 0, y: 0 };
8282

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

@@ -188,10 +191,17 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
188191
}
189192

190193
private transform() {
191-
192194
let translateX = this.tempTrans.x + this.oldTrans.x;
193195
let translateY = this.tempTrans.y + this.oldTrans.y;
194196

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+
195205
// Snap to grid: by grid size
196206
if (this.gridSize > 1) {
197207
translateX = Math.round(translateX / this.gridSize) * this.gridSize;

src/app/views/usage/options/options.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h4 class="card-title">Custom Options</h4>
1313
<p><button (click)="zIndexMoving = '99999'" class="btn btn-outline-primary">set [zIndexMoving] to 99999</button></p>
1414
<p><button (click)="preventDefaultEvent = !preventDefaultEvent" class="btn btn-outline-primary">toggle [preventDefaultEvent]</button></p>
1515
<p><button (click)="trackPosition = !trackPosition" class="btn btn-outline-primary">toggle [trackPosition]</button></p>
16+
<p><button (click)="lockAxis = lockAxis ? undefined : 'y'" class="btn btn-outline-primary">toggle [lockAxis]='y' </button></p>
1617
</div>
1718
<div class="col-sm-6">
1819
<div *ngIf="useHandle">
@@ -24,6 +25,7 @@ <h4 class="card-title">Custom Options</h4>
2425
[handle]="myHandle"
2526
[preventDefaultEvent]="preventDefaultEvent"
2627
[trackPosition]="trackPosition"
28+
[lockAxis]="lockAxis"
2729
class="drag-block-lg">
2830
<div #myHandle class="drag-block-handle"> #myHandle </div>
2931
<p>[handle]="myHandle"</p>
@@ -33,6 +35,7 @@ <h4 class="card-title">Custom Options</h4>
3335
<p>[zIndexMoving] = {{ zIndexMoving === undefined ? 'undefined' : zIndexMoving }}</p>
3436
<p>[preventDefaultEvent] = {{ preventDefaultEvent }}</p>
3537
<p>[trackPosition] = {{ trackPosition }}</p>
38+
<p>[lockAxis] = {{ lockAxis === undefined ? 'undefined' : lockAxis }}</p>
3639
</div>
3740
</div>
3841
<div *ngIf="!useHandle">
@@ -43,13 +46,15 @@ <h4 class="card-title">Custom Options</h4>
4346
[zIndexMoving]="zIndexMoving"
4447
[preventDefaultEvent]="preventDefaultEvent"
4548
[trackPosition]="trackPosition"
49+
[lockAxis]="lockAxis"
4650
class="drag-block-lg">
4751
<p>[ngDraggable] = {{ draggable }}</p>
4852
<p>[position] = {{ position === undefined ? 'undefined' : position | json }}</p>
4953
<p>[zIndex] = {{ zIndex === undefined ? 'undefined' : zIndex }}</p>
5054
<p>[zIndexMoving] = {{ zIndexMoving === undefined ? 'undefined' : zIndexMoving }}</p>
5155
<p>[preventDefaultEvent] = {{ preventDefaultEvent }}</p>
5256
<p>[trackPosition] = {{ trackPosition }}</p>
57+
<p>[lockAxis] = {{ lockAxis === undefined ? 'undefined' : lockAxis }}</p>
5358
</div>
5459
</div>
5560
</div>

src/app/views/usage/options/options.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class OptionsComponent implements OnInit {
1717
preventDefaultEvent = false;
1818
trackPosition = true;
1919
position;
20+
lockAxis;
2021

2122
constructor() { }
2223

src/assets/CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
## 2.2.0 (2018-12-22)
22

3+
#### New
4+
+ **ngDraggable**: add [lockAxis] input to restrict dragging to a specific axis by locking another one.
5+
36
#### Bugfix
47
+ **ngDraggable**:
5-
+ Performance update. Fix [issue #112](https://github.com/xieziyu/angular2-draggable/issues/112): Control change detection with HostListener events.
6-
+ Fix [issue #128](https://github.com/xieziyu/angular2-draggable/issues/128): Multiple phone draggables at the same time.
8+
+ fix [issue #112](https://github.com/xieziyu/angular2-draggable/issues/112): Control change detection with HostListener events. Performance updated.
9+
+ fix [issue #128](https://github.com/xieziyu/angular2-draggable/issues/128): Multiple phone draggables at the same time.
710
+ **ngResizable**:
8-
+ Fix [issue #132](https://github.com/xieziyu/angular2-draggable/issues/132): Aspect ratio feature exits Y-Axis boundary on resize.
11+
+ fix [issue #132](https://github.com/xieziyu/angular2-draggable/issues/132): Aspect ratio feature exits Y-Axis boundary on resize.
12+
+ Performance updated.
913
---
1014

1115
## 2.1.9 (2018-11-29)

0 commit comments

Comments
 (0)