| 5 | const REQUIRED_SPEC_METHODS = ['beginDrag']; |
| 6 | |
| 7 | export default function createSourceFactory(spec) { |
| 8 | Object.keys(spec).forEach((key) => { |
| 9 | invariant( |
| 10 | ALLOWED_SPEC_METHODS.indexOf(key) > -1, |
| 11 | 'Expected the drag source specification to only have ' + |
| 12 | 'some of the following keys: %s. ' + |
| 13 | 'Instead received a specification with an unexpected "%s" key. ' + |
| 14 | 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', |
| 15 | ALLOWED_SPEC_METHODS.join(', '), |
| 16 | key, |
| 17 | ); |
| 18 | invariant( |
| 19 | typeof spec[key] === 'function', |
| 20 | 'Expected %s in the drag source specification to be a function. ' + |
| 21 | 'Instead received a specification with %s: %s. ' + |
| 22 | 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', |
| 23 | key, |
| 24 | key, |
| 25 | spec[key], |
| 26 | ); |
| 27 | }); |
| 28 | REQUIRED_SPEC_METHODS.forEach((key) => { |
| 29 | invariant( |
| 30 | typeof spec[key] === 'function', |
| 31 | 'Expected %s in the drag source specification to be a function. ' + |
| 32 | 'Instead received a specification with %s: %s. ' + |
| 33 | 'Read more: http://react-dnd.github.io/react-dnd/docs-drag-source.html', |
| 34 | key, |
| 35 | key, |
| 36 | spec[key], |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | class Source { |
| 41 | constructor(monitor) { |
| 42 | this.monitor = monitor; |
| 43 | this.props = null; |
| 44 | this.component = null; |
| 45 | } |
| 46 | |
| 47 | receiveProps(props) { |
| 48 | this.props = props; |
| 49 | } |
| 50 | |
| 51 | receiveComponent(component) { |
| 52 | this.component = component; |
| 53 | } |
| 54 | |
| 55 | canDrag() { |
| 56 | if (!spec.canDrag) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return spec.canDrag(this.props, this.monitor); |
| 61 | } |
| 62 | |
| 63 | isDragging(globalMonitor, sourceId) { |
| 64 | if (!spec.isDragging) { |