dnd 0.4.0

Dart Drag and Drop

Drag and Drop for Dart web apps with mouse and touch support.

Star this Repo Pub Package

GitHub | Pub | Demos and Examples

DnD Screenshot

Features

  • Use any HTML Element as Draggable or Dropzone.
  • Mouse and Touch dragging.
  • Draggable events: dragStart, drag, and dragEnd
  • Dropzone events: dragEnter, dragOver, dragLeave, and drop
  • Drag avatars as visual indication of a drag operation:
    • Original element as drag avatar.
    • Clone as drag avatar.
    • Custom drag avatar.
  • Support for Shadow DOM (Web Components, Custom Elements, Polymer, etc.).
  • Much more... see examples.

Usage

Before you read the instructions below, you should take a look at the examples.

Basic Set Up

Create a Draggable and give it some HTML elements; this will make them draggable. You can either pass a single Element to the constructor or an ElementList that is returned by querySelectorAll.

If you also want to drop somewhere, you'll need a Dropzone.

// Install draggable (no avatar).
Draggable draggable = new Draggable(querySelectorAll('.draggable'));

// Install dropzone.
Dropzone dropzone = new Dropzone(querySelector('.dropzone'));

You'll most likely want some drag avatar to show the user that a drag is going on. There are two predefined AvatarHandlers that you can use as follows. But you could also provide your own implementation of AvatarHandler.

// Draggable with clone as avatar.
Draggable draggable = new Draggable(querySelectorAll('.draggable'),
    avatarHandler: new AvatarHandler.clone());


// Draggable with original element as avatar.
Draggable draggable = new Draggable(querySelectorAll('.draggable'),
    avatarHandler: new AvatarHandler.original());

Draggable Options

The following options can be passed as named parameters to the constructor of Draggable:

  • avatarHandler: Is responsible for creating, position, and removing a drag avatar. A drag avatar provides visual feedback during a drag operation. Here are possible options (see above for an example):

    • null (the default) - will not create a drag avatar
    • new AvatarHandler.original() - handler that uses the original draggable as avatar. See OriginalAvatarHandler.
    • new AvatarHandler.clone() - handler that uses a clone of the draggable element as avatar. See CloneAvatarHandler.
    • A custom AvatarHandler - you can provide your own implementation of AvatarHandler.
  • horizontalOnly: If set to true, only horizontal dragging is tracked. This enables vertical touch dragging to be used for scrolling.

  • verticalOnly: If set to true, only vertical dragging is tracked. This enables horizontal touch dragging to be used for scrolling.

  • handle: If handle query String is specified, it restricts the dragging from starting unless it occurs on the specified element(s). Only elements that descend from the draggables elements are permitted.

  • cancel: If cancel query String is specified, drag starting is prevented on specified elements.

  • draggingClass: Is the css class set to the dragged element during a drag. If set to null, no such css class is added.

  • draggingClassBody: Is the css class set to the html body tag during a drag. If set to null, no such css class is added.

Draggable Events

Available event Streams on Draggable:

  • onDragStart: Fired when the user starts dragging.
    Note: onDragStart is fired not on touchStart or mouseDown but as soon as there is a drag movement. When a drag is started an onDrag event will also be fired.

  • onDrag: Fired periodically throughout the drag operation.

  • onDragEnd: Fired when the user ends the dragging.
    Note: Is also fired when the user clicks the 'esc'-key or the window loses focus.

Dropzone Options

The following options can be passed as named parameters to the constructor of Dropzone:

  • acceptor: Is used to determine which Draggables will be accepted by this Dropzone. If none is specified, all Draggables will be accepted.

  • overClass: Is the css class set to the dropzone element when an accepted draggable is dragged over it. If set to null, no such css class is added.

  • invalidClass: Is the css class set to the dropzone element when a not-accepted draggable is dragged over it. If set to null, no such css class is added.

Dropzone Events

Available event Streams on Dropzone:

  • onDragEnter: Fired when a Draggable enters this Dropzone.

  • onDragOver: Fired periodically while a Draggable is moved over a Dropzone.

  • onDragLeave: Fired when a Draggable leaves this Dropzone.

  • onDrop: Fired when a Draggable is dropped inside this Dropzone.

Note: Dropzone events are only fired when the Draggable is accepted by the Acceptor.

