"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TargetConnector = void 0; var _shallowequal = require("@react-dnd/shallowequal"); var _wrapConnectorHooks = require("./wrapConnectorHooks"); var _isRef = require("./isRef"); 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var TargetConnector = /*#__PURE__*/function () { // The drop target may either be attached via ref or connect function function TargetConnector(backend) { var _this = this; _classCallCheck(this, TargetConnector); _defineProperty(this, "hooks", (0, _wrapConnectorHooks.wrapConnectorHooks)({ dropTarget: function dropTarget(node, options) { _this.clearDropTarget(); _this.dropTargetOptions = options; if ((0, _isRef.isRef)(node)) { _this.dropTargetRef = node; } else { _this.dropTargetNode = node; } _this.reconnect(); } })); _defineProperty(this, "handlerId", null); _defineProperty(this, "dropTargetRef", null); _defineProperty(this, "dropTargetNode", void 0); _defineProperty(this, "dropTargetOptionsInternal", null); _defineProperty(this, "unsubscribeDropTarget", void 0); _defineProperty(this, "lastConnectedHandlerId", null); _defineProperty(this, "lastConnectedDropTarget", null); _defineProperty(this, "lastConnectedDropTargetOptions", null); _defineProperty(this, "backend", void 0); this.backend = backend; } _createClass(TargetConnector, [{ key: "connectTarget", get: function get() { return this.dropTarget; } }, { key: "reconnect", value: function reconnect() { // if nothing has changed then don't resubscribe var didChange = this.didHandlerIdChange() || this.didDropTargetChange() || this.didOptionsChange(); if (didChange) { this.disconnectDropTarget(); } var dropTarget = this.dropTarget; if (!this.handlerId) { return; } if (!dropTarget) { this.lastConnectedDropTarget = dropTarget; return; } if (didChange) { this.lastConnectedHandlerId = this.handlerId; this.lastConnectedDropTarget = dropTarget; this.lastConnectedDropTargetOptions = this.dropTargetOptions; this.unsubscribeDropTarget = this.backend.connectDropTarget(this.handlerId, dropTarget, this.dropTargetOptions); } } }, { key: "receiveHandlerId", value: function receiveHandlerId(newHandlerId) { if (newHandlerId === this.handlerId) { return; } this.handlerId = newHandlerId; this.reconnect(); } }, { key: "dropTargetOptions", get: function get() { return this.dropTargetOptionsInternal; }, set: function set(options) { this.dropTargetOptionsInternal = options; } }, { key: "didHandlerIdChange", value: function didHandlerIdChange() { return this.lastConnectedHandlerId !== this.handlerId; } }, { key: "didDropTargetChange", value: function didDropTargetChange() { return this.lastConnectedDropTarget !== this.dropTarget; } }, { key: "didOptionsChange", value: function didOptionsChange() { return !(0, _shallowequal.shallowEqual)(this.lastConnectedDropTargetOptions, this.dropTargetOptions); } }, { key: "disconnectDropTarget", value: function disconnectDropTarget() { if (this.unsubscribeDropTarget) { this.unsubscribeDropTarget(); this.unsubscribeDropTarget = undefined; } } }, { key: "dropTarget", get: function get() { return this.dropTargetNode || this.dropTargetRef && this.dropTargetRef.current; } }, { key: "clearDropTarget", value: function clearDropTarget() { this.dropTargetRef = null; this.dropTargetNode = null; } }]); return TargetConnector; }(); exports.TargetConnector = TargetConnector;