snaphy-react-sortable-dnd
Version:
React Sortable HOC build from React HOC
93 lines (74 loc) • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _find = require('lodash/find');
var _find2 = _interopRequireDefault(_find);
var _sortBy = require('lodash/sortBy');
var _sortBy2 = _interopRequireDefault(_sortBy);
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"); } }
var Manager = function () {
function Manager() {
_classCallCheck(this, Manager);
this.refs = {};
this.selected = [];
}
_createClass(Manager, [{
key: 'add',
value: function add(collection, ref) {
if (!this.refs[collection]) {
this.refs[collection] = [];
}
this.refs[collection].push(ref);
}
}, {
key: 'remove',
value: function remove(collection, ref) {
var index = this.getIndex(collection, ref);
if (index !== -1) {
this.refs[collection].splice(index, 1);
}
}
}, {
key: 'isActive',
value: function isActive() {
return this.active;
}
}, {
key: 'getActive',
value: function getActive() {
var _this = this;
if (!this.active) {
return null;
}
var activeRef = this.refs[this.active.collection];
if (!activeRef) {
return null;
}
return (0, _find2.default)(activeRef,
// eslint-disable-next-line eqeqeq
function (_ref) {
var node = _ref.node;
return node.sortableInfo.index == _this.active.index;
}) || activeRef.slice(-1).pop();
}
}, {
key: 'getIndex',
value: function getIndex(collection, ref) {
return this.refs[collection].indexOf(ref);
}
}, {
key: 'getOrderedRefs',
value: function getOrderedRefs() {
var collection = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.active.collection;
return (0, _sortBy2.default)(this.refs[collection], function (_ref2) {
var node = _ref2.node;
return node.sortableInfo.index;
});
}
}]);
return Manager;
}();
exports.default = Manager;