Shadow DOM

Web Components create a nice ecapsulation through Shadow DOM. But this creates a problem with dropzones inside the Shadow DOM as they never receive events because all events are captured by the host element. To make this work we need to retarget events to the Shadow DOM children through recursive elementFromPoint() calls.

For performance reasons it wouldn't make sense to retarget all drag and drop events. If you wish to retarget events to the Shadow DOM children, you must add a dnd-retarget attribute to the host:

// Retarget drag and drop events to Shadow DOM children.
<my-element dnd-retarget></my-element>

Attribution

The Dart Drag and Drop library is inspired by

Thank you for your work!

License

The MIT License (MIT)

Changelog

Version 0.4.0 (2017-06-27)

  • Fix strong mode errors (#20).

Version 0.3.6 (2017-06-06)

  • Fix bug: Provide reasonable fallback for event target when mouse position exits viewport (#19).

Version 0.3.5 (2016-11-22)

  • Handle the edge case where destroy is called while dragging an avatar (#17).

Version 0.3.4 (2016-10-19)

  • Fix strong-mode type errors (#15).
  • Remove Shadow DOM example and (dev)dependency on Polymer (was causing some confusion).

Version 0.3.3 (2016-09-22)

  • Allow a configurable clickSuppression distance (#13). We found that the click suppression was a little too aggressive for users with less mousing accuracy. They would attempt to click and trigger a small drag. Which then suppressed the click event and prevented the action they intended to complete.

Version 0.3.2 (2016-07-26)

  • Remove null-aware operator since drone.io uses an old version of Dart that doesn't support this yet.

Version 0.3.1 (2016-07-26)

  • Support for programmatic drag abort (see issue #11).

Version 0.3.0 (2015-04-18)

  • BREAKING CHANGE: Refactoring the AvatarHandler. Only if you've implemented a custom AvatarHandler you might need to do some changes:
    • setPointerEventsNone and resetPointerEvents were removed and don't need to be called any more. Pointer event styles are handled automatically.
  • Fix AvatarHandler margin caching: The AvatarHandler only cached the margins once for every Draggable. This caused problems when margins of elements in the same Draggable had different margins or the margins were changed. Now the margins are reset after every drag.

Version 0.2.1 (2015-03-09)

  • Fix #9: Using transformers in the main pubspec.yaml caused problems with projects depending on the dnd package.

Version 0.2.0 (2015-03-09)

  • Fix #3: Shadow DOM is now supported. A dnd-retarget attribute must be added to all custom elements where events should be forwarded to the Shadow DOM children.
  • Fix #7: Add a css class (dnd-invalid by default) to dropzones when a not-accepted draggable is dragged over.

Version 0.1.4 (2014-10-20)

  • Add a sortable example.
  • Change comments according to new Dart Style Guide rule (/// instead of /** */).
  • Move event dispatching calls from EventManager to Draggable (refactoring).

Version 0.1.3 (2014-08-09)

  • Fix #4: Problem when an ancestor of the dragged element was positioned (relative, absolute, fixed).

Version 0.1.2 (2014-07-22)

  • Correcting small bug that occurred when setSelectionRange() was called on an element that does not support it.
  • Fix Pointer Event bug: Too many event listeners in move, end, cancel.
  • Fix for Bug #1 - Not working in Windows 8.1 IE11
  • Adding a cancelled flag to DraggableEvent to indicate if a drag ended because of a cancelling operation like esc key, etc.

Version 0.1.1 (2014-07-21)

  • Support for IE10 and IE11 touch screens through pointer events.
  • Removed disableTouch and disableMouse options. The goal was to unify touch and mouse dragging, so it should not be necessary to disable one or the other.

Version 0.1.0 (2014-07-17)

  • First version.

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  dnd: "^0.4.0"

2. Install it

You can install packages from the command line:

$ pub get

Alternatively, your editor might support 'pub get'. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:dnd/dnd.dart';

Platforms

Web

About

Drag and Drop for Dart web apps with mouse and touch support.

Author

Email majakob@gmx.ch Marco Jakob

Homepage

code.makery.ch/library/dart-drag-and-drop/

Documentation

www.dartdocs.org/documentation/dnd/0.4.0/

Uploader

marco.jkob@gmail.com

License

MIT

Published

Jun 27, 2017

Share