1.0.0-alpha.11 • Published 1 year ago

ngx-explorer-dnd v1.0.0-alpha.11

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

NGX Explorer DnD

Drag & Drop in Angular like in a desktop explorer!

  • Select with mouse (selection rect)
  • Drag & Drop single or multiple files and folders
  • Sorting components in a grid new
  • Feel the ✨magic✨
  • Test it on Stackblitz!

dnd-explorer-dnd-gif

sort-grid

Getting Started

Installation

Install the ngx-explorer-dnd package from npm

npm install ngx-explorer-dnd --save

or use ng add

ng add ngx-explorer-dnd

Usage

HTML Part

Add the ngxExplorerDndContainer directive to your container component. If you wanna use the selection rect, too add it with ngxDragSelection. Add ngxExplorerDndElement to the components to drag, so the file and folders as example.

<div
  class="outer-container"
  ngxExplorerDndContainer
  (dragInProgress)="dragInProgress($event)"
  (selectedElementsChange)="selectedElementsChange($event)"
  [selectionAllowed]="!isDragInProgress"
  (drop)="drop($event)"
  ngxDragSelection
>
  <app-file ngxExplorerDndElement *ngFor="let item of files"> </app-file>
  <app-folder ngxExplorerDndElement *ngFor="let item of files"></app-folder>
</div>
Sorting Function

If you wanna enable sorting so add the enableSorting property to the ngxExplorerDndElement and set it to true.

Code Behind

Simple we set a isDragInProgress boolean. So we don't wanna see the selection rect if we drag some elements. And if we use the selection rect and any elements are under the rect we wanna select it.

export class AppComponent {
  directories = ['Folder 1', 'Folder 2', 'Folder 3', 'Folder 4'];
  files = ['File 1', 'File 2', 'File 3', 'File 4'];

  isDragInProgress: boolean = false;

  @ViewChildren(FileFolder)
  fileFolderComponents!: QueryList<FileFolder>;

  constructor() { }

  dragInProgress(event: boolean) {
    this.isDragInProgress = event;
  }

  selectedElementsChange(event: { count: number; data: FileFolder[] }) {
    for (let _data of this.fileFolderComponents) {
      _data.selected = false;
    }

    for (let _data of event.data) {
      _data.selected = true;
    }
  }

  drop(event: any) {
    console.log(event);
  }
}
The File and Folder Components

If we add the optional FileFolder extension to our components we have access to properties like selected, type, position?, id?.

@Component({
  selector: 'app-file',
  templateUrl: './file.component.html',
  styleUrls: ['./file.component.scss'],
  providers: [
    { provide: FileFolder, useExisting: forwardRef(() => FileComponent) },
  ],
})
export class FileComponent extends FileFolder {}
Optional Target Component

You can set the ngxExplorerDndTarget directive to a folder as example. So if you drag elements into this folder component the drop event will have a target property which includes the optional data you give them.

API

ngxExplorerDndContainer

The highest container of all draggable components.

ngxExplorerDndContainer

The highest container of all draggable components.

ngxExplorerDndElement

A draggable component. Can be a file and a folder

ngxExplorerDndTarget

A target for the ngxExplorerDndElement components. It self can be a ngxExplorerDndElement, too.

ngxDragSelection

Best set this directive to the highest container; so the ngxExplorerDndContainer. Inside this container the selection rect will be show.

Properties and Events

Here are the properties and Events of the directives we have:

Properties

DirectivePropertyTypeDescription
ngxExplorerDndContainerdragDataanyAdd any optional data for the drop event.
ngxExplorerDndContainerbadgestring or undefinedIf set it shows a custom badge inside drag preview.
ngxExplorerDndContainercancelAnimationbooleanIf set to true it cancel the move back animation.
ngxExplorerDndContainerenableSortingbooleanIf set to true sorting is enabled.
ngxDragSelectionselectionAllowedbooleanSet if the selection rect can be showed.
ngxDragSelectionselectionDivElementHTMLElement or nullSet a custom selection rect with custom styles.
ngxExplorerDndElementdndElementDataanyAny optional data for the drop event.
ngxExplorerDndTargetdndTargetDataanyAny optional data for the drop event.
Events

DirectiveEventTypeDescription
ngxExplorerDndContainerdragInProgressEventEmitterEmitted when drag progress was started.
ngxExplorerDndContainerdropEventEmitter<{ item: any, target: any, oprionalDragData?: any, oldIndex: numbernull, newIndex: numbernull }>Occurs on ngxExplorerDndElement will be dropped.
ngxExplorerDndContainertargetChangeEventEmitter<{ target: any }>Occurs on any ngxExplorerDndTarget is under mouse while dragging.
ngxDragSelectionselectedElementsChangeEventEmitter<{ count: number, data: FileFolder[] }>Occurs when selected Elements are changed.

Example

A example says more than 1000 words. So play around with Stackblitz!

Version History

  • Alpha 10: Added sorting functionality
  • Alpha 1 to 9: First release and bug fixes

Authors

  • Florian Heuberger (Flo0806)

License

MIT License © Andrea SonnY

Last But Not Least

If you like the project so give it a star! 😎