Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
feat: drag drop directive
Browse files Browse the repository at this point in the history
  • Loading branch information
artemnih committed Mar 28, 2022
1 parent 9e2361e commit 6eb32cd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DragDropDirective } from './drag-drop.directive';

describe('DragDropDirective', () => {
it('should create an instance', () => {
const directive = new DragDropDirective();
expect(directive).toBeTruthy();
});
});
42 changes: 42 additions & 0 deletions projects/ngx-explorer/src/lib/directives/drag-drop.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Directive, EventEmitter, HostListener, Output } from '@angular/core';

@Directive({
selector: '[nxeDragDrop]'
})
export class DragDropDirective {
@Output() dragEnter = new EventEmitter<any>();
@Output() dragOver = new EventEmitter<any>();
@Output() dragLeave = new EventEmitter<any>();
@Output() dragDropped = new EventEmitter<any>();

@HostListener('dragenter', ['$event'])
public onDragEnter(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
this.dragEnter.emit(event);
}

@HostListener('dragover', ['$event'])
public onDragOver(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
this.dragOver.emit(event);
}

@HostListener('dragleave', ['$event'])
public onDragLeave(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
this.dragLeave.emit(event);
}

@HostListener('dragdrop', ['$event'])
public onDrop(event: DragEvent) {
event.preventDefault();
event.stopPropagation();
const files = event.dataTransfer.files;
if (files.length > 0) {
this.dragDropped.emit(files);
}
}
}
2 changes: 2 additions & 0 deletions projects/ngx-explorer/src/lib/ngx-explorer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SecondMenuBarComponent } from './components/second-menu-bar/second-menu
import { ViewSwitcherComponent } from './components/view-switcher/view-switcher.component';
import { TreeComponent } from './components/tree/tree.component';
import { FilterComponent } from './components/filter/filter.component';
import { DragDropDirective } from './directives/drag-drop.directive';

@NgModule({
declarations: [
Expand All @@ -21,6 +22,7 @@ import { FilterComponent } from './components/filter/filter.component';
ViewSwitcherComponent,
TreeComponent,
FilterComponent,
DragDropDirective,
],
imports: [
CommonModule
Expand Down
1 change: 1 addition & 0 deletions projects/ngx-explorer/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from './lib/components/tree/tree.component';
export * from './lib/components/list/list.component';
export * from './lib/components/filter/filter.component';
export * from './lib/shared/utils';
export * from './lib/directives/drag-drop.directive';

0 comments on commit 6eb32cd

Please sign in to comment.