Skip to content
This repository was archived by the owner on Jun 18, 2018. It is now read-only.

Use manager context instead of window for establishing subscriptions [v2] #67

Merged
merged 4 commits into from
Jan 26, 2017
Merged
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
27 changes: 16 additions & 11 deletions src/HTML5Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class HTML5Backend {
this.actions = manager.getActions();
this.monitor = manager.getMonitor();
this.registry = manager.getRegistry();
this.context = manager.getContext();

this.sourcePreviewNodes = {};
this.sourcePreviewNodeOptions = {};
Expand All @@ -34,25 +35,29 @@ export default class HTML5Backend {
this.endDragNativeItem = this.endDragNativeItem.bind(this);
}

get window() {
return (this.context && this.context.window) || window;
}

setup() {
if (typeof window === 'undefined') {
if (typeof this.window === 'undefined') {
return;
}

if (this.constructor.isSetUp) {
if (this.window.__isReactDndBackendSetUp) { // eslint-disable-line no-underscore-dangle
throw new Error('Cannot have two HTML5 backends at the same time.');
}
this.constructor.isSetUp = true;
this.addEventListeners(window);
this.window.__isReactDndBackendSetUp = true; // eslint-disable-line no-underscore-dangle
this.addEventListeners(this.window);
}

teardown() {
if (typeof window === 'undefined') {
if (typeof this.window === 'undefined') {
return;
}

this.constructor.isSetUp = false;
this.removeEventListeners(window);
this.window.__isReactDndBackendSetUp = false; // eslint-disable-line no-underscore-dangle
this.removeEventListeners(this.window);
this.clearCurrentDragSourceNode();
}

Expand Down Expand Up @@ -180,7 +185,7 @@ export default class HTML5Backend {
// On Firefox, if mousemove fires, the drag is over but browser failed to tell us.
// This is not true for other browsers.
if (isFirefox()) {
window.addEventListener('mousemove', this.endDragNativeItem, true);
this.window.addEventListener('mousemove', this.endDragNativeItem, true);
}
}

Expand All @@ -190,7 +195,7 @@ export default class HTML5Backend {
}

if (isFirefox()) {
window.removeEventListener('mousemove', this.endDragNativeItem, true);
this.window.removeEventListener('mousemove', this.endDragNativeItem, true);
}

this.actions.endDrag();
Expand Down Expand Up @@ -219,15 +224,15 @@ export default class HTML5Backend {
// Receiving a mouse event in the middle of a dragging operation
// means it has ended and the drag source node disappeared from DOM,
// so the browser didn't dispatch the dragend event.
window.addEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true);
this.window.addEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true);
}

clearCurrentDragSourceNode() {
if (this.currentDragSourceNode) {
this.currentDragSourceNode = null;
this.currentDragSourceNodeOffset = null;
this.currentDragSourceNodeOffsetChanged = false;
window.removeEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true);
this.window.removeEventListener('mousemove', this.endDragIfSourceWasRemovedFromDOM, true);
return true;
}

Expand Down