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

Enabled absolute positionning of the cloned node #45

Merged
merged 4 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.DS_Store
node_modules
coverage

*.iml
32 changes: 30 additions & 2 deletions src/resizable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ function getNewBoundingRectangle(startingRect: BoundingRectangle, edges: Edges,

}

function getElementRect(element: ElementRef, enableAbsolutePositioning: boolean): BoundingRectangle {
if (enableAbsolutePositioning) {
return {
top: element.nativeElement.offsetTop,
bottom: element.nativeElement.offsetHeight + element.nativeElement.offsetTop,
left: element.nativeElement.offsetLeft,
right: element.nativeElement.offsetWidth + element.nativeElement.offsetLeft
};
} else {
const boundingRect: BoundingRectangle = element.nativeElement.getBoundingClientRect();
return {
top: boundingRect.top,
bottom: boundingRect.bottom,
left: boundingRect.left,
right: boundingRect.right
};
}
}

function isWithinBoundingY({mouseY, rect}: {mouseY: number, rect: ClientRect}): boolean {
return mouseY >= rect.top && mouseY <= rect.bottom;
}
Expand Down Expand Up @@ -212,6 +231,11 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
*/
@Input() resizeCursorPrecision: number = 3;

/**
* If we want to position cloned node with absolute position
*/
@Input() enableAbsolutePositioning: boolean = false;
Copy link
Owner

Choose a reason for hiding this comment

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

To make this more verbose, can you rename this to

@Input() ghostElementPositioning: 'fixed' | 'absolute' = 'fixed';


/**
* Called when the mouse is pressed and a resize event is about to begin. `$event` is a `ResizeEvent` object.
*/
Expand Down Expand Up @@ -408,7 +432,7 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
if (currentResize) {
removeGhostElement();
}
const startingRect: BoundingRectangle = this.elm.nativeElement.getBoundingClientRect();
const startingRect: BoundingRectangle = getElementRect(this.elm, this.enableAbsolutePositioning);
currentResize = {
edges,
startingRect,
Expand All @@ -419,7 +443,11 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
const resizeCursors: ResizeCursors = Object.assign({}, DEFAULT_RESIZE_CURSORS, this.resizeCursors);
this.elm.nativeElement.parentElement.appendChild(currentResize.clonedNode);
this.renderer.setElementStyle(this.elm.nativeElement, 'visibility', 'hidden');
this.renderer.setElementStyle(currentResize.clonedNode, 'position', 'fixed');
if (this.enableAbsolutePositioning) {
this.renderer.setElementStyle(currentResize.clonedNode, 'position', 'absolute');
} else {
this.renderer.setElementStyle(currentResize.clonedNode, 'position', 'fixed');
}
this.renderer.setElementStyle(currentResize.clonedNode, 'left', `${currentResize.startingRect.left}px`);
this.renderer.setElementStyle(currentResize.clonedNode, 'top', `${currentResize.startingRect.top}px`);
this.renderer.setElementStyle(currentResize.clonedNode, 'height', `${currentResize.startingRect.height}px`);
Expand Down
3 changes: 2 additions & 1 deletion test/resizable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('resizable directive', () => {
[resizeSnapGrid]="resizeSnapGrid"
[resizeCursors]="resizeCursors"
[resizeCursorPrecision]="resizeCursorPrecision"
[enableAbsolutePositioning]="enableAbsolutePositioning"
(resizeStart)="resizeStart($event)"
(resizing)="resizing($event)"
(resizeEnd)="resizeEnd($event)">
Expand All @@ -48,7 +49,7 @@ describe('resizable directive', () => {
public resizeSnapGrid: Object = {};
public resizeCursors: Object = {};
public resizeCursorPrecision: number;

public enableAbsolutePositioning: boolean = false;
}

const triggerDomEvent: Function = (eventType: string, target: HTMLElement | Element, eventData: Object = {}) => {
Expand Down