Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strong mode errors #20

Merged
merged 4 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .analysis_options
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
analyzer:
strong-mode: true
strong-mode: true
linter:
rules:
- annotate_overrides
- cancel_subscriptions
- close_sinks
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 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).
Expand Down
1 change: 0 additions & 1 deletion lib/dnd.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ part 'src/draggable_dispatch.dart';
part 'src/draggable_manager.dart';
part 'src/dropzone.dart';
part 'src/dropzone_acceptor.dart';

60 changes: 29 additions & 31 deletions lib/src/draggable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ part of dnd;
/// the [CustomEvent]s.
_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
/// streams are provided to track touch or mouse dragging:
Expand All @@ -17,7 +16,6 @@ _DragInfo _currentDrag;
///
/// A [Draggable] can be created for one [Element] or an [ElementList].
class Draggable {

/// Counter to generate a unique id for each instance.
static int idCounter = 0;

Expand Down Expand Up @@ -72,17 +70,17 @@ class Draggable {
/// will also be fired.
Stream<DraggableEvent> get onDragStart {
if (_onDragStart == null) {
_onDragStart = new StreamController<DraggableEvent>.broadcast(sync: true,
onCancel: () => _onDragStart = null);
_onDragStart = new StreamController<DraggableEvent>.broadcast(
sync: true, onCancel: () => _onDragStart = null);
}
return _onDragStart.stream;
}

/// Fired periodically throughout the drag operation.
Stream<DraggableEvent> get onDrag {
if (_onDrag == null) {
_onDrag = new StreamController<DraggableEvent>.broadcast(sync: true,
onCancel: () => _onDrag = null);
_onDrag = new StreamController<DraggableEvent>.broadcast(
sync: true, onCancel: () => _onDrag = null);
}
return _onDrag.stream;
}
Expand All @@ -91,8 +89,8 @@ class Draggable {
/// Is also fired when the user clicks the 'esc'-key or the window loses focus.
Stream<DraggableEvent> get onDragEnd {
if (_onDragEnd == null) {
_onDragEnd = new StreamController<DraggableEvent>.broadcast(sync: true,
onCancel: () => _onDragEnd = null);
_onDragEnd = new StreamController<DraggableEvent>.broadcast(
sync: true, onCancel: () => _onDragEnd = null);
}
return _onDragEnd.stream;
}
Expand Down Expand Up @@ -150,28 +148,24 @@ class Draggable {
/// The [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(elementOrElementList,
{ this.avatarHandler: null,
this.horizontalOnly: false,
this.verticalOnly: false,
this.handle: null,
this.cancel: 'input, textarea, button, select, option',
this.draggingClass: 'dnd-dragging',
this.draggingClassBody: 'dnd-drag-occurring'})

{this.avatarHandler: null,
this.horizontalOnly: false,
this.verticalOnly: false,
this.handle: null,
this.cancel: 'input, textarea, button, select, option',
this.draggingClass: 'dnd-dragging',
this.draggingClassBody: 'dnd-drag-occurring'})
: this._elementOrElementList = elementOrElementList {

// Detect IE Pointer Event Support.
JsObject jsWindow = new JsObject.fromBrowserObject(window);
JsObject jsNavigator = jsWindow['navigator'];

if (jsNavigator.hasProperty('pointerEnabled')) {
// We're on IE11 or higher supporting pointerEvents.
_eventManagers.add(new _PointerManager(this));

} else if (jsNavigator.hasProperty('msPointerEnabled')){
} else if (jsNavigator.hasProperty('msPointerEnabled')) {
// We're on IE10 supporting msPointerEvents.
_eventManagers.add(new _PointerManager(this, msPrefix: true));

} else {
// We're on other browsers. Install touch and mouse listeners.
if (TouchEvent.supported) {
Expand All @@ -189,7 +183,8 @@ class Draggable {

// 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.
Expand Down Expand Up @@ -218,7 +213,8 @@ class Draggable {
void _handleDrag(UIEvent 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.
Expand All @@ -244,7 +240,8 @@ class Draggable {
if (_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 @@ -254,8 +251,8 @@ class Draggable {

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

// Prevent TouchEvent from emulating a click after touchEnd event.
Expand Down Expand Up @@ -288,7 +285,8 @@ class Draggable {
/// [MouseEvent]s. We have to wait for and cancel a potential click event
/// happening after the mouseUp event.
void _suppressClickEvent() {
StreamSubscription clickPreventer = _elementOrElementList.onClick.listen((event) {
StreamSubscription clickPreventer =
_elementOrElementList.onClick.listen((event) {
event.stopPropagation();
event.preventDefault();
});
Expand Down Expand Up @@ -346,7 +344,6 @@ class Draggable {
}
}


/// Event used when a drag is detected.
class DraggableEvent {
/// The [Element] that is beeing dragged.
Expand Down Expand Up @@ -377,7 +374,8 @@ class DraggableEvent {
final bool cancelled;

/// Private constructor for [DraggableEvent].
DraggableEvent._(this.originalEvent, _DragInfo dragInfo, {this.cancelled: false})
DraggableEvent._(this.originalEvent, _DragInfo dragInfo,
{this.cancelled: false})
: draggableElement = dragInfo.element,
avatarHandler = dragInfo.avatarHandler,
startPosition = dragInfo.startPosition,
Expand Down Expand Up @@ -409,9 +407,9 @@ class _DragInfo {
final bool verticalOnly;

_DragInfo(this.draggableId, this.element, this.startPosition,
{ this.avatarHandler: null,
this.horizontalOnly: false,
this.verticalOnly: false}) {
{this.avatarHandler: null,
this.horizontalOnly: false,
this.verticalOnly: false}) {
// Initially set current position to startPosition.
_position = startPosition;
}
Expand Down
32 changes: 14 additions & 18 deletions lib/src/draggable_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ part of dnd;
/// a drag avatar. A drag avatar provides visual feedback during the drag
/// operation.
abstract class AvatarHandler {

/// Returns the [avatar] element during a drag operation.
///
/// If there is no drag operation going on, [avatar] will be null.
Expand Down Expand Up @@ -117,9 +116,12 @@ abstract class AvatarHandler {
void updateFunction() {
// Unsing `translate3d` to activate GPU hardware-acceleration (a bit of a hack).
if (avatar != null) {
avatar.style.transform = 'translate3d(${position.x}px, ${position.y}px, 0)';
avatar.style.transform =
'translate3d(${position.x}px, ${position.y}px, 0)';
}
};
}

;

// Use request animation frame to update the transform translate.
AnimationHelper.requestUpdate(updateFunction);
Expand Down Expand Up @@ -149,18 +151,16 @@ abstract class AvatarHandler {
void cacheMargins() {
// Calculate margins.
var computedStyles = avatar.getComputedStyle();
_marginLeft = num.parse(computedStyles.marginLeft.replaceFirst('px', ''),
(s) => 0);
_marginTop = num.parse(computedStyles.marginTop.replaceFirst('px', ''),
(s) => 0);
_marginLeft =
num.parse(computedStyles.marginLeft.replaceFirst('px', ''), (s) => 0);
_marginTop =
num.parse(computedStyles.marginTop.replaceFirst('px', ''), (s) => 0);
}
}


/// The [OriginalAvatarHandler] uses the draggable element itself as drag
/// avatar. It uses absolute positioning of the avatar.
class OriginalAvatarHandler extends AvatarHandler {

Point _draggableStartOffset;

@override
Expand Down Expand Up @@ -191,24 +191,22 @@ class OriginalAvatarHandler extends AvatarHandler {

// Set the new position as left/top. Prevent from moving past the top and
// left borders as the user might not be able to grab the element any more.
Point constrainedPosition = new Point(math.max(1, position.x),
math.max(1, position.y));
Point constrainedPosition =
new Point(math.max(1, position.x), math.max(1, position.y));

setLeftTop(constrainedPosition - startPosition + _draggableStartOffset);
}
}


/// [CloneAvatarHandler] creates a clone of the draggable element as drag avatar.
/// The avatar is removed at the end of the drag operation.
class CloneAvatarHandler extends AvatarHandler {

@override
void dragStart(Element draggable, Point startPosition) {
// Clone the draggable to create the avatar.
avatar = (draggable.clone(true) as Element)
..attributes.remove('id')
..style.cursor = 'inherit';
..attributes.remove('id')
..style.cursor = 'inherit';

// Ensure avatar has an absolute position.
avatar.style.position = 'absolute';
Expand All @@ -233,10 +231,8 @@ class CloneAvatarHandler extends AvatarHandler {
}
}


/// Simple helper class to speed up animation with requestAnimationFrame.
class AnimationHelper {

static Function _lastUpdateFunction;
static bool _updating = false;

Expand Down Expand Up @@ -264,4 +260,4 @@ class AnimationHelper {
_updating = false;
}
}
}
}
24 changes: 9 additions & 15 deletions lib/src/draggable_dispatch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ part of dnd;
/// Those events are only meant for communication between [Draggable]s and
/// [Dropzone]s and not to be consumed by users of the library.
class _DragEventDispatcher {

/// Custom drag enter event that is fired on the element that is entered.
static const String CUSTOM_DRAG_ENTER = '_customDragEnter';

Expand All @@ -21,21 +20,20 @@ class _DragEventDispatcher {
/// Stream provider for [CUSTOM_DRAG_ENTER] events. The relatedTarget contains
/// the [Element] the user entered from (may be null).
static EventStreamProvider<MouseEvent> enterEvent =
new EventStreamProvider(CUSTOM_DRAG_ENTER);
new EventStreamProvider(CUSTOM_DRAG_ENTER);

/// Stream provider for [CUSTOM_DRAG_OVER] events. The relatedTarget is empty.
static EventStreamProvider<MouseEvent> overEvent =
new EventStreamProvider(CUSTOM_DRAG_OVER);
new EventStreamProvider(CUSTOM_DRAG_OVER);

/// Stream provider for [CUSTOM_DRAG_LEAVE] events. The relatedTarget contains
/// the [Element] the user is leaving to (may be null).
static EventStreamProvider<MouseEvent> leaveEvent =
new EventStreamProvider(CUSTOM_DRAG_LEAVE);
new EventStreamProvider(CUSTOM_DRAG_LEAVE);

/// Stream provider for [CUSTOM_DROP] events. The relatedTarget is empty.
static EventStreamProvider<MouseEvent> dropEvent =
new EventStreamProvider(CUSTOM_DROP);

new EventStreamProvider(CUSTOM_DROP);

/// Keeps track of the previous target to be able to fire dragLeave events on it.
static EventTarget previousTarget;
Expand All @@ -45,7 +43,6 @@ class _DragEventDispatcher {
/// The [draggable] is the [Draggable] that is dispatching the event.
/// The [target] is the element that the event will be dispatched on.
static void dispatchEnterOverLeave(Draggable draggable, EventTarget target) {

// Sometimes the target is null (e.g. when user drags over buttons on
// android). Ignore it.
if (target == null) {
Expand All @@ -56,17 +53,16 @@ class _DragEventDispatcher {
// Moved on the same element --> dispatch dragOver.
MouseEvent dragOverEvent = new MouseEvent(CUSTOM_DRAG_OVER);
target.dispatchEvent(dragOverEvent);

} else {
// Entered a new element --> fire dragEnter of new element.
MouseEvent dragEnterEvent = new MouseEvent(CUSTOM_DRAG_ENTER,
relatedTarget: previousTarget);
MouseEvent dragEnterEvent =
new MouseEvent(CUSTOM_DRAG_ENTER, relatedTarget: previousTarget);
target.dispatchEvent(dragEnterEvent);

// Fire dragLeave of old element (if there is one).
if (previousTarget != null) {
MouseEvent dragLeaveEvent = new MouseEvent(CUSTOM_DRAG_LEAVE,
relatedTarget: target);
MouseEvent dragLeaveEvent =
new MouseEvent(CUSTOM_DRAG_LEAVE, relatedTarget: target);
previousTarget.dispatchEvent(dragLeaveEvent);
}

Expand All @@ -78,13 +74,11 @@ class _DragEventDispatcher {
}
}


/// Dispatches drop event.
///
/// The [draggable] is the [Draggable] that is dispatching the event.
/// The [target] is the element that the event will be dispatched on.
static void dispatchDrop(Draggable draggable, EventTarget target) {

// Sometimes the target is null (e.g. when user drags over buttons on
// android). Ignore it.
if (target == null) {
Expand All @@ -106,4 +100,4 @@ class _DragEventDispatcher {
previousTarget = null;
}
}
}
}
Loading