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.

215 lines
7.3 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _invariant = require("@react-dnd/invariant");
var _registry = require("./actions/registry");
var _getNextUniqueId = _interopRequireDefault(require("./utils/getNextUniqueId"));
var _interfaces = require("./interfaces");
var _contracts = require("./contracts");
var _asap = require("@react-dnd/asap");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
function getNextHandlerId(role) {
var id = (0, _getNextUniqueId.default)().toString();
switch (role) {
case _interfaces.HandlerRole.SOURCE:
return "S".concat(id);
case _interfaces.HandlerRole.TARGET:
return "T".concat(id);
default:
throw new Error("Unknown Handler Role: ".concat(role));
}
}
function parseRoleFromHandlerId(handlerId) {
switch (handlerId[0]) {
case 'S':
return _interfaces.HandlerRole.SOURCE;
case 'T':
return _interfaces.HandlerRole.TARGET;
default:
(0, _invariant.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) {
(0, _contracts.validateType)(type);
(0, _contracts.validateSourceContract)(source);
var sourceId = this.addHandler(_interfaces.HandlerRole.SOURCE, type, source);
this.store.dispatch((0, _registry.addSource)(sourceId));
return sourceId;
}
}, {
key: "addTarget",
value: function addTarget(type, target) {
(0, _contracts.validateType)(type, true);
(0, _contracts.validateTargetContract)(target);
var targetId = this.addHandler(_interfaces.HandlerRole.TARGET, type, target);
this.store.dispatch((0, _registry.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;
(0, _invariant.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) {
(0, _invariant.invariant)(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.dropTargets.get(targetId);
}
}, {
key: "getSourceType",
value: function getSourceType(sourceId) {
(0, _invariant.invariant)(this.isSourceId(sourceId), 'Expected a valid source ID.');
return this.types.get(sourceId);
}
}, {
key: "getTargetType",
value: function getTargetType(targetId) {
(0, _invariant.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 === _interfaces.HandlerRole.SOURCE;
}
}, {
key: "isTargetId",
value: function isTargetId(handlerId) {
var role = parseRoleFromHandlerId(handlerId);
return role === _interfaces.HandlerRole.TARGET;
}
}, {
key: "removeSource",
value: function removeSource(sourceId) {
var _this = this;
(0, _invariant.invariant)(this.getSource(sourceId), 'Expected an existing source.');
this.store.dispatch((0, _registry.removeSource)(sourceId));
(0, _asap.asap)(function () {
_this.dragSources.delete(sourceId);
_this.types.delete(sourceId);
});
}
}, {
key: "removeTarget",
value: function removeTarget(targetId) {
(0, _invariant.invariant)(this.getTarget(targetId), 'Expected an existing target.');
this.store.dispatch((0, _registry.removeTarget)(targetId));
this.dropTargets.delete(targetId);
this.types.delete(targetId);
}
}, {
key: "pinSource",
value: function pinSource(sourceId) {
var source = this.getSource(sourceId);
(0, _invariant.invariant)(source, 'Expected an existing source.');
this.pinnedSourceId = sourceId;
this.pinnedSource = source;
}
}, {
key: "unpinSource",
value: function unpinSource() {
(0, _invariant.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 === _interfaces.HandlerRole.SOURCE) {
this.dragSources.set(id, handler);
} else if (role === _interfaces.HandlerRole.TARGET) {
this.dropTargets.set(id, handler);
}
return id;
}
}]);
return HandlerRegistryImpl;
}();
exports.default = HandlerRegistryImpl;