preact-dnd
Version:
Drag and Drop for Preact (port from React DnD)
111 lines (77 loc) • 5.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.default = DragDropContext;
var _preact = require('preact');
var _preact2 = _interopRequireDefault(_preact);
var _dndCore = require('dnd-core');
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _checkDecoratorArguments = require('./utils/checkDecoratorArguments');
var _checkDecoratorArguments2 = _interopRequireDefault(_checkDecoratorArguments);
var _hoistNonReactStatics = require('hoist-non-react-statics');
var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function DragDropContext(backendOrModule) {
_checkDecoratorArguments2.default.apply(undefined, ['DragDropContext', 'backend'].concat(Array.prototype.slice.call(arguments)));
// Auto-detect ES6 default export for people still using ES5
var backend = void 0;
if ((typeof backendOrModule === 'undefined' ? 'undefined' : _typeof(backendOrModule)) === 'object' && typeof backendOrModule.default === 'function') {
backend = backendOrModule.default;
} else {
backend = backendOrModule;
}
(0, _invariant2.default)(typeof backend === 'function', 'Expected the backend to be a function or an ES6 module exporting a default function. ' + 'Read more: http://gaearon.github.io/react-dnd/docs-drag-drop-context.html');
var childContext = {
dragDropManager: new _dndCore.DragDropManager(backend)
};
return function decorateContext(DecoratedComponent) {
var displayName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component';
var DragDropContextContainer = function (_Component) {
_inherits(DragDropContextContainer, _Component);
function DragDropContextContainer() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, DragDropContextContainer);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DragDropContextContainer.__proto__ || Object.getPrototypeOf(DragDropContextContainer)).call.apply(_ref, [this].concat(args))), _this), _this.setChildRef = function (child) {
_this.child = child;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(DragDropContextContainer, [{
key: 'getDecoratedComponentInstance',
value: function getDecoratedComponentInstance() {
return this.child;
}
}, {
key: 'getManager',
value: function getManager() {
return childContext.dragDropManager;
}
}, {
key: 'getChildContext',
value: function getChildContext() {
return childContext;
}
}, {
key: 'render',
value: function render() {
return _preact2.default.h(DecoratedComponent, _extends({}, this.props, { ref: this.setChildRef }));
}
}]);
return DragDropContextContainer;
}(_preact.Component);
DragDropContextContainer.DecoratedComponent = DecoratedComponent;
DragDropContextContainer.displayName = 'DragDropContext(' + displayName + ')';
return (0, _hoistNonReactStatics2.default)(DragDropContextContainer, DecoratedComponent);
};
}