UNPKG

react-sortable-hoc

Version:

Set of higher-order components to turn any list into a sortable, touch-friendly, animated list

74 lines (57 loc) 2.31 kB
'use strict'; 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 = {}; } _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: 'getActive', value: function getActive() { var _this = this; return (0, _find2.default)(this.refs[this.active.collection], function (_ref) { var node = _ref.node; return node.sortableInfo.index == _this.active.index; }); } }, { 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 ? this.active.collection : arguments[0]; return (0, _sortBy2.default)(this.refs[collection], function (_ref2) { var node = _ref2.node; return node.sortableInfo.index; }); } }]); return Manager; }(); exports.default = Manager;