Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the issues when parent element has a CSS scale transform applied to it #123

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions projects/angular2-draggable/src/lib/angular-draggable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
*/
private _helperBlock: HelperBlock = null;

/**
* Flag to indicate whether the element is dragged once after being initialised
*/
private isDragged: boolean = false;

@Output() started = new EventEmitter<any>();
@Output() stopped = new EventEmitter<any>();
@Output() edge = new EventEmitter<any>();
Expand Down Expand Up @@ -102,7 +107,6 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
let element = this.handle ? this.handle : this.el.nativeElement;
this.renderer.addClass(element, 'ng-draggable');
}

this.resetPosition();
}

Expand Down Expand Up @@ -133,6 +137,13 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
this.needTransform = true;
}
}

if (changes['scale'] && !changes['scale'].isFirstChange()) {
let temp = this.currTrans.value;
temp.x = temp.x * this.scale;
temp.y = temp.y * this.scale;
this.oldTrans.set(new Position(temp.x, temp.y));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xieziyu - you may want to optimize this block or may have a better approach for it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it's great. Thanks.

}
}

ngAfterViewInit() {
Expand Down Expand Up @@ -178,12 +189,25 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
translateY = Math.round(translateY / this.gridSize) * this.gridSize;
}

let value = `translate(${translateX}px, ${translateY}px)`;
// done to prevent the element from bouncing off when
// the parent element is scaled and element is dragged for first time
if (this.tempTrans.x !== 0 || this.tempTrans.y !== 0) {
if (this.isDragged === false) {
let temp = this.currTrans.value;
temp.x = temp.x * this.scale;
temp.y = temp.y * this.scale;
this.oldTrans.set(new Position(temp.x, temp.y));
}
this.isDragged = true;
}

if (this.scale !== 1) {
value += ` scale(${this.scale})`;
if (this.scale && this.scale !== 0 && this.isDragged) {
translateX = translateX / this.scale;
translateY = translateY / this.scale;
}

let value = `translate(${translateX}px, ${translateY}px)`;

this.renderer.setStyle(this.el.nativeElement, 'transform', value);
this.renderer.setStyle(this.el.nativeElement, '-webkit-transform', value);
this.renderer.setStyle(this.el.nativeElement, '-ms-transform', value);
Expand Down