UNPKG

11.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _reactDom = require('react-dom');
16
17var _reactDom2 = _interopRequireDefault(_reactDom);
18
19var _reactBeautifulDnd = require('react-beautiful-dnd');
20
21var _util = require('./util');
22
23var _classnames = require('classnames');
24
25var _classnames2 = _interopRequireDefault(_classnames);
26
27var _lodash = require('lodash.isequal');
28
29var _lodash2 = _interopRequireDefault(_lodash);
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
32
33function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
34
35function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
36
37function _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; }
38
39function _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) : _defaults(subClass, superClass); }
40
41var reorder = function reorder(list, startIndex, endIndex) {
42 var result = Array.from(list);
43
44 var _result$splice = result.splice(startIndex, 1),
45 _result$splice2 = _slicedToArray(_result$splice, 1),
46 removed = _result$splice2[0];
47
48 result.splice(endIndex, 0, removed);
49
50 return result;
51};
52
53/**
54 * Moves an item from one list to another list.
55 */
56var move = function move(source, destination, droppableSource, droppableDestination) {
57 var sourceClone = Array.from(source);
58 var destClone = Array.from(destination);
59
60 var _sourceClone$splice = sourceClone.splice(droppableSource.index, 1),
61 _sourceClone$splice2 = _slicedToArray(_sourceClone$splice, 1),
62 removed = _sourceClone$splice2[0];
63
64 destClone.splice(droppableDestination.index, 0, removed);
65
66 var result = {};
67 result[droppableSource.droppableId] = sourceClone;
68 result[droppableDestination.droppableId] = destClone;
69
70 return result;
71};
72
73var Between = function (_Component) {
74 _inherits(Between, _Component);
75
76 function Between(props) {
77 _classCallCheck(this, Between);
78
79 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
80
81 _this.id2List = {
82 droppable: 'items',
83 droppable2: 'selected'
84 };
85
86 _this.getList = function (id) {
87 return _this.state[_this.id2List[id]];
88 };
89
90 _this.onDragEnd = function (result) {
91 console.log(result);
92 var source = result.source,
93 destination = result.destination;
94
95 // dropped outside the list
96
97 if (!destination) {
98 return;
99 }
100 var list = _this.state.items;
101 var otherList = _this.state.selected;
102
103 if (source.droppableId === destination.droppableId) {
104 var items = reorder(_this.getList(source.droppableId), source.index, destination.index);
105
106 var state = { items: items };
107 list = items;
108
109 if (source.droppableId === 'droppable2') {
110 state = { selected: items };
111 otherList = items;
112 list = _this.state.items;
113 }
114 _this.setState(state);
115 } else {
116 var _result = move(_this.getList(source.droppableId), _this.getList(destination.droppableId), source, destination);
117
118 _this.setState({
119 items: _result.droppable,
120 selected: _result.droppable2
121 });
122 list = _result.droppable;
123 otherList = _result.droppable2;
124 }
125 _this.props.onStop(result, {
126 list: list,
127 otherList: otherList
128 });
129 };
130
131 _this.onDragStart = function (result) {
132 _this.props.onStart(result, {
133 list: _this.state.list,
134 otherList: _this.state.selected
135 });
136 };
137
138 _this.state = {
139 items: _this.props.list,
140 selected: _this.props.otherList
141 };
142 return _this;
143 }
144
145 Between.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
146 if (!(0, _lodash2["default"])(this.state.items, nextProps.list)) {
147 this.setState({
148 items: nextProps.list
149 });
150 }
151 if (!(0, _lodash2["default"])(this.state.selected, nextProps.otherList)) {
152 this.setState({
153 selected: nextProps.otherList
154 });
155 }
156 };
157
158 Between.prototype.render = function render() {
159 var _this2 = this;
160
161 var _props = this.props,
162 onStart = _props.onStart,
163 onDrag = _props.onDrag,
164 onStop = _props.onStop,
165 onDragUpdate = _props.onDragUpdate,
166 dropClass = _props.dropClass,
167 dropOverClass = _props.dropOverClass,
168 dragClass = _props.dragClass,
169 dragingClass = _props.dragingClass,
170 showKey = _props.showKey,
171 type = _props.type;
172
173
174 return _react2["default"].createElement(
175 'div',
176 { className: (0, _classnames2["default"])({
177 'u-drag-between': type == 'betweenVertical',
178 'u-drag-between u-drag-between-horizontal': type == 'betweenHorizontal'
179
180 }) },
181 _react2["default"].createElement(
182 _reactBeautifulDnd.DragDropContext,
183 { onDragEnd: this.onDragEnd, onDragStart: this.onDragStart, onDragUpdate: onDragUpdate },
184 _react2["default"].createElement(
185 _reactBeautifulDnd.Droppable,
186 { droppableId: 'droppable', direction: type == 'betweenVertical' ? 'vertical' : 'horizontal' },
187 function (provided, snapshot) {
188 return _react2["default"].createElement(
189 'div',
190 {
191 ref: provided.innerRef,
192 className: (0, _classnames2["default"])(_extends({}, (0, _util.getClass)(_this2.props, snapshot.isDraggingOver).drop)) },
193 _this2.state.items.map(function (item, index) {
194 return _react2["default"].createElement(
195 _reactBeautifulDnd.Draggable,
196 {
197 key: '1' + index,
198 draggableId: '1' + index,
199 index: index },
200 function (provided, snapshot) {
201 return _react2["default"].createElement(
202 'div',
203 _extends({
204 ref: provided.innerRef
205 }, provided.draggableProps, provided.dragHandleProps, {
206 className: (0, _classnames2["default"])(_extends({}, (0, _util.getClass)(_this2.props, snapshot.isDragging).drag)),
207 style: _extends({}, provided.draggableProps.style) }),
208 showKey ? item[showKey] : item
209 );
210 }
211 );
212 }),
213 provided.placeholder
214 );
215 }
216 ),
217 _react2["default"].createElement(
218 _reactBeautifulDnd.Droppable,
219 { droppableId: 'droppable2', direction: type == 'betweenVertical' ? 'vertical' : 'horizontal' },
220 function (provided, snapshot) {
221 return _react2["default"].createElement(
222 'div',
223 {
224 ref: provided.innerRef,
225 className: (0, _classnames2["default"])(_extends({}, (0, _util.getClass)(_this2.props, snapshot.isDraggingOver).drop)) },
226 _this2.state.selected.map(function (item, index) {
227 return _react2["default"].createElement(
228 _reactBeautifulDnd.Draggable,
229 {
230 key: '2' + index,
231 draggableId: '2' + index,
232 index: index },
233 function (provided, snapshot) {
234 return _react2["default"].createElement(
235 'div',
236 _extends({
237 ref: provided.innerRef
238 }, provided.draggableProps, provided.dragHandleProps, {
239 className: (0, _classnames2["default"])(_extends({}, (0, _util.getClass)(_this2.props, snapshot.isDragging).drag)),
240 style: _extends({}, provided.draggableProps.style) }),
241 showKey ? item[showKey] : item
242 );
243 }
244 );
245 }),
246 provided.placeholder
247 );
248 }
249 )
250 )
251 );
252 };
253
254 return Between;
255}(_react.Component);
256
257exports["default"] = Between;
258module.exports = exports['default'];
\No newline at end of file