Skip to content

Commit

Permalink
Migrate to null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
marcojakob committed Mar 6, 2021
1 parent a6d0dd4 commit 33543ce
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 196 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 2.0.0 (2021-03-06)

- Migrate to null safety.

## Version 1.4.3 (2019-12-10)

- Remove unnecessary new keyword.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ a `dnd-retarget` attribute to the host:

The _Dart Drag and Drop_ library is inspired by

- [jQuery UI Draggable](http://jqueryui.com/draggable/)
- [jQuery UI Draggable](https://jqueryui.com/draggable/)
- [Draggabilly](https://draggabilly.desandro.com/)

Thank you for your work!
Expand Down
10 changes: 0 additions & 10 deletions analysis_options.yaml

This file was deleted.

3 changes: 2 additions & 1 deletion example/custom_acceptor/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class MyAcceptor extends Acceptor {
@override
bool accepts(
Element draggableElement, int draggableId, Element dropzoneElement) {
InputElement input = draggableElement.querySelector('input');
InputElement? input =
draggableElement.querySelector('input') as InputElement?;
return input != null && input.value == 'acceptme';
}
}
12 changes: 7 additions & 5 deletions example/custom_avatar/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ main() {
avatarHandler: MyAvatarHandler());

// Install dropzone (trash).
Element trash = querySelector('.trash');
Element trash = querySelector('.trash') as Element;
Dropzone dropzone = Dropzone(trash);

// Keep track if we're over the trash.
Expand All @@ -27,7 +27,7 @@ main() {

// Change the drag avatar during the drag.
draggable.onDrag.listen((DraggableEvent event) {
MyAvatarHandler handler = event.avatarHandler;
MyAvatarHandler handler = event.avatarHandler as MyAvatarHandler;

if (overTrash) {
// Set to last image if over trash.
Expand Down Expand Up @@ -79,10 +79,10 @@ class MyAvatarHandler extends AvatarHandler {
setLeftTop(startPosition + OFFSET);

// Ensure avatar has an absolute position.
avatar.style.position = 'absolute';
avatar!.style.position = 'absolute';

// Add the drag avatar to the body element.
document.body.append(avatar);
document.body!.append(avatar!);
}

@override
Expand All @@ -92,7 +92,9 @@ class MyAvatarHandler extends AvatarHandler {

@override
void dragEnd(Point startPosition, Point position) {
avatar.remove();
if (avatar != null) {
avatar!.remove();
}
}

/// Updates the image to [imageNumber].
Expand Down
2 changes: 1 addition & 1 deletion example/detection_only/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ main() {
Draggable draggable = Draggable(querySelector('.draggable'));

// Paragraph.
Element p = querySelector('.draggable p');
Element p = querySelector('.draggable p') as Element;

// Listen to drag start.
draggable.onDragStart.listen((DraggableEvent event) {
Expand Down
6 changes: 3 additions & 3 deletions example/nested_dropzones/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ main() {
Dropzone innerDropzone = Dropzone(querySelector('.dropzone-inner'));

// The text elements.
Element draggableText = querySelector('.draggable > p');
Element outerText = querySelector('.dropzone-outer > span');
Element innerText = querySelector('.dropzone-inner > span');
Element draggableText = querySelector('.draggable > p') as Element;
Element outerText = querySelector('.dropzone-outer > span') as Element;
Element innerText = querySelector('.dropzone-inner > span') as Element;

// Dropped flags to help for displaying the correct text in dragLeave
// (dragLeave is fired after a drop).
Expand Down
2 changes: 1 addition & 1 deletion example/nested_elements/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ main() {
Dropzone dropzone = Dropzone(querySelector('.dropzone'));

// Text element
Element text = querySelector('.dropzone > span');
Element text = querySelector('.dropzone > span') as Element;

// Listen to dragEnter.
dropzone.onDragEnter.listen((DropzoneEvent event) {
Expand Down
4 changes: 2 additions & 2 deletions example/simple_sortable/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ main() {

/// Simple function to swap two elements.
void swapElements(Element elm1, Element elm2) {
var parent1 = elm1.parent;
var parent1 = elm1.parent as Element;
var next1 = elm1.nextElementSibling;
var parent2 = elm2.parent;
var parent2 = elm2.parent as Element;
var next2 = elm2.nextElementSibling;

parent1.insertBefore(elm2, next1);
Expand Down
88 changes: 40 additions & 48 deletions lib/src/draggable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of dnd;
/// all [Draggable]s know when a drag operation is already handled.
/// Is also used by the [Dropzone] because we can't pass an [Element] through
/// the [CustomEvent]s.
_DragInfo _currentDrag;
_DragInfo? _currentDrag;

/// The [Draggable] detects drag operations for touch and mouse interactions and
/// optionally creates a drag avatar for visual feedback of the drag. Event
Expand All @@ -27,7 +27,7 @@ class Draggable {
// --------------
/// [avatarHandler] is a function to create a [DragAvatar] for this [Draggable].
/// See [Draggable] constructor.
AvatarHandler avatarHandler;
AvatarHandler? avatarHandler;

/// When set to true, only horizontal dragging is tracked. This enables
/// vertical touch dragging to be used for scrolling.
Expand All @@ -39,7 +39,7 @@ class Draggable {

/// Restricts dragging from starting to the [handle].
/// See [Draggable] constructor.
String handle;
String? handle;

/// Prevents dragging from starting on specified elements.
/// See [Draggable] constructor.
Expand All @@ -60,7 +60,7 @@ class Draggable {
/// The minimum distance for a drag to prevent click events.
/// DEPRECATED: Please use [minDragStartDistance] instead.
@deprecated
get clickSuppression => minDragStartDistance;
int get clickSuppression => minDragStartDistance;

/// DEPRECATED: Please use [minDragStartDistance] instead.
@deprecated
Expand All @@ -69,9 +69,9 @@ class Draggable {
// -------------------
// Events
// -------------------
StreamController<DraggableEvent> _onDragStart;
StreamController<DraggableEvent> _onDrag;
StreamController<DraggableEvent> _onDragEnd;
StreamController<DraggableEvent>? _onDragStart;
StreamController<DraggableEvent>? _onDrag;
StreamController<DraggableEvent>? _onDragEnd;

/// Fired when the user starts dragging.
///
Expand All @@ -83,7 +83,7 @@ class Draggable {
_onDragStart = StreamController<DraggableEvent>.broadcast(
sync: true, onCancel: () => _onDragStart = null);
}
return _onDragStart.stream;
return _onDragStart!.stream;
}

/// Fired periodically throughout the drag operation.
Expand All @@ -92,7 +92,7 @@ class Draggable {
_onDrag = StreamController<DraggableEvent>.broadcast(
sync: true, onCancel: () => _onDrag = null);
}
return _onDrag.stream;
return _onDrag!.stream;
}

/// Fired when the user ends the dragging.
Expand All @@ -102,12 +102,12 @@ class Draggable {
_onDragEnd = StreamController<DraggableEvent>.broadcast(
sync: true, onCancel: () => _onDragEnd = null);
}
return _onDragEnd.stream;
return _onDragEnd!.stream;
}

/// Abort the current drag
void abort() {
if (_currentDrag != null && _currentDrag.draggableId == this.id) {
if (_currentDrag != null && _currentDrag!.draggableId == this.id) {
_handleDragEnd(null, null, cancelled: true);
}
}
Expand All @@ -116,7 +116,7 @@ class Draggable {
// Private Properties
// -------------------
/// The list of [Element]s on which a drag is detected.
List<Element> _elements;
late List<Element> _elements;

/// Managers for browser events.
final List<_EventManager> _eventManagers = [];
Expand Down Expand Up @@ -172,7 +172,7 @@ class Draggable {
this.draggingClassBody = 'dnd-drag-occurring',
this.minDragStartDistance = 4}) {
// Wrap in a List if it is not a list but a single Element.
_elements = elementOrElementList is List
_elements = elementOrElementList is List<Element>
? elementOrElementList
: [elementOrElementList];

Expand All @@ -194,29 +194,25 @@ class Draggable {

/// Handles the drag start. The [moveEvent] might either be a
/// [TouchEvent] or a [MouseEvent].
void _handleDragStart(UIEvent moveEvent) {
void _handleDragStart(Event moveEvent) {
// Set the drag started flag.
_currentDrag.started = true;
_currentDrag!.started = true;

// Pass event to AvatarHandler.
if (avatarHandler != null) {
avatarHandler._handleDragStart(
_currentDrag.element, _currentDrag.position);
avatarHandler!
._handleDragStart(_currentDrag!.element, _currentDrag!.position);
}

// Fire the drag start event with start position.
if (_onDragStart != null) {
// The dragStart has the same for startPosition and current position.
_onDragStart.add(DraggableEvent._(moveEvent, _currentDrag));
_onDragStart!.add(DraggableEvent._(moveEvent, _currentDrag!));
}

// Add the css classes during the drag operation.
if (draggingClass != null) {
_currentDrag.element.classes.add(draggingClass);
}
if (draggingClassBody != null) {
document.body.classes.add(draggingClassBody);
}
_currentDrag!.element.classes.add(draggingClass);
document.body!.classes.add(draggingClassBody);

// Text selections should not be a problem, but it seems better usability
// to remove text selection when dragging something.
Expand All @@ -227,19 +223,19 @@ class Draggable {
/// [MouseEvent].
///
/// The [target] is the actual target receiving the event.
void _handleDrag(UIEvent moveEvent, EventTarget target) {
void _handleDrag(Event moveEvent, EventTarget? target) {
// Pass event to AvatarHandler.
if (avatarHandler != null) {
avatarHandler._handleDrag(
_currentDrag.startPosition, _currentDrag.position);
avatarHandler!
._handleDrag(_currentDrag!.startPosition, _currentDrag!.position);
}

// Dispatch internal drag enter, over, or leave event.
_DragEventDispatcher.dispatchEnterOverLeave(this, target);

// Fire the drag event.
if (_onDrag != null) {
_onDrag.add(DraggableEvent._(moveEvent, _currentDrag));
_onDrag!.add(DraggableEvent._(moveEvent, _currentDrag!));
}
}

Expand All @@ -252,13 +248,13 @@ class Draggable {
///
/// Set [cancelled] to true to indicate that this drag ended through a
/// cancel oparation like hitting the `esc` key.
void _handleDragEnd(Event event, EventTarget target, {cancelled = false}) {
void _handleDragEnd(Event? event, EventTarget? target, {cancelled = false}) {
// Only handle drag end if the user actually did drag and not just clicked.
if (_currentDrag.started) {
if (_currentDrag != null && _currentDrag!.started) {
// Pass event to AvatarHandler.
if (avatarHandler != null) {
avatarHandler._handleDragEnd(
_currentDrag.startPosition, _currentDrag.position);
avatarHandler!._handleDragEnd(
_currentDrag!.startPosition, _currentDrag!.position);
}

// Dispatch internal drop event if drag was not cancelled.
Expand All @@ -268,8 +264,8 @@ class Draggable {

// Fire dragEnd event.
if (_onDragEnd != null) {
_onDragEnd
.add(DraggableEvent._(event, _currentDrag, cancelled: cancelled));
_onDragEnd!
.add(DraggableEvent._(event, _currentDrag!, cancelled: cancelled));
}

// Prevent TouchEvent from emulating a click after touchEnd event.
Expand All @@ -279,16 +275,12 @@ class Draggable {

// Prevent MouseEvent from firing a click after mouseUp event.
if (event is MouseEvent) {
_suppressClickEvent(_currentDrag.element);
_suppressClickEvent(_currentDrag!.element);
}

// Remove the css classes.
if (draggingClass != null) {
_currentDrag.element.classes.remove(draggingClass);
}
if (draggingClassBody != null) {
document.body.classes.remove(draggingClassBody);
}
_currentDrag!.element.classes.remove(draggingClass);
document.body!.classes.remove(draggingClassBody);
}

// Reset.
Expand Down Expand Up @@ -320,8 +312,8 @@ class Draggable {
// Destroy all managers with their listeners.
_eventManagers.forEach((m) => m.destroy());
_eventManagers.clear();
if (avatarHandler != null && avatarHandler.avatar != null) {
avatarHandler.avatar.remove();
if (avatarHandler != null && avatarHandler!.avatar != null) {
avatarHandler!.avatar!.remove();
}
}

Expand All @@ -341,7 +333,7 @@ class Draggable {
/// in active textarea or active input element.
void _clearTextSelections() {
// Remove selection.
window.getSelection().removeAllRanges();
window.getSelection()?.removeAllRanges();

// Try to remove selection from textarea or input.
try {
Expand All @@ -364,15 +356,15 @@ class DraggableEvent {
final Element draggableElement;

/// The [AvatarHandler] or null if there is none.
final AvatarHandler avatarHandler;
final AvatarHandler? avatarHandler;

/// The original event which is either ...
/// * a [MouseEvent],
/// * a [TouchEvent],
/// * a [KeyboardEvent] when the user clicks the esc-key,
/// * a normal [Event] when the window loses focus (blur event), or
/// * null if the drag was programmatically aborted.
final Event originalEvent;
final Event? originalEvent;

/// Position where the drag started, relative to the whole document (page
/// position).
Expand Down Expand Up @@ -408,11 +400,11 @@ class _DragInfo {
final Point startPosition;

/// The [AvatarHandler] or null if there is none.
final AvatarHandler avatarHandler;
final AvatarHandler? avatarHandler;

/// The current position of the mouse or touch. This position is constrained
/// by the horizontal/vertical axis.
Point _position;
late Point _position;

/// Flag indicating if the drag started.
bool started = false;
Expand Down
Loading

0 comments on commit 33543ce

Please sign in to comment.