|
| 1 | +import extend from 'lodash/extend'; |
| 2 | +import merge from 'lodash/merge'; |
| 3 | +import React from 'react'; |
| 4 | +import ReactDOM from 'react-dom'; |
| 5 | +import Sortable from 'sortablejs'; |
| 6 | + |
| 7 | +const defaultOptions = { |
| 8 | + ref: 'list', |
| 9 | + model: 'items', |
| 10 | + onStart: 'handleStart', |
| 11 | + onEnd: 'handleEnd', |
| 12 | + onAdd: 'handleAdd', |
| 13 | + onUpdate: 'handleUpdate', |
| 14 | + onRemove: 'handleRemove', |
| 15 | + onSort: 'handleSort', |
| 16 | + onFilter: 'handleFilter', |
| 17 | + onMove: 'handleMove' |
| 18 | +}; |
| 19 | + |
| 20 | +let _nextSibling = null; |
| 21 | +let _activeWrapperComponent = null; |
| 22 | + |
| 23 | +const refName = 'sortableComponent'; |
| 24 | + |
| 25 | +const getModelItems = (wrapperComponent) => { |
| 26 | + const model = wrapperComponent.sortableOptions.model; |
| 27 | + const sortableComponent = wrapperComponent.refs[refName]; |
| 28 | + const { state = {}, props = {} } = sortableComponent; |
| 29 | + const items = state[model] || props[model] || []; |
| 30 | + return items.slice(); // returns a shallow copy of the items array |
| 31 | +}; |
| 32 | + |
| 33 | +const SortableMixin = (Component, sortableOptions = defaultOptions) => class extends React.Component { |
| 34 | + sortableInstance = null; |
| 35 | + sortableOptions = sortableOptions; |
| 36 | + |
| 37 | + componentDidMount() { |
| 38 | + const wrapperComponent = this; |
| 39 | + const sortableComponent = wrapperComponent.refs[refName]; |
| 40 | + const options = merge({}, defaultOptions, wrapperComponent.sortableOptions); |
| 41 | + const emitEvent = (type, evt) => { |
| 42 | + const methodName = options[type]; |
| 43 | + const method = sortableComponent[methodName]; |
| 44 | + method && method.call(sortableComponent, evt, wrapperComponent.sortableInstance); |
| 45 | + }; |
| 46 | + |
| 47 | + let copyOptions = extend({}, options); |
| 48 | + [ // Bind callbacks |
| 49 | + 'onStart', |
| 50 | + 'onEnd', |
| 51 | + 'onAdd', |
| 52 | + 'onSort', |
| 53 | + 'onUpdate', |
| 54 | + 'onRemove', |
| 55 | + 'onFilter', |
| 56 | + 'onMove' |
| 57 | + ].forEach((name) => { |
| 58 | + copyOptions[name] = (evt) => { |
| 59 | + if (name === 'onStart') { |
| 60 | + _nextSibling = evt.item.nextElementSibling; |
| 61 | + _activeWrapperComponent = wrapperComponent; |
| 62 | + } else if (name === 'onAdd' || name === 'onUpdate') { |
| 63 | + evt.from.insertBefore(evt.item, _nextSibling); |
| 64 | + |
| 65 | + const oldIndex = evt.oldIndex; |
| 66 | + const newIndex = evt.newIndex; |
| 67 | + let newState = {}; |
| 68 | + let remoteState = {}; |
| 69 | + let items = getModelItems(wrapperComponent); |
| 70 | + |
| 71 | + if (name === 'onAdd') { |
| 72 | + let remoteItems = getModelItems(_activeWrapperComponent); |
| 73 | + let item = remoteItems.splice(oldIndex, 1)[0]; |
| 74 | + items.splice(newIndex, 0, item); |
| 75 | + |
| 76 | + remoteState[_activeWrapperComponent.sortableOptions.model] = remoteItems; |
| 77 | + } else { |
| 78 | + items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]); |
| 79 | + } |
| 80 | + |
| 81 | + newState[wrapperComponent.sortableOptions.model] = items; |
| 82 | + |
| 83 | + if (copyOptions.stateHandler) { |
| 84 | + sortableComponent[copyOptions.stateHandler](newState); |
| 85 | + } else { |
| 86 | + sortableComponent.setState(newState); |
| 87 | + } |
| 88 | + |
| 89 | + if (_activeWrapperComponent !== wrapperComponent) { |
| 90 | + _activeWrapperComponent.refs[refName].setState(remoteState); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + setTimeout(() => { |
| 95 | + emitEvent(name, evt); |
| 96 | + }, 0); |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + const domNode = ReactDOM.findDOMNode(sortableComponent.refs[options.ref] || sortableComponent); |
| 101 | + this.sortableInstance = Sortable.create(domNode, copyOptions); |
| 102 | + } |
| 103 | + componentWillReceiveProps(nextProps) { |
| 104 | + const wrapperComponent = this; |
| 105 | + const sortableComponent = wrapperComponent.refs[refName]; |
| 106 | + const model = wrapperComponent.sortableOptions.model; |
| 107 | + const items = nextProps[model]; |
| 108 | + |
| 109 | + if (items) { |
| 110 | + let newState = {}; |
| 111 | + newState[model] = items; |
| 112 | + sortableComponent.setState(newState); |
| 113 | + } |
| 114 | + } |
| 115 | + componentWillUnmount() { |
| 116 | + if (this.sortableInstance) { |
| 117 | + this.sortableInstance.destroy(); |
| 118 | + this.sortableInstance = null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + render() { |
| 123 | + return ( |
| 124 | + <Component ref={refName} {...this.props} /> |
| 125 | + ); |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +export default SortableMixin; |
0 commit comments