UNPKG

18 kBJavaScriptView Raw
1var $8zHUo$sortablejs = require("sortablejs");
2var $8zHUo$classnames = require("classnames");
3var $8zHUo$react = require("react");
4var $8zHUo$tinyinvariant = require("tiny-invariant");
5
6function $parcel$interopDefault(a) {
7 return a && a.__esModule ? a.default : a;
8}
9function $parcel$export(e, n, v, s) {
10 Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
11}
12function $parcel$exportWildcard(dest, source) {
13 Object.keys(source).forEach(function(key) {
14 if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
15 return;
16 }
17
18 Object.defineProperty(dest, key, {
19 enumerable: true,
20 get: function get() {
21 return source[key];
22 }
23 });
24 });
25
26 return dest;
27}
28
29$parcel$export(module.exports, "Sortable", () => $882b6d93070905b3$re_export$Sortable);
30$parcel$export(module.exports, "Direction", () => $882b6d93070905b3$re_export$Direction);
31$parcel$export(module.exports, "DOMRect", () => $882b6d93070905b3$re_export$DOMRect);
32$parcel$export(module.exports, "GroupOptions", () => $882b6d93070905b3$re_export$GroupOptions);
33$parcel$export(module.exports, "MoveEvent", () => $882b6d93070905b3$re_export$MoveEvent);
34$parcel$export(module.exports, "Options", () => $882b6d93070905b3$re_export$Options);
35$parcel$export(module.exports, "PullResult", () => $882b6d93070905b3$re_export$PullResult);
36$parcel$export(module.exports, "PutResult", () => $882b6d93070905b3$re_export$PutResult);
37$parcel$export(module.exports, "SortableEvent", () => $882b6d93070905b3$re_export$SortableEvent);
38$parcel$export(module.exports, "SortableOptions", () => $882b6d93070905b3$re_export$SortableOptions);
39$parcel$export(module.exports, "Utils", () => $882b6d93070905b3$re_export$Utils);
40$parcel$export(module.exports, "ReactSortable", () => $7fe8e3ea572bda7a$export$11bbed9ee0012c13);
41
42
43
44
45
46function $eb03e74f8f7db1f3$export$1d0aa160432dfea5(node) {
47 if (node.parentElement !== null) node.parentElement.removeChild(node);
48}
49function $eb03e74f8f7db1f3$export$6d240faa51aa562f(parent, newChild, index) {
50 const refChild = parent.children[index] || null;
51 parent.insertBefore(newChild, refChild);
52}
53function $eb03e74f8f7db1f3$export$d7d742816c28cf91(customs) {
54 $eb03e74f8f7db1f3$export$77f49a256021c8de(customs);
55 $eb03e74f8f7db1f3$export$a6177d5829f70ebc(customs);
56}
57function $eb03e74f8f7db1f3$export$77f49a256021c8de(customs) {
58 customs.forEach((curr)=>$eb03e74f8f7db1f3$export$1d0aa160432dfea5(curr.element));
59}
60function $eb03e74f8f7db1f3$export$a6177d5829f70ebc(customs) {
61 customs.forEach((curr)=>{
62 $eb03e74f8f7db1f3$export$6d240faa51aa562f(curr.parentElement, curr.element, curr.oldIndex);
63 });
64}
65function $eb03e74f8f7db1f3$export$4655efe700f887a(evt, list) {
66 const mode = $eb03e74f8f7db1f3$export$1fc0f6205829e19c(evt);
67 const parentElement = {
68 parentElement: evt.from
69 };
70 let custom = [];
71 switch(mode){
72 case "normal":
73 /* eslint-disable */ const item = {
74 element: evt.item,
75 newIndex: evt.newIndex,
76 oldIndex: evt.oldIndex,
77 parentElement: evt.from
78 };
79 custom = [
80 item
81 ];
82 break;
83 case "swap":
84 const drag = {
85 element: evt.item,
86 oldIndex: evt.oldIndex,
87 newIndex: evt.newIndex,
88 ...parentElement
89 };
90 const swap = {
91 element: evt.swapItem,
92 oldIndex: evt.newIndex,
93 newIndex: evt.oldIndex,
94 ...parentElement
95 };
96 custom = [
97 drag,
98 swap
99 ];
100 break;
101 case "multidrag":
102 custom = evt.oldIndicies.map((curr, index)=>({
103 element: curr.multiDragElement,
104 oldIndex: curr.index,
105 newIndex: evt.newIndicies[index].index,
106 ...parentElement
107 }));
108 break;
109 }
110 /* eslint-enable */ const customs = $eb03e74f8f7db1f3$export$bc06a3af7dc65f53(custom, list);
111 return customs;
112}
113function $eb03e74f8f7db1f3$export$c25cf8080bd305ec(normalized, list) {
114 const a = $eb03e74f8f7db1f3$export$be2da95e6167b0bd(normalized, list);
115 const b = $eb03e74f8f7db1f3$export$eca851ee65ae17e4(normalized, a);
116 return b;
117}
118function $eb03e74f8f7db1f3$export$be2da95e6167b0bd(normalized, list) {
119 const newList = [
120 ...list
121 ];
122 normalized.concat().reverse().forEach((curr)=>newList.splice(curr.oldIndex, 1));
123 return newList;
124}
125function $eb03e74f8f7db1f3$export$eca851ee65ae17e4(normalized, list, evt, clone) {
126 const newList = [
127 ...list
128 ];
129 normalized.forEach((curr)=>{
130 const newItem = clone && evt && clone(curr.item, evt);
131 newList.splice(curr.newIndex, 0, newItem || curr.item);
132 });
133 return newList;
134}
135function $eb03e74f8f7db1f3$export$1fc0f6205829e19c(evt) {
136 if (evt.oldIndicies && evt.oldIndicies.length > 0) return "multidrag";
137 if (evt.swapItem) return "swap";
138 return "normal";
139}
140function $eb03e74f8f7db1f3$export$bc06a3af7dc65f53(inputs, list) {
141 const normalized = inputs.map((curr)=>({
142 ...curr,
143 item: list[curr.oldIndex]
144 })).sort((a, b)=>a.oldIndex - b.oldIndex);
145 return normalized;
146}
147function $eb03e74f8f7db1f3$export$7553c81e62e31b7e(props) {
148 /* eslint-disable */ const { list: // react sortable props
149 list , setList: setList , children: children , tag: tag , style: style , className: className , clone: clone , onAdd: // sortable options that have methods we want to overwrite
150 onAdd , onChange: onChange , onChoose: onChoose , onClone: onClone , onEnd: onEnd , onFilter: onFilter , onRemove: onRemove , onSort: onSort , onStart: onStart , onUnchoose: onUnchoose , onUpdate: onUpdate , onMove: onMove , onSpill: onSpill , onSelect: onSelect , onDeselect: onDeselect , ...options } = props;
151 /* eslint-enable */ return options;
152}
153
154
155/** Holds a global reference for which react element is being dragged */ // @todo - use context to manage this. How does one use 2 different providers?
156const $7fe8e3ea572bda7a$var$store = {
157 dragging: null
158};
159class $7fe8e3ea572bda7a$export$11bbed9ee0012c13 extends (0, $8zHUo$react.Component) {
160 /* eslint-disable-next-line */ static defaultProps = {
161 clone: (item)=>item
162 };
163 constructor(props){
164 super(props);
165 // @todo forward ref this component
166 this.ref = /*#__PURE__*/ (0, $8zHUo$react.createRef)();
167 // make all state false because we can't change sortable unless a mouse gesture is made.
168 const newList = [
169 ...props.list
170 ].map((item)=>Object.assign(item, {
171 chosen: false,
172 selected: false
173 }));
174 props.setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
175 (0, ($parcel$interopDefault($8zHUo$tinyinvariant)))(//@ts-expect-error: Doesn't exist. Will deprecate soon.
176 !props.plugins, `
177Plugins prop is no longer supported.
178Instead, mount it with "Sortable.mount(new MultiDrag())"
179Please read the updated README.md at https://github.com/SortableJS/react-sortablejs.
180 `);
181 }
182 componentDidMount() {
183 if (this.ref.current === null) return;
184 const newOptions = this.makeOptions();
185 (0, ($parcel$interopDefault($8zHUo$sortablejs))).create(this.ref.current, newOptions);
186 }
187 componentDidUpdate(prevProps) {
188 if (prevProps.disabled !== this.props.disabled && this.sortable) this.sortable.option("disabled", this.props.disabled);
189 }
190 render() {
191 const { tag: tag , style: style , className: className , id: id } = this.props;
192 const classicProps = {
193 style: style,
194 className: className,
195 id: id
196 };
197 // if no tag, default to a `div` element.
198 const newTag = !tag || tag === null ? "div" : tag;
199 return /*#__PURE__*/ (0, $8zHUo$react.createElement)(newTag, {
200 // @todo - find a way (perhaps with the callback) to allow AntD components to work
201 ref: this.ref,
202 ...classicProps
203 }, this.getChildren());
204 }
205 getChildren() {
206 const { children: children , dataIdAttr: dataIdAttr , selectedClass: selectedClass = "sortable-selected" , chosenClass: chosenClass = "sortable-chosen" , dragClass: /* eslint-disable */ dragClass = "sortable-drag" , fallbackClass: fallbackClass = "sortable-falback" , ghostClass: ghostClass = "sortable-ghost" , swapClass: swapClass = "sortable-swap-highlight" , filter: /* eslint-enable */ filter = "sortable-filter" , list: list , } = this.props;
207 // if no children, don't do anything.
208 if (!children || children == null) return null;
209 const dataid = dataIdAttr || "data-id";
210 /* eslint-disable-next-line */ return (0, $8zHUo$react.Children).map(children, (child, index)=>{
211 if (child === undefined) return undefined;
212 const item = list[index] || {};
213 const { className: prevClassName } = child.props;
214 // @todo - handle the function if avalable. I don't think anyone will be doing this soon.
215 const filtered = typeof filter === "string" && {
216 [filter.replace(".", "")]: !!item.filtered
217 };
218 const className = (0, ($parcel$interopDefault($8zHUo$classnames)))(prevClassName, {
219 [selectedClass]: item.selected,
220 [chosenClass]: item.chosen,
221 ...filtered
222 });
223 return /*#__PURE__*/ (0, $8zHUo$react.cloneElement)(child, {
224 [dataid]: child.key,
225 className: className
226 });
227 });
228 }
229 /** Appends the `sortable` property to this component */ get sortable() {
230 const el = this.ref.current;
231 if (el === null) return null;
232 const key = Object.keys(el).find((k)=>k.includes("Sortable"));
233 if (!key) return null;
234 //@ts-expect-error: fix me.
235 return el[key];
236 }
237 /** Converts all the props from `ReactSortable` into the `options` object that `Sortable.create(el, [options])` can use. */ makeOptions() {
238 const DOMHandlers = [
239 "onAdd",
240 "onChoose",
241 "onDeselect",
242 "onEnd",
243 "onRemove",
244 "onSelect",
245 "onSpill",
246 "onStart",
247 "onUnchoose",
248 "onUpdate",
249 ];
250 const NonDOMHandlers = [
251 "onChange",
252 "onClone",
253 "onFilter",
254 "onSort",
255 ];
256 const newOptions = (0, $eb03e74f8f7db1f3$export$7553c81e62e31b7e)(this.props);
257 DOMHandlers.forEach((name)=>newOptions[name] = this.prepareOnHandlerPropAndDOM(name));
258 NonDOMHandlers.forEach((name)=>newOptions[name] = this.prepareOnHandlerProp(name));
259 /** onMove has 2 arguments and needs to be handled seperately. */ const onMove1 = (evt, originalEvt)=>{
260 const { onMove: onMove } = this.props;
261 const defaultValue = evt.willInsertAfter || -1;
262 if (!onMove) return defaultValue;
263 const result = onMove(evt, originalEvt, this.sortable, $7fe8e3ea572bda7a$var$store);
264 if (typeof result === "undefined") return false;
265 return result;
266 };
267 return {
268 ...newOptions,
269 onMove: onMove1
270 };
271 }
272 /** Prepares a method that will be used in the sortable options to call an `on[Handler]` prop & an `on[Handler]` ReactSortable method. */ prepareOnHandlerPropAndDOM(evtName) {
273 return (evt)=>{
274 // call the component prop
275 this.callOnHandlerProp(evt, evtName);
276 // calls state change
277 //@ts-expect-error: until @types multidrag item is in
278 this[evtName](evt);
279 };
280 }
281 /** Prepares a method that will be used in the sortable options to call an `on[Handler]` prop */ prepareOnHandlerProp(evtName) {
282 return (evt)=>{
283 // call the component prop
284 this.callOnHandlerProp(evt, evtName);
285 };
286 }
287 /** Calls the `props.on[Handler]` function */ callOnHandlerProp(evt, evtName) {
288 const propEvent = this.props[evtName];
289 if (propEvent) propEvent(evt, this.sortable, $7fe8e3ea572bda7a$var$store);
290 }
291 // SORTABLE DOM HANDLING
292 onAdd(evt) {
293 const { list: list , setList: setList , clone: clone } = this.props;
294 /* eslint-disable-next-line */ const otherList = [
295 ...$7fe8e3ea572bda7a$var$store.dragging.props.list
296 ];
297 const customs = (0, $eb03e74f8f7db1f3$export$4655efe700f887a)(evt, otherList);
298 (0, $eb03e74f8f7db1f3$export$77f49a256021c8de)(customs);
299 const newList = (0, $eb03e74f8f7db1f3$export$eca851ee65ae17e4)(customs, list, evt, clone).map((item)=>Object.assign(item, {
300 selected: false
301 }));
302 setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
303 }
304 onRemove(evt) {
305 const { list: list , setList: setList } = this.props;
306 const mode = (0, $eb03e74f8f7db1f3$export$1fc0f6205829e19c)(evt);
307 const customs = (0, $eb03e74f8f7db1f3$export$4655efe700f887a)(evt, list);
308 (0, $eb03e74f8f7db1f3$export$a6177d5829f70ebc)(customs);
309 let newList = [
310 ...list
311 ];
312 // remove state if not in clone mode. otherwise, keep.
313 if (evt.pullMode !== "clone") newList = (0, $eb03e74f8f7db1f3$export$be2da95e6167b0bd)(customs, newList);
314 else {
315 // switch used to get the clone
316 let customClones = customs;
317 switch(mode){
318 case "multidrag":
319 customClones = customs.map((item, index)=>({
320 ...item,
321 element: evt.clones[index]
322 }));
323 break;
324 case "normal":
325 customClones = customs.map((item)=>({
326 ...item,
327 element: evt.clone
328 }));
329 break;
330 case "swap":
331 default:
332 (0, ($parcel$interopDefault($8zHUo$tinyinvariant)))(true, `mode "${mode}" cannot clone. Please remove "props.clone" from <ReactSortable/> when using the "${mode}" plugin`);
333 }
334 (0, $eb03e74f8f7db1f3$export$77f49a256021c8de)(customClones);
335 // replace selected items with cloned items
336 customs.forEach((curr)=>{
337 const index = curr.oldIndex;
338 /* eslint-disable-next-line */ const newItem = this.props.clone(curr.item, evt);
339 newList.splice(index, 1, newItem);
340 });
341 }
342 // remove item.selected from list
343 newList = newList.map((item)=>Object.assign(item, {
344 selected: false
345 }));
346 setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
347 }
348 onUpdate(evt) {
349 const { list: list , setList: setList } = this.props;
350 const customs = (0, $eb03e74f8f7db1f3$export$4655efe700f887a)(evt, list);
351 (0, $eb03e74f8f7db1f3$export$77f49a256021c8de)(customs);
352 (0, $eb03e74f8f7db1f3$export$a6177d5829f70ebc)(customs);
353 const newList = (0, $eb03e74f8f7db1f3$export$c25cf8080bd305ec)(customs, list);
354 return setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
355 }
356 onStart() {
357 $7fe8e3ea572bda7a$var$store.dragging = this;
358 }
359 onEnd() {
360 $7fe8e3ea572bda7a$var$store.dragging = null;
361 }
362 onChoose(evt) {
363 const { list: list , setList: setList } = this.props;
364 const newList = list.map((item, index)=>{
365 let newItem = item;
366 if (index === evt.oldIndex) newItem = Object.assign(item, {
367 chosen: true
368 });
369 return newItem;
370 });
371 setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
372 }
373 onUnchoose(evt) {
374 const { list: list , setList: setList } = this.props;
375 const newList = list.map((item, index)=>{
376 let newItem = item;
377 if (index === evt.oldIndex) newItem = Object.assign(newItem, {
378 chosen: false
379 });
380 return newItem;
381 });
382 setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
383 }
384 onSpill(evt) {
385 const { removeOnSpill: removeOnSpill , revertOnSpill: revertOnSpill } = this.props;
386 if (removeOnSpill && !revertOnSpill) (0, $eb03e74f8f7db1f3$export$1d0aa160432dfea5)(evt.item);
387 }
388 onSelect(evt) {
389 const { list: list , setList: setList } = this.props;
390 const newList = list.map((item)=>Object.assign(item, {
391 selected: false
392 }));
393 evt.newIndicies.forEach((curr)=>{
394 const index = curr.index;
395 if (index === -1) {
396 console.log(`"${evt.type}" had indice of "${curr.index}", which is probably -1 and doesn't usually happen here.`);
397 console.log(evt);
398 return;
399 }
400 newList[index].selected = true;
401 });
402 setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
403 }
404 onDeselect(evt) {
405 const { list: list , setList: setList } = this.props;
406 const newList = list.map((item)=>Object.assign(item, {
407 selected: false
408 }));
409 evt.newIndicies.forEach((curr)=>{
410 const index = curr.index;
411 if (index === -1) return;
412 newList[index].selected = true;
413 });
414 setList(newList, this.sortable, $7fe8e3ea572bda7a$var$store);
415 }
416}
417
418
419var $faefaad95e5fcca0$exports = {};
420
421
422$parcel$exportWildcard(module.exports, $faefaad95e5fcca0$exports);
423
424
425//# sourceMappingURL=index.js.map