Skip to content

Commit

Permalink
Dragging methods are now private
Browse files Browse the repository at this point in the history
  • Loading branch information
darkomtc11 authored and Maxime Lafarie committed Feb 21, 2020
1 parent b524bef commit 5307d82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit

/**
* Set positionX and positionY to save last position of dragged modal
* @param posX position X
* @param posY position Y
*/
public setPosition(posX: number, posY: number) {
this.positionX = posX;
Expand All @@ -151,6 +153,8 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit

/**
* Moves dialog by changing top and left style of modal dialog by offset
* @param offsetX modal's left offset
* @param offsetY modal's top offset
*/
public moveDialog(offsetX: number, offsetY: number) {
if (!this.nsmDialog.length) {
Expand All @@ -163,9 +167,10 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit

/**
* Listens for mouse down event to initiate dragging of the modal
* @param e MouseEvent
*/
@HostListener('document:mousedown', ['$event'])
public startDrag(e: MouseEvent) {
private startDrag(e: MouseEvent) {
if (!this.nsmContent.length || !this.draggable) {
return false;
}
Expand All @@ -187,9 +192,10 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit

/**
* Listens for mouse move event and reflects the movement of the mouse to modal position
* @param e MouseEvent
*/
@HostListener('document:mousemove', ['$event'])
public elementDrag(e: MouseEvent) {
private elementDrag(e: MouseEvent) {
if (!this.dragging || !this.nsmDialog.length) {
return false;
}
Expand All @@ -209,7 +215,7 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
* Listens for mouse up event to stop moving dragged modal
*/
@HostListener('document:mouseup', ['$event'])
public stopDrag() {
private stopDrag() {
this.dragging = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe('NgxSmartModalComponent', () => {
it('should not startDrag if modal not draggable', async(() => {
component.draggable = false;

let result = component.startDrag({} as MouseEvent);
let result = component['startDrag']({} as MouseEvent);
expect((component as any).dragging).toBeFalsy();
expect(result).toBe(false);
}));
Expand All @@ -284,7 +284,7 @@ describe('NgxSmartModalComponent', () => {
component.draggable = true;
(component as any).nsmContent = { length: 0 };

let result = component.startDrag({} as MouseEvent);
let result = component['startDrag']({} as MouseEvent);
expect((component as any).dragging).toBeFalsy();
expect(result).toBe(false);
}));
Expand All @@ -294,7 +294,7 @@ describe('NgxSmartModalComponent', () => {
(component as any).nsmContent = { length: 1 };
const fakeEvent = { srcElement: null };

let result = component.startDrag(fakeEvent as MouseEvent);
let result = component['startDrag'](fakeEvent as MouseEvent);
expect((component as any).dragging).toBeFalsy();
expect(result).toBe(false);
}));
Expand All @@ -305,7 +305,7 @@ describe('NgxSmartModalComponent', () => {
const fakeSrcElement = document.createElement('div') as any;
const fakeEvent = { srcElement: fakeSrcElement };

const result = component.startDrag(fakeEvent as MouseEvent);
const result = component['startDrag'](fakeEvent as MouseEvent);
expect((component as any).dragging).toBeFalsy();
expect(result).toBeFalsy();
}));
Expand All @@ -318,7 +318,7 @@ describe('NgxSmartModalComponent', () => {
(component as any).nsmContent = { 'length': 1, 'last': { 'nativeElement': { 'contains': (el: any) => el == fakeSrcElement1 } } };
const fakeEvent = { clientX: 10, clientY: 30, srcElement: fakeSrcElement2 };

const result = component.startDrag(fakeEvent as MouseEvent);
const result = component['startDrag'](fakeEvent as MouseEvent);
expect((component as any).dragging).toBeFalsy();
expect(result).toBeFalsy();
}));
Expand All @@ -332,7 +332,7 @@ describe('NgxSmartModalComponent', () => {

spyOn(component, 'setPosition');

const result = component.startDrag(fakeEvent as MouseEvent);
const result = component['startDrag'](fakeEvent as MouseEvent);
expect(result).toBeTruthy();
expect((component as any).dragging).toBeTruthy();
expect(component.setPosition).toHaveBeenCalledWith(fakeEvent.clientX, fakeEvent.clientY);
Expand All @@ -341,15 +341,15 @@ describe('NgxSmartModalComponent', () => {
it('should not elementDrag if not started dragging', async(() => {
(component as any).dragging = false;

const result = component.elementDrag({} as MouseEvent);
const result = component['elementDrag']({} as MouseEvent);
expect(result).toBeFalsy();
}));

it('should not elementDrag if event there is no nmsDialog', async(() => {
(component as any).dragging = true;
(component as any).nsmDialog = { length: 0 };

const result = component.elementDrag({} as MouseEvent);
const result = component['elementDrag']({} as MouseEvent);
expect(result).toBeFalsy();
}));

Expand All @@ -368,7 +368,7 @@ describe('NgxSmartModalComponent', () => {
spyOn((component), 'moveDialog');
spyOn((component), 'setPosition');

const result = component.elementDrag(fakeEvent as MouseEvent);
const result = component['elementDrag'](fakeEvent as MouseEvent);

expect(component.moveDialog).toHaveBeenCalledWith(offsetX, offsetY);
expect(component.setPosition).toHaveBeenCalledWith(fakeEvent.clientX, fakeEvent.clientY);
Expand All @@ -377,7 +377,7 @@ describe('NgxSmartModalComponent', () => {
}));

it('should stopDrag', async(() => {
component.stopDrag();
component['stopDrag']();
expect((component as any).dragging).toBeFalsy();
}));

Expand Down

0 comments on commit 5307d82

Please sign in to comment.