vue3-dnd
Version:
Drag and Drop for Vue Composition API
187 lines (186 loc) • 7.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SourceConnector = void 0;
var _wrapConnectorHooks = require("./wrapConnectorHooks");
var _shallowequal = require("@react-dnd/shallowequal");
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;
}
var SourceConnector = /*#__PURE__*/ function() {
"use strict";
function SourceConnector(backend) {
var _this = this;
_classCallCheck(this, SourceConnector);
this.hooks = (0, _wrapConnectorHooks).wrapConnectorHooks({
dragSource: function(node, options) {
_this.clearDragSource();
_this.dragSourceOptions = options || null;
_this.dragSourceNode = node;
_this.reconnectDragSource();
},
dragPreview: function(node, options) {
_this.clearDragPreview();
_this.dragPreviewOptions = options || null;
_this.dragPreviewNode = node;
_this.reconnectDragPreview();
}
});
this.handlerId = null;
this.dragSourceOptionsInternal = null;
this.dragPreviewOptionsInternal = null;
this.lastConnectedHandlerId = null;
this.lastConnectedDragSource = null;
this.lastConnectedDragSourceOptions = null;
this.lastConnectedDragPreview = null;
this.lastConnectedDragPreviewOptions = null;
this.backend = backend;
}
var _proto = SourceConnector.prototype;
_proto.receiveHandlerId = function receiveHandlerId(newHandlerId) {
if (this.handlerId === newHandlerId) {
return;
}
this.handlerId = newHandlerId;
this.reconnect();
};
_proto.reconnect = function reconnect() {
var didChange = this.reconnectDragSource();
this.reconnectDragPreview(didChange);
};
_proto.reconnectDragSource = function reconnectDragSource() {
var dragSource = this.dragSource;
// if nothing has changed then don't resubscribe
var didChange = this.didHandlerIdChange() || this.didConnectedDragSourceChange() || this.didDragSourceOptionsChange();
if (didChange) {
this.disconnectDragSource();
}
if (!this.handlerId) {
return didChange;
}
if (!dragSource) {
this.lastConnectedDragSource = dragSource;
return didChange;
}
if (didChange) {
this.lastConnectedHandlerId = this.handlerId;
this.lastConnectedDragSource = dragSource;
this.lastConnectedDragSourceOptions = this.dragSourceOptions;
this.dragSourceUnsubscribe = this.backend.connectDragSource(this.handlerId, dragSource, this.dragSourceOptions);
}
return didChange;
};
_proto.reconnectDragPreview = function reconnectDragPreview() {
var forceDidChange = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
var dragPreview = this.dragPreview;
// if nothing has changed then don't resubscribe
var didChange = forceDidChange || this.didHandlerIdChange() || this.didConnectedDragPreviewChange() || this.didDragPreviewOptionsChange();
if (didChange) {
this.disconnectDragPreview();
}
if (!this.handlerId) {
return;
}
if (!dragPreview) {
this.lastConnectedDragPreview = dragPreview;
return;
}
if (didChange) {
this.lastConnectedHandlerId = this.handlerId;
this.lastConnectedDragPreview = dragPreview;
this.lastConnectedDragPreviewOptions = this.dragPreviewOptions;
this.dragPreviewUnsubscribe = this.backend.connectDragPreview(this.handlerId, dragPreview, this.dragPreviewOptions);
}
};
_proto.didHandlerIdChange = function didHandlerIdChange() {
return this.lastConnectedHandlerId !== this.handlerId;
};
_proto.didConnectedDragSourceChange = function didConnectedDragSourceChange() {
return this.lastConnectedDragSource !== this.dragSource;
};
_proto.didConnectedDragPreviewChange = function didConnectedDragPreviewChange() {
return this.lastConnectedDragPreview !== this.dragPreview;
};
_proto.didDragSourceOptionsChange = function didDragSourceOptionsChange() {
return !(0, _shallowequal).shallowEqual(this.lastConnectedDragSourceOptions, this.dragSourceOptions);
};
_proto.didDragPreviewOptionsChange = function didDragPreviewOptionsChange() {
return !(0, _shallowequal).shallowEqual(this.lastConnectedDragPreviewOptions, this.dragPreviewOptions);
};
_proto.disconnectDragSource = function disconnectDragSource() {
if (this.dragSourceUnsubscribe) {
this.dragSourceUnsubscribe();
this.dragSourceUnsubscribe = undefined;
}
};
_proto.disconnectDragPreview = function disconnectDragPreview() {
if (this.dragPreviewUnsubscribe) {
this.dragPreviewUnsubscribe();
this.dragPreviewUnsubscribe = undefined;
this.dragPreviewNode = null;
}
};
_proto.clearDragSource = function clearDragSource() {
this.dragSourceNode = null;
};
_proto.clearDragPreview = function clearDragPreview() {
this.dragPreviewNode = null;
};
_createClass(SourceConnector, [
{
key: "connectTarget",
get: function get() {
return this.dragSource;
}
},
{
key: "dragSourceOptions",
get: function get() {
return this.dragSourceOptionsInternal;
},
set: function set(options) {
this.dragSourceOptionsInternal = options;
}
},
{
key: "dragPreviewOptions",
get: function get() {
return this.dragPreviewOptionsInternal;
},
set: function set(options) {
this.dragPreviewOptionsInternal = options;
}
},
{
key: "dragSource",
get: function get() {
return this.dragSourceNode;
}
},
{
key: "dragPreview",
get: function get() {
return this.dragPreviewNode;
}
}
]);
return SourceConnector;
}();
exports.SourceConnector = SourceConnector;