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

Added a dragging feature #229

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
478213c
Added a dragging feature
darkomtc11 Sep 24, 2019
d26f607
SCSS new line and bracket fix
darkomtc11 Sep 24, 2019
af449b0
Improved dragging feature
darkomtc11 Oct 2, 2019
f20dad9
Simplifying if statement
darkomtc11 Oct 2, 2019
ad0f44e
Refactoring dragging methods and unit tests.
darkomtc11 Oct 3, 2019
14aed23
Fixed requested changes
darkomtc11 Oct 3, 2019
f6fe90e
Merge branch 'master' into feature/draggable
darkomtc11 Oct 3, 2019
f2e1990
New line in template for more visibility.
darkomtc11 Oct 3, 2019
b7e3cad
Merge remote-tracking branch 'origin/feature/draggable' into feature/…
darkomtc11 Oct 3, 2019
196f355
Dragging methods are now public
darkomtc11 Oct 3, 2019
08228af
Fixed some of syntax suggestion 'errors' for continuous-integration bot.
darkomtc11 Oct 3, 2019
cd96a2c
New line fix
darkomtc11 Oct 3, 2019
17094ba
Merged
darkomtc11 Oct 3, 2019
80dae44
Dragging methods are now private
darkomtc11 Oct 3, 2019
8e2b6f8
Package.json removed
darkomtc11 Oct 3, 2019
6102ff7
package-lock.json removed
darkomtc11 Oct 3, 2019
8449630
Merge remote-tracking branch 'origin/feature/draggable' into feature/…
darkomtc11 Oct 3, 2019
31ec9fa
package-lock.json revert
darkomtc11 Oct 4, 2019
22b0f0c
Merge branch 'master' into feature/draggable
darkomtc11 Nov 29, 2019
3203ab5
Added space where needed
darkomtc11 Dec 22, 2019
2e5cb19
Removed new line
darkomtc11 Dec 22, 2019
85e96bf
Removed new line.
darkomtc11 Dec 22, 2019
75dda4e
Merge branch 'master' into feature/draggable
darkomtc11 Jan 7, 2020
7fee944
Merge branch 'master' into feature/draggable
darkomtc11 Feb 3, 2020
1968ffa
package-lock closed bracket
darkomtc11 Feb 3, 2020
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
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/app/demo/main/main.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ <h1 class="title">ngx-smart-modal</h1>
style="position:absolute;top: 300px;left: 900px;">Custom target modal</button>
<button class="button -regular"
(click)="goNewDynamic()">Create dynamic modal<span>new</span></button>
<button class="button -regular"
(click)="draggableModal.open()">Draggable modal<span>new</span></button>
</div>

<h3 class="title">Examples with CSS frameworks</h3>
Expand Down Expand Up @@ -331,3 +333,27 @@ <h1>Hey, I am a modal placed relatively to an element!</h1>
<button class="button -dark"
(click)="customTarget.close()">Close</button>
</ngx-smart-modal>

<ngx-smart-modal #draggableModal
[draggable]="true"
[draggableEdges]="true"
identifier="draggableModal">
<h1>Hey, you can drag me anywhere by holding my edges or handle!</h1>
<p>Add class ".draggable" to any element inside modal to make it a drag handle.</p><br>
<span class="draggable">Handle</span>
<span>Not handle</span>
<br><br>
<button class="button -regular" (click)="secondDraggableModal.open()">Second draggable
modal<span>new</span></button>

darkomtc11 marked this conversation as resolved.
Show resolved Hide resolved
</ngx-smart-modal>

<ngx-smart-modal #secondDraggableModal
[draggable]="true"
identifier="secondDraggableModal">
<h1>Hey, I will move independently from my parent!</h1>
<p>I can only be moved by a handle.</p><br>
<span class="draggable">Handle</span>
<span>Not handle</span>
<br><br>
</ngx-smart-modal>
8 changes: 8 additions & 0 deletions src/app/demo/main/main.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@
}
}
}

