You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.8 KiB

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { invariant } from '@react-dnd/invariant';
import { addSource as _addSource, addTarget as _addTarget, removeSource as _removeSource, removeTarget as _removeTarget } from './actions/registry';
import getNextUniqueId from './utils/getNextUniqueId';
import { HandlerRole } from './interfaces';
import { validateSourceContract, validateTargetContract, validateType } from './contracts';
import { asap } from '@react-dnd/asap';
function getNextHandlerId(role) {
var id = getNextUniqueId().toString();
switch (role) {
case HandlerRole.SOURCE:
return "S".concat(id);
case HandlerRole.TARGET:
return "T".concat(id);
default:
throw new Error("Unknown Handler Role: ".concat(role));
}
}
function parseRoleFromHandlerId(handlerId) {
switch (handlerId[0]) {
case 'S':
return HandlerRole.SOURCE;
case 'T':
return HandlerRole.TARGET;
default:
invariant(false, "Cannot parse handler ID: ".concat(handlerId));
}
}
function mapContainsValue(map, searchValue) {
var entries = map.entries();
var isDone = false;
do {
var _entries$next = entries.next(),
done = _entries$next.done,
_entries$next$value = _slicedToArray(_entries$next.value, 2),
value = _entries$next$value[1];
if (value === searchValue) {
return true;
}
isDone = !!done;
} while (!isDone);
return false;
}
var HandlerRegistryImpl =
/*#__PURE__*/
function () {
function HandlerRegistryImpl(store) {
_classCallCheck(this, HandlerRegistryImpl);
this.types = new Map();
this.dragSources = new Map();
this.dropTargets = new Map();
this.pinnedSourceId = null;
this.pinnedSource = null;
this.store = store;
}
_createClass(HandlerRegistryImpl, [{
key: "addSource",
value: function addSource(type, source) {
validateType(type);
validateSourceContract(source);
var sourceId = this.addHandler(HandlerRole.SOURCE, type, source);
this.store.dispatch(_addSource(sourceId));
return sourceId;
}
}, {
key: "addTarget",
value: function addTarget(type, target) {
validateType(type, true);
validateTargetContract(target);
var targetId = this.addHandler(HandlerRole.TARGET, type, target);
this.store.dispatch(_addTarget(targetId));
return targetId;
}
}, {
key: "containsHandler",
value: function containsHandler(handler) {
return mapContainsValue(this.dragSources, handler) || mapContainsValue(this.dropTargets, handler);
}
}, {
key: "getSource",
value: function getSource(sourceId) {
var includePinned = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.');
var isPinned = includePinned && sourceId === this.pinnedSourceId;
var source = isPinned ? this.pinnedSource : this.dragSources.get(sourceId);
return source;
}
}, {
key: "getTarget",
value: function getTarget(targetId) {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.dropTargets.get(targetId);
}
}, {
key: "getSourceType",
value: function getSourceType(sourceId) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.');
return this.types.get(sourceId);
}
}, {
key: "getTargetType",
value: function getTargetType(targetId) {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.types.get(targetId);
}
}, {
key: "isSourceId",
value: function isSourceId(handlerId) {
var role = parseRoleFromHandlerId(handlerId);
return role === HandlerRole.SOURCE;
}
}, {
key: "isTargetId",
value: function isTargetId(handlerId) {
var role = parseRoleFromHandlerId(handlerId);
return role === HandlerRole.TARGET;
}
}, {
key: "removeSource",
value: function removeSource(sourceId) {
var _this = this;
invariant(this.getSource(sourceId), 'Expected an existing source.');
this.store.dispatch(_removeSource(sourceId));
asap(function () {
_this.dragSources.delete(sourceId);
_this.types.delete(sourceId);
});
}
}, {
key: "removeTarget",
value: function removeTarget(targetId) {
invariant(this.getTarget(targetId), 'Expected an existing target.');
this.store.dispatch(_removeTarget(targetId));
this.dropTargets.delete(targetId);
this.types.delete(targetId);
}
}, {
key: "pinSource",
value: function pinSource(sourceId) {
var source = this.getSource(sourceId);
invariant(source, 'Expected an existing source.');
this.pinnedSourceId = sourceId;
this.pinnedSource = source;
}
}, {
key: "unpinSource",
value: function unpinSource() {
invariant(this.pinnedSource, 'No source is pinned at the time.');
this.pinnedSourceId = null;
this.pinnedSource = null;
}
}, {
key: "addHandler",
value: function addHandler(role, type, handler) {
var id = getNextHandlerId(role);
this.types.set(id, type);
if (role === HandlerRole.SOURCE) {
this.dragSources.set(id, handler);
} else if (role === HandlerRole.TARGET) {
this.dropTargets.set(id, handler);
}
return id;
}
}]);
return HandlerRegistryImpl;
}();
export { HandlerRegistryImpl as default };