span.draggable{
color:white;
darkomtc11 marked this conversation as resolved.
Show resolved Hide resolved
padding: 5px;
text-align: center;
background-color:gray;
margin-right: 10px;
}
83 changes: 81 additions & 2 deletions src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import { NgxSmartModalConfig } from '../config/ngx-smart-modal.config';
[ngClass]="{'transparent':!backdrop, 'overlay':true, 'nsm-overlay-open':openedClass}"
(mousedown)="dismiss($event)" #nsmOverlay>
<div [style.z-index]="visible ? layerPosition : -1"
[ngClass]="['nsm-dialog', customClass, openedClass ? 'nsm-dialog-open': 'nsm-dialog-close']" #nsmDialog>
<div class="nsm-content" #nsmContent>
[ngClass]="['nsm-dialog', customClass, openedClass ? 'nsm-dialog-open': 'nsm-dialog-close']" [style.position]="draggable?'absolute':'relative'" #nsmDialog>
darkomtc11 marked this conversation as resolved.
Show resolved Hide resolved
<div class="nsm-content" #nsmContent [class.draggable]="draggable && draggableEdges">
<div class="nsm-body">
<ng-content></ng-content>
</div>
Expand All @@ -49,6 +49,8 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {
@Input() public hideDelay: number = 500;
@Input() public autostart: boolean = false;
@Input() public target: string = '';
@Input() public draggable: boolean = false;
@Input() public draggableEdges: boolean = false;

@Output() public visibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
@Output() public onClose: EventEmitter<any> = new EventEmitter();
Expand All @@ -71,6 +73,10 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {

private _data: any;

private positionX = 0;
private positionY = 0;
private dragging = false;

@ViewChildren('nsmContent') private nsmContent!: QueryList<ElementRef>;
@ViewChildren('nsmDialog') private nsmDialog!: QueryList<ElementRef>;
@ViewChildren('nsmOverlay') private nsmOverlay!: QueryList<ElementRef>;
Expand All @@ -92,6 +98,79 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy {
this._sendEvent('delete');
}

/**
* Set positionX and positionY to save last position of dragged modal
*/
public setPosition(posX: number, posY: number) {
this.positionX = posX;
this.positionY = posY;
}

/**
* Moves dialog by changing top and left style of modal dialog by offset
*/
public moveDialog(offsetX: number, offsetY: number) {
if (!this.nsmDialog.length) {
return false;
}
this.nsmDialog.last.nativeElement.style.top = (this.nsmDialog.last.nativeElement.offsetTop - offsetY) + "px";
this.nsmDialog.last.nativeElement.style.left = (this.nsmDialog.last.nativeElement.offsetLeft - offsetX) + "px";
return true;
}

/**
* Listens for mouse down event to initiate dragging of the modal
*/
@HostListener('document:mousedown', ['$event'])
startDrag(e: MouseEvent) {
if (!this.nsmContent.length || !this.draggable) {
return false;
}

const src = e.srcElement as HTMLElement;
if (src && src.classList.contains('draggable')) {
if (this.nsmContent.last.nativeElement.contains(src) && !this.dragging) {
e.preventDefault();

this.dragging = true;
this.setPosition(e.clientX, e.clientY);

return true;
}
}

return false;
}

/**
* Listens for mouse move event and reflects the movement of the mouse to modal position
*/
@HostListener('document:mousemove', ['$event'])
elementDrag(e: MouseEvent) {
if (!this.dragging || !this.nsmDialog.length) {
return false;
}
e.preventDefault();

const offsetX = this.positionX - e.clientX;
const offsetY = this.positionY - e.clientY;

this.moveDialog(offsetX, offsetY);

this.setPosition(e.clientX, e.clientY);

return true;
}


/**
* Listens for mouse up event to stop moving dragged modal
*/
@HostListener('document:mouseup', ['$event'])
stopDrag() {
this.dragging = false;
}

/**
* Open the modal instance
*
Expand Down
10 changes: 10 additions & 0 deletions src/ngx-smart-modal/src/ngx-smart-modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ body.dialog-open {
bottom: 0;
left: 0;
right: 0;
display: flex;
justify-content: center;
overflow-x: hidden;
overflow-y: auto;
transition: background-color $transition-duration;
Expand Down Expand Up @@ -165,3 +167,11 @@ body.dialog-open {
transform: translate3d(0, 50%, 0);
}
}

.draggable {
cursor: move;

div {
cursor: default;
}
}
Loading